Completed
Push — development ( 4305c3...8ac1e6 )
by Torben
02:38
created

PageLayoutView::getLanguageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace DERHANSEN\SfEventMgt\Hooks;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Backend\Template\DocumentTemplate;
12
use TYPO3\CMS\Backend\Utility\BackendUtility as BackendUtilityCore;
13
use TYPO3\CMS\Core\Imaging\Icon;
14
use TYPO3\CMS\Core\Imaging\IconFactory;
15
use TYPO3\CMS\Core\Page\PageRenderer;
16
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Fluid\View\StandaloneView;
19
20
/**
21
 * Hook to display verbose information about plugin in Web>Page module
22
 */
23
class PageLayoutView
24
{
25
    /**
26
     * Path to the locallang file
27
     *
28
     * @var string
29
     */
30
    const LLPATH = 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang_be.xlf:';
31
32
    /**
33
     * Data rendered in table of Plugin settings
34
     *
35
     * @var array
36
     */
37
    public $data = [];
38
39
    /**
40
     * Flexform information
41
     *
42
     * @var array
43
     */
44
    public $flexformData = [];
45
46
    /**
47
     * @var IconFactory
48
     */
49
    protected $iconFactory;
50
51
    /**
52
     * PageLayoutView constructor
53
     */
54
    public function __construct()
55
    {
56
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
57
    }
58
59
    /**
60
     * Returns information about this extension's event plugin
61
     *
62
     * @param array $params Parameters to the hook
63
     * @return string Information about plugin
64
     */
65
    public function getEventPluginSummary(array $params)
66
    {
67
        $header = htmlspecialchars($this->getLanguageService()->sL(self::LLPATH . 'plugin.title'));
68
69
        // Add flexform switchable controller actions
70
        $this->flexformData = GeneralUtility::xml2array($params['row']['pi_flexform']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \TYPO3\CMS\Core\Utility\...['row']['pi_flexform']) of type * is incompatible with the declared type array of property $flexformData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
72
        // Extend header by flexible controller action
73
        $action = htmlspecialchars($this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode')) . ': ';
74
        $action .= $this->getSwitchableControllerActionTitle();
75
76
        $this->getPluginPidConfig('listPid', 'additional');
77
        $this->getPluginPidConfig('detailPid', 'additional');
78
        $this->getPluginPidConfig('registrationPid', 'additional');
79
        $this->getPluginPidConfig('paymentPid', 'additional');
80
        $this->getStoragePage('settings.storagePage');
81
        $this->getOrderSettings('settings.orderField', 'settings.orderDirection');
82
        $this->getOverrideDemandSettings();
83
84
        if ($this->showFieldsForListViewOnly()) {
85
            $this->getCategoryConjuction();
86
            $this->getCategorySettings();
87
        }
88
89
        $result = $this->renderSettingsAsTable($header, $action, $this->data);
90
91
        return $result;
92
    }
93
94
    /**
95
     * Returns information about this extension's user registrations plugin
96
     *
97
     * @param array $params Parameters to the hook
98
     * @return string Information about plugin
99
     */
100
    public function getUserRegPluginSummary(array $params)
101
    {
102
        $header = htmlspecialchars($this->getLanguageService()->sL(self::LLPATH . 'plugin_userreg.title'));
103
104
        // Add flexform switchable controller actions
105
        $this->flexformData = GeneralUtility::xml2array($params['row']['pi_flexform']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \TYPO3\CMS\Core\Utility\...['row']['pi_flexform']) of type * is incompatible with the declared type array of property $flexformData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
107
        $this->getPluginPidConfig('registrationPid', 'sDEF');
108
        $this->getStoragePage('settings.userRegistration.storagePage');
109
        $this->getOrderSettings('settings.userRegistration.orderField', 'settings.userRegistration.orderDirection');
110
111
        $result = $this->renderSettingsAsTable($header, null, $this->data);
112
113
        return $result;
114
    }
115
116
    /**
117
     * Returns the current title of the switchableControllerAction
118
     *
119
     * @return string
120
     */
121
    protected function getSwitchableControllerActionTitle()
122
    {
123
        $title = '';
124
        $actions = $this->getFieldFromFlexform('switchableControllerActions');
125
        switch ($actions) {
126
            case 'Event->list':
127
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.list');
128
                break;
129
            case 'Event->detail;Event->icalDownload':
130
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.detail');
131
                break;
132
            case 'Event->registration;Event->saveRegistration;Event->saveRegistrationResult;Event->confirmRegistration;Event->cancelRegistration':
133
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.registration');
134
                break;
135
            case 'Event->search':
136
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.search');
137
                break;
138
            case 'Event->calendar':
139
                $title = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.mode.calendar');
140
                break;
141
            default:
142
        }
143
144
        return $title;
145
    }
146
147
    /**
148
     * Returns, if fields, that are only visible for list view, should be shown
149
     *
150
     * @return bool
151
     */
152
    protected function showFieldsForListViewOnly()
153
    {
154
        $actions = $this->getFieldFromFlexform('switchableControllerActions');
155
        switch ($actions) {
156
            case 'Event->list':
157
            case 'Event->search':
158
            case 'Event->calendar':
159
                $result = true;
160
                break;
161
            default:
162
                $result = false;
163
        }
164
165
        return $result;
166
    }
167
168
    /**
169
     * Returns the PID config for the given PID
170
     *
171
     * @param string $pidSetting
172
     * @param $sheet
173
     */
174
    public function getPluginPidConfig($pidSetting, $sheet = 'sDEF')
175
    {
176
        $pid = (int)$this->getFieldFromFlexform('settings.' . $pidSetting, $sheet);
177
        if ($pid > 0) {
178
            $this->data[] = [
179
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.' . $pidSetting),
180
                'value' => $this->getRecordData($pid)
181
            ];
182
        }
183
    }
184
185
    /**
186
     * @param int $id
187
     * @param string $table
188
     * @return string
189
     */
190
    public function getRecordData($id, $table = 'pages')
191
    {
192
        $content = '';
193
        $record = BackendUtilityCore::getRecord($table, $id);
194
195
        if (is_array($record)) {
196
            $data = '<span data-toggle="tooltip" data-placement="top" data-title="id=' . $record['uid'] . '">'
197
                . $this->iconFactory->getIconForRecord($table, $record, Icon::SIZE_SMALL)->render()
198
                . '</span> ';
199
            $content = BackendUtilityCore::wrapClickMenuOnIcon($data, $table, $record['uid'], true, '', '+info');
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
200
201
            $linkTitle = htmlspecialchars(BackendUtilityCore::getRecordTitle($table, $record));
202
            $content .= $linkTitle;
203
        }
204
205
        return $content;
206
    }
207
208
    /**
209
     * Get the storagePage
210
     *
211
     * @param string $field
212
     */
213
    public function getStoragePage($field)
214
    {
215
        $value = $this->getFieldFromFlexform($field);
216
217
        if (!empty($value)) {
218
            $pageIds = GeneralUtility::intExplode(',', $value, true);
219
            $pagesOut = [];
220
221
            foreach ($pageIds as $id) {
222
                $pagesOut[] = $this->getRecordData($id, 'pages');
223
            }
224
225
            $recursiveLevel = (int)$this->getFieldFromFlexform('settings.recursive');
226
            $recursiveLevelText = '';
227
            if ($recursiveLevel === 250) {
228
                $recursiveLevelText = $this->getLanguageService()->sL('LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:recursive.I.5');
229
            } elseif ($recursiveLevel > 0) {
230
                $recursiveLevelText = $this->getLanguageService()->sL('LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:recursive.I.' . $recursiveLevel);
231
            }
232
233
            if (!empty($recursiveLevelText)) {
234
                $recursiveLevelText = '<br />' .
235
                    htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.recursive')) . ' ' .
236
                    $recursiveLevelText;
237
            }
238
239
            $this->data[] = [
240
                'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_general.xlf:LGL.startingpoint'),
241
                'value' => implode(', ', $pagesOut) . $recursiveLevelText
242
            ];
243
        }
244
    }
245
246
    /**
247
     * Get order settings
248
     *
249
     * @param string $orderByField
250
     * @param string $orderDirectionField
251
     */
252
    public function getOrderSettings($orderByField, $orderDirectionField)
253
    {
254
        $orderField = $this->getFieldFromFlexform($orderByField);
255
        if (!empty($orderField)) {
256
            $text = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderField.' . $orderField);
257
258
            // Order direction (asc, desc)
259
            $orderDirection = $this->getOrderDirectionSetting($orderDirectionField);
260
            if ($orderDirection) {
261
                $text .= ', ' . strtolower($orderDirection);
262
            }
263
264
            $this->data[] = [
265
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderField'),
266
                'value' => $text
267
            ];
268
        }
269
    }
270
271
    /**
272
     * Get order direction
273
     * @param string $orderDirectionField
274
     * @return string
275
     */
276
    public function getOrderDirectionSetting($orderDirectionField)
277
    {
278
        $text = '';
279
280
        $orderDirection = $this->getFieldFromFlexform($orderDirectionField);
281
        if (!empty($orderDirection)) {
282
            $text = $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.orderDirection.' . $orderDirection . 'ending');
283
        }
284
285
        return $text;
286
    }
287
288
    /**
289
     * Get category conjunction if a category is selected
290
     * @return void
291
     */
292
    public function getCategoryConjuction()
293
    {
294
        // If not category is selected, we do not need to display the category mode
295
        if ($this->getFieldFromFlexform('settings.category') === null) {
296
            return;
297
        }
298
299
        $categoryConjunction = $this->getFieldFromFlexform('settings.categoryConjunction');
300
        switch ($categoryConjunction) {
301
            case 'or':
302
            case 'and':
303
            case 'notor':
304
            case 'notand':
305
                $text = htmlspecialchars($this->getLanguageService()->sL(
306
                    self::LLPATH . 'flexforms_general.categoryConjunction.' . $categoryConjunction
307
                ));
308
                break;
309
            default:
310
                $text = htmlspecialchars($this->getLanguageService()->sL(
311
                    self::LLPATH . 'flexforms_general.categoryConjunction.ignore'
312
                ));
313
                $text .= ' <span class="label label-warning">' . htmlspecialchars($this->getLanguageService()->sL(
314
                    self::LLPATH . 'flexforms_general.possibleMisconfiguration'
315
                )) . '</span>';
316
        }
317
318
        $this->data[] = [
319
            'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.categoryConjunction'),
320
            'value' => $text
321
        ];
322
    }
