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