Completed
Push — development ( 1a581c...178cf5 )
by Torben
08:30
created

createCustomNotificationLogentry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 9
cts 9
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use DERHANSEN\SfEventMgt\Utility\MessageType;
18
use DERHANSEN\SfEventMgt\Utility\MessageRecipient;
19
use \TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * NotificationService
23
 *
24
 * @author Torben Hansen <[email protected]>
25
 */
26
class NotificationService
27
{
28
    /**
29
     * The object manager
30
     *
31
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
32
     */
33
    protected $objectManager;
34
35
    /**
36
     * Registration repository
37
     *
38
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
39
     */
40
    protected $registrationRepository = null;
41
42
    /**
43
     * Email Service
44
     *
45
     * @var \DERHANSEN\SfEventMgt\Service\EmailService
46
     */
47
    protected $emailService;
48
49
    /**
50
     * Hash Service
51
     *
52
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
53
     */
54
    protected $hashService;
55
56
    /**
57
     * FluidStandaloneService
58
     *
59
     * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService
60
     */
61
    protected $fluidStandaloneService;
62
63
    /**
64
     * CustomNotificationLogRepository
65
     *
66
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository
67
     */
68
    protected $customNotificationLogRepository = null;
69
70
    /**
71
     * AttachmentService
72
     *
73
     * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService
74
     */
75
    protected $attachmentService;
76
77
    /**
78
     * DI for $attachmentService
79
     *
80
     * @param Notification\AttachmentService $attachmentService
81
     */
82
    public function injectAttachmentService(
83
        \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService $attachmentService
84
    ) {
85
        $this->attachmentService = $attachmentService;
86
    }
87
88
    /**
89
     * DI for $customNotificationLogRepository
90
     *
91
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository
92
     */
93
    public function injectCustomNotificationLogRepository(
94
        \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository
95 4
    ) {
96
        $this->customNotificationLogRepository = $customNotificationLogRepository;
97 4
    }
98 1
99
    /**
100 3
     * DI for $emailService
101
     *
102 3
     * @param EmailService $emailService
103 3
     */
104 3
    public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService)
105
    {
106 3
        $this->emailService = $emailService;
107 1
    }
108 1
109 1
    /**
110 1
     * DI for $fluidStandaloneService
111 1
     *
112
     * @param FluidStandaloneService $fluidStandaloneService
113 1
     */
114 1
    public function injectFluidStandaloneService(
115 1
        \DERHANSEN\SfEventMgt\Service\FluidStandaloneService $fluidStandaloneService
116 1
    ) {
117 1
        $this->fluidStandaloneService = $fluidStandaloneService;
118 3
    }
119 3
120
    /**
121
     * DI for $hashService
122
     *
123
     * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
124
     */
125
    public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
126
    {
127
        $this->hashService = $hashService;
128
    }
129
130
    /**
131 4
     * DI for $objectManager
132
     *
133 4
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
134
     */
135
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
136
    {
137
        $this->objectManager = $objectManager;
138
    }
139
140
    /**
141
     * DI for $registrationRepository
142
     *
143
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
144
     */
145 1
    public function injectRegistrationRepository(
146
        \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
147 1
    ) {
148 1
        $this->registrationRepository = $registrationRepository;
149 1
    }
150 1
151 1
    /**
152 1
     * Sends a custom notification defined by the given customNotification key
153 1
     * to all confirmed users of the event
154
     *
155
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
156
     * @param string $customNotification CustomNotification
157
     * @param array $settings Settings
158
     *
159
     * @return int Number of notifications sent
160
     */
161
    public function sendCustomNotification($event, $customNotification, $settings)
162
    {
163
        if ($this->cantSendCustomNotification($event, $settings, $customNotification)) {
164
            return 0;
165
        }
166 17
        $count = 0;
167
168 17
        $constraints = $settings['notification']['customNotifications'][$customNotification]['constraints'];
169
        $registrations = $this->registrationRepository->findNotificationRegistrations($event, $constraints);
170 17
        foreach ($registrations as $registration) {
171 2
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */
172
            if ($registration->isConfirmed() && !$registration->isIgnoreNotifications()) {
173
                $result = $this->sendUserMessage(
174 15
                    $event,
175 10
                    $registration,
176 10
                    $settings,
177 10
                    MessageType::CUSTOM_NOTIFICATION,
178 10
                    $customNotification
179 10
                );
180
                if ($result) {
181 10
                    $count += 1;
182 10
                }
183 10
            }
184 10
        }
185 10
        return $count;
186 10
    }
187 10
188
    /**
189 10
     * Returns true if conditions are not met to send a custom notification
190
     *
191 5
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
192
     * @param array $settings
193
     * @param string $customNotification
194
     *
195
     * @return bool
196
     */
