

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