text changes to registration mail content
[namibia] / module / Utility / src / Utility / Comms / Notification.php
1 <?php
2 namespace Utility\Comms;
3
4
5 /**
6  * Easy notification functionality for anything that needs it.
7  * @author andre.fourie
8  */
9 class Notification
10 {
11
12         /**
13          * Data for repeater functionality.
14          * @var array
15          */
16         static protected $_data = null;
17         /**
18          * Send template as newsletter to all subscribers on sendFromTemplate.
19          * @var boolean
20          */
21         static protected $_sendAsNewsletter = false;
22         /**
23          * Skip unsubscribe check on sendFromTemplate.
24          * @var boolean
25          */
26         static protected $_skipSubscriptionCheck = false;
27         /**
28          * Gearman client.
29          * @var \GearmanClient
30          */
31         static protected $_gearClient = false;
32
33
34         /**
35          * Set repeater data for next notification sent.
36          * @param array $data
37          */
38         static public function setRepeaterData(array $data)
39         {
40                 self::$_data = $data;
41         }
42
43         /**
44          * Set next sendFromTemplate to send to all users subscribed to newsletters.
45          */
46         static public function setSendAsNewsletter()
47         {
48                 self::$_sendAsNewsletter = true;
49         }
50
51         /**
52          * Skip unsubscribe check on next sendFromTemplate.
53          */
54         static public function skipSubscriptionCheck()
55         {
56                 self::$_skipSubscriptionCheck = false;
57         }
58
59         /**
60          * Send email and/or sms notification.
61          * @param integer $fromCompanyId
62          * @param integer $fromProfileId
63          * @param integer $toCompanyId
64          * @param integer $toProfileId
65          * @param string  $email
66          * @param string  $mobile
67          * @param string  $subject
68          * @param string  $template
69          * @param array   $params
70          * @param array   $attachments
71          * @param boolean $disableSms
72          * @return void
73          */
74         static public function sendFromTemplate(
75                 $fromCompanyId, $fromProfileId, $toCompanyId, $toProfileId,
76                 $email, $mobile, $subject, $templateName, array $params,
77                 array $attachments = array(), array $complexAttachments = array(),
78                 $disableSms = false, $offline = false
79         )
80         {
81                 \Utility\Debug::errorLog("send from template","sms:".$mobile." email:".$email);
82
83                 error_log('Template sending: ' . $templateName);
84
85                 #-> Unsubscribe check.
86                 $em = \Utility\Registry::getEntityManager();
87                 if (!self::$_sendAsNewsletter && !self::$_skipSubscriptionCheck)
88                 {
89                         $oProfile = $em->getRepository('\User\Entity\Profile');
90                         $profile  = $email
91                                 ? $oProfile->findOneBy(array('email' => $email))
92                                 : $oProfile->findOneBy(array('mobile' => $mobile));
93                         if (!is_null($profile) && !$profile->subscribeReminders)
94                         {
95                                 return;
96                         }
97                 }
98                 self::$_skipSubscriptionCheck = false;
99
100                 #-> Retrieve template.
101                 $oTemplate = $em->getRepository('\Utility\Entity\Template');
102                 $oRepeater = $em->getRepository('\Utility\Entity\RepeaterTemplate');
103                 $template  = $oTemplate->findOneBy(array('name' => $templateName));
104
105                 if (is_null($template))
106                 {
107                         error_log('Template not found: ' . $templateName);
108                         return;
109                 }
110
111                 $template = $template->toArray();
112
113                 #-> Compile the template for use.
114                 $subject = ($subject)
115                         ? $subject
116                         : $template['subject'];
117                 $tagList = explode(',', $template['tags']);
118                 $search  = array('{APP_HOST}');
119
120 //        \Utility\Debug::errorLog('sendFromTemplate IS_BROCHURE', IS_BROCHURE ? 'true' : 'false');
121
122                 $replace = !IS_BROCHURE ? array(\Utility\Registry::getConfigParam('Host')) : array(\Utility\Registry::getConfigParam('CashCarsHost'));
123
124                 #-> Catering for data-grid?
125                 if (!is_null($template['repeaterTemplate']))
126                 {
127                         $repeater = $template['repeaterTemplate'];
128                         if (!is_array(self::$_data))
129                         {
130                                 \Utility\Debug::errorLog(__CLASS__, 'Data required but not provided for template: ' . $templateName);
131                                 return;
132                         }
133                         $repeatContent = '';
134                         $groupField    = ($repeater['groupField'])
135                                 ? $repeater['groupField']
136                                 : false;
137                         $group         = '';
138                         $i             = 1;
139                         $dateFormat    = \Utility\Definitions\Locale::getDateFormat();
140                         foreach (self::$_data as $row)
141                         {
142                                 $repSearch = array();
143                                 foreach ($row as $field => $value)
144                                 {
145                                         if (is_array($value))
146                                         {
147                                                 foreach ($value as $subField => $subValue)
148                                                 {
149                                                         $repSearch["$field.$subField"]  = "[$field.$subField]";
150                                                         $repReplace["$field.$subField"] = $subValue;
151                                                 }
152                                         }
153                                         else
154                                         {
155                                                 $repSearch[$field]  = "[$field]";
156                                                 $repReplace[$field] = !is_object($value)
157                                                         ? $value
158                                                         : $value->format($dateFormat);
159                                         }
160                                 }
161                                 if ($groupField && $repReplace[$groupField] != $group)
162                                 {
163                                         $group = $repReplace[$groupField];
164                                         $repeatContent .= str_replace($repSearch, $repReplace, $repeater['groupRepeater']) . "\n";
165                                 }
166                                 $repeatContent .= ($i % 2)
167                                         ? str_replace($repSearch, $repReplace, $repeater['rowRepeaterOdd']) . "\n"
168                                         : str_replace($repSearch, $repReplace, $repeater['rowRepeaterEven']) . "\n";
169                                 $i++;
170                         }
171                         $tagList[]          = 'repeater';
172                         $params['repeater'] = $repeatContent;
173                 }
174
175                 #-> Build up the template(s)
176                 foreach ($tagList as $key)
177                 {
178                         $key = trim($key);
179                         if (empty($key))
180                         {
181                                 continue;
182                         }
183                         if (!isset($params[$key]))
184                         {
185                                 \Utility\Debug::errorLog(__CLASS__, "All template tags not supplied for sending ($templateName): " . $key);
186                                 \Utility\Debug::errorLog('tags', $tagList);
187                                 \Utility\Debug::errorLog('params', $params);
188                                 return;
189                         }
190                         $search[]  = "[$key]";
191                         $replace[] = $params[$key];
192                 }
193                 $emailTemplate = !empty($template['emailTemplate'])
194                         ? str_replace($search, $replace, $template['emailTemplate'])
195                         : false;
196                 $smsTemplate   = !empty($template['smsTemplate'])
197                         ? str_replace($search, $replace, $template['smsTemplate'])
198                         : false;
199
200
201                 if (!self::$_sendAsNewsletter)
202                 {
203                         if (!$offline)
204                         {
205                                 self::send($fromCompanyId, $fromProfileId, $toCompanyId, $toProfileId,
206                                            $email, $mobile, $subject, $emailTemplate, $smsTemplate,
207                                            $attachments, $complexAttachments, $disableSms);
208                         }
209                         else
210                         {
211
212                                 self::sendOffline($fromCompanyId, $fromProfileId, $toCompanyId, $toProfileId,
213                                                   $email, $mobile, $subject, $emailTemplate, $smsTemplate,
214                                                   $attachments, $complexAttachments, $disableSms);
215                         }
216                 }
217                 else
218                 {
219                         $profiles = $em->createQuery(
220                                 'SELECT profile.id AS profileId, IDENTITY(profile.company) AS companyId, profile.email as email '
221                                 . 'FROM \User\Entity\Profile profile '
222                                 . 'LEFT JOIN profile.company company '
223                                 . 'WHERE profile.jobState = :status '
224                                 . 'AND profile.subscribeNewsletter = 1'
225                         )
226                                 ->setParameter('status', 'Active')
227                                 ->getArrayResult();
228                         $ii       = 0;
229                         $oo       = 0;
230                         if (!$offline)
231                         {
232                                 foreach ($profiles as $profile)
233                                 {
234                                         self::send($fromCompanyId, $fromProfileId, $profile['companyId'], $profile['profileId'],
235                                                    $profile['email'], false, $subject, $emailTemplate, $smsTemplate,
236                                                    $attachments, $complexAttachments, $disableSms);
237                                         $em->clear();
238                                         $ii++;
239                                         $oo++;
240                                         if (100 == $ii)
241                                         {
242                                                 $ii = 0;
243                                                 error_log('newsletter mails: ' . $oo);
244                                                 $em->getConnection()->close();
245                                         }
246                                 }
247                         }
248                         else
249                         {
250                                 foreach ($profiles as $profile)
251                                 {
252                                         self::sendOffline($fromCompanyId, $fromProfileId, $profile['companyId'], $profile['profileId'],
253                                                           $profile['email'], false, $subject, $emailTemplate, $smsTemplate,
254                                                           $attachments, $complexAttachments, $disableSms);
255                                         $em->clear();
256                                         $ii++;
257                                         $oo++;
258                                         if (100 == $ii)
259                                         {
260                                                 $ii = 0;
261                                                 error_log('newsletter mails: ' . $oo);
262                                                 $em->getConnection()->close();
263                                         }
264                                 }
265                         }
266                 }
267                 self::$_data             = null;
268                 self::$_sendAsNewsletter = false;
269         }
270
271         /**
272          * Send newsletter to all who are subscribed, or just to admin for a test.
273          * @param integer $newsletterId
274          * @param boolean $test
275          * @return void
276          */
277         static public function sendNewsletter($newsletterId, $test = false, $testProfileId = null)
278         {
279                 #-> Retrieve data handler.
280                 $em = \Utility\Registry::getEntityManager();
281
282                 #-> Collect some data.
283                 $template           = $em->getRepository('\Utility\Entity\Template')
284                         ->findOneBy(array('name' => 'newsletter-basic'))
285                         ->toArray();
286                 $newsletter         = $em->getRepository('\Newsletter\Entity\Newsletter')
287                         ->find($newsletterId)
288                         ->toArray();
289                 $complexAttachments = array();
290                 $search             = array('[headerImageSource]', '[footerImageSource]', '[body]');
291                 $host               = \Utility\Registry::getConfigParam('Host');
292                 $replace            = array(
293                         $host . '/images/EmailHeader.png',
294                         $host . '/images/EmailFooter.png',
295                         $newsletter['content']
296                 );
297
298                 $emailTemplate = str_replace($search, $replace, $template['emailTemplate']);
299                 $attachments   = array();
300                 $attachment    = $newsletter['attachment'];
301                 if (!is_null($attachment))
302                 {
303                         $attachments[$attachment['filename']] = file_get_contents(
304                                 \Utility\Registry::getConfigParam('DocumentPath') . $attachment['filename']
305                         );
306                 }
307
308                 $search = array('[first_name]', '[family_name]', '[mobile]');
309
310                 #-> Send.
311                 $sentTo = 0;
312                 if ($test)
313                 {
314                         if (!is_null($testProfileId))
315                         {
316                                 $testProfile = $em->getRepository('\User\Entity\Profile')
317                                         ->find($testProfileId);
318                                 $replace     = array(
319                                         $testProfile->firstName,
320                                         $testProfile->familyName,
321                                         $testProfile->mobile
322                                 );
323                                 self::send(0, 0, $testProfile->company->id, $testProfileId, $testProfile->email, false,
324                                            str_replace($search, $replace, $newsletter['subject']),
325                                            str_replace($search, $replace, $emailTemplate), '',
326                                            $attachments, $complexAttachments, true);
327                         }
328                         else
329                         {
330                                 $auth    = \Utility\Registry::getAuthData();
331                                 $replace = array(
332                                         'John', 'Doe', '0820820820'
333                                 );
334                                 self::send(0, 0, $auth['company']['id'], $auth['id'], $auth['email'], false,
335                                            str_replace($search, $replace, $newsletter['subject']),
336                                            str_replace($search, $replace, $emailTemplate), '',
337                                            $attachments, $complexAttachments, true);
338                         }
339                         $sentTo++;
340                         return $sentTo;
341                 }
342                 else
343                 {
344                         $profiles = $em->createQuery(
345                                 'SELECT profile.id AS profileId, IDENTITY(profile.company) AS companyId, '
346                                 . 'profile.firstName as firstName, profile.familyName as familyName, profile.email as email '
347                                 . 'FROM \User\Entity\Profile profile '
348                                 . 'LEFT JOIN profile.company company '
349                                 . 'WHERE profile.jobState = :status '
350                                 . 'AND profile.subscribeNewsletter = 1'
351                         )
352                                 ->setParameter('status', 'Active')
353                                 ->getArrayResult();
354                         $sentTo   = 0;
355                         foreach ($profiles as $profile)
356                         {
357                                 $replace = array(
358                                         $profile['firstName'], $profile['familyName'], $profile['mobile']
359                                 );
360                                 self::sendOffline(0, 0, $profile['companyId'], $profile['profileId'],
361                                                   $profile['email'], false,
362                                                   str_replace($search, $replace, $newsletter['subject']),
363                                                   str_replace($search, $replace, $emailTemplate), '',
364                                                   $attachments, $complexAttachments, true);
365                                 $em->clear();
366                                 $sentTo++;
367                         }
368                 }
369                 self::$_data = null;
370                 return $sentTo;
371         }
372
373         public function sendBasicEmail($email, $subject, $body)
374         {
375                 $templatesDir = __DIR__ . '/../../../../../data/templates/';
376                 $template     = file_get_contents($templatesDir . 'general.html');
377                 $body         = str_replace('[body]', $body, $template);
378                 self::send(null, null, null, null, $email, null, $subject, $body, '');
379         }
380
381
382         /**
383          * Send email and/or sms notification.
384          * @param integer $fromCompanyId
385          * @param integer $fromProfileId
386          * @param integer $toCompanyId
387          * @param integer $toProfileId
388          * @param string  $email
389          * @param string  $mobile
390          * @param string  $subject
391          * @param string  $emailTemplate
392          * @param string  $smsTemplate
393          * @param array   $attachments
394          * @param array   $complexAttachments
395          * @param boolean $disableSms
396          * @return void
397          */
398         static private function sendOffline(
399                 $fromCompanyId, $fromProfileId,
400                 $toCompanyId, $toProfileId, $email, $mobile,
401                 $subject, $emailTemplate, $smsTemplate,
402                 array $attachments = array(),
403                 array $complexAttachments = array(),
404                 $disableSms = false
405         )
406         {
407                 //problem with GearmanClient so i'm just using normal send for now
408                 self::send(
409                                 $fromCompanyId, $fromProfileId,
410                                 $toCompanyId, $toProfileId, $email, $mobile,
411                                 $subject, $emailTemplate, $smsTemplate,
412                                 $attachments, $complexAttachments, $disableSms
413                         );
414
415 //              if (IS_BROCHURE)
416 //              {
417 //                      self::send(
418 //                              $fromCompanyId, $fromProfileId,
419 //                              $toCompanyId, $toProfileId, $email, $mobile,
420 //                              $subject, $emailTemplate, $smsTemplate,
421 //                              $attachments, $complexAttachments, $disableSms
422 //                      );
423 //              }
424 //              else
425 //              {
426 //                      $id = 'n' . microtime(true);
427 //                      while (\Utility\FileStore::existsJson($id))
428 //                      {
429 //                              time_nanosleep(0, 1000);
430 //                              $id = 'n' . microtime(true);
431 //                      };
432 //                      foreach ($attachments as $key => $data)
433 //                      {
434 //                              $attachments[$key] = utf8_encode($data);
435 //                      }
436 //                      foreach ($complexAttachments as $key => $data)
437 //                      {
438 //                              $complexAttachments[$key] = utf8_encode($data);
439 //                      }
440 //                      \Utility\FileStore::storeJson(
441 //                              $id,
442 //                              array(
443 //                                      'fromCompanyId'      => $fromCompanyId,
444 //                                      'fromProfileId'      => $fromProfileId,
445 //                                      'toCompanyId'        => $toCompanyId,
446 //                                      'toProfileId'        => $toProfileId,
447 //                                      'email'              => $email,
448 //                                      'mobile'             => $mobile,
449 //                                      'subject'            => $subject,
450 //                                      'emailTemplate'      => $emailTemplate,
451 //                                      'smsTemplate'        => $smsTemplate,
452 //                                      'attachments'        => $attachments,
453 //                                      'complexAttachments' => $complexAttachments,
454 //                                      'disableSms'         => $disableSms
455 //                              )
456 //                      );
457 //                      if (false === self::$_gearClient)
458 //                      {
459 //                              self::$_gearClient = new GearmanClient();
460 //                              self::$_gearClient->addServer();
461 //                      }
462 //                      $job_handle=self::$_gearClient->doBackground(
463 //                              'Notify',
464 //                              $id
465 //                      );
466 //
467 //              }
468         }
469
470
471
472         /**
473          * Send email and/or sms notification.
474          * @param integer $fromCompanyId
475          * @param integer $fromProfileId
476          * @param integer $toCompanyId
477          * @param integer $toProfileId
478          * @param string  $email
479          * @param string  $mobile
480          * @param string  $subject
481          * @param string  $emailTemplate
482          * @param string  $smsTemplate
483          * @param array   $attachments
484          * @param array   $complexAttachments
485          * @param boolean $disableSms
486          * @return void
487          */
488         static private function send(
489                 $fromCompanyId, $fromProfileId,
490                 $toCompanyId, $toProfileId, $email, $mobile,
491                 $subject, $emailTemplate, $smsTemplate,
492                 array $attachments = array(),
493                 array $complexAttachments = array(),
494                 $disableSms = false
495         )
496         {
497                 #-> Send the email off, into the big wide world, with a message of hope, or something.
498                 try
499                 {
500                         if ($emailTemplate && $email)
501                         {
502 //                \Utility\Debug::errorLog('Email Sending IS_BROCHURE', IS_BROCHURE ? 'true' : 'false');
503
504                 $emailTemplate = str_replace(
505                     '{APP_HOST}',
506                     !IS_BROCHURE ? \Utility\Registry::getConfigParam('Host') : \Utility\Registry::getConfigParam('CashCarsHost'),
507                     $emailTemplate
508                 );
509 //                              $emailTemplate = str_replace('{APP_HOST}', \Utility\Registry::getConfigParam('Host'), $emailTemplate);
510                                 $mailer        = new Email();
511                                 $mailer->send(array(
512                                                       'From'              => IS_BROCHURE
513                                                               ? 'noreply@wepay4cars.co.za'
514                                                               : \Utility\Registry::getConfigParam('sourceEmailAddress'),
515                                                       'To'                => $email,
516                                                       'Subject'           => $subject,
517                                                       'Html'              => $emailTemplate,
518                                                       'Attachment'        => $attachments,
519                                                       'ComplexAttachment' => $complexAttachments
520                                               ));
521                         }
522                 }
523                 catch (\Exception $e)
524                 {
525                         \Utility\Debug::errorLog('Email Sending', "$e");
526                         \Utility\Debug::errorLog('Email Sending', '-----------------------------------------------------------------------------------');
527                         \Utility\Debug::errorLog('Email Sending', $emailTemplate);
528                         \Utility\Debug::errorLog('Email Sending', '-----------------------------------------------------------------------------------');
529                 }
530
531                 #-> Send the sms hurtling through cyberspace at insane speeds.
532                 $apiMsgId = '';
533                 try
534                 {
535                         \Utility\Debug::errorLog("tryingNotification","sms");
536
537                         if (!$disableSms && $smsTemplate && $mobile)
538                         {
539 //                              if (IS_STAGE_ENV || 'production' == \Utility\Registry::getConfigParam('Instance'))
540                                 if (IS_STAGE_ENV || IS_LIVE_ENV)
541                                 {
542                                         if (IS_STAGE_ENV)
543                                         {
544                                                 $mobile = '+27826147353';
545                                         }
546                                         $sms      = new Sms();
547                                         $apiMsgId = $sms->send(array(
548                                                                        'To'      => $mobile,
549                                                                        'From'    => \Utility\Registry::getConfigParam('smsSourceAddress'),
550                                                                        'Subject' => IS_BROCHURE
551                                                                                ? 'CashCars: '
552                                                                                : 'Bid4Cars: ',
553                                                                        'Body'    => $smsTemplate
554                                                                ));
555                                         $apiMsgId = (false == $apiMsgId)
556                                                 ? ''
557                                                 : $apiMsgId;
558                                 }
559                         }
560                 }
561                 catch (\Exception $e)
562                 {
563                         \Utility\Debug::errorLog(__CLASS__, "$e");
564                 }
565
566                 #-> Log notification entry.
567                 $em      = \Utility\Registry::getEntityManager();
568                 $log     = new \Utility\Entity\NotificationLog();
569                 $logData = array(
570                         'emailTo'      => $email,
571                         'emailSubject' => $subject,
572                         'emailBody'    => $emailTemplate,
573                         'smsTo'        => $mobile,
574                         'smsBody'      => $smsTemplate,
575                         'apiMsgId'     => $apiMsgId
576                 );
577                 $fromCompanyId
578                 && $logData['fromCompany'] = $em->getReference('Company\Entity\Company', $fromCompanyId);
579                 $fromProfileId
580                 && $logData['fromProfile'] = $em->getReference('User\Entity\Profile', $fromProfileId);
581                 $toCompanyId
582                 && $logData['toCompany'] = $em->getReference('Company\Entity\Company', $toCompanyId);
583                 $toProfileId
584                 && $logData['toProfile'] = $em->getReference('User\Entity\Profile', $toProfileId);
585                 $log->fromArray($logData);
586                 $em->persist($log);
587                 $em->flush();
588                 $em->clear('\Utility\Entity\NotificationLog');
589                 return;
590         }
591
592 }
593