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