custom_search.pages.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the custom_search module.
  5. */
  6. /*
  7. * Presents links to filter the search results.
  8. */
  9. function custom_search_preprocess_search_results(&$variables) {
  10. if ($variables['module'] == 'node') {
  11. $variables['filter_position'] = variable_get('custom_search_filter', 'disabled');
  12. // Save # of results for collapsing advanced search.
  13. $GLOBALS['custom_search_nb_results'] = count($variables['results']);
  14. // Generate the filter.
  15. if (user_access('use custom search') && $variables['filter_position'] != 'disabled') {
  16. // Get search words (minus type:node_type).
  17. $path = explode('/', $_GET['q'], 3);
  18. $keys = empty($_REQUEST['keys']) ? '' : $_REQUEST['keys'];
  19. if (count($path) == 3) {
  20. $keys = $path[2];
  21. }
  22. if (strpos($keys, 'type:') !== FALSE) {
  23. $keys = drupal_substr($keys, 0, strpos($keys, 'type:') - 1);
  24. }
  25. // Get Custom Search authorized types.
  26. $searchable_node_types = variable_get('custom_search_node_types', array());
  27. $searchable_node_types = array_keys(array_filter($searchable_node_types, 'custom_search_filter_array'));
  28. if (!count($searchable_node_types)) {
  29. $searchable_node_types = array_keys(node_type_get_names());
  30. }
  31. $node_types = db_query("SELECT type, name FROM {node_type} WHERE type IN (:ntypes)", array(':ntypes' => $searchable_node_types));
  32. // Build menu.
  33. $items = array();
  34. $items[] = l(variable_get('custom_search_type_selector_all', CUSTOM_SEARCH_ALL_TEXT_DEFAULT), 'search/node/' . $keys);
  35. foreach ($node_types as $node_type) {
  36. // Count # of results per type.
  37. $nbresults = 0;
  38. foreach ($variables['results'] as $result) {
  39. if ($result['node']->type == $node_type->type) {
  40. $nbresults++;
  41. }
  42. }
  43. if ($nbresults) {
  44. $items[] = l($node_type->name, 'search/node/' . $keys . ' type:' . $node_type->type);
  45. }
  46. }
  47. if (!isset($variables['filter-title'])) {
  48. $variables['filter-title'] = filter_xss(variable_get('custom_search_filter_label', CUSTOM_SEARCH_FILTER_LABEL_DEFAULT));
  49. }
  50. if (count($items) > 2) {
  51. $variables['filter'] = theme('item_list', array('items' => $items, 'title' => $variables['filter-title']));
  52. }
  53. }
  54. $variables['theme_hook_suggestions'][] = 'custom_search_results';
  55. }
  56. }
  57. /*
  58. * Customisation of the results info.
  59. */
  60. function custom_search_preprocess_search_result(&$variables) {
  61. $infos = array();
  62. // In Drupal 7 the content type is no longer in the info array.
  63. if (variable_get('custom_search_results_info_type', TRUE) && $variables['module'] == 'node') {
  64. $infos[] = $variables['result']['type'];
  65. }
  66. if (isset($variables['info_split'])) {
  67. foreach ($variables['info_split'] as $key => $info) {
  68. if (variable_get('custom_search_results_info_' . $key, TRUE)) {
  69. array_push($infos, $info);
  70. }
  71. }
  72. }
  73. $variables['info'] = implode(' - ', $infos);
  74. $variables['theme_hook_suggestions'][] = 'custom_search_result';
  75. }