[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/search/ -> search.pages.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * User page callbacks for the search module.
   6   */
   7  
   8  /**
   9   * Menu callback; presents the search form and/or search results.
  10   */
  11  function search_view($type = 'node') {
  12    // Search form submits with POST but redirects to GET. This way we can keep
  13    // the search query URL clean as a whistle:
  14    // search/type/keyword+keyword
  15    if (!isset($_POST['form_id'])) {
  16      if ($type == '') {
  17        // Note: search/node can not be a default tab because it would take on the
  18        // path of its parent (search). It would prevent remembering keywords when
  19        // switching tabs. This is why we drupal_goto to it from the parent instead.
  20        drupal_goto('search/node');
  21      }
  22  
  23      $keys = search_get_keys();
  24      // Only perform search if there is non-whitespace search term:
  25      $results = '';
  26      if (trim($keys)) {
  27        // Log the search keys:
  28        watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));
  29  
  30        // Collect the search results:
  31        $results = search_data($keys, $type);
  32  
  33        if ($results) {
  34          $results = theme('box', t('Search results'), $results);
  35        }
  36        else {
  37          $results = theme('box', t('Your search yielded no results'), search_help('search#noresults', drupal_help_arg()));
  38        }
  39      }
  40  
  41      // Construct the search form.
  42      $output = drupal_get_form('search_form', NULL, $keys, $type);
  43      $output .= $results;
  44  
  45      return $output;
  46    }
  47  
  48    return drupal_get_form('search_form', NULL, empty($keys) ? '' : $keys, $type);
  49  }
  50  
  51  /**
  52   * Process variables for search-results.tpl.php.
  53   *
  54   * The $variables array contains the following arguments:
  55   * - $results
  56   * - $type
  57   *
  58   * @see search-results.tpl.php
  59   */
  60  function template_preprocess_search_results(&$variables) {
  61    $variables['search_results'] = '';
  62    foreach ($variables['results'] as $result) {
  63      $variables['search_results'] .= theme('search_result', $result, $variables['type']);
  64    }
  65    $variables['pager'] = theme('pager', NULL, 10, 0);
  66    // Provide alternate search results template.
  67    $variables['template_files'][] = 'search-results-'. $variables['type'];
  68  }
  69  
  70  /**
  71   * Process variables for search-result.tpl.php.
  72   *
  73   * The $variables array contains the following arguments:
  74   * - $result
  75   * - $type
  76   *
  77   * @see search-result.tpl.php
  78   */
  79  function template_preprocess_search_result(&$variables) {
  80    $result = $variables['result'];
  81    $variables['url'] = check_url($result['link']);
  82    $variables['title'] = check_plain($result['title']);
  83  
  84    $info = array();
  85    if (!empty($result['type'])) {
  86      $info['type'] = check_plain($result['type']);
  87    }
  88    if (!empty($result['user'])) {
  89      $info['user'] = $result['user'];
  90    }
  91    if (!empty($result['date'])) {
  92      $info['date'] = format_date($result['date'], 'small');
  93    }
  94    if (isset($result['extra']) && is_array($result['extra'])) {
  95      $info = array_merge($info, $result['extra']);
  96    }
  97    // Check for existence. User search does not include snippets.
  98    $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  99    // Provide separated and grouped meta information..
 100    $variables['info_split'] = $info;
 101    $variables['info'] = implode(' - ', $info);
 102    // Provide alternate search result template.
 103    $variables['template_files'][] = 'search-result-'. $variables['type'];
 104  }
 105  
 106  /**
 107   * As the search form collates keys from other modules hooked in via
 108   * hook_form_alter, the validation takes place in _submit.
 109   * search_form_validate() is used solely to set the 'processed_keys' form
 110   * value for the basic search form.
 111   */
 112  function search_form_validate($form, &$form_state) {
 113    form_set_value($form['basic']['inline']['processed_keys'], trim($form_state['values']['keys']), $form_state);
 114  }
 115  
 116  /**
 117   * Process a search form submission.
 118   */
 119  function search_form_submit($form, &$form_state) {
 120    $keys = $form_state['values']['processed_keys'];
 121    if ($keys == '') {
 122      form_set_error('keys', t('Please enter some keywords.'));
 123      // Fall through to the drupal_goto() call.
 124    }
 125  
 126    $type = $form_state['values']['module'] ? $form_state['values']['module'] : 'node';
 127    $form_state['redirect'] = 'search/'. $type .'/'. $keys;
 128    return;
 129  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7