1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options; |
19
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet; |
21
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetParser; |
22
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
23
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter; |
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
25
|
|
|
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; |
26
|
|
|
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException; |
27
|
|
|
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class OptionsFacetParser |
31
|
|
|
*/ |
32
|
|
|
class OptionsFacetParser extends AbstractFacetParser |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Dispatcher|null |
36
|
|
|
*/ |
37
|
|
|
protected ?Dispatcher $dispatcher = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Dispatcher $dispatcher |
41
|
|
|
*/ |
42
|
34 |
|
public function injectDispatcher(Dispatcher $dispatcher) |
43
|
|
|
{ |
44
|
34 |
|
$this->dispatcher = $dispatcher; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param SearchResultSet $resultSet |
49
|
|
|
* @param string $facetName |
50
|
|
|
* @param array $facetConfiguration |
51
|
|
|
* @return OptionsFacet|null |
52
|
|
|
* @throws InvalidSlotException |
53
|
|
|
* @throws InvalidSlotReturnException |
54
|
|
|
*/ |
55
|
38 |
|
public function parse(SearchResultSet $resultSet, string $facetName, array $facetConfiguration): ?AbstractFacet |
56
|
|
|
{ |
57
|
38 |
|
$response = $resultSet->getResponse(); |
58
|
38 |
|
$fieldName = $facetConfiguration['field']; |
59
|
38 |
|
$label = $this->getPlainLabelOrApplyCObject($facetConfiguration); |
60
|
38 |
|
$optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response); |
|
|
|
|
61
|
38 |
|
$metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response); |
|
|
|
|
62
|
38 |
|
$optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName); |
63
|
38 |
|
$hasOptionsInResponse = !empty($optionsFromSolrResponse); |
64
|
38 |
|
$hasSelectedOptionsInRequest = count($optionsFromRequest) > 0; |
65
|
38 |
|
$hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest; |
66
|
38 |
|
$hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName); |
67
|
|
|
|
68
|
38 |
|
if ($hasNoOptionsToShow && $hideEmpty) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** @var $facet OptionsFacet */ |
73
|
38 |
|
$facet = GeneralUtility::makeInstance( |
74
|
38 |
|
OptionsFacet::class, |
75
|
38 |
|
$resultSet, |
76
|
38 |
|
$facetName, |
77
|
38 |
|
$fieldName, |
78
|
38 |
|
$label, |
79
|
38 |
|
$facetConfiguration, |
80
|
38 |
|
$this->objectManager |
81
|
38 |
|
); |
82
|
|
|
|
83
|
38 |
|
$hasActiveOptions = count($optionsFromRequest) > 0; |
84
|
38 |
|
$facet->setIsUsed($hasActiveOptions); |
85
|
38 |
|
$facet->setIsAvailable($hasOptionsInResponse); |
86
|
|
|
|
87
|
38 |
|
$optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest); |
88
|
38 |
|
foreach ($optionsToCreate as $optionsValue => $count) { |
89
|
38 |
|
if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
38 |
|
$isOptionsActive = in_array($optionsValue, $optionsFromRequest); |
94
|
38 |
|
$label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration); |
95
|
38 |
|
$facet->addOption( |
96
|
38 |
|
GeneralUtility::makeInstance( |
97
|
38 |
|
Option::class, |
98
|
38 |
|
$facet, |
99
|
38 |
|
$label, |
100
|
38 |
|
$optionsValue, |
101
|
38 |
|
$count, |
102
|
38 |
|
$isOptionsActive, |
103
|
38 |
|
($metricsFromSolrResponse[$optionsValue] ?? []) |
104
|
38 |
|
) |
105
|
38 |
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// after all options have been created we apply a manualSortOrder if configured |
109
|
|
|
// the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not |
110
|
|
|
// need to be handled in the frontend. |
111
|
38 |
|
$this->applyManualSortOrder($facet, $facetConfiguration); |
112
|
38 |
|
$this->applyReverseOrder($facet, $facetConfiguration); |
113
|
|
|
|
114
|
38 |
|
if (!is_null($this->dispatcher)) { |
115
|
34 |
|
$this->dispatcher->dispatch(__CLASS__, 'optionsParsed', [&$facet, $facetConfiguration]); |
116
|
|
|
} |
117
|
|
|
|
118
|
38 |
|
return $facet; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $facetName |
123
|
|
|
* @param ResponseAdapter $response |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
38 |
|
protected function getOptionsFromSolrResponse(string $facetName, ResponseAdapter $response): array |
127
|
|
|
{ |
128
|
38 |
|
$optionsFromSolrResponse = []; |
129
|
38 |
|
if (!isset($response->facets->{$facetName})) { |
130
|
2 |
|
return $optionsFromSolrResponse; |
131
|
|
|
} |
132
|
|
|
|
133
|
36 |
|
foreach ($response->facets->{$facetName}->buckets as $bucket) { |
134
|
36 |
|
$optionValue = $bucket->val; |
135
|
36 |
|
$optionCount = $bucket->count; |
136
|
36 |
|
$optionsFromSolrResponse[(string)$optionValue] = $optionCount; |
137
|
|
|
} |
138
|
|
|
|
139
|
36 |
|
return $optionsFromSolrResponse; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $facetName |
144
|
|
|
* @param ResponseAdapter $response |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
38 |
|
protected function getMetricsFromSolrResponse(string $facetName, ResponseAdapter $response): array |
148
|
|
|
{ |
149
|
38 |
|
$metricsFromSolrResponse = []; |
150
|
|
|
|
151
|
38 |
|
if (!isset($response->facets->{$facetName}->buckets)) { |
152
|
2 |
|
return []; |
153
|
|
|
} |
154
|
|
|
|
155
|
36 |
|
foreach ($response->facets->{$facetName}->buckets as $bucket) { |
156
|
36 |
|
$bucketVariables = get_object_vars($bucket); |
157
|
36 |
|
foreach ($bucketVariables as $key => $value) { |
158
|
36 |
|
if (strpos($key, 'metrics_') === 0) { |
159
|
2 |
|
$metricsKey = str_replace('metrics_', '', $key); |
160
|
2 |
|
$metricsFromSolrResponse[$bucket->val][$metricsKey] = $value; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
36 |
|
return $metricsFromSolrResponse; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|