You are here

function search_config_form_alter in Search configuration 5

Same name and namespace in other branches
  1. 6 search_config.module \search_config_form_alter()

Implementation of hook_form_alter()

File

./search_config.module, line 24
Used to configure the advanced search form and other search behaviour.

Code

function search_config_form_alter($form_id, &$form) {

  // Present the user with the appropriate search results tab as defined by
  // variable "search_config_default_search" (labeled "Default Search")
  // in the "Advanced search settings" fieldset in admin/settings/search
  if ($form_id == 'search_theme_form') {
    $form['#submit'] = array();
    $form['#submit']['search_form_submit'] = array();
    $form['module']['#value'] = variable_get('search_config_default_search', 'node');
    $form['module']['#type'] = 'hidden';
    $form['processed_keys'] = $form['search_theme_form_keys'];
    $form['processed_keys']['#weight'] = -1;
    unset($form['search_theme_form_keys']);
  }
  if ($form_id == 'search_form') {
    if (arg(1) == 'node') {

      // Keywords
      if (user_access('use keyword search')) {
        if (variable_get('search_config_disable_or', 0)) {
          unset($form['advanced']['keywords']['or']);
        }
        if (variable_get('search_config_disable_phrase', 0)) {
          unset($form['advanced']['keywords']['phrase']);
        }
        if (variable_get('search_config_disable_negative', 0)) {
          unset($form['advanced']['keywords']['negative']);
        }
      }
      else {
        unset($form['advanced']['keywords']);
      }

      // Node types
      // We will need to rebuild the checkboxes for the node types
      $remove = variable_get('search_config_disable_type', array());
      if ($remove['all'] || !user_access('search by node type')) {
        unset($form['advanced']['type']);
      }
      else {
        $types = node_get_types('names');
        foreach ($types as $module => $type) {
          if ($remove[$module]) {
            unset($types[$module]);
          }
        }

        // Rebuild form item -- copied from node.module
        if (count($types) == 1) {
          $type_keys = array_keys($types);
          $form['advanced']['type'] = array(
            '#type' => 'hidden',
            '#default_value' => $type_keys[0],
          );
        }
        else {
          $form['advanced']['type'] = array(
            '#type' => 'checkboxes',
            '#title' => t('Only of the type(s)'),
            '#prefix' => '<div class="criterion">',
            '#suffix' => '</div>',
            '#options' => $types,
          );
        }
      }
      if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {

        // Taxonomy
        if (variable_get('search_config_disable_category_all', 0) || !user_access('search by category')) {
          unset($form['advanced']['category']);
        }
        else {
          $terms = variable_get('search_config_disable_category', array());
          if (module_exists('og_vocab')) {
            $groupnode = og_get_group_context();
            $sql = "SELECT t.tid FROM ({vocabulary} v, {term_data} t) LEFT JOIN {og_vocab} ov ON v.vid = ov.vid WHERE (v.module = 'og_vocab' AND ov.nid != %d) AND t.vid=v.vid";
            $result = db_query($sql, $groupnode->nid);
            while ($row = db_fetch_object($result)) {
              $terms[$row->tid] = $row->tid;
            }
          }

          // FIXME: What about multiple hierarchy categories?
          foreach ($taxonomy as $vocab => $term) {
            foreach ($term as $k => $v) {
              if (in_array($k, $terms)) {
                unset($taxonomy[$vocab][$k]);
              }
            }

            // Remove empty vocabs
            if (count($taxonomy[$vocab]) == 0) {
              unset($taxonomy[$vocab]);
            }
          }
          if (count($taxonomy) == 0) {
            unset($form['advanced']['category']);
          }
          else {

            // Taxonomy box:
            $form['advanced']['category'] = array(
              '#type' => 'select',
              '#title' => t('Only in the category(s)'),
              '#prefix' => '<div class="criterion">',
              '#size' => 10,
              '#suffix' => '</div>',
              '#options' => $taxonomy,
              '#multiple' => TRUE,
            );
          }
        }
      }
    }
  }
}