323
324
    /**
325
     * Get information if override demand setting is disabled or not
326
     */
327
    public function getOverrideDemandSettings()
328
    {
329
        $field = $this->getFieldFromFlexform('settings.disableOverrideDemand', 'additional');
330
331
        if ($field == 1) {
332
            $this->data[] = [
333
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.disableOverrideDemand'),
334
                'value' => '<i class="fa fa-check"></i>'
335
            ];
336
        }
337
    }
338
339
    /**
340
     * Get category settings
341
     */
342
    public function getCategorySettings()
343
    {
344
        $categories = GeneralUtility::intExplode(',', $this->getFieldFromFlexform('settings.category'), true);
345
        if (count($categories) > 0) {
346
            $categoriesOut = [];
347
            foreach ($categories as $id) {
348
                $categoriesOut[] = $this->getRecordData($id, 'sys_category');
349
            }
350
351
            $this->data[] = [
352
                'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.category'),
353
                'value' => implode(', ', $categoriesOut)
354
            ];
355
356
            $includeSubcategories = $this->getFieldFromFlexform('settings.includeSubcategories');
357
            if ($includeSubcategories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $includeSubcategories of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
358
                $this->data[] = [
359
                    'title' => $this->getLanguageService()->sL(self::LLPATH . 'flexforms_general.includeSubcategories'),
360
                    'value' => '<i class="fa fa-check"></i>'
361
                ];
362
            }
363
        }
