Passed
Push — master ( 3e1852...ce9806 )
by Timo
22:07
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.351

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 9
cp 0.5556
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2.351
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Controller\Backend;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2012-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Backend\Utility\BackendUtility;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
31
use TYPO3\CMS\Fluid\View\StandaloneView;
32
use TYPO3\CMS\Lang\LanguageService;
33
use TYPO3\CMS\Backend\View\PageLayoutView;
34
35
/**
36
 * Summary to display flexform settings in the page layout backend module.
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 * @author Timo Hund <[email protected]>
40
 */
41
class PageModuleSummary
42
{
43
    /**
44
     * PageLayoutView
45
     *
46
     * @var PageLayoutView
47
     */
48
    protected $pageLayoutView;
49
50
    /**
51
     * @var array
52
     */
53
    protected $pluginContentElement = [];
54
55
    /**
56
     * @var array
57
     */
58
    protected $flexformData = [];
59
60
    /**
61
     * @var array
62
     */
63
    protected $settings = [];
64
65
    /**
66
     * Returns information about a plugin's flexform configuration
67
     *
68
     * @param array $parameters Parameters to the hook
69
     * @return string Plugin configuration information
70
     */
71 1
    public function getSummary(array $parameters)
72
    {
73 1
        $this->initialize($parameters['row'], $parameters['pObj']);
74
75 1
        $this->addTargetPage();
76 1
        $this->addSettingFromFlexForm('Filter', 'search.query.filter');
77 1
        $this->addSettingFromFlexForm('Sorting', 'search.query.sortBy');
78 1
        $this->addSettingFromFlexForm('Results per Page', 'search.results.resultsPerPage');
79 1
        $this->addSettingFromFlexForm('Boost Function', 'search.query.boostFunction');
80 1
        $this->addSettingFromFlexForm('Boost Query', 'search.query.boostQuery');
81 1
        $this->addSettingFromFlexForm('Tie Breaker', 'search.query.tieParameter');
82 1
        $this->addSettingFromFlexForm('Template', 'view.templateFiles.results');
83 1
        return $this->render();
84
    }
85
86
    /**
87
     * @param array $contentElement
88
     * @param PageLayoutView $pObj
89
     */
90 1
    protected function initialize(array $contentElement, PageLayoutView $pObj)
91
    {
92 1
        $this->pageLayoutView = $pObj;
93
94
        /** @var $service \TYPO3\CMS\Extbase\Service\FlexFormService::class */
95 1
        $service = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Service\FlexFormService::class);
96 1
        $this->flexformData = $service->convertFlexFormContentToArray($contentElement['pi_flexform']);
97 1
        $this->pluginContentElement = $contentElement;
98 1
    }
99
100
    /**
101
     * Adds the target page to the settings.
102
     */
103 1
    protected function addTargetPage()
104
    {
105 1
        $targetPageId = $this->getFieldFromFlexform('search.targetPage');
106 1
        if (!empty($targetPageId)) {
107 1
            $page = BackendUtility::getRecord('pages', $targetPageId, 'title');
0 ignored issues
show
Bug introduced by
$targetPageId of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Backend\Utilit...endUtility::getRecord(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
            $page = BackendUtility::getRecord('pages', /** @scrutinizer ignore-type */ $targetPageId, 'title');
Loading history...
108 1
            $this->settings['Target Page'] = '[' . (int)$targetPageId . '] ' . $page['title'];
109
        }
110 1
    }
111
112
    /**
113
     * @param string $settingName
114
     * @param string $flexFormField
115
     */
116 1
    protected function addSettingFromFlexForm($settingName, $flexFormField)
117
    {
118 1
        $value = $this->getFieldFromFlexform($flexFormField);
119
120 1
        if (is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always false.
Loading history...
121 1
            $value = $this->addSettingFromFlexFormArray($settingName, $value);
122
        }
123 1
        $this->addSettingIfNotEmpty($settingName, (string)$value);
124 1
    }
125
126
    /**
127
     * @param string $settingName
128
     * @param array $values
129
     * @return bool
130
     */
131 1
    protected function addSettingFromFlexFormArray($settingName, $values)
132
    {
133 1
        foreach ($values as $item) {
134 1
            if (!isset($item['field'])) {
135
                continue;
136
            }
137 1
            $field = $item['field'];
138
139 1
            $label = $settingName . ' ';
140 1
            $label .= isset($field['field']) ? $field['field'] : '';
141 1
            $fieldValue = isset($field['value']) ? $field['value'] : '';
142 1
            $this->addSettingIfNotEmpty($label, (string)$fieldValue);
143
        }
144 1
    }
145
146
    /**
147
     * @param string $settingName
148
     * @param string $value
149
     */
150 1
    protected function addSettingIfNotEmpty($settingName, $value)
151
    {
152 1
        if (!empty($value)) {
153 1
            $this->settings[$settingName] = $value;
154
        }
155 1
    }
156
157
    /**
158
     * Gets a field's value from flexform configuration, will check if
159
     * flexform configuration is available.
160
     *
161
     * @param string $path name of the field
162
     * @return string if nothing found, value if found
163
     */
164 1
    protected function getFieldFromFlexform($path)
165
    {
166 1
        return ObjectAccess::getPropertyPath($this->flexformData, $path);
167
    }
168
169
    /**
170
     * @return string
171
     */
172 1
    protected function render()
173
    {
174
        /** @var $standaloneView StandaloneView */
175 1
        $standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
176 1
        $standaloneView->setTemplatePathAndFilename(
177 1
            GeneralUtility::getFileAbsFileName('EXT:solr/Resources/Private/Templates/Backend/PageModule/Summary.html')
178
        );
179
180 1
        $standaloneView->assignMultiple([
181 1
            'pluginLabel' => $this->getPluginLabel(),
182 1
            'hidden' => $this->pluginContentElement['hidden'],
183 1
            'settings' => $this->settings,
184
        ]);
185 1
        return $standaloneView->render();
186
    }
187
188
    /**
189
     * Returns the plugin label
190
     *
191
     * @return string
192
     */
193 1
    protected function getPluginLabel()
194
    {
195 1
        $label = BackendUtility::getLabelFromItemListMerged($this->pluginContentElement['pid'], 'tt_content', 'list_type', $this->pluginContentElement['list_type']);
196 1
        if (!empty($label)) {
197
            $label = $this->getLanguageService()->sL($label);
198
        } else {
199 1
            $label = sprintf($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.noMatchingValue'), $this->pluginContentElement['list_type']);
200
        }
201
202 1
        return $this->pageLayoutView->linkEditContent(htmlspecialchars($label), $this->pluginContentElement);
203
    }
204
205
    /**
206
     * Returns the language service
207
     *
208
     * @return LanguageService
209
     */
210 1
    protected function getLanguageService(): LanguageService
211
    {
212 1
        return $GLOBALS['LANG'];
213
    }
214
}
215