1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options; |
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\DefaultFacetQueryBuilder; |
18
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\FacetQueryBuilderInterface; |
19
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\SortingExpression; |
20
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class OptionsFacetQueryBuilder |
24
|
|
|
* |
25
|
|
|
* The Options facet query builder builds the facets as json structure |
26
|
|
|
* |
27
|
|
|
* @Todo: When we use json faceting for other facets some logic of this class can be moved to the base class. |
28
|
|
|
* |
29
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options |
30
|
|
|
*/ |
31
|
|
|
class OptionsFacetQueryBuilder extends DefaultFacetQueryBuilder implements FacetQueryBuilderInterface { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $facetName |
35
|
|
|
* @param TypoScriptConfiguration $configuration |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function build($facetName, TypoScriptConfiguration $configuration) |
39
|
|
|
{ |
40
|
|
|
$facetParameters = []; |
41
|
|
|
$facetConfiguration = $configuration->getSearchFacetingFacetByName($facetName); |
42
|
|
|
|
43
|
|
|
$jsonFacetOptions = [ |
44
|
|
|
'type' => 'terms', |
45
|
|
|
'field' => $facetConfiguration['field'], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$jsonFacetOptions['limit'] = $this->buildLimitForJson($facetConfiguration, $configuration); |
49
|
|
|
$jsonFacetOptions['mincount'] = $this->buildMincountForJson($facetConfiguration, $configuration); |
50
|
|
|
|
51
|
|
|
$sorting = $this->buildSortingForJson($facetConfiguration); |
52
|
|
|
if (!empty($sorting)) { |
53
|
|
|
$jsonFacetOptions['sort'] = $sorting; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (is_array($facetConfiguration['metrics.'])) { |
57
|
|
|
foreach ($facetConfiguration['metrics.'] as $key => $value) { |
58
|
|
|
$jsonFacetOptions['facet']['metrics_' . $key] = $value; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$excludeTags = $this->buildExcludeTagsForJson($facetConfiguration, $configuration); |
63
|
|
|
if (!empty($excludeTags)) { |
64
|
|
|
$jsonFacetOptions['domain']['excludeTags'] = $excludeTags; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$facetParameters['json.facet'][$facetName] = $jsonFacetOptions; |
68
|
|
|
|
69
|
|
|
return $facetParameters; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $facetConfiguration |
74
|
|
|
* @param TypoScriptConfiguration $configuration |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function buildExcludeTagsForJson(array $facetConfiguration, TypoScriptConfiguration $configuration) |
78
|
|
|
{ |
79
|
|
|
$isKeepAllOptionsActiveForSingleFacet = $facetConfiguration['keepAllOptionsOnSelection'] == 1; |
80
|
|
|
$isKeepAllOptionsActiveGlobalsAndCountsEnabled = $configuration->getSearchFacetingKeepAllFacetsOnSelection() |
81
|
|
|
&& $configuration->getSearchFacetingCountAllFacetsForSelection(); |
82
|
|
|
|
83
|
|
|
if ($isKeepAllOptionsActiveForSingleFacet || $isKeepAllOptionsActiveGlobalsAndCountsEnabled) { |
84
|
|
|
return $facetConfiguration['field']; |
85
|
|
|
} else { |
86
|
|
|
// keepAllOptionsOnSelection globally active |
87
|
|
|
$facets = []; |
88
|
|
|
foreach ($configuration->getSearchFacetingFacets() as $facet) { |
89
|
|
|
$facets[] = $facet['field']; |
90
|
|
|
} |
91
|
|
|
return implode(',', $facets); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $facetConfiguration |
97
|
|
|
* @param TypoScriptConfiguration $configuration |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
protected function buildLimitForJson(array $facetConfiguration, TypoScriptConfiguration $configuration) |
101
|
|
|
{ |
102
|
|
|
if (isset($facetConfiguration['facetLimit'])) { |
103
|
|
|
return (int)$facetConfiguration['facetLimit']; |
104
|
|
|
} elseif ($configuration->getSearchFacetingFacetLimit() > 0) { |
105
|
|
|
return $configuration->getSearchFacetingFacetLimit(); |
106
|
|
|
} else { |
107
|
|
|
return -1; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $facetConfiguration |
113
|
|
|
* @param TypoScriptConfiguration $configuration |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
|
|
protected function buildMincountForJson(array $facetConfiguration, TypoScriptConfiguration $configuration) |
117
|
|
|
{ |
118
|
|
|
if (isset($facetConfiguration['minimumCount'])) { |
119
|
|
|
return (int)$facetConfiguration['minimumCount']; |
120
|
|
|
} elseif ($configuration->getSearchFacetingMinimumCount() > 0) { |
121
|
|
|
return $configuration->getSearchFacetingMinimumCount(); |
122
|
|
|
} else { |
123
|
|
|
return 1; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $facetConfiguration |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function buildSortingForJson(array $facetConfiguration) { |
132
|
|
|
if (isset($facetConfiguration['sortBy'])) { |
133
|
|
|
$sortingExpression = new SortingExpression(); |
134
|
|
|
$sorting = $facetConfiguration['sortBy']; |
135
|
|
|
$direction = $facetConfiguration['sortDirection']; |
136
|
|
|
return $sortingExpression->getForJsonFacet($sorting, $direction); |
137
|
|
|
} |
138
|
|
|
return ''; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|