Passed
Pull Request — task/3376-TYPO3_12_compatibili... (#3499)
by Rafael
41:38
created

SettingsPreviewOnPlugins::addSettingFromFlexForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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