You are here

function search_api_stats_search_api_query_alter in Search API Stats 7

Same name and namespace in other branches
  1. 8 search_api_stats.module \search_api_stats_search_api_query_alter()

Implements hook_search_api_query_alter($query).

Parameters

SearchApiQueryInterface $query: The SearchApiQueryInterface object representing the search query.

File

./search_api_stats.module, line 24
search_api_stats.module

Code

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();
  }
}