Passed
Push — develop ( 23485b...49e0f6 )
by Torben
07:36 queued 05:06
created

AbstractPluginPreviewRenderer::getSwitchableControllerActionTitle()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
nc 6
nop 1
dl 0
loc 23
rs 9.0111
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Preview;
13
14
use TYPO3\CMS\Backend\Preview\PreviewRendererInterface;
15
use TYPO3\CMS\Backend\Utility\BackendUtility;
16
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
17
use TYPO3\CMS\Core\Imaging\Icon;
18
use TYPO3\CMS\Core\Imaging\IconFactory;
19
use TYPO3\CMS\Core\Localization\LanguageService;
20
use TYPO3\CMS\Core\Page\PageRenderer;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Fluid\View\StandaloneView;
23
24
abstract class AbstractPluginPreviewRenderer implements PreviewRendererInterface
25
{
26
    protected const LLPATH = 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang_be.xlf:';
27
28
    protected IconFactory $iconFactory;
29
30
    public function __construct()
31
    {
32
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
33
        $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
34
        $pageRenderer->addCssFile('EXT:sf_event_mgt/Resources/Public/Css/Backend/PageLayoutView.css');
35
    }
36
37
    /**
38
     * Renders the header (actually empty, since header is rendered in content)
39
     */
40
    public function renderPageModulePreviewHeader(GridColumnItem $item): string
41
    {
42
        return '';
43
    }
44
45
    /**
46
     * Renders the content of the plugin preview. Must be overwritten in extending class.
47
     */
48
    public function renderPageModulePreviewContent(GridColumnItem $item): string
49
    {
50
        return '';
51
    }
52
53
    /**
54
     * Render the footer. Can be overwritten in extending class if required
55
     */
56
    public function renderPageModulePreviewFooter(GridColumnItem $item): string
57
    {
58
        return '';
59
    }
60
61
    /**
62
     * Render the plugin preview
63
     */
64
    public function wrapPageModulePreview(string $previewHeader, string $previewContent, GridColumnItem $item): string
65
    {
66
        return $previewHeader . $previewContent;
67
    }
68
69
    /**
70
     * Returns the plugin name
71
     */
72
    protected function getPluginName(array $record): string
73
    {
74
        $pluginId = str_replace('sfeventmgt_', '', $record['list_type']);
75
        return htmlspecialchars($this->getLanguageService()->sL(self::LLPATH . 'plugin.' . $pluginId . '.title'));
76
    }
77
78
    /**
79
     * Renders the given data and action as HTML table for plugin preview
80
     */
81
    protected function renderAsTable(array $data, string $pluginName = ''): string
82
    {
83
        $view = GeneralUtility::makeInstance(StandaloneView::class);
84
        $view->setTemplatePathAndFilename(
85
            GeneralUtility::getFileAbsFileName('EXT:sf_event_mgt/Resources/Private/Backend/PageLayoutView.html')
86
        );
87
        $view->assignMultiple([
88
            'data' => $data,
89
            'pluginName' => $pluginName,
90
        ]);
91
92
        return $view->render();
93
    }
94
95
    /**
96
     * Sets the PID config for the configured PID settings in plugin flexform
97
     */
98
    protected function setPluginPidConfig(
99
        array &$data,
100
        array $flexFormData,
101
        string $pidSetting,
102
        string $sheet = 'sDEF'
103
    ): void {
104
        $pid = (int)$this->getFlexFormFieldValue($flexFormData, 'settings.' . $pidSetting, $sheet);
105
        if ($pid > 0) {
106
            $data[] = [
107
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.' . $pidSetting),
108
                'value' => $this->getRecordData($pid),
109
            ];
110
        }
111
    }
112
113
    /**
114
     * Sets the storagePage configuration
115
     */
116
    protected function setStoragePage(array &$data, array $flexFormData, string $field): void
117
    {
118
        $value = $this->getFlexFormFieldValue($flexFormData, $field);
119
120
        if (!empty($value)) {
121
            $pageIds = GeneralUtility::intExplode(',', $value, true);
122
            $pagesOut = [];
123
124
            foreach ($pageIds as $id) {
125
                $pagesOut[] = $this->getRecordData($id, 'pages');
126
            }
127
128
            $recursiveLevel = (int)$this->getFlexFormFieldValue($flexFormData, 'settings.recursive');
129
            $recursiveLevelText = '';
130
            if ($recursiveLevel === 250) {
131
                $recursiveLevelText = $this->getLanguageService()->sL('LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:recursive.I.5');
132
            } elseif ($recursiveLevel > 0) {
133
                $recursiveLevelText = $this->getLanguageService()->sL('LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:recursive.I.' . $recursiveLevel);
134
            }
135
136
            if (!empty($recursiveLevelText)) {
137
                $recursiveLevelText = '<br />' .
138
                    htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.recursive')) . ' ' .
139
                    $recursiveLevelText;
140
            }
141
142
            $data[] = [
143
                'title' => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.startingpoint'),
144
                'value' => implode(', ', $pagesOut) . $recursiveLevelText,
145
            ];
146
        }
147
    }
148
149
    /**
150
     * Sets information to the data array if override demand setting is disabled
151
     */
152
    protected function setOverrideDemandSettings(array &$data, array $flexFormData): void
153
    {
154
        $field = (int)$this->getFlexFormFieldValue($flexFormData, 'settings.disableOverrideDemand', 'additional');
155
156
        if ($field === 1) {
157
            $text = '<i class="fa fa-check"></i>';
158
159
            // Check if plugin action is "calendar" and if so, show warning that calendar action will not work
160
            $action = $this->getFlexFormFieldValue($flexFormData, 'switchableControllerActions');
161
            if ($action === 'Event->calendar') {
162
                $text .= ' <span class="label label-danger">' .
163
                    htmlspecialchars($this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.pluginCalendarMisonfiguration')) . '</span>';
164
            }
165
166
            $data[] = [
167
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.disableOverrideDemand'),
168
                'value' => $text,
169
            ];
170
        }
171
    }
172
173
    /**
174
     * Sets the order settings
175
     */
176
    protected function setOrderSettings(
177
        array &$data,
178
        array $flexFormData,
179
        string $orderByField,
180
        string $orderDirectionField
181
    ): void {
182
        $orderField = $this->getFlexFormFieldValue($flexFormData, $orderByField);
183
        if (!empty($orderField)) {
184
            $text = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderField.' . $orderField);
185
186
            // Order direction (asc, desc)
187
            $orderDirection = $this->getOrderDirectionSetting($flexFormData, $orderDirectionField);
188
            if ($orderDirection) {
189
                $text .= ', ' . strtolower($orderDirection);
190
            }
191
192
            $data[] = [
193
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderField'),
194
                'value' => $text,
195
            ];
196
        }
197
    }
198
199
    /**
200
     * Returns field value from flexform configuration, including checks if flexform configuration is available
201
     */
202
    protected function getFlexFormFieldValue(array $flexformData, string $key, string $sheet = 'sDEF'): ?string
203
    {
204
        return $flexformData['data'][$sheet]['lDEF'][$key]['vDEF'] ?? '';
205
    }
206
207
    /**
208
     * Returns the record data item
209
     */
210
    protected function getRecordData(int $id, string $table = 'pages'): string
211
    {
212
        $content = '';
213
        $record = BackendUtility::getRecord($table, $id);
214
215
        if (is_array($record)) {
216
            $data = '<span data-toggle="tooltip" data-placement="top" data-title="id=' . $record['uid'] . '">'
217
                . $this->iconFactory->getIconForRecord($table, $record, Icon::SIZE_SMALL)->render()
218
                . '</span> ';
219
            $content = BackendUtility::wrapClickMenuOnIcon($data, $table, $record['uid'], '', '', '+info');
220
221
            $linkTitle = htmlspecialchars(BackendUtility::getRecordTitle($table, $record));
222
            $content .= $linkTitle;
223
        }
224
225
        return $content;
226
    }
227
228
    /**
229
     * Returns order direction
230
     */
231
    private function getOrderDirectionSetting(array $flexFormData, string $orderDirectionField): string
232
    {
233
        $text = '';
234
235
        $orderDirection = $this->getFlexFormFieldValue($flexFormData, $orderDirectionField);
236
        if (!empty($orderDirection)) {
237
            $text = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderDirection.' . $orderDirection . 'ending');
238
        }
239
240
        return $text;
241
    }
242
243
    protected function getLanguageService(): LanguageService
244
    {
245
        return $GLOBALS['LANG'];
246
    }
247
}
248