Passed
Push — master ( af9933...6d2375 )
by Torben
04:41 queued 01:21
created

getFlexFormFieldValue()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 7
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 13
rs 8.8333
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
     * @param GridColumnItem $item
41
     * @return string
42
     */
43
    public function renderPageModulePreviewHeader(GridColumnItem $item): string
44
    {
45
        return '';
46
    }
47
48
    /**
49
     * Renders the content of the plugin preview. Must be overwritten in extending class.
50
     *
51
     * @param GridColumnItem $item
52
     * @return string
53
     */
54
    public function renderPageModulePreviewContent(GridColumnItem $item): string
55
    {
56
        return '';
57
    }
58
59
    /**
60
     * Render the footer. Can be overwritten in extending class if required
61
     *
62
     * @param GridColumnItem $item
63
     * @return string
64
     */
65
    public function renderPageModulePreviewFooter(GridColumnItem $item): string
66
    {
67
        return '';
68
    }
69
70
    /**
71
     * Render the plugin preview
72
     *
73
     * @param string $previewHeader
74
     * @param string $previewContent
75
     * @param GridColumnItem $item
76
     * @return string
77
     */
78
    public function wrapPageModulePreview(string $previewHeader, string $previewContent, GridColumnItem $item): string
79
    {
80
        return $previewHeader . $previewContent;
81
    }
82
83
    /**
84
     * Returns the plugin name
85
     *
86
     * @param array $record
87
     * @return string
88
     */
89
    protected function getPluginName(array $record): string
90
    {
91
        $pluginId = str_replace('sfeventmgt_', '', $record['list_type']);
92
        return htmlspecialchars($this->getLanguageService()->sL(self::LLPATH . 'plugin.' . $pluginId . '.title'));
93
    }
94
95
    /**
96
     * Renders the given data and action as HTML table for plugin preview
97
     *
98
     * @param array $data
99
     * @param string $pluginName
100
     * @return string
101
     */
102
    protected function renderAsTable(array $data, string $pluginName = ''): string
103
    {
104
        $view = GeneralUtility::makeInstance(StandaloneView::class);
105
        $view->setTemplatePathAndFilename(
106
            GeneralUtility::getFileAbsFileName('EXT:sf_event_mgt/Resources/Private/Backend/PageLayoutView.html')
107
        );
108
        $view->assignMultiple([
109
            'data' => $data,
110
            'pluginName' => $pluginName,
111
        ]);
112
113
        return $view->render();
114
    }
115
116
    /**
117
     * Sets the PID config for the configured PID settings in plugin flexform
118
     *
119
     * @param array $data
120
     * @param array $flexFormData
121
     * @param string $pidSetting
122
     * @param string $sheet
123
     */
124
    protected function setPluginPidConfig(
125
        array &$data,
126
        array $flexFormData,
127
        string $pidSetting,
128
        string $sheet = 'sDEF'
129
    ): void {
130
        $pid = (int)$this->getFlexFormFieldValue($flexFormData, 'settings.' . $pidSetting, $sheet);
131
        if ($pid > 0) {
132
            $data[] = [
133
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.' . $pidSetting),
134
                'value' => $this->getRecordData($pid),
135
            ];
136
        }
137
    }
138
139
    /**
140
     * Sets the storagePage configuration
141
     *
142
     * @param array $data
143
     * @param array $flexFormData
144
     * @param string $field
145
     */
146
    protected function setStoragePage(array &$data, array $flexFormData, string $field): void
147
    {
148
        $value = $this->getFlexFormFieldValue($flexFormData, $field);
149
150
        if (!empty($value)) {
151
            $pageIds = GeneralUtility::intExplode(',', $value, true);
152
            $pagesOut = [];
153
154
            foreach ($pageIds as $id) {
155
                $pagesOut[] = $this->getRecordData($id, 'pages');
156
            }
157
158
            $recursiveLevel = (int)$this->getFlexFormFieldValue($flexFormData, 'settings.recursive');
159
            $recursiveLevelText = '';
160
            if ($recursiveLevel === 250) {
161
                $recursiveLevelText = $this->getLanguageService()->sL('LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:recursive.I.5');
162
            } elseif ($recursiveLevel > 0) {
163
                $recursiveLevelText = $this->getLanguageService()->sL('LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:recursive.I.' . $recursiveLevel);
164
            }
165
166
            if (!empty($recursiveLevelText)) {
167
                $recursiveLevelText = '<br />' .
168
                    htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.recursive')) . ' ' .
169
                    $recursiveLevelText;
170
            }
171
172
            $data[] = [
173
                'title' => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.startingpoint'),
174
                'value' => implode(', ', $pagesOut) . $recursiveLevelText,
175
            ];
176
        }
177
    }
178
179
    /**
180
     * Sets information to the data array if override demand setting is disabled
181
     *
182
     * @param array $data
183
     * @param array $flexFormData
184
     */
185
    protected function setOverrideDemandSettings(array &$data, array $flexFormData): void
