You are here

public function GoogleSiteSearch::GetSearchHead in Google Site Search 6

Same name and namespace in other branches
  1. 7 includes/GoogleSiteSearch.inc \GoogleSiteSearch::getSearchHead()

Get a head to the search results.

Return value

string The search head HTML code.

File

includes/GoogleSiteSearch.inc, line 388

Class

GoogleSiteSearch
Class for interaction with Google Site Search.

Code

public function GetSearchHead() {

  // get total pages
  $totalResults = $this
    ->GetTotalResults();
  $pageSize = $this
    ->GetPageSize();
  $currentPage = $this
    ->GetCurrentPage();
  $show_start = $pageSize * $currentPage - $pageSize + 1;
  $show_end = $pageSize * $currentPage;
  if ($show_end > $totalResults) {
    $show_end = $totalResults;
  }
  $html = '<div class="searchhead">';
  if (variable_get('gss_labels', TRUE) == 1) {

    // adding the tabs for the search labels
    $html .= '<span>' . t("Show only results of type:") . '</span><ul>';

    // checking which one is the active tab
    $active_html_first_element = "";
    $active_html_others = "";
    if (strpos($this->query, "more:") === FALSE) {

      //there is no faceted search
      $active_html_first_element = 'class="active"';

      //then the first element is active
      $term_from_query = "";
    }
    else {
      $term_from_query = end(explode(":", $this->query));

      //in this case we store the category to check later
    }
    $this->query = current(explode(" more:", $this->query));

    //resume $this->query to its original value (no categories)

    // first item will always be the original query
    $html .= '<li>' . '<a ' . $active_html_first_element . ' href=' . urlencode($this->query) . '>' . t('All results') . '</a>' . '</li>';

    // loop to create all remaining categorized tabs / queries
    if ($totalResults > 0) {
      foreach ($this->categories as $categ) {
        $active_html_others = "";

        //do not inherit from previous loop...
        if ($categ['label'] == $term_from_query) {
          $active_html_others = 'class="active"';
        }
        $new_query = urlencode($this->query) . '+more%3A' . $categ['label'];

        //builds up the path to the tab links
        $html .= '<li>' . '<a ' . $active_html_others . ' href=' . $new_query . '>' . $categ['anchor_text'] . '</a>' . '</li>';
      }
    }
    $html .= '</ul>';
  }
  if ($totalResults !== 0) {
    $html .= t('Shows @show_start to @show_end of approximately @totalResults hits', array(
      '@show_start' => $show_start,
      '@show_end' => $show_end,
      '@totalResults' => $totalResults,
    ));
  }
  $html .= '</div>';

  // return search head
  return $html;
}