You are here

function ad_filter_form in Advertisement 6.3

Same name and namespace in other branches
  1. 5.2 ad.module \ad_filter_form()
  2. 5 ad.module \ad_filter_form()
  3. 6 ad.admin.inc \ad_filter_form()
  4. 6.2 ad.admin.inc \ad_filter_form()
  5. 7 ad.admin.inc \ad_filter_form()

Return form for advertisement administration filters.

2 string references to 'ad_filter_form'
ad_admin_list in ./ad.admin.inc
Build default ad administration page.
ad_channel_form_alter in channel/ad_channel.module
Implementation of hook_form_alter(). Generate a form for selecting channels to associate with an advertisement.

File

./ad.admin.inc, line 364
Advertisement admin pages and functions.

Code

function ad_filter_form($form_state) {
  $session =& $_SESSION['ad_overview_filter'];
  $session = is_array($session) ? $session : array();
  $filters = ad_filters();
  $i = 0;
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Show only ads where'),
    '#theme' => 'ad_filters',
  );
  foreach ($session as $filter) {
    list($type, $value) = $filter;
    if ($type == 'category') {

      // Load term name from DB rather than search and parse options array.
      $value = module_invoke('taxonomy', 'get_term', $value);
      $value = $value->name;
    }
    else {
      if ($type == 'status') {
        $value = $filters['status']['options'][$value];
      }
      else {
        $value = $filters[$type]['options'][$value];
      }
    }
    $string = $i++ ? '<em>and</em> where <strong>%a</strong> is <strong>%b</strong>' : '<strong>%a</strong> is <strong>%b</strong>';
    $form['filters']['current'][] = array(
      '#value' => t($string, array(
        '%a' => $filters[$type]['title'],
        '%b' => $value,
      )),
    );
    if ($type == 'type') {

      // Remove the type option if it is already being filtered on.
      unset($filters['type']);
    }
    else {
      if ($type == 'group') {
        unset($filters['group']);
      }
    }
    if ($type == 'status') {
      foreach ($session as $option) {
        if ($option[0] == 'status') {
          list($value, $key) = explode('-', $option[1], 2);
          if ($key) {

            // One postive key means we can't have any more.
            // Remove the status option if we're already filtering on a positive
            // key (ie, 'active', as an ad can't be 'active' and 'pending')
            unset($filters['status']);
          }
          else {

            // When a key is selected, remove it and its inverse as there's
            // no logic in selecting the same key multiple times, and selecting
            // two opposite keys will always return 0 results.
            $inverse = $key == 1 ? 0 : 1;
            unset($filters['status']['options'][$option[1]]);
            unset($filters['status']['options'][$value . '-' . $inverse]);
          }
        }
      }
    }
  }
  $names = array();
  foreach ($filters as $key => $filter) {
    $names[$key] = $filter['title'];
    $form['filters']['status'][$key] = array(
      '#type' => 'select',
      '#options' => $filter['options'],
    );
  }
  $form['filters']['filter'] = array(
    '#type' => 'radios',
    '#options' => $names,
    '#default_value' => 'status',
  );
  $form['filters']['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => count($session) ? t('Refine') : t('Filter'),
  );
  if (count($session)) {
    $form['filters']['buttons']['undo'] = array(
      '#type' => 'submit',
      '#value' => t('Undo'),
    );
    $form['filters']['buttons']['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset'),
    );
  }
  return $form;
}