custom_search.module 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /**
  3. * @file
  4. * Bring customizations to the default search box
  5. *
  6. * Adds node types and taxonomy options to the search form
  7. */
  8. define('CUSTOM_SEARCH_LABEL_DEFAULT', t('Search this site'));
  9. define('CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT', t('Search'));
  10. define('CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT', t('Search for'));
  11. define('CUSTOM_SEARCH_MAX_LENGTH_DEFAULT', 128);
  12. define('CUSTOM_SEARCH_SIZE_DEFAULT', 15);
  13. define('CUSTOM_SEARCH_ALL_TEXT_DEFAULT', t('-Any-'));
  14. define('CUSTOM_SEARCH_CRITERIA_OR_LABEL_DEFAULT', t('Containing any of the words'));
  15. define('CUSTOM_SEARCH_CRITERIA_PHRASE_LABEL_DEFAULT', t('Containing the phrase'));
  16. define('CUSTOM_SEARCH_CRITERIA_NEGATIVE_LABEL_DEFAULT', t('Containing none of the words'));
  17. define('CUSTOM_SEARCH_FILTER_LABEL_DEFAULT', t('Filter the results'));
  18. define('CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT', t('Customize your search'));
  19. define('CUSTOM_SEARCH_PATHS_TERMS_SEPARATOR_DEFAULT', '+');
  20. /**
  21. * Includes
  22. */
  23. module_load_include('inc', 'custom_search', 'includes/forms');
  24. module_load_include('inc', 'custom_search', 'includes/apachesolr_search');
  25. module_load_include('inc', 'custom_search', 'includes/google_appliance');
  26. module_load_include('inc', 'custom_search', 'includes/luceneapi_node');
  27. module_load_include('inc', 'custom_search', 'includes/search_api');
  28. /**
  29. * Implements hook_menu().
  30. */
  31. function custom_search_menu() {
  32. $items['admin/config/search/custom_search'] = array(
  33. 'title' => 'Custom Search',
  34. 'description' => 'Customize the default search, change labels, default texts, ordering, and display content types and taxonomy selectors.',
  35. 'page callback' => 'drupal_get_form',
  36. 'page arguments' => array('custom_search_admin'),
  37. 'access arguments' => array('administer custom search'),
  38. 'file' => 'custom_search.admin.inc',
  39. );
  40. $items['admin/config/search/custom_search/settings'] = array(
  41. 'title' => 'Settings',
  42. 'description' => 'Change the labels, the default texts and the ordering of elements.',
  43. 'access arguments' => array('administer custom search'),
  44. 'type' => MENU_DEFAULT_LOCAL_TASK,
  45. 'weight' => 0,
  46. );
  47. $items['admin/config/search/custom_search/content'] = array(
  48. 'title' => 'Content',
  49. 'description' => 'Select the content types to present as search options in the search block.',
  50. 'page arguments' => array('custom_search_content_admin'),
  51. 'access arguments' => array('administer custom search'),
  52. 'file' => 'custom_search.admin.inc',
  53. 'type' => MENU_LOCAL_TASK,
  54. 'weight' => 1,
  55. );
  56. $items['admin/config/search/custom_search/results'] = array(
  57. 'title' => 'Results page',
  58. 'description' => 'Customize the search results page.',
  59. 'page arguments' => array('custom_search_results_admin'),
  60. 'access arguments' => array('administer custom search'),
  61. 'file' => 'custom_search.admin.inc',
  62. 'type' => MENU_LOCAL_TASK,
  63. 'weight' => 4,
  64. );
  65. return $items;
  66. }
  67. /**
  68. * Implements hook_permission().
  69. */
  70. function custom_search_permission() {
  71. return array(
  72. 'administer custom search' => array(
  73. 'title' => t('Administer custom search'),
  74. 'description' => t('Allow users to administer custom search settings')
  75. ),
  76. 'use custom search' => array(
  77. 'title' => t('Use custom search'),
  78. 'description' => t('Allow users to use custom search')
  79. )
  80. );
  81. }
  82. /**
  83. * Implements hook_form_alter().
  84. */
  85. function custom_search_form_alter(&$form, &$form_state, $form_id) {
  86. // Filter the form_id value to identify all the custom blocks
  87. $form_id_processed = $form_id;
  88. $delta = '';
  89. for ($a = 1 ; $a <= variable_get('custom_search_blocks_number', 1) ; $a++) {
  90. if ($form_id == 'custom_search_blocks_form_' . $a) {
  91. $form_id_processed = 'custom_search_blocks_form';
  92. $delta = 'blocks_' . $a . '_';
  93. }
  94. }
  95. switch ($form_id_processed) {
  96. case 'search_form':
  97. if (isset($form['module']) && $form['module']['#value'] == 'node' && isset($form['advanced'])) {
  98. // Criteria
  99. if (!variable_get('custom_search_advanced_or_display', TRUE)) $form['advanced']['keywords']['or']['#type'] = 'hidden';
  100. if (!variable_get('custom_search_advanced_phrase_display', TRUE)) $form['advanced']['keywords']['phrase']['#type'] = 'hidden';
  101. if (!variable_get('custom_search_advanced_negative_display', TRUE)) $form['advanced']['keywords']['negative']['#type'] = 'hidden';
  102. // Taxonomy
  103. if (module_exists('taxonomy')) {
  104. $vocabularies = taxonomy_get_vocabularies();
  105. foreach ($vocabularies as $voc) {
  106. if (!variable_get('custom_search_advanced_voc' . $voc->vid . '_display', TRUE)) unset($form['advanced']['category']['#options'][$voc->name]);
  107. }
  108. if (isset($form['advanced']['category']) && !count($form['advanced']['category']['#options'])) unset($form['advanced']['category']['#type']);
  109. }
  110. // Content types
  111. $names = array_keys(node_type_get_names());
  112. foreach ($names as $name) {
  113. if (!variable_get('custom_search_advanced_type_' . $name . '_display', TRUE)) unset($form['advanced']['type']['#options'][$name]);
  114. }
  115. if (!count($form['advanced']['type']['#options'])) unset($form['advanced']['type']['#type']);
  116. if (!variable_get('custom_search_results_search', TRUE)) {
  117. if (isset($form['basic']['keys'])) {
  118. // If basic search is hidden, original search terms are imported into advanced search.
  119. $original_keys = $form['basic']['keys']['#default_value'];
  120. $temp_keys = explode(' ', $original_keys);
  121. foreach ($temp_keys as $value) {
  122. if (drupal_substr($value, 0, 5) != 'type:' && drupal_substr($value, 0, 5) != 'term:') $keys[] = $value;
  123. }
  124. $form['advanced']['keywords']['or']['#default_value'] = implode(' ', $keys);
  125. }
  126. if (!isset($GLOBALS['custom_search_nb_results']) || (isset($GLOBALS['custom_search_nb_results']) && !$GLOBALS['custom_search_nb_results'])) $form['advanced']['#collapsed'] = FALSE;
  127. $form['basic']['#prefix'] = '<div class="element-invisible">';
  128. $form['basic']['#suffix'] = '</div>';
  129. }
  130. $form['advanced']['#collapsible'] = variable_get('custom_search_results_advanced_search_collapsible', TRUE);
  131. $form['advanced']['#collapsed'] = variable_get('custom_search_results_advanced_search_collapsed', TRUE);
  132. if (!variable_get('custom_search_results_advanced_search', TRUE)) $form['advanced']['#type'] = 'hidden';
  133. }
  134. break;
  135. case 'search_theme_form':
  136. case 'search_block_form':
  137. case 'custom_search_blocks_form':
  138. if (user_access('use custom search')) {
  139. // Title.
  140. $form[$form_id]['#title'] = variable_get('custom_search_' . $delta . 'label', CUSTOM_SEARCH_LABEL_DEFAULT);
  141. $form[$form_id]['#title_display'] = (!variable_get('custom_search_' . $delta . 'label_visibility', FALSE)) ? 'invisible' : 'before' ;
  142. // Search box.
  143. $form[$form_id]['#weight'] = variable_get('custom_search_' . $delta . 'search_box_weight', 0);
  144. $form[$form_id]['#attributes'] = array(
  145. 'class' => array('custom-search-box'),
  146. 'placeholder' => array(variable_get('custom_search_' . $delta . 'text', '')),
  147. );
  148. $form[$form_id]['#size'] = variable_get('custom_search_' . $delta . 'size', CUSTOM_SEARCH_SIZE_DEFAULT);
  149. $form[$form_id]['#maxlength'] = variable_get('custom_search_' . $delta . 'max_length', CUSTOM_SEARCH_MAX_LENGTH_DEFAULT);
  150. // CSS
  151. drupal_add_css(drupal_get_path('module', 'custom_search') . '/custom_search.css');
  152. // Criteria
  153. $criteria = array('or' => 6, 'phrase' => 7, 'negative' => 8);
  154. foreach ($criteria as $c => $w) {
  155. if (variable_get('custom_search_' . $delta . 'criteria_' . $c . '_display', FALSE)) {
  156. $form['custom_search_criteria_' . $c] = array(
  157. '#type' => 'textfield',
  158. '#title' => variable_get('custom_search_' . $delta . 'criteria_' . $c . '_label', constant('CUSTOM_SEARCH_CRITERIA_' . strtoupper($c) . '_LABEL_DEFAULT')),
  159. '#size' => 15,
  160. '#maxlength' => 255,
  161. '#weight' => variable_get('custom_search_' . $delta . 'criteria_' . $c . '_weight', $w),
  162. );
  163. }
  164. }
  165. // Content type & other searches.
  166. // Content types.
  167. $toptions = array();
  168. $types = array_keys(array_filter(variable_get('custom_search_' . $delta . 'node_types', array())));
  169. if (count($types)) {
  170. $names = node_type_get_names();
  171. if (count($types) > 1 || variable_get('custom_search_' . $delta . 'any_force', FALSE)) $toptions['c-all'] = variable_get('custom_search_' . $delta . 'type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT);
  172. foreach ($types as $type) {
  173. $toptions['c-' . $type] = $names[$type];
  174. }
  175. }
  176. $options = array();
  177. // Other searches.
  178. $others = array_keys(array_filter(variable_get('custom_search_' . $delta . 'other', array())));
  179. // If content types and other searches are combined, make an optgroup.
  180. if (count($others) && count($toptions) && variable_get('custom_search_' . $delta . 'type_selector', 'select') == 'select') {
  181. $content = module_invoke('node', 'search_info');
  182. $options[$content['title']] = $toptions;
  183. }
  184. else {
  185. $options = $toptions;
  186. }
  187. foreach (module_implements('search_info') as $module) {
  188. if ($module != 'node' && $name = module_invoke($module, 'search_info')) {
  189. if (in_array($module, $others)) $options['o-' . $module] = $name['title'];
  190. }
  191. }
  192. if (count($options)) {
  193. $selector_type = variable_get('custom_search_' . $delta . 'type_selector', 'select');
  194. if ($selector_type == 'selectmultiple') {
  195. $selector_type = 'select';
  196. $multiple = TRUE;
  197. }
  198. else $multiple = FALSE;
  199. $form['custom_search_types'] = array(
  200. '#type' => $selector_type,
  201. '#multiple' => $multiple,
  202. '#title' => variable_get('custom_search_' . $delta . 'type_selector_label', CUSTOM_SEARCH_TYPE_SELECTOR_LABEL_DEFAULT),
  203. '#options' => $options,
  204. '#default_value' => ((variable_get('custom_search_' . $delta . 'type_selector', 'select') == 'checkboxes') ? array('c-all') : 'c-all'),
  205. '#attributes' => array('class' => array('custom-search-selector', 'custom-search-types')),
  206. '#weight' => variable_get('custom_search_' . $delta . 'content_types_weight', 1),
  207. '#validated' => TRUE,
  208. );
  209. // If there's only one type, hide the selector
  210. if (count($others) + count($types) == 1 && !variable_get('custom_search_' . $delta . 'any_force', FALSE)) {
  211. $form['custom_search_types']['#type'] = 'hidden';
  212. $form['custom_search_types']['#default_value'] = key(array_slice($options, count($options)-1));
  213. }
  214. if (!variable_get('custom_search_' . $delta . 'type_selector_label_visibility', TRUE)) $form['custom_search_types']['#title_display'] = 'invisible';
  215. }
  216. // Custom paths
  217. if (variable_get('custom_search_' . $delta . 'paths', '') != '') {
  218. $options = array();
  219. $lines = explode("\n", variable_get('custom_search_' . $delta . 'paths', ''));
  220. foreach ($lines as $line) {
  221. $temp = explode('|', $line);
  222. $options[$temp[0]] = (count($temp) >= 2) ? t($temp[1]) : '';
  223. }
  224. if (count($options) == 1) {
  225. $form['custom_search_paths'] = array(
  226. '#type' => 'hidden',
  227. '#default_value' => key($options),
  228. );
  229. }
  230. else {
  231. $form['custom_search_paths'] = array(
  232. '#type' => variable_get('custom_search_' . $delta . 'paths_selector', 'select'),
  233. '#title' => variable_get('custom_search_' . $delta . 'paths_selector_label', CUSTOM_SEARCH_PATHS_SELECTOR_LABEL_DEFAULT),
  234. '#options' => $options,
  235. '#default_value' => key($options),
  236. '#weight' => variable_get('custom_search_' . $delta . 'custom_paths_weight', 9),
  237. );
  238. if (!variable_get('custom_search_' . $delta . 'paths_selector_label_visibility', TRUE)) $form['custom_search_paths']['#title_display'] = 'invisible';
  239. }
  240. }
  241. // Submit button.
  242. $form['actions']['submit']['#value'] = variable_get('custom_search_' . $delta . 'submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT);
  243. if (variable_get('custom_search_' . $delta . 'image_path', '') != '') {
  244. $form['actions']['submit']['#type'] = 'image_button';
  245. $form['actions']['submit']['#src'] = variable_get('custom_search_' . $delta . 'image_path', '');
  246. $form['actions']['submit']['#name'] = 'op';
  247. $form['actions']['submit']['#attributes'] = array('alt' => array(variable_get('custom_search_' . $delta . 'submit_text', CUSTOM_SEARCH_SUBMIT_TEXT_DEFAULT)), 'class' => array('custom-search-button'));
  248. }
  249. elseif ($form['actions']['submit']['#value'] == '') $form['actions']['submit']['#attributes'] = array('style' => 'display:none;');
  250. $form['actions']['#weight'] = variable_get('custom_search_' . $delta . 'submit_button_weight', 3);
  251. // Popup
  252. $form['popup'] = array(
  253. '#type' => 'fieldset',
  254. '#weight' => 1 + variable_get('custom_search_' . $delta . 'search_box_weight', 0),
  255. '#attributes' => array('class' => array('custom_search-popup')),
  256. );
  257. if (!empty($form['custom_search_types']) && variable_get('custom_search_' . $delta . 'content_types_region', 'block') == 'popup') {
  258. $form['popup']['custom_search_types'] = $form['custom_search_types'];
  259. unset($form['custom_search_types']);
  260. }
  261. if (!empty($form['custom_search_paths']) && variable_get('custom_search_' . $delta . 'custom_paths_region', 'block') == 'popup') {
  262. $form['popup']['custom_search_paths'] = $form['custom_search_paths'];
  263. unset($form['custom_search_paths']);
  264. }
  265. foreach ($criteria as $c => $w) {
  266. if (variable_get('custom_search_' . $delta . 'criteria_' . $c . '_display', FALSE) && variable_get('custom_search_' . $delta . 'criteria_' . $c . '_region', 'block') == 'popup') {
  267. $form['popup']['custom_search_criteria_' . $c] = $form['custom_search_criteria_' . $c];
  268. unset($form['custom_search_criteria_' . $c]);
  269. }
  270. }
  271. // Form attributes
  272. $form['#attributes']['class'] = array('search-form');
  273. if (!in_array('custom_search_submit', $form['#submit'])) {
  274. $form['#submit'][] = 'custom_search_submit';
  275. }
  276. }
  277. break;
  278. }
  279. }
  280. /**
  281. * Alter the search to respect the search modes selected.
  282. */
  283. function custom_search_submit($form, &$form_state) {
  284. $delta = (isset($form_state['values']['delta'])) ? 'blocks_' . $form_state['values']['delta'] . '_' : '' ;
  285. variable_set('custom_search_delta', $delta); // save for later use (exclusion & refresh)
  286. $type = 'node';
  287. $keys = $form_state['values'][$form_state['values']['form_id']];
  288. $original_keywords = $keys;
  289. $types = (isset($form_state['values']['custom_search_types'])) ? $form_state['values']['custom_search_types'] : array();
  290. if (!is_array($types)) $types = array($types);
  291. $types = array_map('_custom_search_filter_keys', array_filter($types));
  292. if (module_exists('taxonomy')) {
  293. $terms = array();
  294. $vocabularies = taxonomy_get_vocabularies();
  295. foreach ($vocabularies as $voc) {
  296. if (isset($form_state['values']['custom_search_vocabulary_' . $voc->vid])) {
  297. $vterms = $form_state['values']['custom_search_vocabulary_' . $voc->vid];
  298. if (!is_array($vterms)) $vterms = array($vterms);
  299. $terms = array_merge($terms, $vterms);
  300. }
  301. }
  302. $terms = array_map('_custom_search_filter_keys', array_values(array_filter($terms)));
  303. // if one or more -Any- is selected, delete them
  304. while (($index = array_search('all', $terms)) !== FALSE) array_splice($terms, $index, 1);
  305. }
  306. $search_types = module_implements('search_info');
  307. if (in_array(current($types), $search_types)) {
  308. $type = current($types);
  309. $info = module_invoke($type, 'search_info');
  310. $path = (isset($info['path'])) ? $info['path'] : $type;
  311. }
  312. else {
  313. $path = $type;
  314. if (isset($form_state['values']['custom_search_criteria_or']) && trim($form_state['values']['custom_search_criteria_or']) != '') $keys .= ' ' . str_replace(' ', ' OR ', trim($form_state['values']['custom_search_criteria_or']));
  315. if (isset($form_state['values']['custom_search_criteria_negative']) && trim($form_state['values']['custom_search_criteria_negative']) != '') $keys .= ' -' . str_replace(' ', ' -', trim($form_state['values']['custom_search_criteria_negative']));
  316. if (isset($form_state['values']['custom_search_criteria_phrase']) && trim($form_state['values']['custom_search_criteria_phrase']) != '') $keys .= ' "' . trim($form_state['values']['custom_search_criteria_phrase']) . '"';
  317. $original_keywords = $keys;
  318. if (count($types)) {
  319. // If a content type is selected, and it's not -Any-, search for that type.
  320. if (!in_array('all', $types)) $keys = search_expression_insert($keys, 'type', implode(',', $types));
  321. // If -Any- is selected and -Any- is set to restrict the search, grab the content types.
  322. elseif (variable_get('custom_search_' . $delta . 'any_restricts', FALSE)) {
  323. $restricted_types = array_keys(array_filter(variable_get('custom_search_' . $delta . 'node_types', array())));
  324. $keys = search_expression_insert($keys, 'type', implode(',', $restricted_types));
  325. }
  326. }
  327. if (module_exists('taxonomy') && count($terms)) {
  328. $keys = search_expression_insert($keys, 'term', implode(',', $terms));
  329. }
  330. if (module_exists('custom_search_i18n')) {
  331. if (variable_get('custom_search_i18n_' . $delta . 'search_language', 'all') == 'current') {
  332. $keys = search_expression_insert($keys, 'language', i18n_language()->language);
  333. }
  334. }
  335. }
  336. $search_path = array(
  337. 'path' => 'search/' . $path . '/' . $keys,
  338. 'query' => array(),
  339. );
  340. // Integrates other search modules
  341. if (module_exists('apachesolr_search')) {
  342. for ($i=0; $i<count($types); $i++) {
  343. // Remove the item from the array if it's not a content type.
  344. if (!in_array($types[$i], array_keys(node_type_get_types()))) {
  345. unset($types[$i]);
  346. }
  347. }
  348. $search_path = _custom_search_apachesolr_search(array(
  349. 'keywords' => $original_keywords,
  350. 'types' => $types,
  351. 'terms' => (!empty($terms)) ? $terms : array(),
  352. ));
  353. }
  354. elseif (module_exists('google_appliance')) {
  355. $search_path = _custom_search_google_appliance_search(array(
  356. 'keys' => $keys,
  357. ));
  358. }
  359. elseif (module_exists('luceneapi_node') && variable_get('luceneapi:default_search', 0)) {
  360. $search_path = _custom_search_lucenapi_search(array(
  361. 'keywords' => $original_keywords,
  362. 'types' => $types,
  363. 'terms' => (!empty($terms)) ? $terms : array(),
  364. ));
  365. }
  366. elseif (module_exists('search_api_page')) {
  367. $search_api_page = search_api_page_load(variable_get('custom_search_' . $delta . 'search_api_page', 0));
  368. if ($search_api_page) {
  369. $search_path = _custom_search_search_api_search(array(
  370. 'keywords' => $original_keywords,
  371. 'types' => $types,
  372. 'terms' => (!empty($terms)) ? $terms : array(),
  373. 'page' => $search_api_page,
  374. ));
  375. }
  376. }
  377. // Build a custom path if needed
  378. if (isset($form_state['values']['custom_search_paths']) && $form_state['values']['custom_search_paths'] != '') {
  379. $custom_path = str_replace('[key]', $form_state['values'][$form_state['values']['form_id']], $form_state['values']['custom_search_paths']);
  380. if (strpos($form_state['values']['custom_search_paths'], '[terms]') !== FALSE) $custom_path = str_replace('[terms]', (count($terms)) ? implode($form_state['values']['custom_search_paths_terms_separator'], $terms) : '', $custom_path);
  381. // Check for a query string
  382. $custom_path_query_position = strpos($custom_path, '?');
  383. $custom_path_query = array();
  384. if ($custom_path_query_position !== FALSE) {
  385. $custom_path_query_tmp = substr($custom_path, 1 + $custom_path_query_position);
  386. $custom_path_query_tmp = str_replace('&amp;', '&', $custom_path_query_tmp);
  387. $custom_path_query_tmp = explode('&', $custom_path_query_tmp);
  388. foreach ($custom_path_query_tmp as $param) {
  389. $param_exploded = explode('=', $param);
  390. $custom_path_query[$param_exploded[0]] = $param_exploded[1];
  391. }
  392. $custom_path = substr($custom_path, 0, $custom_path_query_position);
  393. }
  394. // Check for external path. If not, add base path
  395. if (drupal_substr($custom_path, 0, 4) != 'http') $custom_path = url($custom_path, array('absolute' => TRUE));
  396. // Send the final url
  397. $form_state['redirect'] = url($custom_path, array('query' => $custom_path_query, 'absolute' => TRUE));
  398. }
  399. else $form_state['redirect'] = url($search_path['path'], array('query' => $search_path['query'], 'absolute' => TRUE));
  400. }
  401. /*
  402. * Rewrite the sql query to exclude content types.
  403. */
  404. function custom_search_query_alter(QueryAlterableInterface $query) {
  405. if ($query->hasTag('node_access') && $query->hasTag('pager')) {
  406. $excluded_types = array_filter(variable_get('custom_search_' . variable_get('custom_search_delta', '') . 'node_types_excluded', array()));
  407. if (!empty($excluded_types)) {
  408. $tables = $query->getTables();
  409. foreach ($tables as $table) {
  410. if ($table['table'] == 'search_index') {
  411. $query->condition('n.type', $excluded_types, 'NOT IN');
  412. }
  413. }
  414. }
  415. }
  416. }
  417. /**
  418. * Implements hook_init().
  419. */
  420. function custom_search_init() {
  421. if (user_access('use custom search')) {
  422. drupal_add_js(drupal_get_path('module', 'custom_search') . '/js/custom_search.js');
  423. drupal_add_js(array('custom_search' => array(
  424. 'form_target' => variable_get('custom_search_target', '_self'),
  425. 'solr' => module_exists('apachesolr_search') ? 1 : 0,
  426. )), array('type' => 'setting'));
  427. }
  428. }
  429. /**
  430. * Implements hook_theme().
  431. */
  432. function custom_search_theme() {
  433. $path = drupal_get_path('module', 'custom_search') . '/theme';
  434. $custom_search_theme_array = array(
  435. 'custom_search_javascript' => array(
  436. 'variables' => array(),
  437. ),
  438. 'custom_search_sort_form' => array(
  439. 'render element' => 'form',
  440. 'path' => $path,
  441. 'template' => 'custom_search-sort-form',
  442. ),
  443. 'search_result' => array(
  444. 'variables' => array('result' => NULL, 'module' => NULL),
  445. 'path' => $path,
  446. 'file' => 'custom_search.pages.inc',
  447. 'template' => 'custom_search-result',
  448. ),
  449. 'search_results' => array(
  450. 'variables' => array('results' => NULL, 'module' => NULL),
  451. 'path' => $path,
  452. 'file' => 'custom_search.pages.inc',
  453. 'template' => 'custom_search-results',
  454. ),
  455. );
  456. // Panels integration
  457. $router_item = db_query_range('SELECT page_callback FROM {menu_router} WHERE path = :path', 0, 1, array(':path' => 'search/node/%'))->fetchAssoc();
  458. if ($router_item['page_callback'] == 'page_manager_search_page') {
  459. unset($custom_search_theme_array['search_results'], $custom_search_theme_array['search_result']);
  460. }
  461. return $custom_search_theme_array;
  462. }
  463. /**
  464. * Implements hook_link().
  465. */
  466. function custom_search_contextual_links_view_alter(&$element, $items) {
  467. if (isset($element['#element']['#form_id']) && $element['#element']['#form_id'] == 'search_block_form') {
  468. $element['#links']['custom_search'] = array(
  469. 'title' => t('Custom Search configuration'),
  470. 'href' => 'admin/config/search/custom_search',
  471. 'query' => drupal_get_destination(),
  472. );
  473. }
  474. }
  475. /**
  476. * Filter the types
  477. */
  478. function _custom_search_filter_keys($val) {
  479. return (strlen($val) > 2 && $val[1] == '-') ? drupal_substr($val, 2) : $val;
  480. }
  481. /**
  482. * Function used to filter node_type array to only filter those that are configured in Custom Search Form
  483. */
  484. function custom_search_filter_array($value = FALSE) {
  485. return $value !== 0;
  486. }