You are here

function apachesolr_date_block in Apache Solr Search 6.2

Implementation of hook_block().

File

contrib/apachesolr_date/apachesolr_date.module, line 45
Integration with the Apache Solr search application. Provides faceting for CCK Date fields.

Code

function apachesolr_date_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $enabled_facets = apachesolr_get_enabled_facets('apachesolr_date');
      $facets = apachesolr_date_apachesolr_facets();

      // Add the blocks
      $blocks = array();
      foreach ($enabled_facets as $delta => $facet_field) {
        if (isset($facets[$delta])) {
          $blocks[$delta] = $facets[$delta] + array(
            'cache' => BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE,
          );

          // TODO: This is ugly. The only consolation is that the admin can
          // override block titles anyway.
          $blocks[$delta]['label'] = $blocks[$delta]['label'] . ' (end)';
        }
      }
      return $blocks;
    case 'view':
      if (apachesolr_has_searched()) {

        // Get the query and response. Without these no blocks make sense.
        $response = apachesolr_static_response_cache();
        if (empty($response)) {
          return;
        }
        $query = apachesolr_current_query();
        $facets = apachesolr_get_enabled_facets('apachesolr_date');
        if (empty($facets[$delta])) {
          return;
        }
        if (!apachesolr_block_visibility($query, 'apachesolr_date', $delta)) {
          return;
        }

        // $delta comes to us with _end on the end, which we snip off right here.
        $delta = substr($delta, 0, strlen($delta) - 4);
        if ($fields = apachesolr_cck_fields()) {
          if ($field = $fields[$delta]) {
            $index_key = apachesolr_index_key($field) . '_end';
            $callback = isset($field['display_callback']) ? $field['display_callback'] : FALSE;
            $block_function = isset($field['facet_block_callback']) && function_exists($field['facet_block_callback']) ? $field['facet_block_callback'] : 'apachesolr_facet_block';
            return $block_function($response, $query, 'apachesolr_search', $delta, $index_key, t('Filter by @field end', array(
              '@field' => $field['label'],
            )), $callback);
          }
        }
      }
      break;
    case 'configure':
      return apachesolr_facetcount_form('apachesolr_date', $delta);
    case 'save':
      apachesolr_facetcount_save($edit);
      break;
  }
}