custom_search.module 28 KB

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