Passed
Push — master ( 96ad23...e674df )
by Timo
12:01
created

getParsedSolrFieldsFromSchema()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.1338

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 29
ccs 13
cts 19
cp 0.6842
rs 9.0111
cc 6
nc 1
nop 2
crap 7.1338
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\UserFunctions;
3
4
/*
5
 * Copyright (C) 2016  Daniel Siepmann <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
 * 02110-1301, USA.
21
 */
22
23
use ApacheSolrForTypo3\Solr\ConnectionManager;
24
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
27
28
/**
29
 * This class contains all user functions for flexforms.
30
 *
31
 * @author Daniel Siepmann <[email protected]>
32
 */
33
class FlexFormUserFunctions
34
{
35
36
    /**
37
     * @var FrontendEnvironment
38
     */
39
    protected $frontendEnvironment = null;
40
41
    public function __construct(FrontendEnvironment $frontendEnvironment = null)
42 6
    {
43
        $this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class);
44 6
    }
45 6
46
    /**
47 6
     * Provides all facet fields for a flexform select, enabling the editor to select one of them.
48 1
     *
49 1
     * @param array $parentInformation
50
     *
51
     * @return void
52 5
     */
53 5
    public function getFacetFieldsFromSchema(array &$parentInformation)
54 5
    {
55
        $pageRecord = $parentInformation['flexParentDatabaseRow'];
56
        $configuredFacets = $this->getConfiguredFacetsForPage($pageRecord['pid']);
57
58
        if (!is_array($pageRecord)) {
59
            $parentInformation['items'] = [];
60
            return;
61
        }
62
63 5
        $newItems = $this->getParsedSolrFieldsFromSchema($configuredFacets, $pageRecord);
64
        $parentInformation['items'] = $newItems;
65 5
    }
66
67 5
    /**
68 5
     * This method parses the solr schema fields into the required format for the backend flexform.
69 5
     *
70
     * @param array $configuredFacets
71 5
     * @param array $pageRecord
72 4
     * @return mixed
73 5
     */
74 5
    protected function getParsedSolrFieldsFromSchema($configuredFacets, $pageRecord)
75 5
    {
76 4
        $newItems = [];
77 4
78
        array_map(function($fieldName) use (&$newItems, $configuredFacets) {
79 4
            $value = $fieldName;
80 1
            $label = $fieldName;
81 4
82 1
            $facetNameFilter = function($facet) use ($fieldName) {
83
                return ($facet['field'] === $fieldName);
84 4
            };
85
            $configuredFacets = array_filter($configuredFacets, $facetNameFilter);
86
            if (!empty($configuredFacets)) {
87 5
                $configuredFacet = array_values($configuredFacets);
88 5
                $label = $configuredFacet[0]['label'];
89
                // try to translate LLL: label or leave it unchanged
90 5
                if (GeneralUtility::isFirstPartOfStr($label, 'LLL:') && $this->getTranslation($label) != '') {
91 5
                    $label = $this->getTranslation($label);
92
                } elseif (!GeneralUtility::isFirstPartOfStr($label, 'LLL:') && $configuredFacet[0]['label.']) {
93
                    $label = sprintf('cObject[...faceting.facets.%slabel]', array_keys($configuredFacets)[0]);
94
                }
95
                $label = sprintf('%s (Facet Label: "%s")', $value, $label);
96
            }
97
98
            $newItems[$value] = [$label, $value];
99
        }, $this->getFieldNamesFromSolrMetaDataForPage($pageRecord));
100
101
        ksort($newItems, SORT_NATURAL);
102
        return $newItems;
103
    }
104
105
    /**
106
     * Retrieves the configured facets for a page.
107
     *
108
     * @param integer $pid
109
     * @return array
110
     */
111
    protected function getConfiguredFacetsForPage($pid)
112
    {
113
        $typoScriptConfiguration = $this->getConfigurationFromPageId($pid);
114
        return $typoScriptConfiguration->getSearchFacetingFacets();
115
    }
116
117
    /**
118
     * Retrieves the translation with the LocalizationUtility.
119
     *
120
     * @param string $label
121
     * @return null|string
122
     */
123
    protected function getTranslation($label)
124
    {
125
        return LocalizationUtility::translate($label);
126
    }
127
128
    /**
129
     * Get solr connection.
130
     *
131
     * @param array $pageRecord
132
     *
133
     * @return \ApacheSolrForTypo3\Solr\System\Solr\SolrConnection
134
     */
135
    protected function getConnection(array $pageRecord)
136
    {
137
        return GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($pageRecord['pid'], $pageRecord['sys_language_uid']);
138
    }
139
140
    /**
141
     * Retrieves all fieldnames that occure in the solr schema for one page.
142
     *
143 1
     * @param array $pageRecord
144
     * @return array
145 1
     */
146 1
    protected function getFieldNamesFromSolrMetaDataForPage(array $pageRecord)
147
    {
148
        return array_keys((array)$this->getConnection($pageRecord)->getAdminService()->getFieldsMetaData());
149
    }
150
151 1
    /**
152
     * @param array $parentInformation
153 1
     */
154 1
    public function getAvailableTemplates(array &$parentInformation)
155 1
    {
156
        $pageRecord = $parentInformation['flexParentDatabaseRow'];
157 1
        if (!is_array($pageRecord) || !isset ($pageRecord['pid'])) {
158 1
            $parentInformation['items'] = [];
159
            return;
160
        }
161
162
        $pageId = $pageRecord['pid'];
163
164 1
        $templateKey = $this->getTypoScriptTemplateKeyFromFieldName($parentInformation);
165
        $availableTemplate = $this->getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey);
166 1
        $newItems = $this->buildSelectItemsFromAvailableTemplate($availableTemplate);
167 1
168
        $parentInformation['items'] = $newItems;
169
    }
170
171
    /**
172
     * @param array $parentInformation
173
     * @return string
174
     */
175
    protected function getTypoScriptTemplateKeyFromFieldName(array &$parentInformation)
176
    {
177
        $field = $parentInformation['field'];
178
        return str_replace('view.templateFiles.', '', $field);
179
    }
180
181
    /**
182
     * @param $pid
183
     * @return \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration|array
184
     */
185
    protected function getConfigurationFromPageId($pid)
186
    {
187
        $typoScriptConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($pid);
188
        return $typoScriptConfiguration;
189
    }
190
191
    /**
192
     * Retrieves the configured templates from TypoScript.
193
     *
194
     * @param integer $pageId
195
     * @param string $templateKey
196
     * @return array
197
     */
198
    protected function getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey)
199 1
    {
200
        $configuration = $this->getConfigurationFromPageId($pageId);
201 1
        return $configuration->getAvailableTemplatesByFileKey($templateKey);
202 1
    }
203 1
204 1
    /**
205 1
     * Returns the available templates as needed for the flexform.
206 1
     *
207
     * @param array $availableTemplates
208
     * @return array
209 1
     */
210
    protected function buildSelectItemsFromAvailableTemplate($availableTemplates)
211
    {
212
        $newItems = [];
213
        $newItems['Use Default'] = ['Use Default', null];
214
        foreach ($availableTemplates as $availableTemplate) {
215
            $label = isset($availableTemplate['label']) ? $availableTemplate['label'] : '';
216
            $value = isset($availableTemplate['file']) ? $availableTemplate['file'] : '';
217
            $newItems[$label] = [$label, $value];
218
        }
219
220
        return $newItems;
221
    }
222
}
223