You are here

custom_search_taxonomy.module in Custom Search 7

Same filename and directory in other branches
  1. 6 modules/custom_search_taxonomy/custom_search_taxonomy.module

Bring customizations to the default search box

Adds taxonomy options to the search form

File

modules/custom_search_taxonomy/custom_search_taxonomy.module
View source
<?php

/**
 * @file
 * Bring customizations to the default search box
 *
 * Adds taxonomy options to the search form
 */

/*
 * Includes.
 */
if (module_exists('hs_taxonomy')) {
  module_load_include('inc', 'hierarchical_select', 'includes/common');
}

/**
 * Implements hook_menu().
 */
function custom_search_taxonomy_menu() {
  $items['admin/config/search/custom_search/taxonomy'] = array(
    'title' => 'Taxonomy',
    'description' => 'Select the vocabularies to present as search options in the search block.',
    'page arguments' => array(
      'custom_search_taxonomy_admin',
    ),
    'access arguments' => array(
      'administer custom search',
    ),
    'file' => 'custom_search_taxonomy.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
  );
  return $items;
}

/**
 * Implements hook_custom_search_box().
 */
function custom_search_taxonomy_custom_search_box(&$form, $form_id, $delta = '') {
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $voc) {
    if (variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector', 'disabled') != 'disabled') {
      $options = array();
      $options['c-all'] = variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT);
      $vocabulary_depth = !variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_depth', 0) ? NULL : variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_depth', 0);
      $terms = taxonomy_get_tree($voc->vid, 0, $vocabulary_depth);
      foreach ($terms as $term) {
        $options['c-' . $term->tid] = drupal_substr(variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector', 'disabled'), 0, 6) == 'select' ? str_repeat('-', $term->depth) . ' ' . $term->name : $term->name;
      }
      $selector_type = variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector', 'select');
      if ($selector_type == 'selectmultiple') {
        $selector_type = 'select';
        $multiple = TRUE;
      }
      else {
        $multiple = FALSE;
      }
      $form['custom_search_vocabulary_' . $voc->vid] = array(
        '#type' => $selector_type,
        '#multiple' => $multiple,
        '#title' => variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label', $voc->name),
        '#options' => $options,
        '#default_value' => $selector_type == 'checkboxes' ? array(
          'c-all',
        ) : 'c-all',
        '#attributes' => array(
          'class' => array(
            'custom-search-selector',
            'custom-search-vocabulary',
          ),
        ),
        '#weight' => variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_weight', 2),
      );
      if (module_exists('hs_taxonomy') && $selector_type == 'hierarchical_select') {
        $hs_config = hierarchical_select_common_config_get('taxonomy-' . $voc->vid);
        $hs_config['module'] = 'hs_taxonomy';
        $hs_config['params'] = array(
          'vid' => $voc->vid,
          'exclude_tid' => 0,
          'root_term' => 0,
          'entity_count_for_node_type' => 0,
        );
        $form['custom_search_vocabulary_' . $voc->vid]['#config'] = $hs_config;
        unset($form['custom_search_vocabulary_' . $voc->vid]['#options']);
      }
      if (variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_region', 'block') == 'popup') {
        $form['popup']['custom_search_vocabulary_' . $voc->vid] = $form['custom_search_vocabulary_' . $voc->vid];
        unset($form['custom_search_vocabulary_' . $voc->vid]);
      }
      if (!variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label_visibility', TRUE)) {
        $form['custom_search_vocabulary_' . $voc->vid]['#title_display'] = 'invisible';
      }
    }
  }

  // Custom paths.
  if (variable_get('custom_search_' . $delta . 'paths', '') != '') {
    $form['custom_search_paths_terms_separator'] = array(
      '#type' => 'hidden',
      '#default_value' => variable_get('custom_search_' . $delta . 'paths_terms_separator', CUSTOM_SEARCH_PATHS_TERMS_SEPARATOR_DEFAULT),
    );
  }
}

