cms/drupal/modules/search/search.pages.inc
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     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  * @param $module
       
    12  *   Search module to use for the search.
       
    13  * @param $keys
       
    14  *   Keywords to use for the search.
       
    15  */
       
    16 function search_view($module = NULL, $keys = '') {
       
    17   $info = FALSE;
       
    18   $keys = trim($keys);
       
    19   // Also try to pull search keywords out of the $_REQUEST variable to
       
    20   // support old GET format of searches for existing links.
       
    21   if (!$keys && !empty($_REQUEST['keys'])) {
       
    22     $keys = trim($_REQUEST['keys']);
       
    23   }
       
    24 
       
    25   if (!empty($module)) {
       
    26     $active_module_info = search_get_info();
       
    27     if (isset($active_module_info[$module])) {
       
    28       $info = $active_module_info[$module];
       
    29     }
       
    30   }
       
    31 
       
    32   if (empty($info)) {
       
    33     // No path or invalid path: find the default module. Note that if there
       
    34     // are no enabled search modules, this function should never be called,
       
    35     // since hook_menu() would not have defined any search paths.
       
    36     $info = search_get_default_module_info();
       
    37     // Redirect from bare /search or an invalid path to the default search path.
       
    38     $path = 'search/' . $info['path'];
       
    39     if ($keys) {
       
    40       $path .= '/' . $keys;
       
    41     }
       
    42     drupal_goto($path);
       
    43   }
       
    44 
       
    45   // Default results output is an empty string.
       
    46   $results = array('#markup' => '');
       
    47   // Process the search form. Note that if there is $_POST data,
       
    48   // search_form_submit() will cause a redirect to search/[module path]/[keys],
       
    49   // which will get us back to this page callback. In other words, the search
       
    50   // form submits with POST but redirects to GET. This way we can keep
       
    51   // the search query URL clean as a whistle.
       
    52   if (empty($_POST['form_id']) || ($_POST['form_id'] != 'search_form' && $_POST['form_id'] != 'search_block_form')) {
       
    53     $conditions =  NULL;
       
    54     if (isset($info['conditions_callback']) && function_exists($info['conditions_callback'])) {
       
    55       // Build an optional array of more search conditions.
       
    56       $conditions = call_user_func($info['conditions_callback'], $keys);
       
    57     }
       
    58     // Only search if there are keywords or non-empty conditions.
       
    59     if ($keys || !empty($conditions)) {
       
    60        if (variable_get('search_logging', TRUE)) {
       
    61          // Log the search keys.
       
    62          watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $info['title']), WATCHDOG_NOTICE, l(t('results'), 'search/' . $info['path'] . '/' . $keys));
       
    63        }
       
    64       // Collect the search results.
       
    65       $results = search_data($keys, $info['module'], $conditions);
       
    66     }
       
    67   }
       
    68   // The form may be altered based on whether the search was run.
       
    69   $build['search_form'] = drupal_get_form('search_form', NULL, $keys, $info['module']);
       
    70   $build['search_results'] = $results;
       
    71 
       
    72   return $build;
       
    73 }
       
    74 
       
    75 /**
       
    76  * Process variables for search-results.tpl.php.
       
    77  *
       
    78  * The $variables array contains the following arguments:
       
    79  * - $results: Search results array.
       
    80  * - $module: Module the search results came from (module implementing
       
    81  *   hook_search_info()).
       
    82  *
       
    83  * @see search-results.tpl.php
       
    84  */
       
    85 function template_preprocess_search_results(&$variables) {
       
    86   $variables['search_results'] = '';
       
    87   if (!empty($variables['module'])) {
       
    88     $variables['module'] = check_plain($variables['module']);
       
    89   }
       
    90   foreach ($variables['results'] as $result) {
       
    91     $variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
       
    92   }
       
    93   $variables['pager'] = theme('pager', array('tags' => NULL));
       
    94   $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
       
    95 }
       
    96 
       
    97 /**
       
    98  * Process variables for search-result.tpl.php.
       
    99  *
       
   100  * The $variables array contains the following arguments:
       
   101  * - $result
       
   102  * - $module
       
   103  *
       
   104  * @see search-result.tpl.php
       
   105  */
       
   106 function template_preprocess_search_result(&$variables) {
       
   107   global $language;
       
   108 
       
   109   $result = $variables['result'];
       
   110   $variables['url'] = check_url($result['link']);
       
   111   $variables['title'] = check_plain($result['title']);
       
   112   if (isset($result['language']) && $result['language'] != $language->language && $result['language'] != LANGUAGE_NONE) {
       
   113     $variables['title_attributes_array']['xml:lang'] = $result['language'];
       
   114     $variables['content_attributes_array']['xml:lang'] = $result['language'];
       
   115   }
       
   116 
       
   117   $info = array();
       
   118   if (!empty($result['module'])) {
       
   119     $info['module'] = check_plain($result['module']);
       
   120   }
       
   121   if (!empty($result['user'])) {
       
   122     $info['user'] = $result['user'];
       
   123   }
       
   124   if (!empty($result['date'])) {
       
   125     $info['date'] = format_date($result['date'], 'short');
       
   126   }
       
   127   if (isset($result['extra']) && is_array($result['extra'])) {
       
   128     $info = array_merge($info, $result['extra']);
       
   129   }
       
   130   // Check for existence. User search does not include snippets.
       
   131   $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
       
   132   // Provide separated and grouped meta information..
       
   133   $variables['info_split'] = $info;
       
   134   $variables['info'] = implode(' - ', $info);
       
   135   $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['module'];
       
   136 }
       
   137 
       
   138 /**
       
   139  * As the search form collates keys from other modules hooked in via
       
   140  * hook_form_alter, the validation takes place in _submit.
       
   141  * search_form_validate() is used solely to set the 'processed_keys' form
       
   142  * value for the basic search form.
       
   143  */
       
   144 function search_form_validate($form, &$form_state) {
       
   145   form_set_value($form['basic']['processed_keys'], trim($form_state['values']['keys']), $form_state);
       
   146 }
       
   147 
       
   148 /**
       
   149  * Process a search form submission.
       
   150  */
       
   151 function search_form_submit($form, &$form_state) {
       
   152   $keys = $form_state['values']['processed_keys'];
       
   153   if ($keys == '') {
       
   154     form_set_error('keys', t('Please enter some keywords.'));
       
   155     // Fall through to the form redirect.
       
   156   }
       
   157 
       
   158   $form_state['redirect'] = $form_state['action'] . '/' . $keys;
       
   159 }