search_result.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. if (module_exists('search')) {
  3. /**
  4. * Plugins are described by creating a $plugin array which will be used
  5. * by the system that includes this file.
  6. */
  7. $plugin = array(
  8. 'single' => TRUE,
  9. 'title' => t('Search results'),
  10. 'icon' => 'icon_search.png',
  11. 'description' => t('The results of a search using keywords.'),
  12. 'required context' => new ctools_context_required(t('Keywords'), 'string'),
  13. 'category' => t('Widgets'),
  14. 'defaults' => array(
  15. 'type' => 'node',
  16. 'log' => TRUE,
  17. 'override_empty' => FALSE,
  18. 'empty_title' => '',
  19. 'empty' => '',
  20. 'empty_format' => filter_fallback_format(),
  21. 'override_no_key' => FALSE,
  22. 'no_key_title' => '',
  23. 'no_key' => '',
  24. 'no_key_format' => filter_fallback_format(),
  25. ),
  26. );
  27. }
  28. /**
  29. * Render the custom content type.
  30. */
  31. function ctools_search_result_content_type_render($subtype, $conf, $panel_args, $context) {
  32. $search_info = search_get_info();
  33. if (empty($search_info[$conf['type']])) {
  34. return;
  35. }
  36. $info = $search_info[$conf['type']];
  37. $keys = NULL;
  38. if (!empty($context) && isset($context->data)) {
  39. $keys = $context->data;
  40. }
  41. $conditions = NULL;
  42. if (isset($info['conditions_callback']) && function_exists($info['conditions_callback'])) {
  43. // Build an optional array of more search conditions.
  44. $conditions = $info['conditions_callback']($keys);
  45. }
  46. // Display nothing at all if no keywords were entered.
  47. if (empty($keys) && empty($conditions)) {
  48. if (!empty($conf['override_no_key'])) {
  49. $block->title = $conf['no_key_title'];
  50. $block->content = check_markup($conf['no_key'], $conf['no_key_format'], FALSE);
  51. return $block;
  52. }
  53. return;
  54. }
  55. // Build the content type block.
  56. $block = new stdClass();
  57. $block->module = 'search';
  58. $block->delta = 'result';
  59. $results = '';
  60. // Only search if there are keywords or non-empty conditions.
  61. if ($keys || !empty($conditions)) {
  62. // Collect the search results.
  63. $results = search_data($keys, $info['module'], $conditions);
  64. // A workaround for ApacheSolr.
  65. // @todo see http://drupal.org/node/1343142#comment-5495248
  66. // This workaround is to be removed when a better one can be written.
  67. if (!empty($results['search_results']['#results'])) {
  68. $results['#results'] = $results['search_results']['#results'];
  69. }
  70. }
  71. if (!empty($conf['log'])) {
  72. // Log the search keys:
  73. watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $info['title']), WATCHDOG_NOTICE, l(t('results'), $_GET['q']));
  74. }
  75. if (!empty($results['#results'])) {
  76. $output = "<ol class=\"search-results $conf[type]-results\">\n";
  77. foreach ($results['#results'] as $result) {
  78. $output .= theme('search_result', array('result' => $result, 'module' => $conf['type']));
  79. }
  80. $output .= "</ol>\n";
  81. $output .= theme('pager', array('tags' => NULL));
  82. $block->title = t('Search results');
  83. $block->content = $output;
  84. }
  85. else {
  86. if (empty($conf['override_empty'])) {
  87. $block->title = t('Your search yielded no results');
  88. $block->content = search_help('search#noresults', drupal_help_arg());
  89. }
  90. else {
  91. $block->title = $conf['empty_title'];
  92. $block->content = check_markup($conf['empty'], $conf['empty_format'], FALSE);
  93. }
  94. }
  95. return $block;
  96. }
  97. /**
  98. * Returns an edit form for custom type settings.
  99. */
  100. function ctools_search_result_content_type_edit_form($form, &$form_state) {
  101. $conf = $form_state['conf'];
  102. $types = array();
  103. foreach (search_get_info() as $module => $info) {
  104. $types[$module] = $info['title'];
  105. }
  106. $form['type'] = array(
  107. '#type' => 'select',
  108. '#title' => t('Search type'),
  109. '#options' => $types,
  110. '#default_value' => $conf['type'],
  111. );
  112. $form['log'] = array(
  113. '#type' => 'checkbox',
  114. '#default_value' => $conf['log'],
  115. '#title' => t('Record a watchdog log entry when searches are made'),
  116. );
  117. $form['override_empty'] = array(
  118. '#type' => 'checkbox',
  119. '#default_value' => $conf['override_empty'],
  120. '#title' => t('Override "no result" text'),
  121. );
  122. $form['empty_title'] = array(
  123. '#title' => t('Title'),
  124. '#type' => 'textfield',
  125. '#default_value' => $conf['empty_title'],
  126. '#dependency' => array('edit-override-empty' => array(1)),
  127. );
  128. $form['empty_field'] = array(
  129. '#type' => 'text_format',
  130. '#title' => t('No result text'),
  131. '#default_value' => $conf['empty'],
  132. '#format' => $conf['empty_format'],
  133. '#dependency' => array('edit-override-empty' => array(1)),
  134. );
  135. $form['override_no_key'] = array(
  136. '#type' => 'checkbox',
  137. '#default_value' => $conf['override_no_key'],
  138. '#title' => t('Display text if no search keywords were submitted'),
  139. );
  140. $form['no_key_title'] = array(
  141. '#title' => t('Title'),
  142. '#type' => 'textfield',
  143. '#default_value' => $conf['no_key_title'],
  144. '#dependency' => array('edit-override-no-key' => array(1)),
  145. );
  146. $form['no_key_field'] = array(
  147. '#type' => 'text_format',
  148. '#title' => t('No result text'),
  149. '#default_value' => $conf['no_key'],
  150. '#format' => $conf['no_key_format'],
  151. '#dependency' => array('edit-override-no-key' => array(1)),
  152. );
  153. return $form;
  154. }
  155. /**
  156. * Submit handler for search form.
  157. */
  158. function ctools_search_result_content_type_edit_form_submit($form, &$form_state) {
  159. // Copy the text_format values over to where we normally store them.
  160. $form_state['values']['empty'] = $form_state['values']['empty_field']['value'];
  161. $form_state['values']['empty_format'] = $form_state['values']['empty_field']['format'];
  162. $form_state['values']['no_key'] = $form_state['values']['no_key_field']['value'];
  163. $form_state['values']['no_key_format'] = $form_state['values']['no_key_field']['format'];
  164. // Copy everything from our defaults.
  165. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  166. $form_state['conf'][$key] = $form_state['values'][$key];
  167. }
  168. }
  169. /**
  170. * Returns the administrative title for a type.
  171. */
  172. function ctools_search_result_content_type_admin_title($subtype, $conf, $context) {
  173. $info = search_get_info();
  174. $type = isset($info[$conf['type']]['title']) ? $info[$conf['type']]['title'] : t('Missing/broken type');
  175. return t('@type search result', array('@type' => $type));
  176. }