Passed
Push — master ( b938ea...b96626 )
by Timo
01:07
created

PageModuleSummary::getFieldFromFlexform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1.125
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 2 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
33
/**
34
 * Summary to display flexform settings in the page layout backend module.
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 * @author Timo Hund <[email protected]>
38
 */
39
class PageModuleSummary
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']);
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('Template', 'templateFile.results');
73 1
        return $this->render();
74
    }
75
76
    /**
77
     * @param array $contentElement
78
     */
79 1
    protected function initialize(array $contentElement)
80
    {
81
        /** @var $service \TYPO3\CMS\Extbase\Service\FlexFormService::class */
82 1
        $service = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Service\FlexFormService::class);
83 1
        $this->flexformData  = $service->convertFlexFormContentToArray($contentElement['pi_flexform']);
84 1
        $this->pluginContentElement = $contentElement;
85 1
    }
86
87
    /**
88
     * Adds the target page to the settings.
89
     */
90 1
    protected function addTargetPage()
91
    {
92 1
        $targetPageId = $this->getFieldFromFlexform('search.targetPage');
93 1
        if (!empty($targetPageId)) {
94 1
            $page = BackendUtility::getRecord('pages', $targetPageId, 'title');
95 1
            $this->settings['Target Page'] = '[' . (int)$targetPageId . '] ' . $page['title'];
96
        }
97 1
    }
98
99
    /**
100
     * @param string $settingName
101
     * @param string $flexFormField
102
     */
103 1
    protected function addSettingFromFlexForm($settingName, $flexFormField)
104
    {
105 1
        $value = $this->getFieldFromFlexform($flexFormField);
106
107 1
        if (is_array($value)) {
108 1
            $value = $this->addSettingFromFlexFormArray($settingName, $value);
109
        }
110 1
        $this->addSettingIfNotEmpty($settingName, (string)$value);
111 1
    }
112
113
    /**
114
     * @param string $settingName
115
     * @param array $values
116
     * @return bool
117
     */
118 1
    protected function addSettingFromFlexFormArray($settingName, $values)
119
    {
120 1
        foreach ($values as $item) {
121 1
            if (!isset($item['field'])) {
122
                continue;
123
            }
124 1
            $field = $item['field'];
125
126 1
            $label = $settingName . ' ';
127 1
            $label .= isset($field['field']) ? $field['field'] : '';
128 1
            $fieldValue = isset($field['value']) ? $field['value'] : '';
129 1
            $this->addSettingIfNotEmpty($label, (string)$fieldValue);
130
        }
131 1
    }
132
133
    /**
134
     * @param string $settingName
135
     * @param string $value
136
     */
137 1
    protected function addSettingIfNotEmpty($settingName, $value)
138
    {
139 1
        if (!empty($value)) {
140 1
            $this->settings[$settingName] = $value;
141
        }
142 1
    }
143
144
    /**
145
     * Gets a field's value from flexform configuration, will check if
146
     * flexform configuration is available.
147
     *
148
     * @param string $path name of the field
149
     * @return string if nothing found, value if found
150
     */
151 1
    protected function getFieldFromFlexform($path)
152
    {
153 1
        return ObjectAccess::getPropertyPath($this->flexformData, $path);
154
    }
155
156
    /**
157
     * @return string
158
     */
159 1
    protected function render()
160
    {
161
        /** @var $standaloneView StandaloneView */
162 1
        $standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
163 1
        $standaloneView->setTemplatePathAndFilename(
164 1
            GeneralUtility::getFileAbsFileName('EXT:solr/Resources/Private/Templates/Backend/PageModule/Summary.html')
165
        );
166 1
        $standaloneView->assignMultiple([
167 1
            'hidden' => $this->pluginContentElement['hidden'],
168 1
            'settings' => $this->settings,
169
        ]);
170
171 1
        return $standaloneView->render();
172
    }
173
}
174