1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\LastSearches; |
4
|
|
|
|
5
|
|
|
/*************************************************************** |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2015-2016 Timo Schmidt <[email protected]> |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
29
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
30
|
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The LastSearchesService is responsible to return the LastSearches from the session or database, |
34
|
|
|
* depending on the configuration. |
35
|
|
|
* |
36
|
|
|
* @author Timo Schmidt <[email protected]> |
37
|
|
|
*/ |
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 LastSearchesRepository |
53
|
|
|
*/ |
54
|
|
|
protected $lastSearchesRepository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param TypoScriptConfiguration $typoscriptConfiguration |
58
|
|
|
* @param TypoScriptFrontendController $tsfe |
59
|
|
|
* @param LastSearchesRepository|null $lastSearchesRepository |
60
|
|
|
*/ |
61
|
31 |
|
public function __construct(TypoScriptConfiguration $typoscriptConfiguration, TypoScriptFrontendController $tsfe, LastSearchesRepository $lastSearchesRepository = null) |
62
|
|
|
{ |
63
|
31 |
|
$this->configuration = $typoscriptConfiguration; |
64
|
31 |
|
$this->tsfe = $tsfe; |
65
|
31 |
|
$this->lastSearchesRepository = isset($lastSearchesRepository) ? $lastSearchesRepository : GeneralUtility::makeInstance(LastSearchesRepository::class); |
66
|
31 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieves the last searches from the session or database depending on the configuration. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
30 |
|
public function getLastSearches() |
74
|
|
|
{ |
75
|
30 |
|
$lastSearchesKeywords = []; |
76
|
30 |
|
$mode = $this->configuration->getSearchLastSearchesMode(); |
77
|
30 |
|
$limit = $this->configuration->getSearchLastSearchesLimit(); |
78
|
|
|
|
79
|
|
|
switch ($mode) { |
80
|
30 |
|
case 'user': |
81
|
27 |
|
$lastSearchesKeywords = $this->getLastSearchesFromSession($limit); |
82
|
27 |
|
break; |
83
|
3 |
|
case 'global': |
84
|
3 |
|
$lastSearchesKeywords = $this->lastSearchesRepository->findAllKeywords($limit); |
85
|
3 |
|
break; |
86
|
|
|
} |
87
|
|
|
|
88
|
30 |
|
return $lastSearchesKeywords; |
89
|
|
|
} |
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
|
20 |
|
public function addToLastSearches($keywords) |
98
|
|
|
{ |
99
|
20 |
|
$mode = $this->configuration->getSearchLastSearchesMode(); |
100
|
|
|
switch ($mode) { |
101
|
20 |
|
case 'user': |
102
|
19 |
|
$this->storeKeywordsToSession($keywords); |
103
|
19 |
|
break; |
104
|
1 |
|
case 'global': |
105
|
1 |
|
$this->lastSearchesRepository->add($keywords, (int)$this->configuration->getSearchLastSearchesLimit()); |
106
|
1 |
|
break; |
107
|
|
|
default: |
108
|
|
|
throw new \UnexpectedValueException( |
109
|
|
|
'Unknown mode for plugin.tx_solr.search.lastSearches.mode, valid modes are "user" or "global".', |
110
|
|
|
1342456570 |
111
|
|
|
); |
112
|
|
|
} |
113
|
20 |
|
} |
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
|
27 |
|
protected function getLastSearchesFromSession($limit) |
122
|
|
|
{ |
123
|
27 |
|
$lastSearches = $this->getLastSearchesFromFrontendSession(); |
124
|
|
|
|
125
|
27 |
|
if (!is_array($lastSearches)) { |
126
|
8 |
|
return []; |
127
|
|
|
} |
128
|
|
|
|
129
|
19 |
|
$lastSearches = array_slice(array_reverse(array_unique($lastSearches)), 0, $limit); |
130
|
|
|
|
131
|
19 |
|
return $lastSearches; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
protected function getLastSearchesFromFrontendSession() |
138
|
|
|
{ |
139
|
|
|
return $this->tsfe->fe_user->getKey('ses', 'tx_solr_lastSearches'); |
140
|
3 |
|
} |
141
|
|
|
|
142
|
3 |
|
/** |
143
|
3 |
|
* Stores the keywords from the current query to the user's session. |
144
|
3 |
|
* |
145
|
3 |
|
* @param string $keywords The current query's keywords |
146
|
3 |
|
* @return void |
147
|
3 |
|
*/ |
148
|
|
|
protected function storeKeywordsToSession($keywords) |
149
|
|
|
{ |
150
|
|
|
$currentLastSearches = $this->tsfe->fe_user->getKey('ses', 'tx_solr_lastSearches'); |
151
|
|
|
|
152
|
3 |
|
if (!is_array($currentLastSearches)) { |
153
|
1 |
|
$currentLastSearches = []; |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
$lastSearches = $currentLastSearches; |
157
|
|
|
$newLastSearchesCount = array_push($lastSearches, $keywords); |
158
|
2 |
|
|
159
|
2 |
|
while ($newLastSearchesCount > $this->configuration->getSearchLastSearchesLimit()) { |
160
|
|
|
array_shift($lastSearches); |
161
|
|
|
$newLastSearchesCount = count($lastSearches); |
162
|
2 |
|
} |
163
|
|
|
|
164
|
|
|
$this->tsfe->fe_user->setKey( |
165
|
|
|
'ses', |
166
|
|
|
'tx_solr_lastSearches', |
167
|
|
|
$lastSearches |
168
|
26 |
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|