You are here

top_searches.module in Top Searches 5

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

File

top_searches.module
View source
<?php

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

/**
 * Admin UI. Allow to limit number of items in the results list and allow to clear the results
 */
function top_searches_admin_form() {
  $form = array();
  $op = isset($_POST['op']) ? $_POST['op'] : '';

  // In case we need to clear the DB table, redirect:
  if ($op == t('Reset search counters')) {
    drupal_goto('admin/settings/top_searches/clear');
  }
  $form['top_searches']['top_searches_block_items'] = array(
    '#type' => 'textfield',
    '#maxlength' => 2,
    '#size' => 2,
    '#title' => t('Maximum number of items to show in Top searches block'),
    '#default_value' => variable_get('top_searches_block_items', 50),
  );
  $form['clear_searches'] = array(
    '#type' => 'button',
    '#value' => t('Reset search counters'),
    '#description' => top_searches_count_rows() . ' values',
  );
  return system_settings_form($form);
}

/**
 * Clears the Top Searches table
 */
function top_searches_form_clear() {

  // We first set the message, so we have the right number of rows
  drupal_set_message(t("The Top Searches counters were reset. @number records were deleted", array(
    '@number' => top_searches_count_rows(),
  )));
  db_query("TRUNCATE {top_searches}");
  drupal_goto('admin/settings/top_searches');
}

/**
 * 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_id, &$form) {
  if ($form_id == 'search_form' || $form_id == 'search_block_form') {
    $form['#submit'] = array_merge($form['#submit'], array(
      'top_searches_catch_search_keys' => array(),
    ));
  }
}
function top_searches_catch_search_keys($form_id, $form_values) {
  switch ($form_id) {
    case "search_block_form":
      $keys = $form_values['search_block_form_keys'];
      break;
    case "search_form":
      $keys = $form_values['keys'];
      break;
  }

  // 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();
  if (db_num_rows($result)) {
    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) {
  $output = '';
  foreach ($top_searches as $q) {

    // With counters:

    //$items[] = l($q->q, "search/node/". $q->q) . ' (' . $q->counter . ')';

    // Without counters:
    $items[] = l($q->q, "search/node/" . $q->q);
  }
  return theme('item_list', $items);
}

Functions

Namesort descending Description
theme_top_searches_block
top_searches_admin_form Admin UI. Allow to limit number of items in the results list and allow to clear the results
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_form_clear Clears the Top Searches table
top_searches_menu Implementation of hook_menu().