Failed Conditions
Push — task/2976_TYPO3.11_compatibili... ( 772e40...ecf396 )
by Rafael
23:49
created

PageModuleSummary::getPluginLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0932
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Controller\Backend;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use TYPO3\CMS\Backend\Utility\BackendUtility;
19
use TYPO3\CMS\Core\Service\FlexFormService;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
22
use TYPO3\CMS\Fluid\View\StandaloneView;
23
use TYPO3\CMS\Core\Localization\LanguageService;
24
use TYPO3\CMS\Backend\View\PageLayoutView;
25
26
/**
27
 * Summary to display flexform settings in the page layout backend module.
28
 *
29
 * @author Ingo Renner <[email protected]>
30
 * @author Timo Hund <[email protected]>
31
 */
32
class PageModuleSummary
33
{
34
    /**
35
     * PageLayoutView
36
     *
37
     * @var PageLayoutView
38
     */
39
    protected $pageLayoutView;
40
41
    /**
42
     * @var array
43
     */
44
    protected $pluginContentElement = [];
45
46
    /**
47
     * @var array
48
     */
49
    protected $flexformData = [];
50
51
    /**
52
     * @var array
53
     */
54
    protected $settings = [];
55
56
    /**
57
     * Returns information about a plugin's flexform configuration
58
     *
59
     * @param array $parameters Parameters to the hook
60
     * @return string Plugin configuration information
61
     */
62 1
    public function getSummary(array $parameters)
63
    {
64 1
        $this->initialize($parameters['row'], $parameters['pObj']);
65
66 1
        $this->addTargetPage();
67 1
        $this->addSettingFromFlexForm('Filter', 'search.query.filter');
68 1
        $this->addSettingFromFlexForm('Sorting', 'search.query.sortBy');
69 1
        $this->addSettingFromFlexForm('Results per Page', 'search.results.resultsPerPage');
70 1
        $this->addSettingFromFlexForm('Boost Function', 'search.query.boostFunction');
71 1
        $this->addSettingFromFlexForm('Boost Query', 'search.query.boostQuery');
72 1
        $this->addSettingFromFlexForm('Tie Breaker', 'search.query.tieParameter');
73 1
        $this->addSettingFromFlexForm('Template', 'view.templateFiles.results');
74 1
        return $this->render();
75
    }
76
77
    /**
78
     * @param array $contentElement
79
     * @param PageLayoutView $pObj
80
     */
81 1
    protected function initialize(array $contentElement, PageLayoutView $pObj)
82
    {
83 1
        $this->pageLayoutView = $pObj;
84
85
        /** @var $service \TYPO3\CMS\Core\Service\FlexFormService::class */
86 1
        $service = GeneralUtility::makeInstance(FlexFormService::class);
87 1
        $this->flexformData = $service->convertFlexFormContentToArray($contentElement['pi_flexform']);
88 1
        $this->pluginContentElement = $contentElement;
89 1
    }
90
91
    /**
92
     * Adds the target page to the settings.
93
     */
94 1
    protected function addTargetPage()
95
    {
96 1
        $targetPageId = $this->getFieldFromFlexform('search.targetPage');
97 1
        if (!empty($targetPageId)) {
98 1
            $page = BackendUtility::getRecord('pages', $targetPageId, 'title');
99 1
            $this->settings['Target Page'] = '[' . (int)$targetPageId . '] ' . $page['title'];
100
        }
101 1
    }
102
103
    /**
104
     * @param string $settingName
105
     * @param string $flexFormField
106
     */
107 1
    protected function addSettingFromFlexForm($settingName, $flexFormField)
108
    {
109 1
        $value = $this->getFieldFromFlexform($flexFormField);
110
111 1
        if (is_array($value)) {
112 1
            $value = $this->addSettingFromFlexFormArray($settingName, $value);
113
        }
114 1
        $this->addSettingIfNotEmpty($settingName, (string)$value);
115 1
    }
116
117
    /**
118
     * @param string $settingName
119
     * @param array $values
120
     * @return bool
121
     */
122 1
    protected function addSettingFromFlexFormArray($settingName, $values)
123
    {
124 1
        foreach ($values as $item) {
125 1
            if (!isset($item['field'])) {
126
                continue;
127
            }
128 1
            $field = $item['field'];
129
130 1
            $label = $settingName . ' ';
131 1
            $label .= isset($field['field']) ? $field['field'] : '';
132 1
            $fieldValue = isset($field['value']) ? $field['value'] : '';
133 1
            $this->addSettingIfNotEmpty($label, (string)$fieldValue);
134
        }
135 1
    }
136
137
    /**
138
     * @param string $settingName
139
     * @param string $value
140
     */
141 1
    protected function addSettingIfNotEmpty($settingName, $value)
142
    {
143 1
        if (!empty($value)) {
144 1
            $this->settings[$settingName] = $value;
145
        }
146 1
    }
147
148
    /**
149
     * Gets a field's value from flexform configuration, will check if
150
     * flexform configuration is available.
151
     *
152
     * @param string $path name of the field
153
     * @return mixed|null if nothing found, value if found
154
     */
155 1
    protected function getFieldFromFlexform(string $path)
156
    {
157 1
        return ObjectAccess::getPropertyPath($this->flexformData, $path);
158
    }
159
160
    /**
161
     * @return string
162
     */
163 1
    protected function render()
164
    {
165
        /** @var $standaloneView StandaloneView */
166 1
        $standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
167 1
        $standaloneView->setTemplatePathAndFilename(
168 1
            GeneralUtility::getFileAbsFileName('EXT:solr/Resources/Private/Templates/Backend/PageModule/Summary.html')
169
        );
170
171 1
        $standaloneView->assignMultiple([
172 1
            'pluginLabel' => $this->getPluginLabel(),
173 1
            'hidden' => $this->pluginContentElement['hidden'],
174 1
            'settings' => $this->settings,
175
        ]);
176 1
        return $standaloneView->render();
177
    }
178
179
    /**
180
     * Returns the plugin label
181
     *
182
     * @return string
183
     */
184 1
    protected function getPluginLabel()
185
    {
186 1
        $label = BackendUtility::getLabelFromItemListMerged($this->pluginContentElement['pid'], 'tt_content', 'list_type', $this->pluginContentElement['list_type']);
187 1
        if (!empty($label)) {
188
            $label = $this->getLanguageService()->sL($label);
189
        } else {
190 1
            $label = sprintf($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.noMatchingValue'), $this->pluginContentElement['list_type']);
191
        }
192
193 1
        return $this->pageLayoutView->linkEditContent(htmlspecialchars($label), $this->pluginContentElement);
194
    }
195
196
    /**
197
     * Returns the language service
198
     *
199
     * @return LanguageService
200
     */
201 1
    protected function getLanguageService(): LanguageService
202
    {
203 1
        return $GLOBALS['LANG'];
204
    }
205
}
206