Completed
Pull Request — master (#909)
by Timo
33:33
created

PageModuleSummary::getFieldFromFlexform()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 2
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\Fluid\View\StandaloneView;
31
32
/**
33
 * Summary to display flexform settings in the page layout backend module.
34
 *
35
 * @author Ingo Renner <[email protected]>
36
 * @author Timo Hund <[email protected]>
37
 */
38
class PageModuleSummary
39
{
40
    /**
41
     * @var array
42
     */
43
    protected $pluginContentElement = [];
44
45
    /**
46
     * @var array
47
     */
48
    protected $flexformData = [];
49
50
    /**
51
     * @var array
52
     */
53
    protected $settings = [];
54
55
    /**
56
     * Returns information about a plugin's flexform configuration
57
     *
58
     * @param array $parameters Parameters to the hook
59
     * @return string Plugin configuration information
60
     */
61
    public function getSummary(array $parameters)
62
    {
63
        $this->initialize($parameters['row']);
64
65
        $this->addTargetPage();
66
        $this->addSettingFromFlexForm('Filter', 'filter');
67
        $this->addSettingFromFlexForm('Sorting', 'sortBy');
68
        $this->addSettingFromFlexForm('Results per Page', 'resultsPerPage');
69
        $this->addSettingFromFlexForm('Boost Function', 'boostFunction');
70
        $this->addSettingFromFlexForm('Boost Query', 'boostQuery');
71
        $this->addSettingFromFlexForm('Template', 'templateFile', 'sOptions');
72
73
        return $this->render();
74
    }
75
76
    /**
77
     * @param array $contentElement
78
     */
79
    protected function initialize(array $contentElement)
80
    {
81
        $this->pluginContentElement = $contentElement;
82
83
        $flexformAsArray = GeneralUtility::xml2array($contentElement['pi_flexform']);
84
        $this->flexformData = $flexformAsArray['data'];
85
    }
86
87
    /**
88
     * Adds the target page to the settings.
89
     */
90
    protected function addTargetPage()
91
    {
92
        $targetPageId = $this->getFieldFromFlexform('targetPage');
93
        if (!empty($targetPageId)) {
94
            $page = BackendUtility::getRecord('pages', $targetPageId, 'title');
95
            $this->settings['Target Page'] = '[' . (int)$targetPageId . '] ' . $page['title'];
96
        }
97
    }
98
99
    /**
100
     * @param string $settingName
101
     * @param string $flexFormField
102
     * @param string $sheetName
103
     */
104
    protected function addSettingFromFlexForm($settingName, $flexFormField, $sheetName = 'sQuery')
105
    {
106
        $templateFile = $this->getFieldFromFlexform($flexFormField, $sheetName);
107
108
        if (!empty($templateFile)) {
109
            $this->settings[$settingName] = $templateFile;
110
        }
111
    }
112
113
    /**
114
     * Gets a field's value from flexform configuration, will check if
115
     * flexform configuration is available.
116
     *
117
     * @param string $fieldName name of the field
118
     * @param string $sheetName name of the sheet, defaults to "sDEF"
119
     * @return string if nothing found, value if found
120
     */
121
    protected function getFieldFromFlexform($fieldName, $sheetName = 'sDEF')
122
    {
123
        $fieldValue = '';
124
125
        if (array_key_exists($sheetName,
126
                $this->flexformData) && array_key_exists($fieldName,
127
                $this->flexformData[$sheetName]['lDEF'])
128
        ) {
129
            $fieldValue = $this->flexformData[$sheetName]['lDEF'][$fieldName]['vDEF'];
130
        }
131
132
        return $fieldValue;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    protected function render()
139
    {
140
        /** @var $standaloneView StandaloneView */
141
        $standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
142
        $standaloneView->setTemplatePathAndFilename(
143
            GeneralUtility::getFileAbsFileName('EXT:solr/Resources/Private/Templates/Backend/PageModule/Summary.html')
144
        );
145
        $standaloneView->assignMultiple([
146
            'hidden' => $this->pluginContentElement['hidden'],
147
            'settings' => $this->settings,
148
        ]);
149
150
        return $standaloneView->render();
151
    }
152
}
153