|
1
|
|
|
<?php |
|
2
|
|
|
namespace Azine\EmailBundle\Services; |
|
3
|
|
|
use Azine\EmailBundle\Entity\Repositories\NotificationRepository; |
|
4
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Translation\Translator; |
|
6
|
|
|
use Azine\EmailBundle\DependencyInjection\AzineEmailExtension; |
|
7
|
|
|
use Azine\EmailBundle\Entity\Notification; |
|
8
|
|
|
use Azine\EmailBundle\Entity\RecipientInterface; |
|
9
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This Service compiles and renders the emails to be sent. |
|
13
|
|
|
* @author Dominik Businger |
|
14
|
|
|
*/ |
|
15
|
|
|
class AzineNotifierService implements NotifierServiceInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Override this function to fill in any non-recipient-specific parameters that are required to |
|
19
|
|
|
* render the notifications-template or one of the notification-item-templates that |
|
20
|
|
|
* are rendered into the notifications-template |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
3 |
|
protected function getVarsForNotificationsEmail() |
|
25
|
|
|
{ |
|
26
|
3 |
|
$params = array(); |
|
27
|
|
|
|
|
28
|
3 |
|
return $params; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Override this function to fill in any recipient-specific parameters that are required to |
|
33
|
|
|
* render the notifications-template or one of the notification-item-templates that |
|
34
|
|
|
* are rendered into the notifications-template |
|
35
|
|
|
* |
|
36
|
|
|
* @param RecipientInterface $recipient |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
2 |
|
protected function getRecipientVarsForNotificationsEmail(RecipientInterface $recipient) |
|
40
|
|
|
{ |
|
41
|
2 |
|
$recipientParams = array(); |
|
42
|
2 |
|
$recipientParams['recipient'] = $recipient; |
|
43
|
2 |
|
$recipientParams['mode'] = $recipient->getNotificationMode(); |
|
44
|
|
|
|
|
45
|
2 |
|
return $recipientParams; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the subject for the notifications-email to send. Override this function to implement your custom subject-lines. |
|
50
|
|
|
* @param array of array $contentItems |
|
51
|
|
|
* @param RecipientInterface $recipient |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function getRecipientSpecificNotificationsSubject($contentItems, RecipientInterface $recipient) |
|
55
|
|
|
{ |
|
56
|
2 |
|
$count = sizeof($contentItems); |
|
57
|
|
|
|
|
58
|
2 |
|
if($count == 1){ |
|
59
|
|
|
// get the content-item out of the boxed associative array => array(array('templateId' => contentItem)) |
|
60
|
2 |
|
$onlyItem = current(current($contentItems)); |
|
61
|
|
|
// get the title out of the notification in the contentItem |
|
62
|
2 |
|
return $onlyItem['notification']->getTitle(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->translatorService->transChoice("_az.email.notifications.subject.%count%", $count, array('%count%' => $count)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Override this function to fill in any non-recipient-specific parameters that are required |
|
70
|
|
|
* to render the newsletter-template and are not provided by the TemplateProvider. e.g. the total number of recipients of this newsletter |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
1 |
|
protected function getGeneralVarsForNewsletter() |
|
75
|
|
|
{ |
|
76
|
1 |
|
$vars = array(); |
|
77
|
1 |
|
$vars['recipientCount'] = sizeof($this->recipientProvider->getNewsletterRecipientIDs()); |
|
78
|
|
|
|
|
79
|
1 |
|
return $vars; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Override this function to fill in any non-recipient-specific content items that are the same |
|
84
|
|
|
* for all recipients of the newsletter. |
|
85
|
|
|
* |
|
86
|
|
|
* E.g. a list of featured events or news-articles. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array of templatesIds (without ending) as key and params to render the template as value. => array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)); |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getNonRecipientSpecificNewsletterContentItems() |
|
91
|
|
|
{ |
|
92
|
|
|
// @codeCoverageIgnoreStart |
|
93
|
|
|
$contentItems = array(); |
|
94
|
|
|
|
|
95
|
|
|
//$contentItems[] = array('AcmeBundle:foo:barSameForAllRecipientsTemplate' => $templateParams); |
|
96
|
|
|
return $contentItems; |
|
97
|
|
|
// @codeCoverageIgnoreEnd |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Override this function to add more parameters that are required to render the newsletter template. |
|
102
|
|
|
* @param RecipientInterface $recipient |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
2 |
|
public function getRecipientSpecificNewsletterParams(RecipientInterface $recipient) |
|
106
|
|
|
{ |
|
107
|
2 |
|
return array('recipient' => $recipient); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Override this function to fill in any recipient-specific content items that are different |
|
112
|
|
|
* depending on the recipient of the newsletter. |
|
113
|
|
|
* |
|
114
|
|
|
* E.g. a list of the recipients latest activites. |
|
115
|
|
|
* |
|
116
|
|
|
* @param RecipientInterface $recipient |
|
117
|
|
|
* @return array of arrays with templatesIds (without ending) as key and params to render the template as value. |
|
118
|
|
|
* => array( |
|
119
|
|
|
* array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification1, 'goToUrl' => 'http://example.com/1', ...)) |
|
120
|
|
|
* array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification2, 'goToUrl' => 'http://example.com/2', ...)) |
|
121
|
|
|
* ); |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getRecipientSpecificNewsletterContentItems(RecipientInterface $recipient) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
// @codeCoverageIgnoreStart |
|
126
|
|
|
$contentItems = array(); |
|
127
|
|
|
|
|
128
|
|
|
//$contentItems[] = array('AcmeBundle:foo:barDifferentForEachRecipientTemplate' => $recipientSpecificTemplateParams); |
|
129
|
|
|
//$contentItems[] = array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('notification' => array('title' => 'SampleMessage', 'created' => new \DateTime('1 hour ago'), 'content' => 'Sample Text. Lorem Ipsum.'))); |
|
130
|
|
|
return $contentItems; |
|
131
|
|
|
// @codeCoverageIgnoreEnd |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Override this function to use a custom subject line for each newsletter-recipient. |
|
136
|
|
|
* |
|
137
|
|
|
* @param $generalContentItems array of content items. => e.g. array of array('templateID' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)) |
|
138
|
|
|
* @param $recipientContentItems array of content items. => e.g. array of array('templateID' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)) |
|
139
|
|
|
* @param $params array the array with all general template-params, including the item with the key 'subject' containing the default-subject |
|
140
|
|
|
* @param $recipient RecipientInterface |
|
141
|
|
|
* @param $locale string The language-code for translation of the subject |
|
142
|
|
|
* @return the subject line |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function getRecipientSpecificNewsletterSubject(array $generalContentItems, array $recipientContentItems, array $params, RecipientInterface $recipient, $locale) |
|
145
|
|
|
{ |
|
146
|
1 |
|
return $params['subject']; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* By overriding this function you can rearrange the content items to you liking. By default no ordering is done, so the order is as follows: |
|
151
|
|
|
* |
|
152
|
|
|
* - all user-specific content items as returned by AzineNotifierService::getRecipientSpecificNewsletterContentItems |
|
153
|
|
|
* - all non-user-specific content items as returned by AzineNotifierService::getNonRecipientSpecificNewsletterContentItems |
|
154
|
|
|
* |
|
155
|
|
|
* @param array $contentItems |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
2 |
|
public function orderContentItems(array $contentItems){ |
|
159
|
2 |
|
return $contentItems; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Over ride this constructor if you need to inject more dependencies to get all the data together that you need for your newsletter/notifications. |
|
164
|
|
|
* |
|
165
|
|
|
* @param TemplateTwigSwiftMailerInterface $mailer |
|
166
|
|
|
* @param \Twig_Environment $twig |
|
167
|
|
|
* @param UrlGeneratorInterface $router |
|
168
|
|
|
* @param ManagerRegistry $managerRegistry |
|
169
|
|
|
* @param TemplateProviderInterface $templateProvider |
|
170
|
|
|
* @param RecipientProviderInterface $recipientProvider |
|
171
|
|
|
* @param Translator $translatorService |
|
172
|
|
|
* @param array $parameters |
|
173
|
|
|
*/ |
|
174
|
8 |
|
public function __construct(TemplateTwigSwiftMailerInterface $mailer, \Twig_Environment $twig, UrlGeneratorInterface $router, |
|
175
|
|
|
ManagerRegistry $managerRegistry, TemplateProviderInterface $templateProvider, RecipientProviderInterface $recipientProvider, |
|
176
|
|
|
Translator $translatorService, array $parameters) { |
|
177
|
|
|
|
|
178
|
8 |
|
$this->mailer = $mailer; |
|
179
|
8 |
|
$this->twig = $twig; |
|
180
|
8 |
|
$this->router = $router; |
|
181
|
8 |
|
$this->managerRegistry = $managerRegistry; |
|
182
|
8 |
|
$this->templateProvider = $templateProvider; |
|
183
|
8 |
|
$this->recipientProvider = $recipientProvider; |
|
184
|
8 |
|
$this->translatorService = $translatorService; |
|
185
|
8 |
|
$this->configParameter = $parameters; |
|
186
|
8 |
|
} |
|
187
|
|
|
|
|
188
|
|
|
////////////////////////////////////////////////////////////////////////// |
|
189
|
|
|
/* You probably don't need to change or override any of the stuff below */ |
|
190
|
|
|
////////////////////////////////////////////////////////////////////////// |
|
191
|
|
|
|
|
192
|
|
|
const CONTENT_ITEMS = 'contentItems'; |
|
193
|
|
|
/** |
|
194
|
|
|
* @var TemplateTwigSwiftMailerInterface |
|
195
|
|
|
*/ |
|
196
|
|
|
protected $mailer; |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @var \Twig_Environment |
|
200
|
|
|
*/ |
|
201
|
|
|
protected $twig; |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @var UrlGeneratorInterface |
|
205
|
|
|
*/ |
|
206
|
|
|
protected $router; |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @var TemplateProviderInterface |
|
210
|
|
|
*/ |
|
211
|
|
|
protected $templateProvider; |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @var RecipientProviderInterface |
|
215
|
|
|
*/ |
|
216
|
|
|
protected $recipientProvider; |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @var ManagerRegistry |
|
220
|
|
|
*/ |
|
221
|
|
|
protected $managerRegistry; |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Array of configuration-parameters from the config.yml |
|
225
|
|
|
* @var array |
|
226
|
|
|
*/ |
|
227
|
|
|
protected $configParameter; |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* The translator |
|
231
|
|
|
* @var Translator |
|
232
|
|
|
*/ |
|
233
|
|
|
protected $translatorService; |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Get the number of seconds in a "one-hour-interval" |
|
237
|
|
|
* @return integer of seconds to consider as an hour. |
|
238
|
|
|
*/ |
|
239
|
4 |
|
protected function getHourInterval() |
|
240
|
|
|
{ |
|
241
|
|
|
// about an hour ago (57min) |
|
242
|
|
|
// this is because if the last run started 60min. ago, then the notifications |
|
243
|
|
|
// for any recipient have been send after that and would be skipped until the next run. |
|
244
|
|
|
// if your cron-job runs every minute, this is not needed. |
|
245
|
4 |
|
return 60*60 - 3*60; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Get the number of seconds in a "one-day-interval" |
|
250
|
|
|
* @return integer of seconds to consider as a day. |
|
251
|
|
|
*/ |
|
252
|
3 |
|
protected function getDayInterval() |
|
253
|
|
|
{ |
|
254
|
|
|
// about a day ago (23h57min) |
|
255
|
|
|
// this is because if the last run started 24h. ago, then the notifications |
|
256
|
|
|
// for any recipient have been send after that and would be skipped until the next run. |
|
257
|
|
|
// if your cron-job runs every minute, this is not needed. |
|
258
|
3 |
|
return 60 * 60 * 24 - 3 * 60; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* (non-PHPdoc) |
|
263
|
|
|
* @see Azine\EmailBundle\Services.NotifierServiceInterface::sendNotifications() |
|
264
|
|
|
*/ |
|
265
|
3 |
|
public function sendNotifications(array &$failedAddresses) |
|
266
|
|
|
{ |
|
267
|
|
|
// get all recipientIds with pending notifications in the database, that are due to be sent |
|
268
|
3 |
|
$recipientIds = $this->getNotificationRecipientIds(); |
|
269
|
|
|
|
|
270
|
|
|
// get vars that are the same for all recipients of this notification-mail-batch |
|
271
|
3 |
|
$params = $this->getVarsForNotificationsEmail(); |
|
272
|
|
|
|
|
273
|
3 |
|
$notificationsTemplate = $this->configParameter[AzineEmailExtension::TEMPLATES."_".AzineEmailExtension::NOTIFICATIONS_TEMPLATE]; |
|
274
|
|
|
|
|
275
|
3 |
|
$sentCount = 0; |
|
276
|
3 |
View Code Duplication |
foreach ($recipientIds as $recipientId) { |
|
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
// send the mail for this recipient |
|
279
|
3 |
|
$failedAddress = $this->sendNotificationsFor($recipientId, $notificationsTemplate, $params); |
|
280
|
3 |
|
if ($failedAddress !== null && strlen($failedAddress) > 0) { |
|
281
|
2 |
|
$failedAddresses[] = $failedAddress; |
|
282
|
2 |
|
} else { |
|
283
|
3 |
|
$sentCount++; |
|
284
|
|
|
} |
|
285
|
3 |
|
} |
|
286
|
|
|
|
|
287
|
3 |
|
return $sentCount; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Send the notifications-email for one recipient |
|
292
|
|
|
* @param integer $recipientId |
|
293
|
|
|
* @param string $wrapperTemplateName |
|
294
|
|
|
* @param array $params array of parameters for this recipient |
|
295
|
|
|
* @return null|string or the failed email addressess |
|
296
|
|
|
*/ |
|
297
|
3 |
|
public function sendNotificationsFor($recipientId, $wrapperTemplateName, $params) |
|
298
|
|
|
{ |
|
299
|
|
|
// get the recipient |
|
300
|
3 |
|
$recipient = $this->recipientProvider->getRecipient($recipientId); |
|
301
|
|
|
|
|
302
|
|
|
// get all Notification-Items for the recipient from the database |
|
303
|
3 |
|
$notifications = $this->getNotificationsFor($recipient); |
|
304
|
3 |
|
if (sizeof($notifications) == 0 ) { |
|
305
|
1 |
|
return null; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
// get the recipient specific parameters for the twig-templates |
|
309
|
2 |
|
$recipientParams = $this->getRecipientVarsForNotificationsEmail($recipient); |
|
310
|
2 |
|
$params = array_merge($recipientParams, $params); |
|
311
|
|
|
|
|
312
|
|
|
// prepare the arrays with template and template-variables for each notification |
|
313
|
2 |
|
$contentItems = array(); |
|
314
|
2 |
|
foreach ($notifications as $notification) { |
|
315
|
|
|
|
|
316
|
|
|
// decode the $params from the json in the notification-entity |
|
317
|
2 |
|
$itemVars = $notification->getVariables(); |
|
318
|
2 |
|
$itemVars = array_merge($params, $itemVars); |
|
319
|
2 |
|
$itemVars['notification'] = $notification; |
|
320
|
2 |
|
$itemVars['recipient'] = $recipient; |
|
321
|
|
|
|
|
322
|
2 |
|
$itemTemplateName = $notification->getTemplate(); |
|
323
|
|
|
|
|
324
|
2 |
|
$contentItems[] = array($itemTemplateName => $itemVars); |
|
325
|
2 |
|
} |
|
326
|
|
|
|
|
327
|
|
|
// add the notifications to the params array so they will be rendered later |
|
328
|
2 |
|
$params[self::CONTENT_ITEMS] = $contentItems; |
|
329
|
2 |
|
$params['recipient'] = $recipient; |
|
330
|
2 |
|
$params['_locale'] = $recipient->getPreferredLocale(); |
|
331
|
|
|
|
|
332
|
2 |
|
$subject = $this->getRecipientSpecificNotificationsSubject($contentItems, $recipient); |
|
333
|
|
|
|
|
334
|
|
|
// send the email with the right wrapper-template |
|
335
|
2 |
|
$sent = $this->mailer->sendSingleEmail($recipient->getEmail(), $recipient->getDisplayName(), $subject, $params, $wrapperTemplateName . ".txt.twig", $recipient->getPreferredLocale()); |
|
336
|
|
|
|
|
337
|
2 |
|
if ($sent) { |
|
338
|
|
|
// save the updated notifications |
|
339
|
2 |
|
$this->setNotificationsAsSent($notifications); |
|
340
|
2 |
|
return null; |
|
341
|
|
|
|
|
342
|
|
|
} else { |
|
343
|
2 |
|
return $recipient->getEmail(); |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* (non-PHPdoc) |
|
349
|
|
|
* @see Azine\EmailBundle\Services.NotifierServiceInterface::sendNewsletter() |
|
350
|
|
|
*/ |
|
351
|
2 |
|
public function sendNewsletter(array &$failedAddresses) |
|
352
|
|
|
{ |
|
353
|
|
|
// params array for all recipients |
|
354
|
2 |
|
$params = array(); |
|
355
|
|
|
|
|
356
|
|
|
// set a default subject |
|
357
|
2 |
|
$params['subject'] = $this->translatorService->trans("_az.email.newsletter.subject"); |
|
358
|
|
|
|
|
359
|
|
|
// get the the non-recipient-specific contentItems of the newsletter |
|
360
|
2 |
|
$params[self::CONTENT_ITEMS] = $this->getNonRecipientSpecificNewsletterContentItems(); |
|
361
|
|
|
|
|
362
|
|
|
// get recipientIds for the newsletter |
|
363
|
2 |
|
$recipientIds = $this->recipientProvider->getNewsletterRecipientIDs(); |
|
364
|
|
|
|
|
365
|
2 |
|
$newsletterTemplate = $this->configParameter[AzineEmailExtension::TEMPLATES."_".AzineEmailExtension::NEWSLETTER_TEMPLATE]; |
|
366
|
|
|
|
|
367
|
2 |
View Code Duplication |
foreach ($recipientIds as $recipientId) { |
|
|
|
|
|
|
368
|
2 |
|
$failedAddress = $this->sendNewsletterFor($recipientId, $params, $newsletterTemplate); |
|
369
|
|
|
|
|
370
|
2 |
|
if ($failedAddress !== null && strlen($failedAddress) > 0) { |
|
371
|
2 |
|
$failedAddresses[] = $failedAddress; |
|
372
|
2 |
|
} |
|
373
|
2 |
|
} |
|
374
|
|
|
|
|
375
|
2 |
|
return sizeof($recipientIds) - sizeof($failedAddresses); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* Send the newsletter for one recipient |
|
380
|
|
|
* @param integer $recipientId |
|
381
|
|
|
* @param array $params params and contentItems that are the same for all recipients |
|
382
|
|
|
* @param string $wrapperTemplate |
|
383
|
|
|
* @return string|null or the failed email addressess |
|
384
|
|
|
*/ |
|
385
|
2 |
|
public function sendNewsletterFor($recipientId, array $params, $wrapperTemplate) |
|
386
|
|
|
{ |
|
387
|
2 |
|
$recipient = $this->recipientProvider->getRecipient($recipientId); |
|
388
|
|
|
|
|
389
|
|
|
// create new array for each recipient. |
|
390
|
2 |
|
$recipientParams = array_merge($params, $this->getRecipientSpecificNewsletterParams($recipient)); |
|
391
|
|
|
|
|
392
|
|
|
// get the recipient-specific contentItems of the newsletter |
|
393
|
2 |
|
$recipientContentItems = $this->getRecipientSpecificNewsletterContentItems($recipient); |
|
394
|
|
|
|
|
395
|
|
|
// merge the recipient-specific and the general content items. recipient-specific first/at the top! |
|
396
|
2 |
|
$recipientParams[self::CONTENT_ITEMS] = $this->orderContentItems(array_merge($recipientContentItems, $params[self::CONTENT_ITEMS])); |
|
397
|
2 |
|
$recipientParams['_locale'] = $recipient->getPreferredLocale(); |
|
398
|
|
|
|
|
399
|
2 |
|
if (sizeof($recipientParams[self::CONTENT_ITEMS]) == 0) { |
|
400
|
1 |
|
return $recipient->getEmail(); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
1 |
|
$subject = $this->getRecipientSpecificNewsletterSubject($params[self::CONTENT_ITEMS], $recipientContentItems, $params, $recipient, $recipient->getPreferredLocale()); |
|
404
|
|
|
|
|
405
|
|
|
// render and send the email with the right wrapper-template |
|
406
|
1 |
|
$sent = $this->mailer->sendSingleEmail($recipient->getEmail(), $recipient->getDisplayName(), $subject, $recipientParams, $wrapperTemplate.".txt.twig", $recipient->getPreferredLocale()); |
|
407
|
|
|
|
|
408
|
1 |
|
if ($sent) { |
|
409
|
|
|
// save that this recipient has recieved the newsletter |
|
410
|
1 |
|
return null; |
|
411
|
|
|
|
|
412
|
|
|
} else { |
|
413
|
1 |
|
return $recipient->getEmail(); |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Get the Notifications that have not yet been sent yet. |
|
420
|
|
|
* Ordered by "template" and "title". |
|
421
|
|
|
* @param RecipientInterface $recipient |
|
422
|
|
|
* @return array of Notification |
|
423
|
|
|
*/ |
|
424
|
3 |
|
protected function getNotificationsFor(RecipientInterface $recipient) |
|
425
|
|
|
{ |
|
426
|
|
|
// get the notification mode |
|
427
|
3 |
|
$notificationMode = $recipient->getNotificationMode(); |
|
428
|
|
|
|
|
429
|
|
|
// get the date/time of the last notification |
|
430
|
3 |
|
$lastNotificationDate = $this->getNotificationRepository()->getLastNotificationDate($recipient->getId()); |
|
431
|
|
|
|
|
432
|
3 |
|
$sendNotifications = false; |
|
433
|
3 |
|
$timeDelta = time() - $lastNotificationDate->getTimestamp(); |
|
434
|
|
|
|
|
435
|
3 |
|
if ($notificationMode == RecipientInterface::NOTIFICATION_MODE_IMMEDIATELY) { |
|
436
|
3 |
|
$sendNotifications = true; |
|
437
|
|
|
|
|
438
|
3 |
|
} elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_HOURLY) { |
|
439
|
3 |
|
$sendNotifications = ($timeDelta > $this->getHourInterval()); |
|
440
|
|
|
|
|
441
|
3 |
|
} elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_DAYLY) { |
|
442
|
3 |
|
$sendNotifications = ($timeDelta > $this->getDayInterval()); |
|
443
|
|
|
|
|
444
|
3 |
|
} elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_NEVER) { |
|
445
|
|
|
$this->markAllNotificationsAsSentFarInThePast($recipient); |
|
446
|
|
|
|
|
447
|
|
|
return array(); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
// regularly sent notifications now |
|
451
|
3 |
|
if ($sendNotifications) { |
|
452
|
3 |
|
$notifications = $this->getNotificationRepository()->getNotificationsToSend($recipient->getId()); |
|
453
|
|
|
|
|
454
|
|
|
// if notifications exist, that should be sent immediately, then send those now disregarding the users mailing-preferences. |
|
455
|
3 |
|
} else { |
|
456
|
|
|
$notifications = $this->getNotificationRepository()->getNotificationsToSendImmediately($recipient->getId()); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
3 |
|
return $notifications; |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* Get all IDs for Recipients of pending notifications |
|
464
|
|
|
* @return array of IDs |
|
465
|
|
|
*/ |
|
466
|
3 |
|
protected function getNotificationRecipientIds() |
|
467
|
|
|
{ |
|
468
|
3 |
|
return $this->getNotificationRepository()->getNotificationRecipientIds(); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Update (set sent = now) and save the notifications |
|
473
|
|
|
* @param array $notifications |
|
474
|
|
|
*/ |
|
475
|
2 |
|
protected function setNotificationsAsSent(array $notifications) |
|
476
|
|
|
{ |
|
477
|
2 |
|
foreach ($notifications as $notification) { |
|
478
|
2 |
|
$notification->setSent(new \DateTime()); |
|
479
|
2 |
|
$this->managerRegistry->getManager()->persist($notification); |
|
480
|
2 |
|
} |
|
481
|
2 |
|
$this->managerRegistry->getManager()->flush(); |
|
482
|
2 |
|
} |
|
483
|
|
|
|
|
484
|
|
|
/** |
|
485
|
|
|
* Mark all Notifications as sent long ago, as the recipient never want's to get any notifications. |
|
486
|
|
|
* @param RecipientInterface $recipient |
|
487
|
|
|
*/ |
|
488
|
1 |
|
protected function markAllNotificationsAsSentFarInThePast(RecipientInterface $recipient) |
|
489
|
|
|
{ |
|
490
|
1 |
|
$this->getNotificationRepository()->markAllNotificationsAsSentFarInThePast($recipient->getId()); |
|
491
|
1 |
|
} |
|
492
|
|
|
|
|
493
|
|
|
/** |
|
494
|
|
|
* Get the interval in days between newsletter mailings |
|
495
|
|
|
*/ |
|
496
|
1 |
|
protected function getNewsletterInterval() |
|
497
|
|
|
{ |
|
498
|
1 |
|
return $this->configParameter[AzineEmailExtension::NEWSLETTER."_".AzineEmailExtension::NEWSLETTER_INTERVAL]; |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* Get the time of the day when the newsletter should be sent. |
|
503
|
|
|
* @return string Time of the day in the format HH:mm |
|
504
|
|
|
*/ |
|
505
|
1 |
|
protected function getNewsletterSendTime() |
|
506
|
|
|
{ |
|
507
|
1 |
|
return $this->configParameter[AzineEmailExtension::NEWSLETTER."_".AzineEmailExtension::NEWSLETTER_SEND_TIME]; |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
/** |
|
511
|
|
|
* Get the DateTime at which the last newsletter mailing probably has taken place, if a newsletter is sent today. |
|
512
|
|
|
* (Calculated: send-time-today - interval in days) |
|
513
|
|
|
* @return \DateTime |
|
514
|
|
|
*/ |
|
515
|
1 |
|
protected function getDateTimeOfLastNewsletter() |
|
516
|
|
|
{ |
|
517
|
1 |
|
return new \DateTime($this->getNewsletterInterval()." days ago ".$this->getNewsletterSendTime()); |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
/** |
|
521
|
|
|
* Get the DateTime at which the next newsletter mailing will take place, if a newsletter is sent today. |
|
522
|
|
|
* (Calculated: send-time-today + interval in days) |
|
523
|
|
|
*/ |
|
524
|
1 |
|
protected function getDateTimeOfNextNewsletter() |
|
525
|
|
|
{ |
|
526
|
1 |
|
return new \DateTime("+".$this->getNewsletterInterval()." days ".$this->getNewsletterSendTime()); |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
/** |
|
530
|
|
|
* Convenience-function to add and save a Notification-entity |
|
531
|
|
|
* |
|
532
|
|
|
* @param integer $recipientId the ID of the recipient of this notification => see RecipientProvider.getRecipient($id) |
|
533
|
|
|
* @param string $title the title of the notification. depending on the recipients settings, multiple notifications are sent in one email. |
|
534
|
|
|
* @param string $content the content of the notification |
|
535
|
|
|
* @param string $template the twig-template to render the notification with |
|
536
|
|
|
* @param array $templateVars the parameters used in the twig-template, 'notification' => Notification and 'recipient' => RecipientInterface will be added to this array when rendering the twig-template. |
|
537
|
|
|
* @param integer $importance important messages are at the top of the notification-emails, un-important at the bottom. |
|
538
|
|
|
* @param boolean $sendImmediately whether or not to ignore the recipients mailing-preference and send the notification a.s.a.p. |
|
539
|
|
|
* @return Notification |
|
540
|
|
|
*/ |
|
541
|
2 |
|
public function addNotification($recipientId, $title, $content, $template, $templateVars, $importance, $sendImmediately) |
|
542
|
|
|
{ |
|
543
|
2 |
|
$notification = new Notification(); |
|
544
|
2 |
|
$notification->setRecipientId($recipientId); |
|
545
|
2 |
|
$notification->setTitle($title); |
|
546
|
2 |
|
$notification->setContent($content); |
|
547
|
2 |
|
$notification->setTemplate($template); |
|
548
|
2 |
|
$notification->setImportance($importance); |
|
549
|
2 |
|
$notification->setSendImmediately($sendImmediately); |
|
550
|
2 |
|
$notification->setVariables($templateVars); |
|
551
|
2 |
|
$this->managerRegistry->getManager()->persist($notification); |
|
552
|
2 |
|
$this->managerRegistry->getManager()->flush($notification); |
|
553
|
|
|
|
|
554
|
2 |
|
return $notification; |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
/** |
|
558
|
|
|
* Convenience-function to add and save a Notification-entity for a message => see AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TYPE |
|
559
|
|
|
* |
|
560
|
|
|
* The following default are used: |
|
561
|
|
|
* $importance = NORMAL |
|
562
|
|
|
* $sendImmediately = fale |
|
563
|
|
|
* $template = template for type AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TYPE |
|
564
|
|
|
* $templateVars = only those from the template-provider |
|
565
|
|
|
* |
|
566
|
|
|
* @param integer $recipientId |
|
567
|
|
|
* @param string $title |
|
568
|
|
|
* @param string $content nl2br will be applied in the html-version of the email |
|
569
|
|
|
* @param string $goToUrl if this is supplied, a link "Go to message" will be added. |
|
570
|
|
|
*/ |
|
571
|
1 |
|
public function addNotificationMessage($recipientId, $title, $content, $goToUrl = null) |
|
572
|
|
|
{ |
|
573
|
1 |
|
$contentItemTemplate = $this->configParameter[AzineEmailExtension::TEMPLATES."_".AzineEmailExtension::CONTENT_ITEM_TEMPLATE]; |
|
574
|
1 |
|
$templateVars = array(); |
|
575
|
1 |
|
if ($goToUrl !== null && strlen($goToUrl) > 0) { |
|
576
|
1 |
|
$templateVars['goToUrl'] = $goToUrl; |
|
577
|
1 |
|
} |
|
578
|
1 |
|
$this->addNotification($recipientId, $title, $content, $contentItemTemplate, $this->templateProvider->addTemplateVariablesFor($contentItemTemplate, $templateVars), Notification::IMPORTANCE_NORMAL, false); |
|
579
|
1 |
|
} |
|
580
|
|
|
|
|
581
|
|
|
|
|
582
|
|
|
/** |
|
583
|
|
|
* @return NotificationRepository |
|
584
|
|
|
*/ |
|
585
|
4 |
|
protected function getNotificationRepository(){ |
|
586
|
4 |
|
return $this->managerRegistry->getRepository('AzineEmailBundle:Notification'); |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
} |
|
590
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.