You are here

function ds_search_search_page in Display Suite 7

Same name and namespace in other branches
  1. 7.2 modules/ds_search/ds_search.module \ds_search_search_page()

Implements hook_search_page().

File

modules/ds_search/ds_search.module, line 248
Display Suite search.

Code

function ds_search_search_page($results) {

  // Build shared variables.
  $build = array(
    '#type' => 'node',
  );
  ds_build_shared_page_variables($build);

  // Multi site Apache Solr support.
  if (variable_get('ds_search_apachesolr_multisite') && variable_get('ds_search_type', 'node') == 'apachesolr_search') {
    $build['search_results'] = $results;
  }
  else {
    $render_nodes = node_view_multiple($results, variable_get('ds_search_view_mode', 'search_result'));
    $build['search_results'] = $render_nodes['nodes'];
    unset($build['search_results']['#sorted']);
  }

  // Group by type.
  if (variable_get('ds_search_group_by_type') && variable_get('ds_search_group_by_type_settings') && !empty($build['search_results'])) {
    $settings = variable_get('ds_search_group_by_type_settings');
    foreach ($build['search_results'] as $id => $result) {
      if ($settings[$result['#bundle']]['status']) {

        // Type group.
        if (!isset($build['search_results'][$result['#bundle']])) {
          $type = $settings[$result['#bundle']]['wrapper'];
          $title = check_plain(t($settings[$result['#bundle']]['label']));
          $class = 'group-result group-result-' . strtr($result['#bundle'], '_', '-');
          $build['search_results'][$result['#bundle']] = array(
            '#type' => $type,
            '#title' => $title,
            '#weight' => $settings[$result['#bundle']]['weight'],
            '#attributes' => array(
              'class' => array(
                $class,
              ),
            ),
          );
          if ($type == 'markup') {
            $build['search_results'][$result['#bundle']]['#prefix'] = '<div class="' . $class . '">' . (!empty($title) ? ' <h2>' . $title . '</h2>' : '');
            $build['search_results'][$result['#bundle']]['#suffix'] = '</div>';
          }
        }

        // Move result into the wrapper of its type and unset previous.
        $build['search_results'][$result['#bundle']][$id] = $result;
        unset($build['search_results'][$id]);
      }
      else {

        // Other group.
        if (!isset($build['search_results']['ds-other'])) {
          $title = check_plain(t(variable_get('ds_search_group_by_type_other', 'Other')));
          $type = variable_get('ds_search_group_by_type_other_wrapper', 'fieldset');
          $class = 'group-result group-result-other';
          $build['search_results']['ds-other'] = array(
            '#type' => $type,
            '#title' => $title,
            '#weight' => 100,
            '#attributes' => array(
              'class' => array(
                $class,
              ),
            ),
          );
          if ($type == 'markup') {
            $build['search_results']['ds-other']['#prefix'] = '<div class="' . $class . '">' . (!empty($title) ? '<h2>' . $title . '</h2>' : '');
            $build['search_results']['ds-other']['#suffix'] = '</div>';
          }
        }

        // Move result into other wrapper and unset previous.
        $build['search_results']['ds-other'][$id] = $result;
        unset($build['search_results'][$id]);
      }
    }
  }

  // Apache Solr multisearch grouping.
  if (variable_get('ds_search_apachesolr_multisite') && variable_get('ds_search_apachesolr_multisite_group') && variable_get('ds_search_type', 'node') == 'apachesolr_search') {
    $site_counter = array();
    $conf_array = array();
    $config = explode("\n", variable_get('ds_search_apachesolr_multisite_group_config'));
    foreach ($config as $weight => $conf) {
      $conf = trim($conf);
      if (empty($conf)) {
        continue;
      }
      $site_conf = explode('|', $conf);
      $conf_array[$site_conf[0]] = array(
        'label' => $site_conf[1],
        'wrapper' => $site_conf[2],
        'weight' => $weight,
      );
    }

    // Iterate over results.
    foreach ($build['search_results'] as $id => $result) {
      if (!isset($build['search_results'][$result['#site_hash']])) {
        $class = 'group-result group-result-' . strtr($result['#site_hash'], '_', '-');
        $build['search_results'][$result['#site_hash']] = array(
          '#type' => 'fieldset',
          '#weight' => $conf_array[$result['#site_hash']]['weight'],
          '#attributes' => array(
            'class' => array(
              $class,
            ),
          ),
        );

        // Create site counter.
        $site_counter[$result['#site_hash']] = array(
          'counter' => 0,
          'title' => $conf_array[$result['#site_hash']]['label'],
          'type' => $conf_array[$result['#site_hash']]['wrapper'],
          'class' => $class,
        );
      }

      // Move result into other wrapper and unset previous. Also count for
      // every site so we can populate @total_per_site later on.
      $site_counter[$result['#site_hash']]['counter']++;
      $build['search_results'][$result['#site_hash']][$id] = $result;
      unset($build['search_results'][$id]);
    }

    // Site counter.
    foreach ($site_counter as $hash => $values) {
      $title = check_plain(t($values['title'], array(
        '!total_per_site' => format_plural($values['counter'], '1 result', '@count results'),
      )));
      if ($values['type'] == 'div') {
        $build['search_results'][$hash]['#prefix'] = '<div class="' . $values['class'] . '">' . (!empty($title) ? '<h2>' . $title . '</h2>' : '');
        $build['search_results'][$hash]['#suffix'] = '</div>';
      }
      else {
        $build['search_results'][$hash]['#title'] = $title;
      }
    }
  }
  return theme('ds_search_page', $build);
}