Passed
Push — master ( 5e1177...4c215b )
by Timo
24:36
created

FlexFormUserFunctions   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 57.47%

Importance

Changes 0
Metric Value
wmc 21
eloc 53
dl 0
loc 166
ccs 50
cts 87
cp 0.5747
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getFacetFieldsFromSchema() 0 12 2
A getTypoScriptTemplateKeyFromFieldName() 0 4 1
A getFieldNamesFromSolrMetaDataForPage() 0 3 1
A getConfiguredFacetsForPage() 0 4 1
A getAvailableTemplates() 0 15 3
A getConnection() 0 3 1
A getConfigurationFromPageId() 0 4 1
A getAvailableTemplateFromTypoScriptConfiguration() 0 4 1
A getParsedSolrFieldsFromSchema() 0 29 6
A buildSelectItemsFromAvailableTemplate() 0 11 4
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:') && LocalizationUtility::translate($label) != '') {
80 1
                    $label = LocalizationUtility::translate($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
     * Get solr connection.
108
     *
109
     * @param array $pageRecord
110
     *
111
     * @return \ApacheSolrForTypo3\Solr\System\Solr\SolrConnection
112
     */
113
    protected function getConnection(array $pageRecord)
114
    {
115
        return GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($pageRecord['pid'], $pageRecord['sys_language_uid']);
116
    }
117
118
    /**
119
     * Retrieves all fieldnames that occure in the solr schema for one page.
120
     *
121
     * @param array $pageRecord
122
     * @return array
123
     */
124
    protected function getFieldNamesFromSolrMetaDataForPage(array $pageRecord)
125
    {
126
        return array_keys((array)$this->getConnection($pageRecord)->getAdminService()->getFieldsMetaData());
127
    }
128
129
    /**
130
     * @param array $parentInformation
131
     */
132 1
    public function getAvailableTemplates(array &$parentInformation)
133
    {
134 1
        $pageRecord = $parentInformation['flexParentDatabaseRow'];
135 1
        if (!is_array($pageRecord) || !isset ($pageRecord['pid'])) {
136
            $parentInformation['items'] = [];
137
            return;
138
        }
139
140 1
        $pageId = $pageRecord['pid'];
141
142 1
        $templateKey = $this->getTypoScriptTemplateKeyFromFieldName($parentInformation);
143 1
        $availableTemplate = $this->getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey);
144 1
        $newItems = $this->buildSelectItemsFromAvailableTemplate($availableTemplate);
145
146 1
        $parentInformation['items'] = $newItems;
147 1
    }
148
149
    /**
150
     * @param array $parentInformation
151
     * @return string
152
     */
153 1
    protected function getTypoScriptTemplateKeyFromFieldName(array &$parentInformation)
154
    {
155 1
        $field = $parentInformation['field'];
156 1
        return str_replace('view.templateFiles.', '', $field);
157
    }
158
159
    /**
160
     * @param $pid
161
     * @return \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration|array
162
     */
163
    protected function getConfigurationFromPageId($pid)
164
    {
165
        $typoScriptConfiguration = Util::getSolrConfigurationFromPageId($pid);
166
        return $typoScriptConfiguration;
167
    }
168
169
    /**
170
     * Retrieves the configured templates from TypoScript.
171
     *
172
     * @param integer $pageId
173
     * @param string $templateKey
174
     * @return array
175
     */
176
    protected function getAvailableTemplateFromTypoScriptConfiguration($pageId, $templateKey)
177
    {
178
        $configuration = $this->getConfigurationFromPageId($pageId);
179
        return $configuration->getAvailableTemplatesByFileKey($templateKey);
180
    }
181
182
    /**
183
     * Returns the available templates as needed for the flexform.
184
     *
185
     * @param array $availableTemplates
186
     * @return array
187
     */
188 1
    protected function buildSelectItemsFromAvailableTemplate($availableTemplates)
189
    {
190 1
        $newItems = [];
191 1
        $newItems['Use Default'] = ['Use Default', null];
192 1
        foreach ($availableTemplates as $availableTemplate) {
193 1
            $label = isset($availableTemplate['label']) ? $availableTemplate['label'] : '';
194 1
            $value = isset($availableTemplate['file']) ? $availableTemplate['file'] : '';
195 1
            $newItems[$label] = [$label, $value];
196
        }
197
198 1
        return $newItems;
199
    }
200
}
201