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 3 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\Domain\Search\Query\Query; |
28
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The Grouping ParameterProvider is responsible to build the solr query parameters |
32
|
|
|
* that are needed for the grouping. |
33
|
|
|
* |
34
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder |
35
|
|
|
*/ |
36
|
|
|
class Grouping extends AbstractDeactivatableParameterBuilder implements ParameterBuilder |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $fields = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $sortings = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $queries = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
protected $numberOfGroups = 5; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
protected $resultsPerGroup = 1; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Grouping constructor. |
66
|
|
|
* |
67
|
|
|
* @param bool $isEnabled |
68
|
|
|
* @param array $fields |
69
|
|
|
* @param array $sortings |
70
|
|
|
* @param array $queries |
71
|
|
|
* @param int $numberOfGroups |
72
|
|
|
* @param int $resultsPerGroup |
73
|
|
|
*/ |
74
|
172 |
|
public function __construct($isEnabled, array $fields = [], array $sortings = [], array $queries = [], $numberOfGroups = 5, $resultsPerGroup = 1) |
75
|
|
|
{ |
76
|
172 |
|
$this->isEnabled = $isEnabled; |
77
|
172 |
|
$this->fields = $fields; |
78
|
172 |
|
$this->sortings = $sortings; |
79
|
172 |
|
$this->queries = $queries; |
80
|
172 |
|
$this->numberOfGroups = $numberOfGroups; |
81
|
172 |
|
$this->resultsPerGroup = $resultsPerGroup; |
82
|
172 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Query $query |
86
|
|
|
* @return Query |
87
|
|
|
*/ |
88
|
117 |
|
public function build(Query $query): Query |
89
|
|
|
{ |
90
|
117 |
|
if (!$this->isEnabled) { |
91
|
113 |
|
$query->getQueryParametersContainer()->removeMany(['group', 'group.format', 'group.ngroups', 'group.limit', 'group.query', 'group.sort', 'group.field']); |
92
|
|
|
|
93
|
113 |
|
return $query; |
94
|
|
|
} |
95
|
5 |
|
$groupingParameter = []; |
96
|
5 |
|
$groupingParameter ['group'] = 'true'; |
97
|
5 |
|
$groupingParameter ['group.format'] = 'grouped'; |
98
|
5 |
|
$groupingParameter ['group.ngroups'] = 'true'; |
99
|
|
|
|
100
|
5 |
|
if ($this->resultsPerGroup) { |
101
|
5 |
|
$groupingParameter['group.limit'] = $this->resultsPerGroup; |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
if (count($this->queries) > 0) { |
105
|
1 |
|
$groupingParameter['group.query'] = $this->queries; |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
if (count($this->sortings) > 0) { |
109
|
2 |
|
$groupingParameter['group.sort'] = $this->sortings; |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
if (count($this->fields) > 0) { |
113
|
1 |
|
$groupingParameter['group.field'] = $this->fields; |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
$query->getQueryParametersContainer()->merge($groupingParameter); |
117
|
5 |
|
return $query; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
1 |
|
public function getFields() |
124
|
|
|
{ |
125
|
1 |
|
return $this->fields; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $fields |
130
|
|
|
*/ |
131
|
|
|
public function setFields(array $fields) |
132
|
|
|
{ |
133
|
|
|
$this->fields = $fields; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $field |
138
|
|
|
*/ |
139
|
2 |
|
public function addField(string $field) |
140
|
|
|
{ |
141
|
2 |
|
$this->fields[] = $field; |
142
|
2 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
2 |
|
public function getSortings() |
148
|
|
|
{ |
149
|
2 |
|
return $this->sortings; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $sorting |
154
|
|
|
*/ |
155
|
3 |
|
public function addSorting($sorting) |
156
|
|
|
{ |
157
|
3 |
|
$this->sortings[] = $sorting; |
158
|
3 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $sortings |
162
|
|
|
*/ |
163
|
|
|
public function setSortings(array $sortings) |
164
|
|
|
{ |
165
|
|
|
$this->sortings = $sortings; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
1 |
|
public function getQueries(): array |
172
|
|
|
{ |
173
|
1 |
|
return $this->queries; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $query |
178
|
|
|
*/ |
179
|
2 |
|
public function addQuery($query) |
180
|
|
|
{ |
181
|
2 |
|
$this->queries[] = $query; |
182
|
2 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $queries |
186
|
|
|
*/ |
187
|
|
|
public function setQueries(array $queries) |
188
|
|
|
{ |
189
|
|
|
$this->queries = $queries; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return int |
194
|
|
|
*/ |
195
|
2 |
|
public function getNumberOfGroups() |
196
|
|
|
{ |
197
|
2 |
|
return $this->numberOfGroups; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param int $numberOfGroups |
202
|
|
|
*/ |
203
|
2 |
|
public function setNumberOfGroups($numberOfGroups) |
204
|
|
|
{ |
205
|
2 |
|
$this->numberOfGroups = $numberOfGroups; |
206
|
2 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return int |
210
|
|
|
*/ |
211
|
1 |
|
public function getResultsPerGroup() |
212
|
|
|
{ |
213
|
1 |
|
return $this->resultsPerGroup; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param int $resultsPerGroup |
218
|
|
|
*/ |
219
|
1 |
|
public function setResultsPerGroup($resultsPerGroup) |
220
|
|
|
{ |
221
|
1 |
|
$resultsPerGroup = max(intval($resultsPerGroup), 0); |
222
|
1 |
|
$this->resultsPerGroup = $resultsPerGroup; |
223
|
1 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param TypoScriptConfiguration $solrConfiguration |
227
|
|
|
* @return Grouping |
228
|
|
|
*/ |
229
|
142 |
|
public static function fromTypoScriptConfiguration(TypoScriptConfiguration $solrConfiguration) |
230
|
|
|
{ |
231
|
142 |
|
$isEnabled = $solrConfiguration->getSearchGrouping(); |
232
|
142 |
|
if (!$isEnabled) { |
233
|
141 |
|
return new Grouping(false); |
234
|
|
|
} |
235
|
|
|
|
236
|
1 |
|
$fields = []; |
237
|
1 |
|
$queries = []; |
238
|
1 |
|
$sortings = []; |
239
|
|
|
|
240
|
1 |
|
$resultsPerGroup = $solrConfiguration->getSearchGroupingHighestGroupResultsLimit(); |
241
|
1 |
|
$configuredGroups = $solrConfiguration->getSearchGroupingGroupsConfiguration(); |
242
|
1 |
|
$numberOfGroups = $solrConfiguration->getSearchGroupingNumberOfGroups(); |
243
|
1 |
|
$sortBy = $solrConfiguration->getSearchGroupingSortBy(); |
244
|
|
|
|
245
|
1 |
|
foreach ($configuredGroups as $groupName => $groupConfiguration) { |
246
|
|
|
if (isset($groupConfiguration['field'])) { |
247
|
|
|
$fields[] = $groupConfiguration['field']; |
248
|
|
|
} elseif (isset($groupConfiguration['query'])) { |
249
|
|
|
$queries[] = $groupConfiguration['query']; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
1 |
|
if (!empty(trim($sortBy))) { |
254
|
1 |
|
$sortings[] = $sortBy; |
255
|
|
|
} |
256
|
|
|
|
257
|
1 |
|
return new Grouping($isEnabled, $fields, $sortings, $queries, $numberOfGroups, $resultsPerGroup); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return Grouping |
262
|
|
|
*/ |
263
|
|
|
public static function getEmpty() |
264
|
|
|
{ |
265
|
|
|
return new Grouping(false); |
266
|
|
|
} |
267
|
|
|
} |