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\Event\Parser\AfterFacetParsedEvent; |
24
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter; |
25
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
26
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class OptionsFacetParser |
30
|
|
|
*/ |
31
|
|
|
class OptionsFacetParser extends AbstractFacetParser |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var EventDispatcherInterface|null |
35
|
|
|
*/ |
36
|
|
|
protected ?EventDispatcherInterface $eventDispatcher; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
40
|
|
|
*/ |
41
|
|
|
public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher) |
42
|
|
|
{ |
43
|
|
|
$this->eventDispatcher = $eventDispatcher; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param SearchResultSet $resultSet |
48
|
|
|
* @param string $facetName |
49
|
|
|
* @param array $facetConfiguration |
50
|
|
|
* @return OptionsFacet|null |
51
|
|
|
*/ |
52
|
|
|
public function parse(SearchResultSet $resultSet, string $facetName, array $facetConfiguration): ?AbstractFacet |
53
|
|
|
{ |
54
|
|
|
$response = $resultSet->getResponse(); |
55
|
|
|
$fieldName = $facetConfiguration['field']; |
56
|
|
|
$label = $this->getPlainLabelOrApplyCObject($facetConfiguration); |
57
|
|
|
$optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response); |
|
|
|
|
58
|
|
|
$metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response); |
|
|
|
|
59
|
|
|
$optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName); |
60
|
|
|
$hasOptionsInResponse = !empty($optionsFromSolrResponse); |
61
|
|
|
$hasSelectedOptionsInRequest = count($optionsFromRequest) > 0; |
62
|
|
|
$hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest; |
63
|
|
|
$hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName); |
64
|
|
|
|
65
|
|
|
if ($hasNoOptionsToShow && $hideEmpty) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @var $facet OptionsFacet */ |
70
|
|
|
$facet = GeneralUtility::makeInstance( |
71
|
|
|
OptionsFacet::class, |
72
|
|
|
$resultSet, |
73
|
|
|
$facetName, |
74
|
|
|
$fieldName, |
75
|
|
|
$label, |
76
|
|
|
$facetConfiguration |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$hasActiveOptions = count($optionsFromRequest) > 0; |
80
|
|
|
$facet->setIsUsed($hasActiveOptions); |
81
|
|
|
$facet->setIsAvailable($hasOptionsInResponse); |
82
|
|
|
|
83
|
|
|
$optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest); |
84
|
|
|
foreach ($optionsToCreate as $optionsValue => $count) { |
85
|
|
|
if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) { |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$isOptionsActive = in_array($optionsValue, $optionsFromRequest); |
90
|
|
|
$label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration); |
91
|
|
|
$facet->addOption( |
92
|
|
|
GeneralUtility::makeInstance( |
93
|
|
|
Option::class, |
94
|
|
|
$facet, |
95
|
|
|
$label, |
96
|
|
|
$optionsValue, |
97
|
|
|
$count, |
98
|
|
|
$isOptionsActive, |
99
|
|
|
($metricsFromSolrResponse[$optionsValue] ?? []) |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// after all options have been created we apply a manualSortOrder if configured |
105
|
|
|
// the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not |
106
|
|
|
// need to be handled in the frontend. |
107
|
|
|
$this->applyManualSortOrder($facet, $facetConfiguration); |
108
|
|
|
$this->applyReverseOrder($facet, $facetConfiguration); |
109
|
|
|
|
110
|
|
|
if (isset($this->eventDispatcher)) { |
111
|
|
|
/* @var AfterFacetParsedEvent $afterFacetParsedEvent */ |
112
|
|
|
$afterFacetParsedEvent = $this->eventDispatcher |
113
|
|
|
->dispatch(new AfterFacetParsedEvent($facet, $facetConfiguration)); |
|
|
|
|
114
|
|
|
$facet = $afterFacetParsedEvent->getFacet(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $facet; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $facetName |
122
|
|
|
* @param ResponseAdapter $response |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function getOptionsFromSolrResponse(string $facetName, ResponseAdapter $response): array |
126
|
|
|
{ |
127
|
|
|
$optionsFromSolrResponse = []; |
128
|
|
|
if (!isset($response->facets->{$facetName})) { |
129
|
|
|
return $optionsFromSolrResponse; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
foreach ($response->facets->{$facetName}->buckets as $bucket) { |
133
|
|
|
$optionValue = $bucket->val; |
134
|
|
|
$optionCount = $bucket->count; |
135
|
|
|
$optionsFromSolrResponse[$optionValue] = $optionCount; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $optionsFromSolrResponse; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $facetName |
143
|
|
|
* @param ResponseAdapter $response |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
protected function getMetricsFromSolrResponse(string $facetName, ResponseAdapter $response): array |
147
|
|
|
{ |
148
|
|
|
$metricsFromSolrResponse = []; |
149
|
|
|
|
150
|
|
|
if (!isset($response->facets->{$facetName}->buckets)) { |
151
|
|
|
return []; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
foreach ($response->facets->{$facetName}->buckets as $bucket) { |
155
|
|
|
$bucketVariables = get_object_vars($bucket); |
156
|
|
|
foreach ($bucketVariables as $key => $value) { |
157
|
|
|
if (str_starts_with($key, 'metrics_')) { |
158
|
|
|
$metricsKey = str_replace('metrics_', '', $key); |
159
|
|
|
$metricsFromSolrResponse[$bucket->val][$metricsKey] = $value; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $metricsFromSolrResponse; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|