1: <?php
2:
3: /**
4: * This file contains the collections and items for search tracking
5: *
6: * @package Core
7: * @subpackage GenericDB_Model
8: * @author Mischa Holz
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: /**
17: * Tracking collection
18: *
19: * @package Core
20: * @subpackage GenericDB_Model
21: */
22: class cApiSearchTrackingCollection extends ItemCollection {
23: /**
24: * Constructor to create an instance of this class.
25: *
26: * @throws cInvalidArgumentException
27: */
28: public function __construct() {
29: global $cfg;
30: parent::__construct($cfg['tab']['search_tracking'], 'idsearchtracking');
31:
32: $this->_setItemClass('cApiSearchTracking');
33: }
34:
35: /**
36: * Create a new tracking row
37: *
38: * @param string $searchTerm
39: * Term the user searched for
40: * @param int $searchResults
41: * Number of results
42: * @param string $timestamp [optional]
43: * Timestamp of the search
44: * @param int $idclient [optional]
45: * Client
46: * @param int $idlang [optional]
47: * Language
48: *
49: * @return bool
50: * @throws cDbException
51: * @throws cException
52: * @throws cInvalidArgumentException
53: */
54: public function create($searchTerm, $searchResults, $timestamp = "", $idclient = 0, $idlang = 0) {
55: $item = $this->createNewItem();
56: $item->set("searchterm", $searchTerm);
57: $item->set("results", $searchResults);
58: $item->set("datesearched", ($timestamp == "") ? date('Y-m-d H:i:s') : $timestamp);
59: $item->set("idclient", ($idclient == 0) ? cRegistry::getClientId() : $idclient);
60: $item->set("idlang", ($idlang == 0) ? cRegistry::getLanguageId() : $idlang);
61:
62: return $item->store();
63: }
64:
65: /**
66: * Track a search if the setting allows it.
67: *
68: * @param string $searchTerm
69: * Term the user searched for
70: * @param int $resultCount
71: * Number of results
72: *
73: * @return bool
74: * @throws cDbException
75: * @throws cException
76: * @throws cInvalidArgumentException
77: */
78: public function trackSearch($searchTerm, $resultCount) {
79: if (getEffectiveSetting("search", "term_tracking", "on") != "on") {
80: return false;
81: }
82:
83: return $this->create($searchTerm, $resultCount);
84: }
85:
86: /**
87: * Select all search terms of this client and language and sort them by
88: * popularity
89: *
90: * @param int $idclient [optional]
91: * Use this client instead of the current one
92: * @param int $idlang [optional]
93: * Use this language instead of the current one
94: * @return bool
95: * @throws cDbException
96: * @deprecated Since 4.10.1, We can't use fields created by AVG or COUNT here! Result sets received by this function will contain all search term entries, not the cumulated ones.
97: */
98: public function selectPopularSearchTerms($idclient = 0, $idlang = 0) {
99: return $this->select('idclient=' . (($idclient == 0) ? cRegistry::getClientId() : $idclient) . ' AND idlang=' . (($idlang == 0) ? cRegistry::getLanguageId() : $idlang), 'searchterm, idsearchtracking, idclient, idlang, results, datesearched', 'COUNT(searchterm) DESC');
100: }
101:
102: /**
103: * Query all search terms of this client and language, group them by search
104: * term and sort them by popularity.
105: *
106: * The record sets created by this query contain following fields:
107: * - searchterm = The search term
108: * - avgresults = Average result of the search term
109: * - countsearchterm = The number of search for the search term
110: *
111: * @param int $idclient [optional]
112: * Use this client instead of the current one
113: * @param int $idlang [optional]
114: * Use this language instead of the current one
115: * @return cDb
116: * @throws cDbException
117: */
118: public function queryPopularSearchTerms($idclient = 0, $idlang = 0) {
119: $idclient = ($idclient == 0) ? cRegistry::getClientId() : $idclient;
120: $idlang = ($idlang == 0) ? cRegistry::getLanguageId() : $idclient;
121: $db = cRegistry::getDb(); // Don't use own db instance, use a new one!
122: $sql = 'SELECT searchterm, AVG(results) AS avgresults, COUNT(searchterm) AS countsearchterm FROM `%s` '
123: . 'WHERE idclient=%d AND idlang=%d GROUP BY searchterm ORDER BY COUNT(searchterm) DESC';
124: $db->query($sql, $this->table, $idclient, $idlang);
125: return $db;
126: }
127:
128: /**
129: * Select all entries about one search term for this client and language
130: * sorted by the date
131: *
132: * @param string $term
133: * Term the user searched for
134: * @param int $idclient [optional]
135: * Use this client instead of the current one
136: * @param int $idlang [optional]
137: * Use this language instead of the current one
138: * @return bool
139: * @throws cDbException
140: */
141: public function selectSearchTerm($term, $idclient = 0, $idlang = 0) {
142: return $this->select('searchterm=\'' . addslashes($term) . '\' AND idclient=' . (($idclient == 0) ? cRegistry::getClientId() : $idclient) . ' AND idlang=' . (($idlang == 0) ? cRegistry::getLanguageId() : $idlang), '', 'datesearched DESC');
143: }
144:
145: }
146:
147: /**
148: * SearchTracking item
149: *
150: * @package Core
151: * @subpackage GenericDB_Model
152: */
153: class cApiSearchTracking extends Item
154: {
155: /**
156: * Constructor to create an instance of this class.
157: *
158: * @param bool $mId [optional]
159: * Item Id
160: *
161: * @throws cDbException
162: * @throws cException
163: */
164: public function __construct($mId = false) {
165: global $cfg;
166:
167: parent::__construct($cfg['tab']['search_tracking'], 'idsearchtracking');
168: $this->setFilters(array(
169: 'addslashes'
170: ), array(
171: 'stripslashes'
172: ));
173:
174: if ($mId !== false) {
175: $this->loadByPrimaryKey($mId);
176: }
177: }
178:
179: }
180: