Passed
Pull Request — dev (#35)
by Romain
03:52
created

EntityEmailTcaService::getMailBodyFlexFormList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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\Domain\Notification\Email\Application\EntityEmail\TCA;
18
19
use CuyZ\Notiz\Definition\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\EntityEmailSettings;
21
use CuyZ\Notiz\Domain\Notification\Email\Application\EntityEmail\Settings\GlobalRecipients\Recipient;
22
use CuyZ\Notiz\Domain\Property\Email;
23
use CuyZ\Notiz\Notification\Service\NotificationTcaService;
24
use CuyZ\Notiz\Notification\Settings\NotificationSettings;
25
use CuyZ\Notiz\Service\Container;
26
use CuyZ\Notiz\Service\LocalizationService;
27
use CuyZ\Notiz\View\Slot\Service\SlotFlexFormService;
28
use CuyZ\Notiz\View\Slot\Service\SlotViewService;
29
30
class EntityEmailTcaService extends NotificationTcaService
31
{
32
    /**
33
     * @var SlotViewService
34
     */
35
    protected $slotViewService;
36
37
    /**
38
     * @var SlotFlexFormService
39
     */
40
    protected $slotFlexFormService;
41
42
    /**
43
     * Manual dependency injection.
44
     */
45
    public function __construct()
46
    {
47
        parent::__construct();
48
49
        $this->slotViewService = Container::get(SlotViewService::class);
50
        $this->slotFlexFormService = Container::get(SlotFlexFormService::class);
51
    }
52
53
    /**
54
     * Loads all recipients provided by the selected event and stores them as an
55
     * array to be used in the TCA.
56
     *
57
     * @param array $parameters
58
     */
59
    public function getRecipientsList(array &$parameters)
60
    {
61
        if ($this->definitionHasErrors()) {
62
            return;
63
        }
64
65
        $eventDefinition = $this->getSelectedEvent($parameters['row']);
66
67
        $eventRecipients = array_map(
68
            function (Email $recipient) {
69
                return [
70
                    'label' => $recipient->getLabel(),
71
                    'value' => $recipient->getName(),
72
                ];
73
            },
74
            $eventDefinition->getPropertiesDefinition(Email::class)
75
        );
76
77
        $globalRecipients = array_map(
78
            function (Recipient $recipient) {
79
                return [
80
                    'label' => $recipient->getName(),
81
                    'value' => $recipient->getIdentifier(),
82
                ];
83
            },
84
            $this->getNotificationSettings()->getGlobalRecipients()->getRecipients()
85
        );
86
87
        if (!empty($eventRecipients)) {
88
            $this->appendOptionGroup($eventRecipients, LocalizationService::localize('Notification/Email/Entity:field.recipients.event_recipients'));
89
        }
90
91
        if (!empty($globalRecipients)) {
92
            $this->appendOptionGroup($globalRecipients, LocalizationService::localize('Notification/Email/Entity:field.recipients.global_recipients'));
93
        }
94
95
        $recipients = array_merge_recursive($eventRecipients, $globalRecipients);
96
97
        foreach ($recipients as $recipient) {
98
            $parameters['items'][] = [
99
                $recipient['label'],
100
                $recipient['value'],
101
            ];
102
        }
103
    }
104
105
    /**
106
     * List all available layouts and stores them as an array
107
     * to be used in the TCA.
108
     *
109
     * @param array $parameters
110
     */
111
    public function getLayoutList(array &$parameters)
112
    {
113
        if ($this->definitionHasErrors()) {
114
            return;
115
        }
116
117
        $layouts = $this->getNotificationSettings()->getView()->getLayouts();
118
119
        foreach ($layouts as $key => $layout) {
120
            $label = $layout->hasLabel()
121
                ? $layout->getLabel()
122
                : LocalizationService::localize('Notification/Email/Entity:field.layout.undefined_label', [$key]);
123
124
            $parameters['items'][] = [
125
                $label,
126
                $layout->getIdentifier(),
127
            ];
128
        }
129
    }
130
131
    /**
132
     * Builds a condition allowing the mail body to be shown only if the
133
     * selected events does provide slots for the Fluid template.
134
     *
135
     * By default, an event with no custom Fluid template does have a single
136
     * slot.
137
     *
138
     * @return array
139
     */
140
    public function getMailBodyDisplayCond()
141
    {
142
        $eventsWithSlots = [];
143
        $events = $this->slotViewService->getEventsWithoutSlots($this->getNotificationSettings()->getView());
144
145
        foreach ($events as $event => $view) {
146
            /** @var EventDefinition $event */
147
            $eventsWithSlots[] = $event->getFullIdentifier();
148
        }
149
150
        return [
151
            'AND' => [
152
                'FIELD:event:!IN:' . implode(',', $eventsWithSlots),
153
                'FIELD:event:!=:' // Hide the body when no event is selected.
154
            ]
155
        ];
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    public function getMailBodyFlexFormList()
162
    {
163
        $viewSettings = $this->getNotificationSettings()->getView();
164
165
        return $this->slotFlexFormService->getNotificationFlexFormList($viewSettings);
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getDefaultSender()
172
    {
173
        if ($this->definitionHasErrors()) {
174
            return '';
175
        }
176
177
        return $this->getNotificationSettings()->getDefaultSender();
178
    }
179
180
    /**
181
     * This methods returns true if the current selected event has one provided
182
     * email address. This is used as a displayCond in the TCA.
183
     *
184
     * @param array $parameters
185
     * @return bool
186
     */
187
    public function shouldShowProvidedRecipientsSelect(array $parameters)
188
    {
189
        if ($this->definitionHasErrors()) {
190
            return false;
191
        }
192
193
        $eventDefinition = $this->getSelectedEvent($parameters['record']);
194
195
        /** @var Email[] $recipients */
196
        $recipients = $eventDefinition->getPropertiesDefinition(Email::class);
197
198
        $globalRecipients = $this->getNotificationSettings()
199
            ->getGlobalRecipients()
200
            ->getRecipients();
201
202
        return count($recipients) > 0 || count($globalRecipients) > 0;
203
    }
204
205
    /**
206
     * @return EntityEmailSettings|NotificationSettings
207
     */
208
    protected function getNotificationSettings()
209
    {
210
        return $this->getNotificationDefinition()->getSettings();
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    protected function getNotificationIdentifier()
217
    {
218
        return 'entityEmail';
219
    }
220
}
221