You are here

search_api_stats.module in Search API Stats 7

Same filename and directory in other branches
  1. 8 search_api_stats.module

File

search_api_stats.module
View source
<?php

/**
 * @file
 * search_api_stats.module
 */

/**
 * Implements hook_permission().
 */
function search_api_stats_permission() {
  return array(
    'access search api stats' => array(
      'title' => t('access search api stats'),
      'description' => t('access search api stats.'),
    ),
  );
}

/**
 * Implements hook_search_api_query_alter($query).
 *
 * @param SearchApiQueryInterface $query
 *  The SearchApiQueryInterface object representing the search query.
 */
function search_api_stats_search_api_query_alter(SearchApiQueryInterface $query) {
  global $user, $language;

  // Assign some object variables to avoid chained member access.
  $index = $query
    ->getIndex();
  if (!empty($index)) {
    $server = $index
      ->server();
  }

  // Fail out if there's no valid index or server objects to work from.
  if (empty($index) || empty($server)) {
    return;
  }
  $keywords = trim(drupal_strtolower($query
    ->getOriginalKeys()));

  //to avoid to insert empty keywords value into database.
  if (!empty($keywords)) {
    db_insert('search_api_stats')
      ->fields(array(
      's_name' => $server->machine_name,
      'i_name' => $index->machine_name,
      'timestamp' => REQUEST_TIME,
      'uid' => $user->uid,
      'sid' => session_id(),
      'keywords' => $keywords,
      'filters' => '',
      'sort' => '',
      'language' => $language->language,
    ))
      ->execute();
  }
}

/**
 * Implements of hook_views_api().
 */
function search_api_stats_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'search_api_stats') . '/includes/views',
  );
}