Passed
Pull Request — dev (#13)
by Romain
03:24
created

NotificationTcaService::appendOptionGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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