Passed
Push — master ( 4fcae8...b8fbdf )
by Romain
03:40
created

NotificationTcaService::getNotification()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 5
nop 1
dl 0
loc 11
rs 9.2
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\Service;
18
19
use CuyZ\Notiz\Core\Definition\DefinitionService;
20
use CuyZ\Notiz\Core\Definition\Tree\Definition;
21
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
22
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
23
use CuyZ\Notiz\Core\Event\Service\EventFactory;
24
use CuyZ\Notiz\Core\Exception\NotImplementedException;
25
use CuyZ\Notiz\Core\Notification\Notification;
26
use CuyZ\Notiz\Core\Support\NotizConstants;
27
use CuyZ\Notiz\Domain\Property\Marker;
28
use CuyZ\Notiz\Service\BackendUriBuilder;
29
use CuyZ\Notiz\Service\Container;
30
use CuyZ\Notiz\Service\LocalizationService;
31
use CuyZ\Notiz\Service\StringService;
32
use CuyZ\Notiz\Service\ViewService;
33
use TYPO3\CMS\Core\SingletonInterface;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Core\Utility\MathUtility;
36
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
37
38
/**
39
 * Utility service to ease TCA manipulation for TYPO3 notification records.
40
 */
41
abstract class NotificationTcaService implements SingletonInterface
42
{
43
    /**
44
     * @var EventFactory
45
     */
46
    protected $eventFactory;
47
48
    /**
49
     * @var DefinitionService
50
     */
51
    protected $definitionService;
52
53
    /**
54
     * @var ViewService
55
     */
56
    protected $viewService;
57
58
    /**
59
     * @var BackendUriBuilder
60
     */
61
    protected $backendUriBuilder;
62
63
    /**
64
     * @var DataMapper
65
     */
66
    protected $dataMapper;
67
68
    /**
69
     * @var Notification[]
70
     */
71
    private $notification;
72
73
    /**
74
     * Manual dependency injection.
75
     */
76
    public function __construct()
77
    {
78
        $this->eventFactory = Container::get(EventFactory::class);
79
        $this->definitionService = Container::get(DefinitionService::class);
80
        $this->viewService = Container::get(ViewService::class);
81
        $this->backendUriBuilder = Container::get(BackendUriBuilder::class);
82
        $this->dataMapper = Container::get(DataMapper::class);
83
    }
84
85
    /**
86
     * @param array $parameters
87
     * @return bool
88
     */
89
    public function definitionContainsErrors(array $parameters)
90
    {
91
        $result = !$this->definitionHasErrors();
92
93
        if (in_array('inverted', $parameters['conditionParameters'])) {
94
            $result = !$result;
95
        }
96
97
        return $result;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getErrorMessage()
104
    {
105
        $view = $this->viewService->getStandaloneView('Backend/TCA/DefinitionErrorMessage');
106
107
        $view->assign('showDefinitionUri', $this->backendUriBuilder->uriFor('showDefinition'));
108
        $view->assign('result', $this->definitionService->getValidationResult());
109
110
        return $view->render();
111
    }
112
113
    /**
114
     * Loads all available events and stores them as an array to be used in the
115
     * TCA.
116
     *
117
     * @param array $parameters
118
     */
119
    public function getEventsList(array &$parameters)
120
    {
121
        if ($this->definitionHasErrors()) {
122
            return;
123
        }
124
125
        $eventGroups = $this->getDefinition()->getEventGroups();
126
127
        foreach ($eventGroups as $eventGroup) {
128
            $parameters['items'][] = [
129
                $eventGroup->getLabel(),
130
                '--div--',
131
            ];
132
133
            foreach ($eventGroup->getEvents() as $event) {
134
                $parameters['items'][] = [
135
                    $event->getLabel(),
136
                    $event->getFullIdentifier(),
137
                ];
138
            }
139
        }
140
    }
141
142
    /**
143
     * This function will fetch the event selected in the given notification.
144
     *
145
     * If the notification is new or if the event identifier is not found, the
146
     * first event from the first event group is returned.
147
     *
148
     * @param array $row
149
     * @return EventDefinition
150
     */
151
    protected function getSelectedEvent(array $row)
152
    {
153
        $definition = $this->getDefinition();
154
155
        // The first configured event is selected by default.
156
        $event = $definition->getFirstEventGroup()->getFirstEvent();
157
158
        // We check if the record already exists in the database...
159
        if (MathUtility::canBeInterpretedAsInteger($row['uid'])) {
160
            // @PHP7
161
            $eventValue = is_array($row['event'])
162
                ? $row['event'][0]
163
                : $row['event'];
164
165
            list($eventGroupIdentifier, $eventIdentifier) = GeneralUtility::trimExplode('.', $eventValue);
166
167
            if ($definition->hasEventGroup($eventGroupIdentifier)) {
168
                $eventGroup = $definition->getEventGroup($eventGroupIdentifier);
169
170
                if ($eventGroup->hasEvent($eventIdentifier)) {
171
                    $event = $eventGroup->getEvent($eventIdentifier);
172
                }
173
            }
174
        }
175
176
        return $event;
177
    }
178
179
    /**
180
     * Loads all markers for the current selected event and formats them as a
181
     * list to be displayed on the edit form.
182
     *
183
     * @param array $parameters
184
     * @return string
185
     */
186
    public function getMarkersLabel(array &$parameters)
187
    {
188
        if ($this->definitionHasErrors()) {
189
            return '';
190
        }
191
192
        $row = $parameters['row'];
193
        $eventDefinition = $this->getSelectedEvent($row);
194
        $notification = $this->getNotification($row);
195
196
        /** @var Marker[] $markers */
197
        $markers = $eventDefinition->getPropertiesDefinition(Marker::class, $notification);
198
199
        $output = '';
200
201
        foreach ($markers as $marker) {
202
            $label = StringService::mark($marker->getLabel());
203
            $output .= "<tr><td><strong>{$marker->getFormattedName()}</strong></td><td>$label</td></tr>";
204
        }
205
206
        $description = LocalizationService::localize('Notification/Entity/Fields:field.markers.description', [$eventDefinition->getLabel()]);
207
208
        return <<<HTML
209
<p>$description</p>
210
211
<table class="table table-striped table-hover">
212
    <tbody>
213
        $output
214
    </tbody>
215
</table>
216
HTML;
217
    }
218
219
    /**
220
     * Loads all available channels and stores them as an array to be used in
221
     * the TCA.
222
     *
223
     * @param array $parameters
224
     */
225
    public function getChannelsList(array &$parameters)
226
    {
227
        if ($this->definitionHasErrors()) {
228
            return;
229
        }
230
231
        foreach ($this->getNotificationDefinition()->getChannels() as $channelDefinition) {
232
            $label = $channelDefinition->getLabel();
233
234
            if (empty($label)) {
235
                $label = $channelDefinition->getIdentifier();
236
            }
237
238
            $parameters['items'][] = [$label, $channelDefinition->getClassName()];
239
        }
240
    }
241
242
    /**
243
     * If a RTE is using a missing CKEditor preset, a message is shown to the
244
     * user to help him fix it.
245
     *
246
     * @param array $parent
247
     * @return string
248
     */
249
    public function showCKEditorPresetMissing(array $parent)
250
    {
251
        $preset = $parent['parameters']['preset'];
252
253
        $message = LocalizationService::localize('Notification/Entity/Fields:field.rte.ck_editor_preset_missing', [$preset]);
254
        $message = StringService::mark($message, '<code>$1</code>');
255
256
        return '<span class="bg-danger">' . $message . '</span>';
257
    }
258
259
    /**
260
     * Returns a notification object based on an array containing the
261
     * notification properties.
262
     *
263
     * @param array $row
264
     * @return Notification
265
     */
266
    protected function getNotification(array $row)
267
    {
268
        $hash = json_encode($row);
269
270
        if (!isset($this->notification[$hash])) {
271
            $this->notification[$hash] = isset($row['uid']) && is_integer($row['uid'])
272
                ? $this->getNotificationDefinition()->getProcessor()->getNotificationFromIdentifier($row['uid'])
273
                : reset($this->dataMapper->map($this->getNotificationDefinition()->getClassName(), [$row]));
274
        }
275
276
        return $this->notification[$hash];
277
    }
278
279
    /**
280
     * @param array $array
281
     * @param $label
282
     */
283
    protected function appendOptionGroup(array &$array, $label)
284
    {
285
        array_unshift($array, ['label' => "––– $label –––", 'value' => '--div--']);
286
    }
287
288
    /**
289
     * @return string
290
     */
291
    public function getNotificationIconPath()
292
    {
293
        if ($this->definitionService->getValidationResult()->hasErrors()) {
294
            return NotizConstants::EXTENSION_ICON_DEFAULT;
295
        }
296
297
        return $this->getNotificationDefinition()->getIconPath();
298
    }
299
300
    /**
301
     * @return Definition
302
     */
303
    public function getDefinition()
304
    {
305
        return $this->definitionService->getDefinition();
306
    }
307
308
    /**
309
     * @return NotificationDefinition
310
     */
311
    protected function getNotificationDefinition()
312
    {
313
        return $this->getDefinition()->getNotification($this->getNotificationIdentifier());
314
    }
315
316
    /**
317
     * This method must return the current notification identifier to be used to
318
     * retrieve the current notification definition.
319
     *
320
     * @return string
321
     * @throws NotImplementedException
322
     */
323
    protected function getNotificationIdentifier()
324
    {
325
        throw NotImplementedException::tcaServiceNotificationIdentifierMissing(__METHOD__);
326
    }
327
328
    /**
329
     * @return bool
330
     */
331
    public function definitionHasErrors()
332
    {
333
        return $this->definitionService->getValidationResult()->hasErrors();
334
    }
335
}
336