Passed
Push — master ( 587e90...d59eb5 )
by Timo
56:07 queued 26:14
created

FlexFormUserFunctions::getFacetFieldsFromSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
ccs 8
cts 10
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
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\Util;
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
     * Provides all facet fields for a flexform select, enabling the editor to select one of them.
37
     *
38
     * @param array $parentInformation
39
     *
40
     * @return void
41
     */
42 6
    public function getFacetFieldsFromSchema(array &$parentInformation)
43
    {
44 6
        $pageRecord = $parentInformation['flexParentDatabaseRow'];
45 6
        $configuredFacets = $this->getConfiguredFacetsForPage($pageRecord['pid']);
46
47 6
        if (!is_array($pageRecord)) {
48 1
            $parentInformation['items'] = [];
49 1
            return;
50
        }
51
52 5
        $newItems = $this->getParsedSolrFieldsFromSchema($configuredFacets, $pageRecord);
53 5
        $parentInformation['items'] = $newItems;
54 5
    }
55
56
    /**
57
     * This method parses the solr schema fields into the required format for the backend flexform.
58
     *
59
     * @param array $configuredFacets
60
     * @param array $pageRecord
61
     * @return mixed
62
     */
63 5
    protected function getParsedSolrFieldsFromSchema($configuredFacets, $pageRecord)
64
    {
65 5
        $newItems = [];
66
67 5
        array_map(function($fieldName) use (&$newItems, $configuredFacets) {
68 5
            $value = $fieldName;
69 5
            $label = $fieldName;
70
71 5
            $facetNameFilter = function($facet) use ($fieldName) {
72 4
                return ($facet['field'] === $fieldName);
73 5
            };
74 5
            $configuredFacets = array_filter($configuredFacets, $facetNameFilter);
75 5
            if (!empty($configuredFacets)) {
76 4
                $configuredFacet = array_values($configuredFacets);
77 4
                $label = $configuredFacet[0]['label'];
78
                // try to translate LLL: label or leave it unchanged
79 4
                if (GeneralUtility::isFirstPartOfStr($label, 'LLL:') && $this->getTranslation($label) != '') {
80 1
                    $label = $this->getTranslation($label);
81 4
                } elseif (!GeneralUtility::isFirstPartOfStr($label, 'LLL:') && $configuredFacet[0]['label.']) {
82 1
                    $label = sprintf('cObject[...faceting.facets.%slabel]', array_keys($configuredFacets)[0]);
83
                }
84 4
                $label = sprintf('%s (Facet Label: "%s")', $value, $label);
85
            }
86
87 5
            $newItems[$value] = [$label, $value];
88 5
        }, $this->getFieldNamesFromSolrMetaDataForPage($pageRecord));
89
90 5
        ksort($newItems, SORT_NATURAL);
91 5
        return $newItems;
92
    }
93
94
    /**
95
     * Retrieves the configured facets for a page.
96
     *
97
     * @param integer $pid
98
     * @return array
99
     */
100
    protected function getConfiguredFacetsForPage($pid)
101
    {
102
        $typoScriptConfiguration = $this->getConfigurationFromPageId($pid);
103
        return $typoScriptConfiguration->getSearchFacetingFacets();
104
    }
105
106
    /**
107
     * Retrieves the translation with the LocalizationUtility.
108
     *
109
     * @param string $label
110
     * @return null|string
111
     */
112
    protected function getTranslation($label)
113
    {
114
        return LocalizationUtility::translate($label);
115
    }
116
117
    /**
118
     * Get solr connection.
119
     *
120
     * @param array $pageRecord
121
     *
122
     * @return \ApacheSolrForTypo3\Solr\System\Solr\SolrConnection
123
     */
124
    protected function getConnection(array $pageRecord)
125
    {
126
        return GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($pageRecord['pid'], $pageRecord['sys_language_uid']);
127
    }
128
129
    /**
130
     * Retrieves all fieldnames that occure in the solr schema for one page.
131
     *
132
     * @param array $pageRecord
133
     * @return array
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
     * @return string
163
     */
164 1
    protected function getTypoScriptTemplateKeyFromFieldName(array &$parentInformation)
165
    {
166 1
        $field = $parentInformation['field'];
167 1
        return str_replace('view.templateFiles.', '', $field);
168
    }
169
170
    /**
171
     * @param $pid
172
     * @return \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration|array
173
     */
174
    protected function getConfigurationFromPageId($pid)
175
    {
176
        $typoScriptConfiguration = Util::getSolrConfigurationFromPageId($pid);
177
        return $typoScriptConfiguration;
178
    }
179
180
    /**
181
     * Retrieves the configured templates from TypoScript.
182
     *
183
     * @param integer $pageId
184
     * @param string $templateKey
185
     * @return array
186
     */
187
    protected function getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey)
188
    {
189
        $configuration = $this->getConfigurationFromPageId($pageId);
190
        return $configuration->getAvailableTemplatesByFileKey($templateKey);
191
    }
192
193
    /**
194
     * Returns the available templates as needed for the flexform.
195
     *
196
     * @param array $availableTemplates
197
     * @return array
198
     */
199 1
    protected function buildSelectItemsFromAvailableTemplate($availableTemplates)
200
    {
201 1
        $newItems = [];
202 1
        $newItems['Use Default'] = ['Use Default', null];
203 1
        foreach ($availableTemplates as $availableTemplate) {
204 1
            $label = isset($availableTemplate['label']) ? $availableTemplate['label'] : '';
205 1
            $value = isset($availableTemplate['file']) ? $availableTemplate['file'] : '';
206 1
            $newItems[$label] = [$label, $value];
207
        }
208
209 1
        return $newItems;
210
    }
211
}
212