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 \TYPO3\CMS\Core\Utility\GeneralUtility; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* NotificationService |
22
|
|
|
* |
23
|
|
|
* @author Torben Hansen <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class NotificationService |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The object manager |
30
|
|
|
* |
31
|
|
|
* @var \TYPO3\CMS\Extbase\Object\ObjectManager |
32
|
|
|
* @inject |
33
|
|
|
*/ |
34
|
|
|
protected $objectManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Registration repository |
38
|
|
|
* |
39
|
|
|
* @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
40
|
|
|
* @inject |
41
|
|
|
*/ |
42
|
|
|
protected $registrationRepository = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Email Service |
46
|
|
|
* |
47
|
|
|
* @var \DERHANSEN\SfEventMgt\Service\EmailService |
48
|
|
|
* @inject |
49
|
|
|
*/ |
50
|
|
|
protected $emailService; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Hash Service |
54
|
|
|
* |
55
|
|
|
* @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
56
|
|
|
* @inject |
57
|
|
|
*/ |
58
|
|
|
protected $hashService; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* FluidStandaloneService |
62
|
|
|
* |
63
|
|
|
* @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
64
|
|
|
* @inject |
65
|
|
|
*/ |
66
|
|
|
protected $fluidStandaloneService; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* CustomNotificationLogRepository |
70
|
|
|
* |
71
|
|
|
* @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
72
|
|
|
* @inject |
73
|
|
|
*/ |
74
|
|
|
protected $customNotificationLogRepository = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sends a custom notification defined by the given customNotification key |
78
|
|
|
* to all confirmed users of the event |
79
|
|
|
* |
80
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
81
|
|
|
* @param string $customNotification CustomNotification |
82
|
|
|
* @param array $settings Settings |
83
|
|
|
* |
84
|
|
|
* @return int Number of notifications sent |
85
|
|
|
*/ |
86
|
|
|
public function sendCustomNotification($event, $customNotification, $settings) |
87
|
|
|
{ |
88
|
|
|
if ($this->cantSendCustomNotification($event, $settings, $customNotification)) { |
89
|
|
|
return 0; |
90
|
|
|
} |
91
|
|
|
$count = 0; |
92
|
|
|
|
93
|
|
|
$constraints = $settings['notification']['customNotifications'][$customNotification]['constraints']; |
94
|
|
|
$registrations = $this->registrationRepository->findNotificationRegistrations($event, $constraints); |
95
|
|
|
foreach ($registrations as $registration) { |
96
|
|
|
/** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */ |
97
|
|
|
if ($registration->isConfirmed() && !$registration->isIgnoreNotifications()) { |
98
|
|
|
$result = $this->sendUserMessage( |
99
|
|
|
$event, |
100
|
|
|
$registration, |
101
|
|
|
$settings, |
102
|
|
|
MessageType::CUSTOM_NOTIFICATION, |
103
|
|
|
$customNotification |
104
|
|
|
); |
105
|
|
|
if ($result) { |
106
|
|
|
$count += 1; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $count; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns true if conditions are not met to send a custom notification |
115
|
|
|
* |
116
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
117
|
|
|
* @param array $settings |
118
|
|
|
* @param string $customNotification |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
protected function cantSendCustomNotification($event, $settings, $customNotification) |
123
|
|
|
{ |
124
|
|
|
return is_null($event) || $customNotification == '' || $settings == '' || !is_array($settings); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Adds a logentry to the custom notification log |
129
|
|
|
* |
130
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
131
|
|
|
* @param string $details Details |
132
|
|
|
* @param int $emailsSent E-Mails sent |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function createCustomNotificationLogentry($event, $details, $emailsSent) |
137
|
|
|
{ |
138
|
|
|
$notificationlogEntry = new \DERHANSEN\SfEventMgt\Domain\Model\CustomNotificationLog(); |
139
|
|
|
$notificationlogEntry->setEvent($event); |
140
|
|
|
$notificationlogEntry->setDetails($details); |
141
|
|
|
$notificationlogEntry->setEmailsSent($emailsSent); |
142
|
|
|
$notificationlogEntry->setCruserId($GLOBALS['BE_USER']->user['uid']); |
143
|
|
|
$this->customNotificationLogRepository->add($notificationlogEntry); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sends a message to the user based on the given type |
148
|
|
|
* |
149
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
150
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
151
|
|
|
* @param array $settings Settings |
152
|
|
|
* @param int $type Type |
153
|
|
|
* @param string $customNotification CustomNotification |
154
|
|
|
* |
155
|
|
|
* @return bool TRUE if successful, else FALSE |
156
|
|
|
*/ |
157
|
|
|
public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
158
|
|
|
{ |
159
|
|
|
list($template, $subject) = $this->getUserMessageTemplateSubject($settings, $type, $customNotification); |
160
|
|
|
|
161
|
|
|
if (is_null($event) || is_null($registration) || !is_array($settings) || (substr($template, -5) != '.html')) { |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (!$registration->isIgnoreNotifications()) { |
166
|
|
|
$body = $this->getNotificationBody($event, $registration, $template, $settings); |
167
|
|
|
return $this->emailService->sendEmailMessage( |
168
|
|
|
$settings['notification']['senderEmail'], |
169
|
|
|
$registration->getEmail(), |
170
|
|
|
$subject, |
171
|
|
|
$body, |
172
|
|
|
$settings['notification']['senderName'] |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns an array with template and subject for the user message |
180
|
|
|
* |
181
|
|
|
* @param array $settings |
182
|
|
|
* @param int $type Type |
183
|
|
|
* @param string $customNotification |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
187
|
|
|
{ |
188
|
|
|
$template = 'Notification/User/RegistrationNew.html'; |
189
|
|
|
$subject = $settings['notification']['registrationNew']['userSubject']; |
190
|
|
|
switch ($type) { |
191
|
|
|
case MessageType::REGISTRATION_CONFIRMED: |
192
|
|
|
$template = 'Notification/User/RegistrationConfirmed.html'; |
193
|
|
|
$subject = $settings['notification']['registrationConfirmed']['userSubject']; |
194
|
|
|
break; |
195
|
|
|
case MessageType::REGISTRATION_CANCELLED: |
196
|
|
|
$template = 'Notification/User/RegistrationCancelled.html'; |
197
|
|
|
$subject = $settings['notification']['registrationCancelled']['userSubject']; |
198
|
|
|
break; |
199
|
|
|
case MessageType::CUSTOM_NOTIFICATION: |
200
|
|
|
$template = 'Notification/User/Custom/' . $settings['notification']['customNotifications'][$customNotification]['template']; |
201
|
|
|
$subject = $settings['notification']['customNotifications'][$customNotification]['subject']; |
202
|
|
|
break; |
203
|
|
|
case MessageType::REGISTRATION_NEW: |
204
|
|
|
default: |
205
|
|
|
} |
206
|
|
|
return [ |
207
|
|
|
$template, |
208
|
|
|
$subject |
209
|
|
|
]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Sends a message to the admin based on the given type |
214
|
|
|
* |
215
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
216
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
217
|
|
|
* @param array $settings Settings |
218
|
|
|
* @param int $type Type |
219
|
|
|
* |
220
|
|
|
* @return bool TRUE if successful, else FALSE |
221
|
|
|
*/ |
222
|
|
|
public function sendAdminMessage($event, $registration, $settings, $type) |
223
|
|
|
{ |
224
|
|
|
list($template, $subject) = $this->getAdminMessageTemplateSubject($settings, $type); |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
if (is_null($event) || is_null($registration || !is_array($settings)) || |
228
|
|
|
($event->getNotifyAdmin() === false && $event->getNotifyOrganisator() === false) |
229
|
|
|
) { |
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$allEmailsSent = true; |
234
|
|
|
$body = $this->getNotificationBody($event, $registration, $template, $settings); |
235
|
|
|
if ($event->getNotifyAdmin()) { |
236
|
|
|
$adminEmailArr = GeneralUtility::trimExplode(',', $settings['notification']['adminEmail'], true); |
237
|
|
|
foreach ($adminEmailArr as $adminEmail) { |
238
|
|
|
$allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage( |
239
|
|
|
$settings['notification']['senderEmail'], |
240
|
|
|
$adminEmail, |
241
|
|
|
$subject, |
242
|
|
|
$body, |
243
|
|
|
$settings['notification']['senderName'] |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
if ($event->getNotifyOrganisator() && $event->getOrganisator()) { |
248
|
|
|
$allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage( |
249
|
|
|
$settings['notification']['senderEmail'], |
250
|
|
|
$event->getOrganisator()->getEmail(), |
251
|
|
|
$subject, |
252
|
|
|
$body, |
253
|
|
|
$settings['notification']['senderName'] |
254
|
|
|
); |
255
|
|
|
} |
256
|
|
|
return $allEmailsSent; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns an array with template and subject for the admin message |
261
|
|
|
* |
262
|
|
|
* @param array $settings |
263
|
|
|
* @param int $type Type |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
|
protected function getAdminMessageTemplateSubject($settings, $type) |
267
|
|
|
{ |
268
|
|
|
$template = 'Notification/Admin/RegistrationNew.html'; |
269
|
|
|
$subject = $settings['notification']['registrationNew']['adminSubject']; |
270
|
|
|
switch ($type) { |
271
|
|
|
case MessageType::REGISTRATION_CONFIRMED: |
272
|
|
|
$template = 'Notification/Admin/RegistrationConfirmed.html'; |
273
|
|
|
$subject = $settings['notification']['registrationConfirmed']['adminSubject']; |
274
|
|
|
break; |
275
|
|
|
case MessageType::REGISTRATION_CANCELLED: |
276
|
|
|
$template = 'Notification/Admin/RegistrationCancelled.html'; |
277
|
|
|
$subject = $settings['notification']['registrationCancelled']['adminSubject']; |
278
|
|
|
break; |
279
|
|
|
case MessageType::REGISTRATION_NEW: |
280
|
|
|
default: |
281
|
|
|
} |
282
|
|
|
return [ |
283
|
|
|
$template, |
284
|
|
|
$subject |
285
|
|
|
]; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Returns the rendered HTML for the given template |
290
|
|
|
* |
291
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
292
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
293
|
|
|
* @param string $template Template |
294
|
|
|
* @param array $settings Settings |
295
|
|
|
* |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
|
|
protected function getNotificationBody($event, $registration, $template, $settings) |
299
|
|
|
{ |
300
|
|
|
/** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView */ |
301
|
|
|
$emailView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView'); |
302
|
|
|
$emailView->setFormat('html'); |
303
|
|
|
$layoutRootPaths = $this->fluidStandaloneService->getTemplateFolders('layout'); |
304
|
|
|
$partialRootPaths = $this->fluidStandaloneService->getTemplateFolders('partial'); |
305
|
|
|
|
306
|
|
|
if (TYPO3_MODE === 'BE' && $registration->getLanguage() !== '') { |
307
|
|
|
// Temporary set Language of current BE user to given language |
308
|
|
|
$GLOBALS['BE_USER']->uc['lang'] = $registration->getLanguage(); |
309
|
|
|
$emailView->getRequest()->setControllerExtensionName('SfEventMgt'); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$emailView->setLayoutRootPaths($layoutRootPaths); |
313
|
|
|
$emailView->setPartialRootPaths($partialRootPaths); |
314
|
|
|
$emailView->setTemplatePathAndFilename($this->fluidStandaloneService->getTemplatePath($template)); |
315
|
|
|
$emailView->assignMultiple([ |
316
|
|
|
'event' => $event, |
317
|
|
|
'registration' => $registration, |
318
|
|
|
'settings' => $settings, |
319
|
|
|
'hmac' => $this->hashService->generateHmac('reg-' . $registration->getUid()), |
320
|
|
|
'reghmac' => $this->hashService->appendHmac((string)$registration->getUid()) |
321
|
|
|
]); |
322
|
|
|
$emailBody = $emailView->render(); |
323
|
|
|
return $emailBody; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|