1 | <?php |
||
38 | class LastSearchesService |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * @var TypoScriptConfiguration |
||
43 | */ |
||
44 | protected $configuration; |
||
45 | |||
46 | /** |
||
47 | * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController |
||
48 | */ |
||
49 | protected $tsfe; |
||
50 | |||
51 | /** |
||
52 | * @var \TYPO3\CMS\Core\Database\DatabaseConnection |
||
53 | */ |
||
54 | protected $database; |
||
55 | |||
56 | /** |
||
57 | * @param TypoScriptConfiguration $typoscriptConfiguration |
||
58 | * @param TypoScriptFrontendController $tsfe |
||
59 | * @param DatabaseConnection $database |
||
60 | */ |
||
61 | 25 | public function __construct(TypoScriptConfiguration $typoscriptConfiguration, TypoScriptFrontendController $tsfe, DatabaseConnection $database) |
|
67 | |||
68 | /** |
||
69 | * Retrieves the last searches from the session or database depending on the configuration. |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | 25 | public function getLastSearches() |
|
90 | |||
91 | /** |
||
92 | * Saves the keywords to the last searches in the database or session depending on the configuration. |
||
93 | * |
||
94 | * @param string $keywords |
||
95 | * @throws \UnexpectedValueException |
||
96 | */ |
||
97 | 15 | public function addToLastSearches($keywords) |
|
114 | |||
115 | /** |
||
116 | * Gets the last searched keywords from the user's session |
||
117 | * |
||
118 | * @param int $limit |
||
119 | * @return array An array containing the last searches of the current user |
||
120 | */ |
||
121 | 23 | protected function getLastSearchesFromSession($limit) |
|
133 | |||
134 | /** |
||
135 | * Gets the last searched keywords from the database |
||
136 | * |
||
137 | * @param int $limit |
||
138 | * @return array An array containing the last searches of the current user |
||
139 | */ |
||
140 | 2 | protected function getLastSearchesFromDatabase($limit = 10) |
|
141 | { |
||
142 | 2 | $lastSearchesRows = $this->database->exec_SELECTgetRows( |
|
143 | 2 | 'DISTINCT keywords', |
|
144 | 2 | 'tx_solr_last_searches', |
|
145 | 2 | '', |
|
146 | 2 | '', |
|
147 | 2 | 'tstamp DESC', |
|
148 | 2 | $limit |
|
149 | ); |
||
150 | 2 | ||
151 | // If no records could be found return empty result |
||
152 | 2 | if (empty($lastSearchesRows)) { |
|
153 | 2 | return []; |
|
154 | 2 | } |
|
155 | 2 | ||
156 | $lastSearches = []; |
||
157 | 2 | ||
158 | foreach ($lastSearchesRows as $row) { |
||
159 | $lastSearches[] = html_entity_decode($row['keywords'], ENT_QUOTES, 'UTF-8'); |
||
160 | } |
||
161 | |||
162 | return $lastSearches; |
||
163 | 22 | } |
|
164 | |||
165 | 22 | /** |
|
166 | * @return mixed |
||
167 | */ |
||
168 | protected function getLastSearchesFromFrontendSession() |
||
172 | |||
173 | /** |
||
174 | 14 | * Stores the keywords from the current query to the user's session. |
|
175 | * |
||
176 | 14 | * @param string $keywords The current query's keywords |
|
177 | * @return void |
||
178 | 14 | */ |
|
179 | 14 | protected function storeKeywordsToSession($keywords) |
|
201 | |||
202 | /** |
||
203 | 1 | * Stores the keywords from the current query to the database. |
|
204 | * |
||
205 | 1 | * @param string $keywords The current query's keywords |
|
206 | * @return void |
||
207 | 1 | */ |
|
208 | protected function storeKeywordsToDatabase($keywords) |
||
224 | |||
225 | 1 | /** |
|
226 | * Gets the sequence id for the next search entry. |
||
227 | 1 | * |
|
228 | 1 | * @return int The id to be used as the next sequence id for storing the last search keywords. |
|
229 | */ |
||
230 | 1 | protected function getNextSequenceId() |
|
250 | } |
||
251 |