You are here

gss.theme.inc in Google Site Search 7

Same filename and directory in other branches
  1. 6 gss.theme.inc

Themeable functions for Google Site Search.

File

gss.theme.inc
View source
<?php

/**
 * @file
 * Themeable functions for Google Site Search.
 */

/**
 * Process variables for gss-result.tpl.php.
 *
 * @see gss-result.tpl.php
 */
function template_preprocess_gss_result(&$variables) {
  $result = $variables['result'];
  $variables['url'] = check_url($result['link']);
  $variables['title'] = $result['title'];

  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';

  // Check for existence.
  $variables['thumbnail_url'] = isset($result['thumbnail_url']) ? check_url($result['thumbnail_url']) : '';

  // Info variables.
  $variables['info_split'] = array();
  $variables['info'] = '';
  $variables['show_info'] = FALSE;
  $url = parse_url($result['link']);
  $url = trim($url['path'], '/');
  $languages = array_keys(language_list());
  $url_lang = NULL;
  foreach ($languages as $lang) {
    if ($lang . '/' === substr($url, 0, 3)) {
      $url_lang = substr($url, 0, 2);
      $url = substr($url, 3);
      break;
    }
  }

  // Get the Drupal system path for the URL alias.
  preg_match('/node\\/(\\d+)/', $url, $matches);
  if (isset($matches[1])) {
    $nid = (int) $matches[1];
  }
  else {
    $system_path = drupal_lookup_path('source', $url, $url_lang);
    preg_match('/node\\/(\\d+)/', $system_path, $matches);
    $nid = isset($matches[1]) ? (int) $matches[1] : 0;
  }
  if ($nid && ($node = node_load($nid))) {
    $user = user_load($node->uid);
    $user_name = l($user->name, 'user/' . $user->uid);
    $info = array(
      'type' => $node->type,
      'user' => $user_name,
      'date' => format_date($node->created, 'short'),
    );
    $variables['info_split'] = $info;
    $variables['info'] = implode(' | ', $info);
    $variables['show_info'] = variable_get('gss_info', FALSE);
  }
}

/**
 * Process variables for gss-results.tpl.php.
 *
 * @see gss-results.tpl.php
 */
function template_preprocess_gss_results(&$variables) {
  $results = $variables['results'];
  $variables['head'] = $results['head'];
  unset($results['head']);
  $variables['pager'] = $results['pager'];
  unset($results['pager']);
  $variables['search_results'] = '';
  foreach ($results as $entry) {
    $variables['search_results'] .= theme('gss_result', array(
      'result' => $entry,
    ));
  }
}

/**
 * Returns HTML for a query pager.
 *
 * Menu callbacks that display paged query results should call
 * theme('gss_pager') to retrieve a pager control so that users can view other
 * results.
 *
 * Display the mini pager with only Next/Previous Button.
 *
 * @param $variables
 *   An associative array containing:
 *   - tags: An array of labels for the controls in the pager.
 *   - element: An optional integer to distinguish between multiple pagers on
 *     one page.
 *   - parameters: An associative array of query string parameters to append to
 *     the pager links.
 *
 * @ingroup themeable
 */
function theme_gss_pager($variables) {
  $tags = $variables['tags'];
  $element = $variables['element'];
  $parameters = $variables['parameters'];
  global $pager_page_array, $pager_total;

  // Current is the page we are currently paged to.
  $pager_current = $pager_page_array[$element] + 1;

  // End of generation loop preparation.
  $li_previous = theme('pager_previous', array(
    'text' => isset($tags[1]) ? $tags[1] : t('‹ previous'),
    'element' => $element,
    'interval' => 1,
    'parameters' => $parameters,
  ));
  $li_next = theme('pager_next', array(
    'text' => isset($tags[3]) ? $tags[3] : t('next ›'),
    'element' => $element,
    'interval' => 1,
    'parameters' => $parameters,
  ));
  if ($pager_total[$element] > 1) {
    if ($li_previous) {
      $items[] = array(
        'class' => array(
          'pager-previous',
        ),
        'data' => $li_previous,
      );
    }
    $items[] = array(
      'class' => array(
        'pager-current',
      ),
      'data' => t('You are on page @current', array(
        '@current' => $pager_current,
      )),
    );

    // End generation.
    if ($li_next) {
      $items[] = array(
        'class' => array(
          'pager-next',
        ),
        'data' => $li_next,
      );
    }
    return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array(
      'items' => $items,
      'attributes' => array(
        'class' => array(
          'pager',
        ),
      ),
    ));
  }
}

Functions

Namesort descending Description
template_preprocess_gss_result Process variables for gss-result.tpl.php.
template_preprocess_gss_results Process variables for gss-results.tpl.php.
theme_gss_pager Returns HTML for a query pager.