current_search.block.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @file
  4. * Block hook implementations and block form alterations.
  5. */
  6. /**
  7. * Implements hook_form_FORM_ID_alter().
  8. *
  9. * Adds the searcher visibility settings to the block form.
  10. */
  11. function current_search_form_block_admin_configure_alter(&$form, &$form_state) {
  12. if ('current_search' == $form['module']['#value']) {
  13. $form['visibility']['current_search'] = array(
  14. '#type' => 'fieldset',
  15. '#title' => t('Search page'),
  16. '#collapsible' => TRUE,
  17. '#collapsed' => TRUE,
  18. '#group' => 'visibility',
  19. '#weight' => -5,
  20. '#attached' => array(
  21. 'js' => array(drupal_get_path('module', 'current_search') . '/current_search.js'),
  22. ),
  23. );
  24. // Gets the default value for this block.
  25. $searcher = db_query("SELECT searcher FROM {block_current_search} WHERE delta = :delta", array(
  26. ':delta' => $form['delta']['#value'],
  27. ))->fetchField();
  28. $form['visibility']['current_search']['searcher'] = array(
  29. '#type' => 'radios',
  30. '#title' => t('Search page'),
  31. '#options' => current_search_get_searcher_options(),
  32. '#description' => t('Select the search page this block is active on.'),
  33. '#default_value' => ($searcher) ? $searcher : current_search_get_default_searcher(),
  34. );
  35. // Adds submit handler to save the searcher data.
  36. $form['#submit'][] = 'current_search_form_block_admin_configure_submit';
  37. }
  38. }
  39. /**
  40. * Form submit handler for block configuration form.
  41. *
  42. * @see current_search_form_block_admin_configure_alter()
  43. */
  44. function current_search_form_block_admin_configure_submit($form, &$form_state) {
  45. $values = $form_state['values'];
  46. current_search_set_block_searcher($values['delta'], $values['searcher']);
  47. }
  48. /**
  49. * Implements hook_block_info().
  50. */
  51. function current_search_block_info() {
  52. $blocks = array();
  53. // Loads settings for enabled facets.
  54. ctools_include('export');
  55. foreach (ctools_export_crud_load_all('current_search') as $config) {
  56. if (empty($config->disabled)) {
  57. $blocks[$config->name] = array(
  58. 'info' => 'Current search: ' . $config->label,
  59. 'cache' => DRUPAL_NO_CACHE,
  60. );
  61. }
  62. }
  63. // Returns available blocks.
  64. return $blocks;
  65. }
  66. /**
  67. * Implements hook_ctools_block_info().
  68. *
  69. * @see http://drupal.org/node/1669918
  70. */
  71. function current_search_ctools_block_info($module, $delta, &$info) {
  72. // Give the current search blocks it's own categories.
  73. $info['category'] = t('Current Search Blocks');
  74. // Allow blocks to be used before the search results in Panels.
  75. $info['render last'] = TRUE;
  76. }
  77. /**
  78. * Returns the content for a facet based on the delta.
  79. */
  80. function current_search_block_view($delta = '') {
  81. // Test block visibility.
  82. $searcher = current_search_get_block_searcher($delta);
  83. if (!current_search_check_visibility($delta)) {
  84. return;
  85. }
  86. // Makes sure the adapter and configuration can be loaded.
  87. $adapter = facetapi_adapter_load($searcher);
  88. if ($adapter && ($config = ctools_export_crud_load('current_search', $delta))) {
  89. $build = array();
  90. // Iterates over configs and executes the plugins.
  91. foreach ($config->settings['items'] as $name => $settings) {
  92. if ($class = ctools_plugin_load_class('current_search', 'items', $settings['id'], 'handler')) {
  93. $plugin = new $class($name, $config);
  94. if ($return = $plugin->execute($adapter)) {
  95. $build[$name] = $return;
  96. $build[$name]['#theme_wrappers'][] = 'current_search_item_wrapper';
  97. $build[$name]['#current_search_id'] = $settings['id'];
  98. $build[$name]['#current_search_name'] = $name;
  99. }
  100. }
  101. }
  102. // Returns the block content.
  103. if ($build) {
  104. $build['#contextual_links'] = array(
  105. 'current_search' => array('admin/config/search/current_search/list', array($delta)),
  106. );
  107. return array(
  108. 'subject' => t('Current search'),
  109. 'content' => $build,
  110. );
  111. }
  112. }
  113. }
  114. /**
  115. * Sets the block searcher for a configuration.
  116. *
  117. * @param $name
  118. * A string containing the machine readable name of the configuration. The
  119. * name also doubles as the block delta.
  120. * @param $searcher
  121. * The machine readable name of the searcher.
  122. */
  123. function current_search_set_block_searcher($name, $searcher) {
  124. // Deletes current search data.
  125. db_delete('block_current_search')
  126. ->condition('delta', $name)
  127. ->execute();
  128. // Inserts new data into database.
  129. db_insert('block_current_search')
  130. ->fields(array('delta', 'searcher'))
  131. ->values(array(
  132. 'delta' => $name,
  133. 'searcher' => $searcher,
  134. ))
  135. ->execute();
  136. }
  137. /**
  138. * Checks whether the block should be displayed.
  139. *
  140. * In cases where modules like Context are being used, hook_block_list_alter()
  141. * is not invoked and we get fatal errors. We have to test whether or not the
  142. * hook has been invoked and call this function manually otherwise.
  143. *
  144. * @param $delta
  145. * The block delta.
  146. *
  147. * @return
  148. * A boolean flagging whether to display this block or not.
  149. */
  150. function current_search_check_visibility($delta) {
  151. $searcher = current_search_get_block_searcher($delta);
  152. // Checks whether block should be displayed.
  153. if (!facetapi_is_active_searcher($searcher)) {
  154. return FALSE;
  155. }
  156. if (!$adapter = facetapi_adapter_load($searcher)) {
  157. return FALSE;
  158. }
  159. if (!$adapter->searchExecuted($searcher)) {
  160. return FALSE;
  161. }
  162. if (!$config = current_search_item_load($delta)) {
  163. return FALSE;
  164. }
  165. // Returns TRUE based on the empty_searches setting and the current search.
  166. switch ($config->settings['advanced']['empty_searches']) {
  167. case CURRENT_SEARCH_DISPLAY_KEYS:
  168. return ($adapter->getSearchKeys());
  169. case CURRENT_SEARCH_DISPLAY_FILTERS:
  170. return ($adapter->getAllActiveItems());
  171. case CURRENT_SEARCH_DISPLAY_KEYS_FILTERS:
  172. return ($adapter->getSearchKeys() || $adapter->getAllActiveItems());
  173. case CURRENT_SEARCH_DISPLAY_ALWAYS:
  174. default:
  175. return TRUE;
  176. }
  177. }
  178. /**
  179. * Gets the searcher associated with the delta.
  180. *
  181. * @param string $delta
  182. * The block delta.
  183. *
  184. * @return string
  185. * The machine name of the searcher associated with the block.
  186. */
  187. function current_search_get_block_searcher($delta) {
  188. $map = &drupal_static('current_search_delta_map');
  189. if (NULL === $map) {
  190. $map = array();
  191. $result = db_query('SELECT delta, searcher FROM {block_current_search}');
  192. foreach ($result as $record) {
  193. $map[$record->delta] = $record->searcher;
  194. }
  195. }
  196. if (!isset($map[$delta])) {
  197. $map[$delta] = current_search_get_default_searcher();
  198. }
  199. return $map[$delta];
  200. }
  201. /**
  202. * Gets the default searcher.
  203. *
  204. * @return
  205. * The default searcher.
  206. *
  207. * @todo Figure out a beter default system.
  208. */
  209. function current_search_get_default_searcher() {
  210. $options = current_search_get_searcher_options();
  211. return key($options);
  212. }