|
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
|
|
|
$this->objectManager |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$hasActiveOptions = count($optionsFromRequest) > 0; |
|
81
|
|
|
$facet->setIsUsed($hasActiveOptions); |
|
82
|
|
|
$facet->setIsAvailable($hasOptionsInResponse); |
|
83
|
|
|
|
|
84
|
|
|
$optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest); |
|
85
|
|
|
foreach ($optionsToCreate as $optionsValue => $count) { |
|
86
|
|
|
if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$isOptionsActive = in_array($optionsValue, $optionsFromRequest); |
|
91
|
|
|
$label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration); |
|
92
|
|
|
$facet->addOption( |
|
93
|
|
|
GeneralUtility::makeInstance( |
|
94
|
|
|
Option::class, |
|
95
|
|
|
$facet, |
|
96
|
|
|
$label, |
|
97
|
|
|
$optionsValue, |
|
98
|
|
|
$count, |
|
99
|
|
|
$isOptionsActive, |
|
100
|
|
|
($metricsFromSolrResponse[$optionsValue] ?? []) |
|
101
|
|
|
) |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// after all options have been created we apply a manualSortOrder if configured |
|
106
|
|
|
// the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not |
|
107
|
|
|
// need to be handled in the frontend. |
|
108
|
|
|
$this->applyManualSortOrder($facet, $facetConfiguration); |
|
109
|
|
|
$this->applyReverseOrder($facet, $facetConfiguration); |
|
110
|
|
|
|
|
111
|
|
|
if (isset($this->eventDispatcher)) { |
|
112
|
|
|
/* @var AfterFacetParsedEvent $afterFacetParsedEvent */ |
|
113
|
|
|
$afterFacetParsedEvent = $this->eventDispatcher |
|
114
|
|
|
->dispatch(new AfterFacetParsedEvent($facet, $facetConfiguration)); |
|
|
|
|
|
|
115
|
|
|
$facet = $afterFacetParsedEvent->getFacet(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $facet; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $facetName |
|
123
|
|
|
* @param ResponseAdapter $response |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function getOptionsFromSolrResponse(string $facetName, ResponseAdapter $response): array |
|
127
|
|
|
{ |
|
128
|
|
|
$optionsFromSolrResponse = []; |
|
129
|
|
|
if (!isset($response->facets->{$facetName})) { |
|
130
|
|
|
return $optionsFromSolrResponse; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
foreach ($response->facets->{$facetName}->buckets as $bucket) { |
|
134
|
|
|
$optionValue = $bucket->val; |
|
135
|
|
|
$optionCount = $bucket->count; |
|
136
|
|
|
$optionsFromSolrResponse[$optionValue] = $optionCount; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $optionsFromSolrResponse; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $facetName |
|
144
|
|
|
* @param ResponseAdapter $response |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function getMetricsFromSolrResponse(string $facetName, ResponseAdapter $response): array |
|
148
|
|
|
{ |
|
149
|
|
|
$metricsFromSolrResponse = []; |
|
150
|
|
|
|
|
151
|
|
|
if (!isset($response->facets->{$facetName}->buckets)) { |
|
152
|
|
|
return []; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
foreach ($response->facets->{$facetName}->buckets as $bucket) { |
|
156
|
|
|
$bucketVariables = get_object_vars($bucket); |
|
157
|
|
|
foreach ($bucketVariables as $key => $value) { |
|
158
|
|
|
if (str_starts_with($key, 'metrics_')) { |
|
159
|
|
|
$metricsKey = str_replace('metrics_', '', $key); |
|
160
|
|
|
$metricsFromSolrResponse[$bucket->val][$metricsKey] = $value; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $metricsFromSolrResponse; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|