<?php

function views_checkboxes_help($section='') {
  $output = '';
  $newFeatures = '<p>Also, you can optionally override the size of select lists (the number of elements displayed).</p>';

  switch ($section) {
    case 'admin/help#views_checkboxes':
      $output .= '<p>Alters view filter forms to replace select elements wth checkboxes and radio buttons.</p>';
      $output .= $newFeatures;
      break;
    case 'admin/settings/views_checkboxes':
      $output .= '<p>Enable this module to replace all select form elements in view filters with checkboxes or radio buttons, as appropriate.</p>';
      $output .= $newFeatures;
      break;
  }
  return $output;
}

function views_checkboxes_menu() {
  $items = array();

  $items[] = array(
    'path' => 'admin/settings/views_checkboxes',
    'title' => t('Views checkboxes'),
    'description' => t('Alter view filter forms'),
    'callback' => 'drupal_get_form',
    'callback arguments' => 'views_checkboxes_admin',
    'access' => user_access('administer views'),
    'type' => MENU_NORMAL_ITEM
  );

  return $items;
}

function views_checkboxes_admin() {
  $form['views_checkboxes_checkbox_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Replace multi-select boxes with checkboxes.'),
    '#default_value' => variable_get('views_checkboxes_checkbox_enable', FALSE),
    '#description' => t('Enable this to replace all multi-select view filter form elements with checkboxes.'),
  );

  $form['views_checkboxes_radio_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Replace single-select boxes with radio buttons.'),
    '#default_value' => variable_get('views_checkboxes_radio_enable', FALSE),
    '#description' => t('Enable this to replace all single-select view filter form elements with radio buttons.'),
  );

  $form['views_checkboxes_limit'] = array(
    '#type' => 'textfield',
    '#title' => t('Limit'),
    '#default_value' => variable_get('views_checkboxes_limit', 5),
    '#size' => 2,
    '#description' => t('Do not use checkboxes/radios if the number of options is greater than this value.'),
  );

  $form['views_checkboxes_listsize'] = array(
    '#type' => 'textfield',
    '#title' => t('Size of list'),
    '#default_value' => variable_get('views_checkboxes_listsize', 5),
    '#size' => 2,
    '#description' => t('When using a select-list, display only this many entries. Enter zero to use Views default.'),
  );

  $form['views_checkboxes_sizeup'] = array(
    '#type' => 'checkbox',
    '#title' => t('Increase size of list to fit'),
    '#default_value' => variable_get('views_checkboxes_sizeup', FALSE),
    '#description' => t('When the above option is enabled, automatically increase the size by one if that will show the whole list.'),
  );

  return system_settings_form($form);
}

function views_checkboxes_form_alter($form_id, &$form) {
  if (isset($form['view']['#value']->exposed_filter)) {
    // view filter forms don't seem to have a form_id, so I'm using this conditional to identify them.

    $view = $form['view']['#value'];
    $limit = variable_get('views_checkboxes_limit', 5);

    foreach ($view->exposed_filter as $count => $exposed) {
      //Start looping through through all exposed filters

      $listCount = count($form['filter'.$count]['#options']); // get number of options that will be listed

      // If enabled, use listsize setting
      if ($listSize = variable_get('views_checkboxes_listsize', 0)) {
        // increase listsize by one if enabled and appropriate
        if (variable_get('views_checkboxes_sizeup', FALSE) && $listSize+1 == $listCount)  ++$listSize;
        $form['filter'.$count]['#size'] = $listSize;
      }

      // If the number of options that would be displayed is greater than limit, use a listbox anyway
      if ($listCount > $limit)  continue;

      $recreate_options = FALSE;

      if (variable_get('views_checkboxes_radio_enable', false) && $exposed['single'] == '1' && $form['filter'.$count]['#type'] == 'select') {
        // This is a select box with the single limitation.  Should be radio buttons and need to unset
        // the theme or else the views module will still make this a select box.
        $form['filter'.$count]['#type'] = 'radios';
        unset($form['filter'.$count]['#theme']);
        $recreate_options = TRUE;
      }

      if (variable_get('views_checkboxes_checkbox_enable', false) && $exposed['single'] == '0' && $form['filter'.$count]['#type'] == 'select') {
        // This is a select box with no single limitation.  Should be checkboxes and need to unset
        // the theme or else the views module will still make this a select box.
        $form['filter'.$count]['#type'] = 'checkboxes';
        unset($form['filter'.$count]['#theme']);
        $recreate_options = TRUE;
      }

      if ($recreate_options AND !is_object($form['filter'.$count]['#options'][0])) {
        # We won't recreate the options if they are already of the simple array type.
        # Taxonomy options will be an array of objects handled below.
        $recreate_options = FALSE;
      }

      if ($recreate_options) {
        // The view options are an array of objects, instead of the typical array.  This will recreate
        // them as a typical form option array.
        $newoptions = array();

        foreach ($form['filter'.$count]['#options'] as $option_id => $option) {
          // I'm disabling the **ALL** option entirely, if it exists.
          if ($option_id === '**ALL**') continue;
          // I'm disabling the "- Please choose -" option some have reported.
          if ($option_id === '') continue;
          
          foreach ($form['filter'.$count]['#options'][$option_id]->option as $num => $val) {
            $newoptions[$num] = $val;
          }
        }
        $form['filter'.$count]['#options'] = $newoptions;
      }
    }
  }
}