/**
 * Implements hook_form_alter().
 */
function custom_search_taxonomy_form_alter(&$form, &$form_state, $form_id) {

  // Filter the form_id value to identify all the custom blocks.
  $form_id_processed = $form_id;
  $delta = '';
  for ($a = 1; $a <= variable_get('custom_search_blocks_number', 1); $a++) {
    if ($form_id == 'custom_search_blocks_form_' . $a) {
      $form_id_processed = 'custom_search_blocks_form';
      $delta = 'blocks_' . $a . '_';
    }
  }
  switch ($form_id_processed) {
    case 'block_admin_configure':
      if (isset($form['module']) && $form['module']['#value'] == 'custom_search_blocks') {
        $delta = $form['delta']['#value'];
        $vocabularies = taxonomy_get_vocabularies();
        if (count($vocabularies)) {
          $form['settings']['taxonomy'] = array(
            '#type' => 'fieldset',
            '#title' => t('Taxonomy'),
            '#description' => t('Select the vocabularies to present as search options in the search block. If none is selected, no selector will be displayed.'),
            '#collapsible' => TRUE,
          );

          // Get vocabularies forms.
          $form['settings']['taxonomy'] = _custom_search_taxonomy_admin_form($vocabularies, $delta);
          $form['settings']['order']['custom_search_blocks_' . $delta . '_order'] = array_merge($form['settings']['order']['custom_search_blocks_' . $delta . '_order'], _custom_search_taxonomy_sort_form($vocabularies, $delta));
          $collapsed = TRUE;
          foreach ($vocabularies as $voc) {
            if (variable_get('custom_search_blocks_' . $delta . '_voc' . $voc->vid . '_selector', 'disabled') != 'disabled') {
              $collapsed = FALSE;
            }
          }
          $form['settings']['taxonomy']['#collapsed'] = $collapsed;
        }
        $form['settings']['custom_search_paths_admin']['custom_search_blocks_' . $delta . '_paths']['#description'] = t('If you want to use custom search paths, enter them here in the form <em>path</em>|<em>label</em>, one per line (if only one path is specified, the selector will be hidden). The [key] token will be replaced by what is entered in the search box, and the [terms] token will be replaced by the selected taxonomy term id(s). Ie: mysearch/[key]/[terms]|My custom search label.');
        $form['settings']['custom_search_paths_admin']['custom_search_blocks_' . $delta . '_paths_terms_separator'] = array(
          '#type' => 'textfield',
          '#title' => t('Taxonomy terms separator'),
          '#default_value' => variable_get('custom_search_blocks_' . $delta . '_paths_terms_separator', CUSTOM_SEARCH_PATHS_TERMS_SEPARATOR_DEFAULT),
          '#size' => 2,
        );
      }
      break;
    case 'custom_search_admin':
      $vocabularies = taxonomy_get_vocabularies();
      if (count($vocabularies)) {
        $form['order']['custom_search_order'] = array_merge($form['order']['custom_search_order'], _custom_search_taxonomy_sort_form($vocabularies));
      }
      $form['custom_search_paths_admin']['custom_search_paths']['#description'] = t('If you want to use custom search paths, enter them here in the form <em>path</em>|<em>label</em>, one per line (if only one path is specified, the selector will be hidden). The [key] token will be replaced by what is entered in the search box, and the [terms] token will be replaced by the selected taxonomy term id(s). Ie: mysearch/[key]/[terms]|My custom search label.');
      $form['custom_search_paths_admin']['custom_search_paths_terms_separator'] = array(
        '#type' => 'textfield',
        '#title' => t('Taxonomy terms separator'),
        '#default_value' => variable_get('custom_search_paths_terms_separator', CUSTOM_SEARCH_PATHS_TERMS_SEPARATOR_DEFAULT),
        '#size' => 2,
      );
      break;
  }
}

/**
 * Default admin form.
 */
