Passed
Push — master ( e9c972...8e746e )
by Nathan
02:57
created

EntityNotification::isCreatable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
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\Domain\Notification;
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\Channel\ChannelDefinition;
23
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
24
use CuyZ\Notiz\Core\Notification\MultipleChannelsNotification;
25
use CuyZ\Notiz\Core\Notification\Notification;
26
use CuyZ\Notiz\Service\Container;
27
use TYPO3\CMS\Backend\Utility\BackendUtility;
28
use TYPO3\CMS\Core\Utility\ArrayUtility;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
31
use TYPO3\CMS\Extbase\Domain\Model\BackendUser;
32
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
33
use TYPO3\CMS\Extbase\Service\FlexFormService;
34
35
abstract class EntityNotification extends AbstractEntity implements Notification, MultipleChannelsNotification
36
{
37
    /**
38
     * @var string
39
     */
40
    protected $title;
41
42
    /**
43
     * @var string
44
     */
45
    protected $description;
46
47
    /**
48
     * @var string
49
     */
50
    protected $event;
51
52
    /**
53
     * @var string
54
     */
55
    protected $channel;
56
57
    /**
58
     * @var string
59
     */
60
    protected $eventConfigurationFlex;
61
62
    /**
63
     * @var array
64
     */
65
    protected $eventConfiguration;
66
67
    /**
68
     * @var \TYPO3\CMS\Extbase\Domain\Model\BackendUser
69
     * @lazy
70
     */
71
    protected $backendUser;
72
73
    /**
74
     * @return string
75
     */
76
    public function getTitle()
77
    {
78
        return $this->title;
79
    }
80
81
    /**
82
     * @param string $title
83
     */
84
    public function setTitle($title)
85
    {
86
        $this->title = $title;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getDescription()
93
    {
94
        return $this->description;
95
    }
96
97
    /**
98
     * @param string $description
99
     */
100
    public function setDescription($description)
101
    {
102
        $this->description = $description;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getEvent()
109
    {
110
        return $this->event;
111
    }
112
113
    /**
114
     * @param string $event
115
     */
116
    public function setEvent($event)
117
    {
118
        $this->event = $event;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getChannel()
125
    {
126
        return $this->channel;
127
    }
128
129
    /**
130
     * @param string $channel
131
     */
132
    public function setChannel($channel)
133
    {
134
        $this->channel = $channel;
135
    }
136
137
    /**
138
     * @param string $eventConfigurationFlex
139
     */
140
    public function setEventConfigurationFlex($eventConfigurationFlex)
141
    {
142
        $this->eventConfigurationFlex = $eventConfigurationFlex;
143
    }
144
145
    /**
146
     * @return NotificationDefinition
147
     */
148
    public function getNotificationDefinition()
149
    {
150
        return $this->getDefinition()->getNotification(static::getDefinitionIdentifier());
151
    }
152
153
    /**
154
     * @return EventDefinition
155
     */
156
    public function getEventDefinition()
157
    {
158
        return $this->getDefinition()->getEventFromFullIdentifier($this->getEvent());
159
    }
160
161
    /**
162
     * Returns the event configuration stored as a FlexForm string.
163
     *
164
     * @return array
165
     */
166
    public function getEventConfiguration()
167
    {
168
        if (null === $this->eventConfiguration) {
169
            /** @var FlexFormService $flexFormService */
170
            $flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
171
172
            $this->eventConfiguration = $flexFormService->convertFlexFormContentToArray($this->eventConfigurationFlex);
173
        }
174
175
        return $this->eventConfiguration;
176
    }
177
178
    /**
179
     * @return BackendUser
180
     */
181
    public function getBackendUser()
182
    {
183
        return $this->backendUser;
184
    }
185
186
    /**
187
     * @return bool
188
     */
189
    public static function isCreatable()
190
    {
191
        return Container::getBackendUser()
192
            && Container::getBackendUser()->check('tables_modify', self::getTableName());
193
    }
194
195
    /**
196
     * @param string $selectedEvent
197
     * @return string
198
     */
199
    public static function getCreationUri($selectedEvent = null)
200
    {
201
        $tableName = static::getTableName();
202
203
        $href = BackendUtility::getModuleUrl(
204
            'record_edit',
205
            [
206
                "edit[$tableName][0]" => 'new',
207
                'returnUrl' => GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'),
208
            ]
209
        );
210
211
        if ($selectedEvent) {
212
            $href .= "&selectedEvent=$selectedEvent";
213
        }
214
215
        return $href;
216
    }
217
218
    /**
219
     * The selected channel is stored in the `$channel` property.
220
     *
221
     * @inheritdoc
222
     */
223
    public function shouldDispatch(ChannelDefinition $definition)
224
    {
225
        return $definition->getClassName() === $this->getChannel();
226
    }
227
228
    /**
229
     * Returns the name of the table for this notification. It is fetched in the
230
     * global TypoScript configuration.
231
     *
232
     * @return string
233
     */
234
    public static function getTableName()
235
    {
236
        /** @var ConfigurationManagerInterface $configurationManager */
237
        $configurationManager = Container::get(ConfigurationManagerInterface::class);
238
        $configuration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
239
240
        $className = self::getDefinition()
241
            ->getNotification(static::getDefinitionIdentifier())
242
            ->getClassName();
243
244
        return ArrayUtility::getValueByPath($configuration, "persistence/classes/$className/mapping/tableName");
245
    }
246
247
    /**
248
     * @return string
249
     */
250
    abstract public static function getDefinitionIdentifier();
251
252
    /**
253
     * @return Definition
254
     */
255
    protected static function getDefinition()
256
    {
257
        return DefinitionService::get()->getDefinition();
258
    }
259
}
260