Passed
Push — master ( efbfe4...f2d6e9 )
by Timo
23:10
created

PageModuleSummary::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0046

Importance

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