|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Hierarchy; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the TYPO3 CMS project. |
|
6
|
|
|
* |
|
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
9
|
|
|
* of the License, or any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
* |
|
14
|
|
|
* The TYPO3 project - inspiring people to share! |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetParser; |
|
18
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
|
19
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\ParsingUtil; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class HierarchyFacetParser |
|
23
|
|
|
*/ |
|
24
|
|
|
class HierarchyFacetParser extends AbstractFacetParser |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @param SearchResultSet $resultSet |
|
28
|
|
|
* @param string $facetName |
|
29
|
|
|
* @param array $facetConfiguration |
|
30
|
|
|
* @return HierarchyFacet|null |
|
31
|
35 |
|
*/ |
|
32
|
|
|
public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration) |
|
33
|
35 |
|
{ |
|
34
|
35 |
|
$response = $resultSet->getResponse(); |
|
35
|
35 |
|
$fieldName = $facetConfiguration['field']; |
|
36
|
35 |
|
$label = $this->getPlainLabelOrApplyCObject($facetConfiguration); |
|
37
|
35 |
|
$optionsFromSolrResponse = isset($response->facet_counts->facet_fields->{$fieldName}) ? ParsingUtil::getMapArrayFromFlatArray($response->facet_counts->facet_fields->{$fieldName}) : []; |
|
38
|
35 |
|
$optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName); |
|
39
|
35 |
|
$hasOptionsInResponse = !empty($optionsFromSolrResponse); |
|
40
|
35 |
|
$hasSelectedOptionsInRequest = count($optionsFromRequest) > 0; |
|
41
|
35 |
|
$hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest; |
|
42
|
|
|
$hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName); |
|
43
|
35 |
|
|
|
44
|
4 |
|
if ($hasNoOptionsToShow && $hideEmpty) { |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
32 |
|
/** @var $facet HierarchyFacet */ |
|
49
|
|
|
$facet = $this->objectManager->get(HierarchyFacet::class, $resultSet, $facetName, $fieldName, $label, $facetConfiguration); |
|
|
|
|
|
|
50
|
32 |
|
|
|
51
|
32 |
|
$hasActiveOptions = count($optionsFromRequest) > 0; |
|
52
|
|
|
$facet->setIsUsed($hasActiveOptions); |
|
53
|
32 |
|
|
|
54
|
|
|
$facet->setIsAvailable($hasOptionsInResponse); |
|
55
|
32 |
|
|
|
56
|
|
|
$nodesToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest); |
|
57
|
32 |
|
|
|
58
|
32 |
|
if ($this->facetOptionsMustBeResorted($facetConfiguration)) { |
|
59
|
|
|
$nodesToCreate = $this->sortFacetOptionsInNaturalOrder($nodesToCreate); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
32 |
|
|
|
62
|
32 |
|
foreach ($nodesToCreate as $value => $count) { |
|
63
|
32 |
|
if ($this->getIsExcludedFacetValue($value, $facetConfiguration)) { |
|
64
|
32 |
|
continue; |
|
65
|
32 |
|
} |
|
66
|
32 |
|
$isActive = in_array($value, $optionsFromRequest); |
|
67
|
32 |
|
$delimiterPosition = strpos($value, '-'); |
|
68
|
32 |
|
$path = substr($value, $delimiterPosition + 1); |
|
69
|
|
|
$pathArray = $this->getPathAsArray($path); |
|
70
|
32 |
|
$key = array_pop($pathArray); |
|
71
|
|
|
$parentKey = array_pop($pathArray); |
|
72
|
|
|
$value = '/' . $path; |
|
73
|
32 |
|
$label = $this->getLabelFromRenderingInstructions($key, $count, $facetName, $facetConfiguration); |
|
74
|
|
|
|
|
75
|
|
|
$facet->createNode($parentKey, $key, $label, $value, $count, $isActive); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $facet; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Sorts facet options in natural order. |
|
83
|
32 |
|
* Options must be sorted in natural order, |
|
84
|
|
|
* because lower nesting levels must be instantiated first, to serve as parents for higher nested levels. |
|
85
|
32 |
|
* See implementation of HierarchyFacet::createNode(). |
|
86
|
32 |
|
* |
|
87
|
32 |
|
* @param array $flatOptionsListForFacet |
|
88
|
32 |
|
* @return void sorted list of facet options |
|
89
|
32 |
|
*/ |
|
90
|
32 |
|
protected function sortFacetOptionsInNaturalOrder(array $flatOptionsListForHierarchyFacet) |
|
91
|
|
|
{ |
|
92
|
|
|
uksort($flatOptionsListForHierarchyFacet, "strnatcmp"); |
|
93
|
|
|
return $flatOptionsListForHierarchyFacet; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Checks if options must be resorted. |
|
98
|
|
|
* |
|
99
|
35 |
|
* Apache Solr facet.sort can be set globally or per facet. |
|
100
|
|
|
* Relevant TypoScript paths: |
|
101
|
35 |
|
* plugin.tx_solr.search.faceting.sortBy causes facet.sort Apache Solr parameter |
|
102
|
35 |
|
* plugin.tx_solr.search.faceting.facets.[facetName].sortBy causes f.<fieldname>.facet.sort parameter |
|
103
|
|
|
* |
|
104
|
35 |
|
* see: https://lucene.apache.org/solr/guide/6_6/faceting.html#Faceting-Thefacet.sortParameter |
|
105
|
|
|
* see: https://wiki.apache.org/solr/SimpleFacetParameters#facet.sort : "This parameter can be specified on a per field basis." |
|
106
|
3 |
|
* |
|
107
|
3 |
|
* @param array $facetConfiguration |
|
108
|
3 |
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
3 |
|
protected function facetOptionsMustBeResorted(array $facetConfiguration) |
|
111
|
|
|
{ |
|
112
|
35 |
|
if (isset($facetConfiguration['sortBy']) && $facetConfiguration['sortBy'] === 'index') { |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* This method is used to get the path array from a hierarchical facet. It substitutes escaped slashes to keep them |
|
121
|
|
|
* when they are used inside a facetValue. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $path |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function getPathAsArray($path) |
|
127
|
|
|
{ |
|
128
|
|
|
$path = str_replace('\/', '@@@', $path); |
|
129
|
|
|
$path = rtrim($path, "/"); |
|
130
|
|
|
$segments = explode('/', $path); |
|
131
|
|
|
return array_map(function($item) { |
|
132
|
|
|
return str_replace('@@@', '/', $item); |
|
133
|
|
|
}, $segments); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Retrieves the active facetValue for a facet from the search request. |
|
138
|
|
|
* @param SearchResultSet $resultSet |
|
139
|
|
|
* @param string $facetName |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function getActiveFacetValuesFromRequest(SearchResultSet $resultSet, $facetName) |
|
143
|
|
|
{ |
|
144
|
|
|
$activeFacetValues = []; |
|
145
|
|
|
$values = $resultSet->getUsedSearchRequest()->getActiveFacetValuesByName($facetName); |
|
146
|
|
|
|
|
147
|
|
|
foreach (is_array($values) ? $values : [] as $valueFromRequest) { |
|
148
|
|
|
// Attach the 'depth' param again to the value |
|
149
|
|
|
if (strpos($valueFromRequest, '-') === false) { |
|
150
|
|
|
$valueFromRequest = HierarchyTool::substituteSlashes($valueFromRequest); |
|
151
|
|
|
$valueFromRequest = trim($valueFromRequest, '/'); |
|
152
|
|
|
$valueFromRequest = (count(explode('/', $valueFromRequest)) - 1) . '-' . $valueFromRequest . '/'; |
|
153
|
|
|
$valueFromRequest = HierarchyTool::unSubstituteSlashes($valueFromRequest); |
|
154
|
|
|
} |
|
155
|
|
|
$activeFacetValues[] = $valueFromRequest; |
|
156
|
|
|
} |
|
157
|
|
|
return $activeFacetValues; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.