You are here

top_searches.module in Top Searches 7

Same filename and directory in other branches
  1. 5 top_searches.module
  2. 6 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',
      'variables' => array(
        'top_searches' => array(),
      ),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function top_searches_menu() {
  $items = array();
  $items['admin/config/search/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/config/search/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() for 7.x ver:
* the hook_block functionallity is splited into 4 different functions
 */
function top_searches_block_info() {
  $blocks[0]['info'] = t('Top Searches');
  return $blocks;
}
function top_searches_block_view($delta = 0) {
  switch ($delta) {
    case 0:
      $block['subject'] = "";
      $top_searches = top_searches_collect_results();
      if (count($top_searches)) {
        $variable = array();
        $block['subject'] = t("Top Searches");
        foreach ($top_searches as $searchq) {
          $variable['top_searches'][] = array(
            'ts_q' => $searchq->q,
            'ts_counter' => $searchq->counter,
          );
        }
        $block['content'] = theme('top_searches_block', $variable);
      }
      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);
  }
}

//  version 7.x The db_query function has been splitted into db_ý* family functions
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_query("SELECT qid FROM {top_searches} WHERE q = :phrase", array(
    ':phrase' => $keys,
  ))
    ->fetchField();
  if ($results_qid) {
    db_query("UPDATE {top_searches} SET counter = (counter + 1) WHERE qid = {$results_qid}");

    /* failed to make the D7 syntax works. the ->fields property doesn't work. need more research:
         db_update('top_searches')//->expression('counter', 'counter + :counter', array(':counter' => 1))
               ->fields(array('counter'))
               ->condition('qid' , $results_qid)
               ->execute();
    */
  }
  else {
    db_insert('top_searches')
      ->fields(array(
      'q' => $keys,
      'counter' => 1,
    ))
      ->execute();
  }
}

// version 7.x
function top_searches_collect_results() {
  $limit = variable_get('top_searches_block_items', 50);
  $result = db_query("SELECT q, counter FROM {top_searches} ORDER by counter DESC LIMIT {$limit} ");

  //:limit_rows", array( ':limit_rows' => variable_get('top_searches_block_items', 50)));
  $top_searches = array();
  while ($row = $result
    ->fetchObject()) {
    $top_searches[] = $row;
  }
  return $top_searches;
}

// for 7.x is not recommanded to use "SELECT COUNT(*) FROM {table}" query,
// but no alternative is suggested. so we stay with this.
function top_searches_count_rows() {
  $result = db_query("SELECT COUNT(*) FROM {top_searches}")
    ->fetchField();
  return $result ? $result : '0';
}
function theme_top_searches_block($variables) {
  $show_counters = variable_get('top_searches_show_counters', 0);
  foreach ($variables['top_searches'] as $value) {
    $items['items'][] = l($value['ts_q'], 'search/node/' . $value['ts_q']) . ($show_counters ? ' (' . $value['ts_counter'] . ')' : '');
  }
  return theme('item_list', $items);
}

Functions

Namesort descending Description
theme_top_searches_block
top_searches_block_info
top_searches_block_view
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