|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class FluentSiteState|Firesphere\SolrFluent\States\FluentSiteState Set the site state for each indexing group for |
|
4
|
|
|
* Fluent translations |
|
5
|
|
|
* |
|
6
|
|
|
* @package Firesphere\SolrFluent\States |
|
7
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
|
8
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Firesphere\SolrFluent\States; |
|
12
|
|
|
|
|
13
|
|
|
use Firesphere\SolrSearch\Interfaces\SiteStateInterface; |
|
14
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
|
15
|
|
|
use Firesphere\SolrSearch\States\SiteState; |
|
16
|
|
|
use ReflectionException; |
|
17
|
|
|
use TractorCow\Fluent\Extension\FluentExtension; |
|
18
|
|
|
use TractorCow\Fluent\Model\Locale; |
|
19
|
|
|
use TractorCow\Fluent\State\FluentState; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class \Firesphere\SolrFluent\States\FluentSiteState |
|
23
|
|
|
* |
|
24
|
|
|
* Manage the state of the site to apply the correct locale from Fluent to search |
|
25
|
|
|
* |
|
26
|
|
|
* @package Firesphere\SolrFluent\States |
|
27
|
|
|
*/ |
|
28
|
|
|
class FluentSiteState extends SiteState implements SiteStateInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array get/set methods that needs to be called to update the query |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $methods = [ |
|
34
|
|
|
'BoostedFields', |
|
35
|
|
|
'Filter', |
|
36
|
|
|
'Exclude' |
|
37
|
|
|
]; |
|
38
|
|
|
/** |
|
39
|
|
|
* Does the state apply to this class |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $class Class to check |
|
42
|
|
|
* @return bool |
|
43
|
|
|
* @throws ReflectionException |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function appliesTo($class): bool |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->isEnabled() && |
|
48
|
1 |
|
SiteState::hasExtension($class, FluentExtension::class) && |
|
49
|
1 |
|
Locale::getCached()->count(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Is this state applicable to this extension |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $state State to validate |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function stateIsApplicable($state): bool |
|
59
|
|
|
{ |
|
60
|
1 |
|
$locales = Locale::get()->column('Locale'); |
|
61
|
|
|
|
|
62
|
1 |
|
return in_array($state, $locales, true) && count($locales); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Reset the SiteState to it's default state |
|
67
|
|
|
* |
|
68
|
|
|
* @param string|null $state Reset to default state |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function setDefaultState($state = null) |
|
72
|
|
|
{ |
|
73
|
1 |
|
FluentState::singleton()->setLocale($state); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return the current state of the site |
|
78
|
|
|
* |
|
79
|
|
|
* @return string|null |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function currentState() |
|
82
|
|
|
{ |
|
83
|
1 |
|
return FluentState::singleton()->getLocale(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Activate a given state. This should only be done if the state is applicable |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $state Activate the given state |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function activateState($state) |
|
93
|
|
|
{ |
|
94
|
1 |
|
FluentState::singleton()->setLocale($state); |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Update the Solr query to match the current State |
|
99
|
|
|
* |
|
100
|
|
|
* @param BaseQuery $query Query to update |
|
101
|
|
|
*/ |
|
102
|
|
|
public function updateQuery(&$query) |
|
103
|
|
|
{ |
|
104
|
|
|
$locale = FluentState::singleton()->getLocale(); |
|
105
|
|
|
if ($locale === '') { |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
foreach (self::$methods as $method) { |
|
110
|
|
|
$this->updatePart($query, $locale, $method); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$fields = []; |
|
114
|
|
|
foreach ($query->getFields() as $field) { |
|
115
|
|
|
$fields[] = $field . '_' . $locale; |
|
116
|
|
|
} |
|
117
|
|
|
$query->setFields($fields); |
|
118
|
|
|
|
|
119
|
|
|
$localisedTerms = []; |
|
120
|
|
|
foreach ($query->getTerms() as $term) { |
|
121
|
|
|
$localisedTerms = $this->updateTerms($term, $locale, $localisedTerms); |
|
122
|
|
|
} |
|
123
|
|
|
$query->setTerms($localisedTerms); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Update a part of the query for the get and set methods. |
|
128
|
|
|
* |
|
129
|
|
|
* @param BaseQuery $query Query that needs updating for the given method |
|
130
|
|
|
* @param string $locale Localisation to use |
|
131
|
|
|
* @param string $method Get method to call |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function updatePart(&$query, string $locale, string $method): void |
|
134
|
|
|
{ |
|
135
|
|
|
if (!$locale) { |
|
136
|
|
|
return; |
|
137
|
|
|
} |
|
138
|
|
|
$new = []; |
|
139
|
|
|
$getMethod = 'get' . $method; |
|
140
|
|
|
$setMethod = 'set' . $method; |
|
141
|
|
|
foreach ($query->$getMethod() as $filterField => $value) { |
|
142
|
|
|
$fieldName = $filterField . '_' . $locale; |
|
143
|
|
|
$new[$fieldName] = $value; |
|
144
|
|
|
} |
|
145
|
|
|
$query->$setMethod($new); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Update the field filters to localised filters |
|
150
|
|
|
* |
|
151
|
|
|
* @param string|array $term Array of terms |
|
152
|
|
|
* @param string $locale Localisation to use |
|
153
|
|
|
* @param array $localisedTerms Currently localised terms |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
private function updateTerms($term, string $locale, array $localisedTerms): array |
|
157
|
|
|
{ |
|
158
|
|
|
if (count($term['fields'])) { |
|
159
|
|
|
foreach ($term['fields'] as &$termField) { |
|
160
|
|
|
$termField .= '_' . $locale; |
|
161
|
|
|
} |
|
162
|
|
|
unset($termField); |
|
163
|
|
|
} |
|
164
|
|
|
$localisedTerms[] = $term; |
|
165
|
|
|
|
|
166
|
|
|
return $localisedTerms; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|