You are here

function top_searches_process_keys in Top Searches 7

Same name and namespace in other branches
  1. 6 top_searches.module \top_searches_process_keys()
1 call to top_searches_process_keys()
top_searches_top_searches in ./top_searches.module
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/*"

File

./top_searches.module, line 138

Code

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