You are here

function apachesolr_block in Apache Solr Search 5

Same name and namespace in other branches
  1. 5.2 apachesolr.module \apachesolr_block()
  2. 6 apachesolr.module \apachesolr_block()
  3. 6.2 apachesolr.module \apachesolr_block()

Implementation of hook_block().

1 call to apachesolr_block()
apachesolrlang_block in contrib/apachesolr_lang/apachesolrlang.module
Implementation of hook_block().

File

./apachesolr.module, line 498
Integration with the Apache Solr search application.

Code

function apachesolr_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':

      // Sorting block
      $blocks['sort'] = array(
        'info' => t('ApacheSolr Core: Sorting'),
      );
      $blocks['type'] = array(
        'info' => t('ApacheSolr Core: Filter by type'),
      );
      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_drupal_query();

        // Get information needed by the rest of the blocks about limits.
        $facet_display_limits = variable_get('apachesolr_facet_query_limits', array());
        switch ($delta) {
          case 'sort':
            $sorts = array(
              'relevancy' => array(
                'name' => t('Relevancy'),
                'default' => 'asc',
              ),
              'stitle' => array(
                'name' => t('Title'),
                'default' => 'asc',
              ),
              'type' => array(
                'name' => t('Type'),
                'default' => 'asc',
              ),
              'name' => array(
                'name' => t('Author'),
                'default' => 'asc',
              ),
              'changed' => array(
                'name' => t('Date'),
                'default' => 'desc',
              ),
            );
            $solrsorts = array();
            $sort_parameter = isset($_GET['solrsort']) ? check_plain($_GET['solrsort']) : FALSE;
            foreach (explode(',', $sort_parameter) as $solrsort) {
              $parts = explode(' ', $solrsort);
              if (!empty($parts[0]) && !empty($parts[1])) {
                $solrsorts[$parts[0]] = $parts[1];
              }
            }
            $sort_links = array();
            $path = 'search/' . arg(1) . '/' . $query
              ->get_query();
            foreach ($sorts as $type => $sort) {
              $new_sort = isset($solrsorts[$type]) ? $solrsorts[$type] == 'asc' ? 'desc' : 'asc' : $sort['default'];
              $sort_links[] = theme('apachesolr_sort_link', $sort['name'], $path, $type == "relevancy" ? '' : "solrsort={$type} {$new_sort}", isset($solrsorts[$type]) ? $solrsorts[$type] : '');
            }
            return array(
              'subject' => t('Sort by'),
              'content' => theme('apachesolr_sort_list', $sort_links),
            );
          case 'type':
            $filter_by = t('Filter by type');
            return apachesolr_facet_block($response, $query, $delta, $filter_by, 'apachesolr_get_type');
          default:
            break;
        }
      }
      break;
    case 'configure':
      return apachesolr_facetcount_form($delta);
      break;
    case 'save':
      apachesolr_facetcount_save($delta, $edit);
      break;
  }
}