Passed
Pull Request — master (#146)
by Romain
04:03
created

EntityTcaWriter::getEventConfiguration()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 10
nop 0
dl 0
loc 37
rs 8.9137
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Core\Notification\TCA;
18
19
use CuyZ\Notiz\Backend\FormEngine\DataProvider\DefaultEventFromGet;
20
use CuyZ\Notiz\Backend\FormEngine\DataProvider\EventConfigurationProvider;
21
use CuyZ\Notiz\Core\Notification\Service\NotificationTcaService;
22
use CuyZ\Notiz\Service\Traits\SelfInstantiateTrait;
23
use TYPO3\CMS\Core\SingletonInterface;
24
use TYPO3\CMS\Core\Utility\ArrayUtility;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
abstract class EntityTcaWriter implements SingletonInterface
28
{
29
    use SelfInstantiateTrait;
30
31
    const ENTITY_NOTIFICATION = '__entityNotification';
32
33
    const LLL_FIELDS = 'LLL:EXT:notiz/Resources/Private/Language/Notification/Entity/Fields.xlf';
34
    const LLL_TABS = 'LLL:EXT:notiz/Resources/Private/Language/Notification/Entity/Tabs.xlf';
35
36
    /**
37
     * Contains the name of the TCA table.
38
     *
39
     * @var string
40
     */
41
    protected $tableName;
42
43
    /**
44
     * @var NotificationTcaService
45
     */
46
    protected $service;
47
48
    /**
49
     * @var array
50
     */
51
    private $data = [];
52
53
    /**
54
     * Manual injection for the TCA service depending on the entity type.
55
     */
56
    public function __construct()
57
    {
58
        $this->service = GeneralUtility::makeInstance($this->getNotificationTcaServiceClass());
59
    }
60
61
    /**
62
     * This method must create the basic TCA configuration. It must fill at
63
     * least the `ctrl` and `columns` sections.
64
     *
65
     * Default TYPO3 columns are added automatically, so no need to add them.
66
     * Common notifications columns are also added automatically.
67
     *
68
     * @return array
69
     */
70
    abstract protected function buildTcaArray();
71
72
    /**
73
     * This method builds a TCA array and returns it to be used in a
74
     * configuration file.
75
     *
76
     * @param string $tableName
77
     * @return array
78
     */
79
    final public function getTcaArray($tableName)
80
    {
81
        $this->tableName = $tableName;
82
83
        if ($this->service->definitionHasErrors()) {
84
            $this->data = [
85
                'ctrl' => $this->getDefaultCtrl(),
86
            ];
87
        } else {
88
            // Each sub-class starts to fill the array.
89
            $this->data = $this->buildTcaArray();
90
91
            // Some columns are common for all notification types
92
            $this->addCommonColumns();
93
94
            // The default columns are always the same.
95
            $this->addDefaultTypo3Columns();
96
        }
97
98
        return $this->data;
99
    }
100
101
    /**
102
     * This method returns the TCA service class for the current entity type.
103
     * You can override it to return a class extending `NotificationTcaService`.
104
     *
105
     * @return string
106
     */
107
    abstract protected function getNotificationTcaServiceClass();
108
109
    /**
110
     * Returns the title of the entity, can be a LLL reference.
111
     *
112
     * @return string
113
     */
114
    abstract protected function getEntityTitle();
115
116
    /**
117
     * This method returns the LLL string to use for the `channel` column.
118
     *
119
     * @return string
120
     */
121
    protected function getChannelLabel()
122
    {
123
        return self::LLL_FIELDS . ':field.channel';
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    protected function getDefaultCtrl()
130
    {
131
        return [
132
            'title' => $this->getEntityTitle(),
133
134
            'label' => 'title',
135
            'descriptionColumn' => 'description',
136
            'tstamp' => 'tstamp',
137
            'crdate' => 'crdate',
138
            'cruser_id' => 'cruser_id',
139
            'dividers2tabs' => true,
140
141
            'rootLevel' => -1,
142
            'security' => [
143
                'ignoreWebMountRestriction' => true,
144
                'ignoreRootLevelRestriction' => true,
145
            ],
146
147
            'requestUpdate' => 'event',
148
149
            'languageField' => 'sys_language_uid',
150
            'transOrigPointerField' => 'l10n_parent',
151
            'transOrigDiffSourceField' => 'l10n_diffsource',
152
            'delete' => 'deleted',
153
            'enablecolumns' => [
154
                'disabled' => 'hidden',
155
                'starttime' => 'starttime',
156
                'endtime' => 'endtime',
157
            ],
158
            'searchFields' => 'title,event',
159
            'iconfile' => $this->service->getNotificationIconPath(),
160
161
            self::ENTITY_NOTIFICATION => true,
162
163
            DefaultEventFromGet::ENABLE_DEFAULT_VALUE => true,
164
            EventConfigurationProvider::COLUMN => 'event_configuration_flex',
165
        ];
166
    }
167
168
    /**
169
     * Returns the default TYPO3 columns to include in the final TCA array.
170
     */
171
    private function addDefaultTypo3Columns()
172
    {
173
        $defaultColumns = [
174
            'sys_language_uid' => [
175
                'exclude' => 1,
176
                'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.language',
177
                'config' => [
178
                    'type' => 'select',
179
                    'foreign_table' => 'sys_language',
180
                    'foreign_table_where' => 'ORDER BY sys_language.title',
181
                    'items' => [
182
                        ['LLL:EXT:lang/locallang_general.xlf:LGL.allLanguages', -1],
183
                        ['LLL:EXT:lang/locallang_general.xlf:LGL.default_value', 0],
184
                    ],
185
                ],
186
            ],
187
            'l10n_parent' => [
188
                'displayCond' => 'FIELD:sys_language_uid:>:0',
189
                'exclude' => 1,
190
                'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.l18n_parent',
191
                'l10n_display' => 'defaultAsReadonly',
192
                'config' => [
193
                    'type' => 'select',
194
                    'foreign_table' => $this->tableName,
195
                    'foreign_table_where' => "AND {$this->tableName}.pid=###CURRENT_PID### AND {$this->tableName}.sys_language_uid IN (-1,0)",
196
                    'items' => [
197
                        ['', 0],
198
                    ],
199
                ],
200
            ],
201
            'l10n_diffsource' => [
202
                'config' => [
203
                    'type' => 'passthrough',
204
                ],
205
            ],
206
            't3ver_label' => [
207
                'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.versionLabel',
208
                'config' => [
209
                    'type' => 'input',
210
                    'size' => 30,
211
                    'max' => 255,
212
                ],
213
            ],
214
            'hidden' => [
215
                'exclude' => 1,
216
                'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.hidden',
217
                'config' => [
218
                    'type' => 'check',
219
                ],
220
            ],
221
            'starttime' => [
222
                'exclude' => 1,
223
                'l10n_mode' => 'mergeIfNotBlank',
224
                'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.starttime',
225
                'config' => [
226
                    'type' => 'input',
227
                    'size' => 13,
228
                    'max' => 20,
229
                    'eval' => 'datetime',
230
                    'checkbox' => 0,
231
                    'default' => 0,
232
                    'range' => [
233
                        'lower' => mktime(0, 0, 0, (int)date('m'), (int)date('d'), (int)date('Y')),
234
                    ],
235
                ],
236
            ],
237
            'endtime' => [
238
                'exclude' => 1,
239
                'l10n_mode' => 'mergeIfNotBlank',
240
                'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.endtime',
241
                'config' => [
242
                    'type' => 'input',
243
                    'size' => 13,
244
                    'max' => 20,
245
                    'eval' => 'datetime',
246
                    'checkbox' => 0,
247
                    'default' => 0,
248
                    'range' => [
249
                        'lower' => mktime(0, 0, 0, (int)date('m'), (int)date('d'), (int)date('Y')),
250
                    ],
251
                ],
252
            ],
253
        ];
254
255
        ArrayUtility::mergeRecursiveWithOverrule(
256
            $this->data['columns'],
257
            $defaultColumns
258
        );
259
    }
260
261
    /**
262
     * This method adds the common columns used by all Entity Notifications.
263
     */
264
    private function addCommonColumns()
265
    {
266
        $commonColumns = [
267
            'title' => [
268
                'exclude' => 1,
269
                'label' => self::LLL_FIELDS . ":field.title",
270
                'config' => [
271
                    'type' => 'input',
272
                    'size' => 30,
273
                    'eval' => 'trim,required',
274
                ],
275
            ],
276
277
            'description' => [
278
                'exclude' => 1,
279
                'label' => self::LLL_FIELDS . ":field.description",
280
                'config' => [
281
                    'type' => 'text',
282
                    'cols' => 40,
283
                    'rows' => 5,
284
                ],
285
            ],
286
287
            // Event configuration
288
289
            'event' => [
290
                'exclude' => 1,
291
                'label' => self::LLL_FIELDS . ":field.event",
292
                'l10n_mode' => 'exclude',
293
                'l10n_display' => 'defaultAsReadonly',
294
                'config' => [
295
                    'type' => 'select',
296
                    'size' => 8,
297
                    'itemsProcFunc' => $this->getNotificationTcaServiceClass() . '->getEventsList',
298
                    'eval' => 'required',
299
                ],
300
            ],
301
302
            'event_configuration_flex' => [
303
                'label' => self::LLL_FIELDS . ':field.event_configuration',
304
                'config' => [
305
                    'type' => 'flex',
306
                    'ds_pointerField' => 'event',
307
                    'behaviour' => [
308
                        'allowLanguageSynchronization' => true,
309
                    ],
310
                ],
311
            ],
312
313
            // Channel configuration
314
315
            'channel' => [
316
                'exclude' => 1,
317
                'label' => $this->getChannelLabel(),
318
                'l10n_mode' => 'exclude',
319
                'l10n_display' => 'defaultAsReadonly',
320
                'config' => [
321
                    'type' => 'select',
322
                    'renderType' => 'selectSingle',
323
                    'itemsProcFunc' => $this->getNotificationTcaServiceClass() . '->getChannelsList',
324
                    'eval' => 'required',
325
                ],
326
            ],
327
328
            // Markers configuration
329
330
            'markers' => [
331
                'exclude' => 1,
332
                'label' => self::LLL_FIELDS . ":field.markers",
333
                'l10n_display' => 'defaultAsReadonly',
334
                'config' => [
335
                    'type' => 'user',
336
                    'userFunc' => $this->getNotificationTcaServiceClass() . '->getMarkersLabel',
337
                ],
338
            ],
339
        ];
340
341
        ArrayUtility::mergeRecursiveWithOverrule(
342
            $this->data['columns'],
343
            $commonColumns
344
        );
345
    }
346
}
347