364
    }
365
366
    /**
367
     * Render the settings as table for Web>Page module
368
     * System settings are displayed in mono font
369
     *
370
     * @param string $header
371
     * @param string $action
372
     * @param array $data
373
     * @return string
374
     */
375
    protected function renderSettingsAsTable($header, $action, $data)
376
    {
377
        $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
378
        $pageRenderer->loadRequireJsModule('TYPO3/CMS/News/PageLayout');
379
        $pageRenderer->addCssFile(
380
            ExtensionManagementUtility::extRelPath('sf_event_mgt') . 'Resources/Public/Css/Backend/PageLayoutView.css'
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Core\Utility\E...ntUtility::extRelPath() has been deprecated with message: since TYPO3 v8, will be removed in TYPO3 v9, use PathUtility::getAbsoluteWebPath(), or ->siteRelPath()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
381
        );
382
383
        $view = GeneralUtility::makeInstance(StandaloneView::class);
384
        $view->setTemplatePathAndFilename(
385
            GeneralUtility::getFileAbsFileName('EXT:sf_event_mgt/Resources/Private/Backend/PageLayoutView.html')
386
        );
387
        $view->assignMultiple([
388
            'header' => $header,
389
            'action' => $action,
390
            'data' => $data
391
        ]);
392
393
        return $view->render();
394
    }
395
396
    /**
397
     * Get field value from flexform configuration,
398
     * including checks if flexform configuration is available
399
     *
400
     * @param string $key name of the key
401
     * @param string $sheet name of the sheet
402
     * @return string|null if nothing found, value if found
403
     */
404
    public function getFieldFromFlexform($key, $sheet = 'sDEF')
405
    {
406
        $flexform = $this->flexformData;
407
        if (isset($flexform['data'])) {
408
            $flexform = $flexform['data'];
409
            if (is_array($flexform) && is_array($flexform[$sheet]) && is_array($flexform[$sheet]['lDEF'])
410
                && is_array($flexform[$sheet]['lDEF'][$key]) && isset($flexform[$sheet]['lDEF'][$key]['vDEF'])
411
            ) {
412
                return $flexform[$sheet]['lDEF'][$key]['vDEF'];
413
            }
414
        }
415
416
        return null;
417
    }
418
419
    /**
420
     * Return language service instance
421
     *
422
     * @return \TYPO3\CMS\Lang\LanguageService
423
     */
424
    public function getLanguageService()
425
    {
426
        return $GLOBALS['LANG'];
427
    }
428
429
    /**
430
     * Get the DocumentTemplate
431
     *
432
     * @return DocumentTemplate
433
     */
434
    protected function getDocumentTemplate()
435
    {
436
        return $GLOBALS['TBE_TEMPLATE'];
437
    }
438
}
439