1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\System\UserFunctions; |
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\ConnectionManager; |
18
|
|
|
use ApacheSolrForTypo3\Solr\FrontendEnvironment; |
19
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration; |
20
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
21
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class contains all user functions for flexforms. |
25
|
|
|
* |
26
|
|
|
* @author Daniel Siepmann <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class FlexFormUserFunctions |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FrontendEnvironment |
33
|
|
|
*/ |
34
|
|
|
protected $frontendEnvironment = null; |
35
|
|
|
|
36
|
7 |
|
public function __construct(FrontendEnvironment $frontendEnvironment = null) |
37
|
|
|
{ |
38
|
7 |
|
$this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class); |
39
|
7 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Provides all facet fields for a flexform select, enabling the editor to select one of them. |
43
|
|
|
* |
44
|
|
|
* @param array $parentInformation |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
6 |
|
public function getFacetFieldsFromSchema(array &$parentInformation) |
49
|
|
|
{ |
50
|
6 |
|
$pageRecord = $parentInformation['flexParentDatabaseRow']; |
51
|
6 |
|
$configuredFacets = $this->getConfiguredFacetsForPage($pageRecord['pid']); |
52
|
|
|
|
53
|
6 |
|
if (!is_array($pageRecord)) { |
54
|
1 |
|
$parentInformation['items'] = []; |
55
|
1 |
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
$newItems = $this->getParsedSolrFieldsFromSchema($configuredFacets, $pageRecord); |
59
|
5 |
|
$parentInformation['items'] = $newItems; |
60
|
5 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* This method parses the solr schema fields into the required format for the backend flexform. |
64
|
|
|
* |
65
|
|
|
* @param array $configuredFacets |
66
|
|
|
* @param array $pageRecord |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
5 |
|
protected function getParsedSolrFieldsFromSchema($configuredFacets, $pageRecord) |
70
|
|
|
{ |
71
|
5 |
|
$newItems = []; |
72
|
|
|
|
73
|
|
|
array_map(function($fieldName) use (&$newItems, $configuredFacets) { |
74
|
5 |
|
$value = $fieldName; |
75
|
5 |
|
$label = $fieldName; |
76
|
|
|
|
77
|
|
|
$facetNameFilter = function($facet) use ($fieldName) { |
78
|
4 |
|
return ($facet['field'] === $fieldName); |
79
|
5 |
|
}; |
80
|
5 |
|
$configuredFacets = array_filter($configuredFacets, $facetNameFilter); |
81
|
5 |
|
if (!empty($configuredFacets)) { |
82
|
4 |
|
$configuredFacet = array_values($configuredFacets); |
83
|
4 |
|
$label = $configuredFacet[0]['label']; |
84
|
|
|
// try to translate LLL: label or leave it unchanged |
85
|
4 |
|
if (GeneralUtility::isFirstPartOfStr($label, 'LLL:') && $this->getTranslation($label) != '') { |
86
|
1 |
|
$label = $this->getTranslation($label); |
87
|
4 |
|
} elseif (!GeneralUtility::isFirstPartOfStr($label, 'LLL:') && $configuredFacet[0]['label.']) { |
88
|
1 |
|
$label = sprintf('cObject[...faceting.facets.%slabel]', array_keys($configuredFacets)[0]); |
89
|
|
|
} |
90
|
4 |
|
$label = sprintf('%s (Facet Label: "%s")', $value, $label); |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
$newItems[$value] = [$label, $value]; |
94
|
5 |
|
}, $this->getFieldNamesFromSolrMetaDataForPage($pageRecord)); |
95
|
|
|
|
96
|
5 |
|
ksort($newItems, SORT_NATURAL); |
97
|
5 |
|
return $newItems; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Retrieves the configured facets for a page. |
102
|
|
|
* |
103
|
|
|
* @param integer $pid |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
protected function getConfiguredFacetsForPage($pid) |
107
|
|
|
{ |
108
|
|
|
$typoScriptConfiguration = $this->getConfigurationFromPageId($pid); |
109
|
|
|
return $typoScriptConfiguration->getSearchFacetingFacets(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieves the translation with the LocalizationUtility. |
114
|
|
|
* |
115
|
|
|
* @param string $label |
116
|
|
|
* @return null|string |
117
|
|
|
*/ |
118
|
|
|
protected function getTranslation($label) |
119
|
|
|
{ |
120
|
|
|
return LocalizationUtility::translate($label); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get solr connection. |
125
|
|
|
* |
126
|
|
|
* @param array $pageRecord |
127
|
|
|
* |
128
|
|
|
* @return \ApacheSolrForTypo3\Solr\System\Solr\SolrConnection |
129
|
|
|
*/ |
130
|
|
|
protected function getConnection(array $pageRecord) |
131
|
|
|
{ |
132
|
|
|
return GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($pageRecord['pid'], $pageRecord['sys_language_uid']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Retrieves all fieldnames that occure in the solr schema for one page. |
137
|
|
|
* |
138
|
|
|
* @param array $pageRecord |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
protected function getFieldNamesFromSolrMetaDataForPage(array $pageRecord) |
142
|
|
|
{ |
143
|
|
|
return array_keys((array)$this->getConnection($pageRecord)->getAdminService()->getFieldsMetaData()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param array $parentInformation |
148
|
|
|
*/ |
149
|
1 |
|
public function getAvailableTemplates(array &$parentInformation) |
150
|
|
|
{ |
151
|
1 |
|
$pageRecord = $parentInformation['flexParentDatabaseRow']; |
152
|
1 |
|
if (!is_array($pageRecord) || !isset ($pageRecord['pid'])) { |
153
|
|
|
$parentInformation['items'] = []; |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
$pageId = $pageRecord['pid']; |
158
|
|
|
|
159
|
1 |
|
$templateKey = $this->getTypoScriptTemplateKeyFromFieldName($parentInformation); |
160
|
1 |
|
$availableTemplate = $this->getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey); |
161
|
1 |
|
$newItems = $this->buildSelectItemsFromAvailableTemplate($availableTemplate); |
162
|
|
|
|
163
|
1 |
|
$parentInformation['items'] = $newItems; |
164
|
1 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param array $parentInformation |
168
|
|
|
*/ |
169
|
|
|
public function getAvailablePluginNamespaces(array &$parentInformation) |
170
|
|
|
{ |
171
|
|
|
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class); |
172
|
|
|
$namespaces = []; |
173
|
|
|
foreach ($extensionConfiguration->getAvailablePluginNamespaces() as $namespace) { |
174
|
|
|
$label = $namespace === 'tx_solr' ? 'Default' : ''; |
175
|
|
|
$namespaces[$namespace] = [$label, $namespace]; |
176
|
|
|
} |
177
|
|
|
$parentInformation['items'] = $namespaces; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array $parentInformation |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
1 |
|
protected function getTypoScriptTemplateKeyFromFieldName(array &$parentInformation) |
185
|
|
|
{ |
186
|
1 |
|
$field = $parentInformation['field']; |
187
|
1 |
|
return str_replace('view.templateFiles.', '', $field); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param $pid |
192
|
|
|
* @return \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration|array |
193
|
|
|
*/ |
194
|
|
|
protected function getConfigurationFromPageId($pid) |
195
|
|
|
{ |
196
|
|
|
$typoScriptConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($pid); |
197
|
|
|
return $typoScriptConfiguration; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Retrieves the configured templates from TypoScript. |
202
|
|
|
* |
203
|
|
|
* @param integer $pageId |
204
|
|
|
* @param string $templateKey |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
protected function getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey) |
208
|
|
|
{ |
209
|
|
|
$configuration = $this->getConfigurationFromPageId($pageId); |
210
|
|
|
return $configuration->getAvailableTemplatesByFileKey($templateKey); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the available templates as needed for the flexform. |
215
|
|
|
* |
216
|
|
|
* @param array $availableTemplates |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
1 |
|
protected function buildSelectItemsFromAvailableTemplate($availableTemplates) |
220
|
|
|
{ |
221
|
1 |
|
$newItems = []; |
222
|
1 |
|
$newItems['Use Default'] = ['Use Default', null]; |
223
|
1 |
|
foreach ($availableTemplates as $availableTemplate) { |
224
|
1 |
|
$label = isset($availableTemplate['label']) ? $availableTemplate['label'] : ''; |
225
|
1 |
|
$value = isset($availableTemplate['file']) ? $availableTemplate['file'] : ''; |
226
|
1 |
|
$newItems[$label] = [$label, $value]; |
227
|
|
|
} |
228
|
|
|
|
229
|
1 |
|
return $newItems; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|