1 <?php
 2 /**
 3  * @package     Joomla.Libraries
 4  * @subpackage  Search
 5  *
 6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
 7  * @license     GNU General Public License version 2 or later; see LICENSE.txt
 8  */
 9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Helper class for Joomla! Search components
14  *
15  * @since  3.0
16  */
17 class JSearchHelper
18 {
19     /**
20      * Method to log search terms to the database
21      *
22      * @param   string  $term       The term being searched
23      * @param   string  $component  The component being used for the search
24      *
25      * @return  void
26      *
27      * @since   3.0
28      */
29     public static function logSearch($term, $component)
30     {
31         // Initialise our variables
32         $db = JFactory::getDbo();
33         $query = $db->getQuery(true);
34         $enable_log_searches = JComponentHelper::getParams($component)->get('enabled');
35 
36         // Sanitise the term for the database
37         $search_term = $db->escape(trim(strtolower($term)));
38 
39         if ($enable_log_searches)
40         {
41             // Query the table to determine if the term has been searched previously
42             $query->select($db->quoteName('hits'))
43                 ->from($db->quoteName('#__core_log_searches'))
44                 ->where($db->quoteName('search_term') . ' = ' . $db->quote($search_term));
45             $db->setQuery($query);
46             $hits = (int) $db->loadResult();
47 
48             // Reset the $query object
49             $query->clear();
50 
51             // Update the table based on the results
52             if ($hits)
53             {
54                 $query->update($db->quoteName('#__core_log_searches'))
55                     ->set('hits = (hits + 1)')
56                     ->where($db->quoteName('search_term') . ' = ' . $db->quote($search_term));
57             }
58             else
59             {
60                 $query->insert($db->quoteName('#__core_log_searches'))
61                     ->columns(array($db->quoteName('search_term'), $db->quoteName('hits')))
62                     ->values($db->quote($search_term) . ', 1');
63             }
64 
65             // Execute the update query
66             $db->setQuery($query);
67             $db->execute();
68         }
69     }
70 }
71