<?php

/**
 * @file
 */

function stormidea_list() {
  $breadcrumb = array();
  $breadcrumb[] = l(t('SuiteDesk'), 'dashboard');
  drupal_set_breadcrumb($breadcrumb);

  $i = new stdClass();
  $i->type = 'stormidea';

  $params = $_GET;

  $header = array(
    array(
      'data' => t('Idea'),
      'field' => 'n.title',
    ),
    array(
      'data' => t('Date'),
      'field' => 'n.changed',
      'class' => 'storm_list_date',
      'sort' => 'desc',
    ),
    array(
      'data' => ' ',
      'class' => 'storm_list_operations',
    ),
  );

  $s  = "SELECT n.*, nre.format FROM {node} AS n
    INNER JOIN {node_revisions} AS nre ON n.vid = nre.vid
    WHERE n.status=1 AND n.type='stormidea'";

  $where = array();
  $args = array();
  $filterfields = array();

  if (isset($_SESSION['stormidea_list_filter']['title']) && $_SESSION['stormidea_list_filter']['title'] != '') {
    $where[] = "LOWER(n.title) LIKE CONCAT('%', LOWER('%s'), '%')";
    $args[] = $_SESSION['stormidea_list_filter']['title'];
    $filterfields[] = t('Title');
  }

  // This section only provides the value for the fieldset label, doesn't control actual filter
  $itemsperpage = isset($_SESSION['stormidea_list_filter']['itemsperpage']) ? $_SESSION['stormidea_list_filter']['itemsperpage'] : variable_get('storm_default_items_per_page', 10);

  if (count($filterfields) == 0) {
    $filterdesc = t('Not filtered');
  }
  else {
    $filterdesc = t('Filtered by !fields', array('!fields' => implode(", ", array_unique($filterfields))));
  }
  $filterdesc .= ' | '. t('!items items per page', array('!items' => $itemsperpage));

  $o = drupal_get_form('stormidea_list_filter', $filterdesc);

  $s = stormidea_access_sql($s, $where);
  $s = db_rewrite_sql($s);
  $tablesort = tablesort_sql($header);

  $r = pager_query($s . $tablesort, $itemsperpage, 0, NULL, $args);

  $ideas = array();
  while ($item = db_fetch_object($r)) {
    $ideas[] = $item;
  }

  $o .= theme('stormidea_list', $header, $ideas);
  $o .= theme('pager', NULL, $itemsperpage, 0);
  print theme('page', $o);
}

function stormidea_list_filter(&$form_state, $filterdesc = 'Filter') {
  $title = isset($_SESSION['stormidea_list_filter']['title']) ? $_SESSION['stormidea_list_filter']['title'] : '';

  $itemsperpage = isset($_SESSION['stormidea_list_filter']['itemsperpage']) ? $_SESSION['stormidea_list_filter']['itemsperpage'] : variable_get('storm_default_items_per_page', 10);
  $_SESSION['stormidea_list_filter']['itemsperpage'] = $itemsperpage;

  $form = array();

  $form['filter'] = array(
    '#type' => 'fieldset',
    '#title' => $filterdesc,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  $form['filter']['group2'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
  );

  $form['filter']['group2']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Idea'),
    '#default_value' => $title,
  );

  $form['filter']['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'storm_form_group',
    '#attributes' => array('class' => 'formgroup-submit'),
  );

  $form['filter']['group3']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
    '#submit' => array('stormidea_list_filter_filter'),
  );

  $form['filter']['group3']['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
    '#submit' => array('stormidea_list_filter_reset'),
  );

  $form['filter']['group3']['itemsperpage'] = array(
    '#type' => 'textfield',
    '#title' => t('Items'),
    '#size' => 10,
    '#default_value' => $itemsperpage,
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );

  return $form;

}

function stormidea_list_filter_reset($form, &$form_state) {
  unset($_SESSION['stormidea_list_filter']);
}

function stormidea_list_filter_filter($form, &$form_state) {
  $_SESSION['stormidea_list_filter']['title'] = $form_state['values']['title'];
  $_SESSION['stormidea_list_filter']['itemsperpage'] = $form_state['values']['itemsperpage'];
}