197
    protected function cantSendCustomNotification($event, $settings, $customNotification)
198
    {
199
        return is_null($event) || $customNotification == '' || $settings == '' || !is_array($settings);
200
    }
201
202 17
    /**
203
     * Adds a logentry to the custom notification log
204 17
     *
205 17
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
206
     * @param string $details Details
207 17
     * @param int $emailsSent E-Mails sent
208 3
     *
209 3
     * @return void
210 3
     */
211 14
    public function createCustomNotificationLogentry($event, $details, $emailsSent)
212 3
    {
213 3
        $notificationlogEntry = new \DERHANSEN\SfEventMgt\Domain\Model\CustomNotificationLog();
214 3
        $notificationlogEntry->setEvent($event);
215 11
        $notificationlogEntry->setDetails($details);
216 3
        $notificationlogEntry->setEmailsSent($emailsSent);
217 3
        $notificationlogEntry->setCruserId($GLOBALS['BE_USER']->user['uid']);
218 3
        $this->customNotificationLogRepository->add($notificationlogEntry);
219 8
    }
220
221
    /**
222
     * Sends a message to the user based on the given type
223 8
     *
224 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
225 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
226 1
     * @param array $settings Settings
227 7
     * @param int $type Type
228 7
     * @param string $customNotification CustomNotification
229 7
     *
230
     * @return bool TRUE if successful, else FALSE
231 17
     */
232
    public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '')
233 17
    {
234
        list($template, $subject) = $this->getUserMessageTemplateSubject($settings, $type, $customNotification);
235
236
        if (is_null($event) || is_null($registration) || !is_array($settings) || (substr($template, -5) != '.html')) {
237
            return false;
238
        }
239
240
        if (!$registration->isIgnoreNotifications()) {
241
            $body = $this->getNotificationBody($event, $registration, $template, $settings);
242
            $subject = $this->fluidStandaloneService->parseStringFluid(
243
                $subject,
244
                [
245
                    'event' => $event,
246 26
                    'registration' => $registration
247
                ]
248 26
            );
249
            $attachments = $this->attachmentService->getAttachments(
250
                $settings,
251 26
                $registration,
252 25
                $type,
253 26
                MessageRecipient::USER
254 6
            );
255
            return $this->emailService->sendEmailMessage(
256
                $settings['notification']['senderEmail'],
257 20
                $registration->getEmail(),
258 20
                $subject,
259 20
                $body,
260 20
                $settings['notification']['senderName'],
261 20
                $attachments
262 20
            );
263
        }
264 20
        return false;
265 20
    }
266 15
267 15
    /**
268 15
     * Returns an array with template and subject for the user message
269 15
     *
270 15
     * @param array $settings
271 15
     * @param int $type Type
272 15
     * @param string $customNotification
273 15
     * @return array
274
     */
275 15
    protected function getUserMessageTemplateSubject($settings, $type, $customNotification)
276 15
    {
277 15
        $template = 'Notification/User/RegistrationNew.html';
278 20
        $subject = $settings['notification']['registrationNew']['userSubject'];
279 5
        switch ($type) {
280 5
            case MessageType::REGISTRATION_WAITLIST_NEW:
281 5
                $template = 'Notification/User/RegistrationWaitlistNew.html';
282 5
                $subject = $settings['notification']['registrationWaitlistNew']['userSubject'];
283 5
                break;
284 5
            case MessageType::REGISTRATION_CONFIRMED:
285
                $template = 'Notification/User/RegistrationConfirmed.html';
286 5
                $subject = $settings['notification']['registrationConfirmed']['userSubject'];
287 5
                break;
288 20
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
289
                $template = 'Notification/User/RegistrationWaitlistConfirmed.html';
290
                $subject = $settings['notification']['registrationWaitlistConfirmed']['userSubject'];
291
                break;
292
            case MessageType::REGISTRATION_CANCELLED:
293
                $template = 'Notification/User/RegistrationCancelled.html';
294
                $subject = $settings['notification']['registrationCancelled']['userSubject'];
295
                break;
296
            case MessageType::CUSTOM_NOTIFICATION:
297
                $template = 'Notification/User/Custom/' . $settings['notification']['customNotifications'][$customNotification]['template'];
298 26
                $subject = $settings['notification']['customNotifications'][$customNotification]['subject'];
299
                break;
300 26
            case MessageType::REGISTRATION_NEW:
301 26
            default:
302
        }
303 26
        return [
304 5
            $template,
305 5
            $subject
306 5
        ];
307 21
    }
