Passed
Pull Request — release-11.2.x (#3497)
by Rafael
11:26
created

getParsedSolrFieldsFromSchema()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

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