function _custom_search_taxonomy_admin_form($vocabularies, $delta = '') {
  if ($delta != '') {
    $delta = 'blocks_' . $delta . '_';
  }
  $form = array();
  foreach ($vocabularies as $voc) {
    $form[$voc->name] = array(
      '#type' => 'fieldset',
      '#title' => check_plain($voc->name),
      '#collapsible' => TRUE,
      '#collapsed' => variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector', 'disabled') == 'disabled' ? TRUE : FALSE,
    );
    $form[$voc->name]['custom_search_' . $delta . 'voc' . $voc->vid . '_selector'] = array(
      '#type' => 'select',
      '#title' => t('Selector type'),
      '#options' => array(
        'disabled' => t('Disabled'),
        'select' => t('Drop-down list'),
        'selectmultiple' => t('Drop-down list with multiple choices'),
        'radios' => t('Radio buttons'),
        'checkboxes' => t('Checkboxes'),
      ),
      '#description' => t('Choose which selector type to use.'),
      '#default_value' => variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector', 'disabled'),
    );
    if (module_exists('hs_taxonomy')) {
      $form[$voc->name]['custom_search_' . $delta . 'voc' . $voc->vid . '_selector']['#options']['hierarchical_select'] = t('Hierarchical selector');
    }
    $form[$voc->name]['custom_search_' . $delta . 'voc' . $voc->vid . '_selector_depth'] = array(
      '#type' => 'textfield',
      '#title' => t('Depth'),
      '#size' => 2,
      '#default_value' => variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_depth', 0),
      '#description' => t('Define the maximum depth of terms being displayed. The default value is "0" which disables the limit.'),
    );
    $form[$voc->name]['custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label_visibility'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display label'),
      '#default_value' => variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label_visibility', TRUE),
    );
    $form[$voc->name]['custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label text'),
      '#default_value' => variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label', $voc->name),
      '#description' => t('Enter the label text for the selector. The default value is "@default".', array(
        '@default' => $voc->name,
      )),
      '#states' => array(
        'visible' => array(
          ':input[name="custom_search_' . $delta . 'voc' . $voc->vid . '_selector_label_visibility"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form[$voc->name]['custom_search_' . $delta . 'voc' . $voc->vid . '_selector_all'] = array(
      '#type' => 'textfield',
      '#title' => t('-Any- text'),
      '#default_value' => variable_get('custom_search_' . $delta . 'voc' . $voc->vid . '_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT),
      '#required' => TRUE,
      '#description' => t('Enter the text for "any term" choice. The default value is "!default".', array(
        '!default' => CUSTOM_SEARCH_ALL_TEXT_DEFAULT,
      )),
    );
  }
  return $form;
}

/**
 * Sort form.
 */
function _custom_search_taxonomy_sort_form($vocabularies, $delta = '') {
  if ($delta != '') {
    $delta = 'blocks_' . $delta . '_';
  }
  $form = array();
  $w = 1;
  foreach ($vocabularies as $voc) {
    $form['taxonomy' . $voc->vid] = array(
      '#title' => t('Taxonomy') . ': ' . check_plain($voc->name),
      '#weight' => variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_weight', $w),
    );
    $form['taxonomy' . $voc->vid]['sort'] = array(
      '#type' => 'weight',
      '#default_value' => variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_weight', $w),
      '#attributes' => array(
        'class' => array(
          'sort-select',
          'sort-select-' . variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_region', 'block'),
        ),
      ),
    );
    $form['taxonomy' . $voc->vid]['region'] = array(
      '#type' => 'select',
      '#options' => array(
        'block' => t('Block'),
        'popup' => t('Popup'),
      ),
      '#default_value' => variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_region', 'block'),
      '#attributes' => array(
        'class' => array(
          'region-select',
          'region-select-' . variable_get('custom_search_' . $delta . 'taxonomy' . $voc->vid . '_region', 'block'),
        ),
      ),
    );
    $w++;
  }
  return $form;
}