Passed
Push — typo3_11 ( 7f497e...38eed7 )
by Torben
04:08
created

AbstractPluginPreviewRenderer::setOrderSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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