1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The Grouping ParameterProvider is responsible to build the solr query parameters |
31
|
|
|
* that are needed for the grouping. |
32
|
|
|
* |
33
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder |
34
|
|
|
*/ |
35
|
|
|
class Grouping implements ParameterBuilder |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var boolean |
40
|
|
|
*/ |
41
|
|
|
protected $isEnabled = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $fields = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $sortings = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $queries = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
protected $numberOfGroups = 5; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var int |
65
|
|
|
*/ |
66
|
|
|
protected $resultsPerGroup = 1; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Grouping constructor. |
70
|
|
|
* |
71
|
|
|
* private constructor should only be created with the from* methods |
72
|
|
|
* |
73
|
|
|
* @param bool $isEnabled |
74
|
|
|
* @param array $fields |
75
|
|
|
* @param array $sortings |
76
|
|
|
* @param array $queries |
77
|
|
|
* @param int $numberOfGroups |
78
|
|
|
* @param int $resultsPerGroup |
79
|
|
|
*/ |
80
|
|
|
private function __construct($isEnabled, array $fields = [], array $sortings = [], array $queries = [], $numberOfGroups = 5, $resultsPerGroup = 1) |
81
|
|
|
{ |
82
|
|
|
$this->isEnabled = $isEnabled; |
83
|
|
|
$this->fields = $fields; |
84
|
|
|
$this->sortings = $sortings; |
85
|
|
|
$this->queries = $queries; |
86
|
|
|
$this->numberOfGroups = $numberOfGroups; |
87
|
|
|
$this->resultsPerGroup = $resultsPerGroup; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function build() |
94
|
|
|
{ |
95
|
|
|
if (!$this->isEnabled) { |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
$groupingParameter = []; |
99
|
|
|
$groupingParameter ['group'] = 'true'; |
100
|
|
|
$groupingParameter ['group.format'] = 'grouped'; |
101
|
|
|
$groupingParameter ['group.ngroups'] = 'true'; |
102
|
|
|
|
103
|
|
|
if ($this->resultsPerGroup) { |
104
|
|
|
$groupingParameter['group.limit'] = $this->resultsPerGroup; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (count($this->queries) > 0) { |
108
|
|
|
$groupingParameter['group.query'] = $this->queries; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (count($this->sortings) > 0) { |
112
|
|
|
$groupingParameter['group.sort'] = $this->sortings; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (count($this->fields) > 0) { |
116
|
|
|
$groupingParameter['group.field'] = $this->sortings; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $groupingParameter; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
|
|
public function getIsEnabled() |
126
|
|
|
{ |
127
|
|
|
return $this->isEnabled; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param boolean $isEnabled |
132
|
|
|
*/ |
133
|
|
|
public function setIsEnabled($isEnabled) |
134
|
|
|
{ |
135
|
|
|
$this->isEnabled = $isEnabled; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function getFields() |
142
|
|
|
{ |
143
|
|
|
return $this->fields; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param array $fields |
148
|
|
|
*/ |
149
|
|
|
public function setFields(array $fields) |
150
|
|
|
{ |
151
|
|
|
$this->fields = $fields; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $field |
156
|
|
|
*/ |
157
|
|
|
public function addField(string $field) |
158
|
|
|
{ |
159
|
|
|
$this->fields[] = $field; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function getSortings() |
166
|
|
|
{ |
167
|
|
|
return $this->sortings; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $sorting |
172
|
|
|
*/ |
173
|
|
|
public function addSorting($sorting) |
174
|
|
|
{ |
175
|
|
|
$this->sortings[] = $sorting; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $sortings |
180
|
|
|
*/ |
181
|
|
|
public function setSortings(array $sortings) |
182
|
|
|
{ |
183
|
|
|
$this->sortings = $sortings; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
public function getQueries(): array |
190
|
|
|
{ |
191
|
|
|
return $this->queries; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $query |
196
|
|
|
*/ |
197
|
|
|
public function addQuery($query) |
198
|
|
|
{ |
199
|
|
|
$this->queries[] = $query; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param array $queries |
204
|
|
|
*/ |
205
|
|
|
public function setQueries(array $queries) |
206
|
|
|
{ |
207
|
|
|
$this->queries = $queries; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return int |
212
|
|
|
*/ |
213
|
|
|
public function getNumberOfGroups() |
214
|
|
|
{ |
215
|
|
|
return $this->numberOfGroups; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param int $numberOfGroups |
220
|
|
|
*/ |
221
|
|
|
public function setNumberOfGroups($numberOfGroups) |
222
|
|
|
{ |
223
|
|
|
$this->numberOfGroups = $numberOfGroups; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return int |
228
|
|
|
*/ |
229
|
|
|
public function getResultsPerGroup() |
230
|
|
|
{ |
231
|
|
|
return $this->resultsPerGroup; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param int $resultsPerGroup |
236
|
|
|
*/ |
237
|
|
|
public function setResultsPerGroup($resultsPerGroup) |
238
|
|
|
{ |
239
|
|
|
$resultsPerGroup = max(intval($resultsPerGroup), 0); |
240
|
|
|
$this->resultsPerGroup = $resultsPerGroup; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param TypoScriptConfiguration $solrConfiguration |
245
|
|
|
* @return Grouping |
246
|
|
|
*/ |
247
|
|
|
public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration) |
248
|
|
|
{ |
249
|
|
|
$isEnabled = $solrConfiguration->getSearchGrouping(); |
250
|
|
|
if (!$isEnabled) { |
251
|
|
|
return new Grouping(false); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$fields = []; |
255
|
|
|
$queries = []; |
256
|
|
|
$sortings = []; |
257
|
|
|
|
258
|
|
|
$resultsPerGroup = $solrConfiguration->getSearchGroupingHighestGroupResultsLimit(); |
259
|
|
|
$configuredGroups = $solrConfiguration->getSearchGroupingGroupsConfiguration(); |
260
|
|
|
$numberOfGroups = $solrConfiguration->getSearchGroupingNumberOfGroups(); |
261
|
|
|
|
262
|
|
|
foreach ($configuredGroups as $groupName => $groupConfiguration) { |
263
|
|
|
if (isset($groupConfiguration['field'])) { |
264
|
|
|
$fields[] = $groupConfiguration['field']; |
265
|
|
|
} elseif (isset($groupConfiguration['query'])) { |
266
|
|
|
$queries[] = $groupConfiguration['query']; |
267
|
|
|
} |
268
|
|
|
if (isset($groupConfiguration['sortBy'])) { |
269
|
|
|
$sortings[] = $groupConfiguration['sortBy']; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return new Grouping($isEnabled, $fields, $sortings, $queries, $numberOfGroups, $resultsPerGroup); |
274
|
|
|
} |
275
|
|
|
} |