186
    {
187
        $field = (int)$this->getFlexFormFieldValue($flexFormData, 'settings.disableOverrideDemand', 'additional');
188
189
        if ($field === 1) {
190
            $text = '<i class="fa fa-check"></i>';
191
192
            // Check if plugin action is "calendar" and if so, show warning that calendar action will not work
193
            $action = $this->getFlexFormFieldValue($flexFormData, 'switchableControllerActions');
194
            if ($action === 'Event->calendar') {
195
                $text .= ' <span class="label label-danger">' .
196
                    htmlspecialchars($this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.pluginCalendarMisonfiguration')) . '</span>';
197
            }
198
199
            $data[] = [
200
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.disableOverrideDemand'),
201
                'value' => $text,
202
            ];
203
        }
204
    }
205
206
    /**
207
     * Sets the order settings
208
     *
209
     * @param array $data
210
     * @param array $flexFormData
211
     * @param string $orderByField
212
     * @param string $orderDirectionField
213
     */
214
    protected function setOrderSettings(
215
        array &$data,
216
        array $flexFormData,
217
        string $orderByField,
218
        string $orderDirectionField
219
    ): void {
220
        $orderField = $this->getFlexFormFieldValue($flexFormData, $orderByField);
221
        if (!empty($orderField)) {
222
            $text = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderField.' . $orderField);
223
224
            // Order direction (asc, desc)
225
            $orderDirection = $this->getOrderDirectionSetting($flexFormData, $orderDirectionField);
226
            if ($orderDirection) {
227
                $text .= ', ' . strtolower($orderDirection);
228
            }
229
230
            $data[] = [
231
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderField'),
232
                'value' => $text,
233
            ];
234
        }
235
    }
236
237
    /**
238
     * Returns the current title of the switchableControllerAction
239
     *
240
     * @param array $flexFormData
241
     * @return string
242
     */
243
    protected function getSwitchableControllerActionTitle(array $flexFormData): string
244
    {
245
        $title = '';
246
        $actions = $this->getFlexFormFieldValue($flexFormData, 'switchableControllerActions');
247
        switch ($actions) {
248
            case 'Event->list':
249
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.list');
250
                break;
251
            case 'Event->detail;Event->icalDownload':
252
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.detail');
253
                break;
254
            case 'Event->registration;Event->saveRegistration;Event->saveRegistrationResult;Event->confirmRegistration;Event->cancelRegistration':
255
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.registration');
256
                break;
257
            case 'Event->search':
258
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.search');
259
                break;
260
            case 'Event->calendar':
261
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.calendar');
262
                break;
263
            default:
264
        }
265
266
        return $title;
267
    }
268
269
    /**
270
     * Returns field value from flexform configuration, including checks if flexform configuration is available
271
     *
272
     * @param string $key name of the key
273
     * @param string $sheet name of the sheet
274
     * @return string|null if nothing found, value if found
275
     */
276
    protected function getFlexFormFieldValue(array $flexformData, string $key, string $sheet = 'sDEF'): ?string
277
    {
278
        if (isset($flexformData['data'])) {
279
            $flexform = $flexformData['data'];
280
            if (is_array($flexform ?? false) && is_array($flexform[$sheet] ?? false) &&
281
                is_array($flexform[$sheet]['lDEF'] ?? false) && is_array($flexform[$sheet]['lDEF'][$key] ?? false) &&
282
                isset($flexform[$sheet]['lDEF'][$key]['vDEF'])
283
            ) {
284
                return $flexform[$sheet]['lDEF'][$key]['vDEF'];
285
            }
286
        }
287
288
        return null;
289
    }
290
291
    /**
292
     * Returns the record data item
293
     *
294
     * @param int $id
295
     * @param string $table
296
     * @return string
297
     */
298
    protected function getRecordData(int $id, string $table = 'pages'): string
299
    {
300
        $content = '';
301
        $record = BackendUtility::getRecord($table, $id);
302
303
        if (is_array($record)) {
304
            $data = '<span data-toggle="tooltip" data-placement="top" data-title="id=' . $record['uid'] . '">'
305
                . $this->iconFactory->getIconForRecord($table, $record, Icon::SIZE_SMALL)->render()
306
                . '</span> ';
307
            $content = BackendUtility::wrapClickMenuOnIcon($data, $table, $record['uid'], '', '', '+info');
308
309
            $linkTitle = htmlspecialchars(BackendUtility::getRecordTitle($table, $record));
310
            $content .= $linkTitle;
311
        }
312
313
        return $content;
314
    }
315
316
    /**
317
     * Returns order direction
318
     *
319
     * @param array $flexFormData
320
     * @param string $orderDirectionField
321
     * @return string
322
     */
323
    private function getOrderDirectionSetting(array $flexFormData, string $orderDirectionField): string
324
    {
325
        $text = '';
326
327
        $orderDirection = $this->getFlexFormFieldValue($flexFormData, $orderDirectionField);
328
        if (!empty($orderDirection)) {
329
            $text = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderDirection.' . $orderDirection . 'ending');
330
        }
331
332
        return $text;
333
    }
334
335
    protected function getLanguageService(): LanguageService
336
    {
337
        return $GLOBALS['LANG'];
338
    }
339
}
340