| Drupal | PHP Cross Reference | Content Management Systems |
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 $redirect = FALSE; 19 $keys = trim($keys); 20 // Also try to pull search keywords out of the $_REQUEST variable to 21 // support old GET format of searches for existing links. 22 if (!$keys && !empty($_REQUEST['keys'])) { 23 $keys = trim($_REQUEST['keys']); 24 } 25 26 if (!empty($module)) { 27 $active_module_info = search_get_info(); 28 if (isset($active_module_info[$module])) { 29 $info = $active_module_info[$module]; 30 } 31 } 32 33 if (empty($info)) { 34 // No path or invalid path: find the default module. Note that if there 35 // are no enabled search modules, this function should never be called, 36 // since hook_menu() would not have defined any search paths. 37 $info = search_get_default_module_info(); 38 // Redirect from bare /search or an invalid path to the default search path. 39 $path = 'search/' . $info['path']; 40 if ($keys) { 41 $path .= '/' . $keys; 42 } 43 drupal_goto($path); 44 } 45 46 // Default results output is an empty string. 47 $results = array('#markup' => ''); 48 // Process the search form. Note that if there is $_POST data, 49 // search_form_submit() will cause a redirect to search/[module path]/[keys], 50 // which will get us back to this page callback. In other words, the search 51 // form submits with POST but redirects to GET. This way we can keep 52 // the search query URL clean as a whistle. 53 if (empty($_POST['form_id']) || $_POST['form_id'] != 'search_form') { 54 $conditions = NULL; 55 if (isset($info['conditions_callback']) && function_exists($info['conditions_callback'])) { 56 // Build an optional array of more search conditions. 57 $conditions = call_user_func($info['conditions_callback'], $keys); 58 } 59 // Only search if there are keywords or non-empty conditions. 60 if ($keys || !empty($conditions)) { 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title