You are here

top_searches.module in Top Searches 6

Same filename and directory in other branches
  1. 5 top_searches.module
  2. 7 top_searches.module

File

top_searches.module
View source
<?php

/**
 * Registering theming functions
 */
function top_searches_theme() {
  return array(
    'top_searches_block' => array(
      'file' => 'top_searches.module',
      'arguments' => array(
        'top_searches' => array(),
      ),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function top_searches_menu() {
  $items = array();
  $items['admin/settings/top_searches'] = array(
    'access arguments' => array(
      'administer blocks',
    ),
    'title' => 'Top Searches',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'top_searches_admin_form',
    ),
    'description' => 'General settings for the Top Searches module.',
    'file' => 'top_searches.admin.inc',
  );
  $items['admin/settings/top_searches/clear'] = array(
    'access arguments' => array(
      'administer blocks',
    ),
    'title' => 'Top Searches',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'top_searches_form_clear_confirm',
    ),
    'description' => 'General settings for the Top Searches module.',
    'file' => 'top_searches.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_block().
 */
function top_searches_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Top Searches');
      return $blocks;
    case 'view':
      switch ($delta) {
        case 0:
          $top_searches = top_searches_collect_results();
          if (count($top_searches)) {
            $block['subject'] = t("Top Searches");
            $block['content'] = theme('top_searches_block', $top_searches);
          }
          break;
      }
      return $block;
  }
}
function top_searches_form_alter(&$form, &$form_state, $form_id) {

  // Don't count admin searches (like admin/user/search)
  if (arg(0) == 'admin') {
    return;
  }

  // Only count node searches
  // TODO: allow different blocks for different types of seraches
  if (isset($form['module']['#value']) && $form['module']['#value'] != 'node') {
    return;
  }

  // We consider forms which have "search_box_form_submit" as '#submit' to be search forms:
  if (in_array($form_id, array(
    'search_form',
    'search_block_form',
    'search_theme_form',
  ))) {
    $form['#submit'][] = 'top_searches_catch_search_keys';
  }
}
function top_searches_catch_search_keys($form, $form_state) {
  switch ($form['form_id']['#value']) {
    case "search_block_form":
      $keys = $form_state['values']['search_block_form'];
      break;
    case "search_theme_form":
      $keys = $form_state['values']['search_theme_form'];
      break;
    case "search_form":
      $keys = $form_state['values']['keys'];
      break;
  }

  // Send keys to top_searches hook
  if ($keys) {
    $valid = _top_searches_valid_phrase($keys);
    if ($valid) {
      top_searches_top_searches($keys);
    }
  }
}

/**
 * Verify the keys contain at least one valid word
 * @return True on valid phrase, FALSE on non-valid
 */
function _top_searches_valid_phrase($keys) {

  // Check for minimum search word size, according to the site wide search configuration:
  $min = variable_get('minimum_word_size', 3);
  $words = explode(' ', $keys);
  $pass = FALSE;
  foreach ($words as $word) {
    _search_index_truncate($word);

    // We need at least one word with minimal length, for the whole search phrase to be valid.
    if (drupal_strlen($word) >= $min) {
      $pass = TRUE;
      break;
    }
  }
  return $pass;
}

/**
 * Hook top_searches.
 * Modules may use this hook to add their search queries. 
 * They can later theme the results, to alter the url of the search phrases, to point elsewhere than "search/node/*"
 *
 * @param $keys
 *    Search phrases to be registered by top_searches.
 */
function top_searches_top_searches($keys = NULL) {
  if (!is_string($keys)) {
    return;
  }
  else {
    top_searches_process_keys($keys);
  }
}
function top_searches_process_keys($keys) {

  // Beautify the search phrase
  $keys = preg_replace("/[' ']{2,}/", ' ', ucwords(strtolower(trim($keys))));

  // Search the DB for existing keys:
  $results_qid = db_result(db_query("SELECT qid FROM {top_searches} WHERE q = '%s'", $keys));
  if ($results_qid) {
    db_query("UPDATE {top_searches} SET counter = (counter + 1) WHERE qid = %d", $results_qid);
  }
  else {
    db_query("INSERT INTO {top_searches} (q, counter) VALUES ('%s', %d)", $keys, 1);
  }
}
function top_searches_collect_results() {
  $result = db_query("SELECT q, counter FROM {top_searches} ORDER by counter DESC LIMIT %d", variable_get('top_searches_block_items', 50));
  $top_searches = array();
  while ($row = db_fetch_object($result)) {
    $top_searches[] = $row;
  }
  return $top_searches;
}
function top_searches_count_rows() {
  $result = db_result(db_query("SELECT COUNT(*) FROM {top_searches}"));
  return $result ? $result : '0';
}
function theme_top_searches_block($top_searches) {
  $show_counters = variable_get('top_searches_show_counters', 0);
  $output = '';
  foreach ($top_searches as $q) {
    $items[] = l($q->q, "search/node/" . $q->q) . ($show_counters ? ' (' . $q->counter . ')' : '');
  }
  return theme('item_list', $items);
}

Functions

Namesort descending Description
theme_top_searches_block
top_searches_block Implementation of hook_block().
top_searches_catch_search_keys
top_searches_collect_results
top_searches_count_rows
top_searches_form_alter
top_searches_menu Implementation of hook_menu().
top_searches_process_keys
top_searches_theme Registering theming functions
top_searches_top_searches Hook top_searches. Modules may use this hook to add their search queries. They can later theme the results, to alter the url of the search phrases, to point elsewhere than "search/node/*"
_top_searches_valid_phrase Verify the keys contain at least one valid word