308 5
309 5
    /**
310 5
     * Sends a message to the admin based on the given type
311 16
     *
312 5
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
313 5
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
314 5
     * @param array $settings Settings
315 11
     * @param int $type Type
316
     *
317
     * @return bool TRUE if successful, else FALSE
318
     */
319 11
    public function sendAdminMessage($event, $registration, $settings, $type)
320 11
    {
321 11
        list($template, $subject) = $this->getAdminMessageTemplateSubject($settings, $type);
322
323 26
324
        if (is_null($event) || is_null($registration || !is_array($settings)) ||
325 26
            ($event->getNotifyAdmin() === false && $event->getNotifyOrganisator() === false)
326
        ) {
327
            return false;
328
        }
329
330
        $allEmailsSent = true;
331
        $body = $this->getNotificationBody($event, $registration, $template, $settings);
332
        $subject = $this->fluidStandaloneService->parseStringFluid(
333
            $subject,
334
            [
335
                'event' => $event,
336
                'registration' => $registration
337
            ]
338 30
        );
339
        $attachments = $this->attachmentService->getAttachments(
340
            $settings,
341 30
            $registration,
342 30
            $type,
343 30
            MessageRecipient::ADMIN
344 30
        );
345
        if ($event->getNotifyAdmin()) {
346 30
            $adminEmailArr = GeneralUtility::trimExplode(',', $settings['notification']['adminEmail'], true);
347
            foreach ($adminEmailArr as $adminEmail) {
348
                $allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage(
349
                    $settings['notification']['senderEmail'],
350
                    $adminEmail,
351
                    $subject,
352 30
                    $body,
353 30
                    $settings['notification']['senderName'],
354 30
                    $attachments
355 30
                );
356 30
            }
357 30
        }
358 30
        if ($event->getNotifyOrganisator() && $event->getOrganisator()) {
359 30
            $allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage(
360 30
                $settings['notification']['senderEmail'],
361 30
                $event->getOrganisator()->getEmail(),
362 30
                $subject,
363 30
                $body,
364
                $settings['notification']['senderName'],
365
                $attachments
366
            );
367
        }
368
        return $allEmailsSent;
369
    }
370
371
    /**
372
     * Returns an array with template and subject for the admin message
373
     *
374
     * @param array $settings
375
     * @param int $type Type
376
     * @return array
377
     */
378
    protected function getAdminMessageTemplateSubject($settings, $type)
379
    {
380
        $template = 'Notification/Admin/RegistrationNew.html';
381
        $subject = $settings['notification']['registrationNew']['adminSubject'];
382
        switch ($type) {
383
            case MessageType::REGISTRATION_WAITLIST_NEW:
384
                $template = 'Notification/Admin/RegistrationWaitlistNew.html';
385
                $subject = $settings['notification']['registrationWaitlistNew']['adminSubject'];
386
                break;
387
            case MessageType::REGISTRATION_CONFIRMED:
388
                $template = 'Notification/Admin/RegistrationConfirmed.html';
389
                $subject = $settings['notification']['registrationConfirmed']['adminSubject'];
390
                break;
391
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
392
                $template = 'Notification/Admin/RegistrationWaitlistConfirmed.html';
393
                $subject = $settings['notification']['registrationWaitlistConfirmed']['adminSubject'];
394
                break;
395
            case MessageType::REGISTRATION_CANCELLED:
396
                $template = 'Notification/Admin/RegistrationCancelled.html';
397
                $subject = $settings['notification']['registrationCancelled']['adminSubject'];
398
                break;
399
            case MessageType::REGISTRATION_NEW:
400
            default:
401
        }
402
        return [
403
            $template,
404
            $subject
405
        ];
406
    }
407
408
    /**
409
     * Returns the rendered HTML for the given template
410
     *
411
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
412
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
413
     * @param string $template Template
414
     * @param array $settings Settings
415
     *
416
     * @return string
417
     */
418
    protected function getNotificationBody($event, $registration, $template, $settings)
419
    {
420
        $templatePathAndFilename = $this->fluidStandaloneService->getTemplatePath($template);
421
        if (TYPO3_MODE === 'BE' && $registration->getLanguage() !== '') {
422
            // Temporary set Language of current BE user to given language
423
            $GLOBALS['BE_USER']->uc['lang'] = $registration->getLanguage();
424
        }
425
        $variables = [
426
            'event' => $event,
427
            'registration' => $registration,
428
            'settings' => $settings,
429
            'hmac' => $this->hashService->generateHmac('reg-' . $registration->getUid()),
430
            'reghmac' => $this->hashService->appendHmac((string)$registration->getUid())
431
        ];
432
        return $this->fluidStandaloneService->renderTemplate($templatePathAndFilename, $variables);
433
    }
434
}
435