|
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
|
43 |
|
public function build($facetName, TypoScriptConfiguration $configuration) |
|
39
|
|
|
{ |
|
40
|
43 |
|
$facetParameters = []; |
|
41
|
43 |
|
$facetConfiguration = $configuration->getSearchFacetingFacetByName($facetName); |
|
42
|
|
|
|
|
43
|
|
|
$jsonFacetOptions = [ |
|
44
|
43 |
|
'type' => 'terms', |
|
45
|
43 |
|
'field' => $facetConfiguration['field'], |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
43 |
|
$jsonFacetOptions['limit'] = $this->buildLimitForJson($facetConfiguration, $configuration); |
|
49
|
43 |
|
$jsonFacetOptions['mincount'] = $this->buildMincountForJson($facetConfiguration, $configuration); |
|
50
|
|
|
|
|
51
|
43 |
|
$sorting = $this->buildSortingForJson($facetConfiguration); |
|
52
|
43 |
|
if (!empty($sorting)) { |
|
53
|
4 |
|
$jsonFacetOptions['sort'] = $sorting; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
43 |
|
if (is_array($facetConfiguration['metrics.'])) { |
|
57
|
1 |
|
foreach ($facetConfiguration['metrics.'] as $key => $value) { |
|
58
|
1 |
|
$jsonFacetOptions['facet']['metrics_' . $key] = $value; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
43 |
|
$excludeTags = $this->buildExcludeTagsForJson($facetConfiguration, $configuration); |
|
63
|
43 |
|
if (!empty($excludeTags)) { |
|
64
|
37 |
|
$jsonFacetOptions['domain']['excludeTags'] = $excludeTags; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
43 |
|
$facetParameters['json.facet'][$facetName] = $jsonFacetOptions; |
|
68
|
|
|
|
|
69
|
43 |
|
return $facetParameters; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $facetConfiguration |
|
74
|
|
|
* @param TypoScriptConfiguration $configuration |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
43 |
|
protected function buildExcludeTagsForJson(array $facetConfiguration, TypoScriptConfiguration $configuration) |
|
78
|
|
|
{ |
|
79
|
43 |
|
$isKeepAllOptionsActiveForSingleFacet = $facetConfiguration['keepAllOptionsOnSelection'] == 1; |
|
80
|
43 |
|
$isKeepAllOptionsActiveGlobalsAndCountsEnabled = $configuration->getSearchFacetingKeepAllFacetsOnSelection() |
|
81
|
43 |
|
&& $configuration->getSearchFacetingCountAllFacetsForSelection(); |
|
82
|
|
|
|
|
83
|
43 |
|
if ($isKeepAllOptionsActiveForSingleFacet || $isKeepAllOptionsActiveGlobalsAndCountsEnabled) { |
|
84
|
30 |
|
return $facetConfiguration['field']; |
|
85
|
|
|
} else { |
|
86
|
|
|
// keepAllOptionsOnSelection globally active |
|
87
|
43 |
|
$facets = []; |
|
88
|
43 |
|
foreach ($configuration->getSearchFacetingFacets() as $facet) { |
|
89
|
37 |
|
$facets[] = $facet['field']; |
|
90
|
|
|
} |
|
91
|
43 |
|
return implode(',', $facets); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param array $facetConfiguration |
|
97
|
|
|
* @param TypoScriptConfiguration $configuration |
|
98
|
|
|
* @return int |
|
99
|
|
|
*/ |
|
100
|
43 |
|
protected function buildLimitForJson(array $facetConfiguration, TypoScriptConfiguration $configuration) |
|
101
|
|
|
{ |
|
102
|
43 |
|
if (isset($facetConfiguration['facetLimit'])) { |
|
103
|
1 |
|
return (int)$facetConfiguration['facetLimit']; |
|
104
|
42 |
|
} elseif ($configuration->getSearchFacetingFacetLimit() > 0) { |
|
105
|
38 |
|
return $configuration->getSearchFacetingFacetLimit(); |
|
106
|
|
|
} else { |
|
107
|
4 |
|
return -1; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param array $facetConfiguration |
|
113
|
|
|
* @param TypoScriptConfiguration $configuration |
|
114
|
|
|
* @return int |
|
115
|
|
|
*/ |
|
116
|
43 |
|
protected function buildMincountForJson(array $facetConfiguration, TypoScriptConfiguration $configuration) |
|
117
|
|
|
{ |
|
118
|
43 |
|
if (isset($facetConfiguration['minimumCount'])) { |
|
119
|
1 |
|
return (int)$facetConfiguration['minimumCount']; |
|
120
|
42 |
|
} elseif ($configuration->getSearchFacetingMinimumCount() > 0) { |
|
121
|
38 |
|
return $configuration->getSearchFacetingMinimumCount(); |
|
122
|
|
|
} else { |
|
123
|
4 |
|
return 1; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param array $facetConfiguration |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
43 |
|
protected function buildSortingForJson(array $facetConfiguration) { |
|
132
|
43 |
|
if (isset($facetConfiguration['sortBy'])) { |
|
133
|
4 |
|
$sortingExpression = new SortingExpression(); |
|
134
|
4 |
|
$sorting = $facetConfiguration['sortBy']; |
|
135
|
4 |
|
$direction = $facetConfiguration['sortDirection']; |
|
136
|
4 |
|
return $sortingExpression->getForJsonFacet($sorting, $direction); |
|
137
|
|
|
} |
|
138
|
40 |
|
return ''; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|