| 1: | <?php |
| 2: | namespace TIC\MailBundle\Entity; |
| 3: | |
| 4: | use TIC\DormBundle\Base\TICEntity as BaseEntity; |
| 5: | use TIC\MailBundle\Repository\TemplateRepository as EntityRepo; |
| 6: | |
| 7: | use Doctrine\ORM\Mapping as ORM; |
| 8: | use Symfony\Component\Validator\Constraints as Assert; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | class Template extends BaseEntity |
| 17: | { |
| 18: | use \TIC\DormBundle\Traits\EntityTimestampable; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | private $ref; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | private $label; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | private $target; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | private $sender; |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | private $return; |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | private $bccAdmins; |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | private $bccMore; |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | private $subject; |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | private $bodyText; |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | private $bodyHtml; |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | private $bodySms; |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | private $enabled; |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | private $translations; |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function __construct(?string $ref = null) |
| 93: | { |
| 94: | $this->ref = $ref; |
| 95: | $this->bccAdmins = true; |
| 96: | $this->enabled = true; |
| 97: | $this->translations = new \Doctrine\Common\Collections\ArrayCollection(); |
| 98: | } |
| 99: | |
| 100: | public function __toString() |
| 101: | { |
| 102: | return $this->label; |
| 103: | } |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | public function import(array $messages): void |
| 109: | { |
| 110: | \krsort($messages); |
| 111: | foreach ($messages as $locale => $data) { |
| 112: | if (! \is_array($data)) continue; |
| 113: | $lang = ($locale == '_DEFAULT_') ? '' : '_' . $locale; |
| 114: | foreach ($data as $key => $val) switch (\strtolower($key)) { |
| 115: | |
| 116: | case 'x-tic-enabled' : $this->enabled = ($val !== 'false'); break; |
| 117: | case 'comments' : $this->label = \trim($val); break; |
| 118: | case 'keywords' : $this->target = \trim($val); break; |
| 119: | case 'return-path' : $this->return = \trim($val); break; |
| 120: | case 'from' : $this->sender = \trim($val); break; |
| 121: | |
| 122: | case 'bcc' : |
| 123: | $this->bccAdmins = \preg_match('/^Admins(,.*)?\s*$/', $val); |
| 124: | $this->bccMore = \trim(\preg_replace('/^Admins(,(.*))?$/', '$2', $val)); |
| 125: | break; |
| 126: | case 'subject' : $this->{'subject' . $lang} = \trim($val); break; |
| 127: | case 'body_plain' : $this->{'bodyText' . $lang} = \trim($val); break; |
| 128: | case 'body_html' : $this->{'bodyHtml' . $lang} = \trim($val); break; |
| 129: | case 'body_sms' : $this->{'bodySms' . $lang} = \trim($val); break; |
| 130: | case 'date' : $this->{'createdAt' . $lang} = \DateTime::createFromFormat('j M Y H:i:s e', \trim($val)); break; |
| 131: | case 'resent-date' : $this->{'updatedAt' . $lang} = \DateTime::createFromFormat('j M Y H:i:s e', \trim($val)); break; |
| 132: | } |
| 133: | } |
| 134: | |
| 135: | foreach ($this->translations as $translation) $translation->doTimestampable(false); |
| 136: | $this->doTimestampable(false); |
| 137: | } |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | public function exportMail(string $locale = null, array $formats = null): string |
| 144: | { |
| 145: | $translation = ($locale === null) ? false : $this->getTranslation($locale); |
| 146: | if (empty($formats)) $formats = array('TEXT','HTML','SMS'); |
| 147: | |
| 148: | $headers = array( |
| 149: | 'Message-ID' => 'template_' . $this->ref, |
| 150: | 'Return-Path' => $this->return, |
| 151: | 'From' => $this->sender, |
| 152: | 'To' => null, |
| 153: | 'Bcc' => \trim((($this->bccAdmins) ? 'Admins, ' : '') . $this->bccMore, " \t\r\n,"), |
| 154: | 'Subject' => $this->subject, |
| 155: | 'Content-Language' => null, |
| 156: | 'Date' => ($this->createdAt) ? $this->createdAt->format('j M Y H:i:s e') : null, |
| 157: | 'Resent-Date' => ($this->updatedAt) ? $this->updatedAt->format('j M Y H:i:s e') : null, |
| 158: | 'Comments' => $this->label, |
| 159: | 'Keywords' => $this->target, |
| 160: | 'X-TIC-Reference' => $this->ref, |
| 161: | 'X-TIC-Enabled' => $this->enabled ? 'true' : 'false', |
| 162: | ); |
| 163: | $mailsep = 'From tic.mail@bundle.teicee.fr ' . \date('D M j H:i:s Y') . "\n"; |
| 164: | $bounds = '----TIC_TemplatePart_' . $this->ref; |
| 165: | $messages= ($locale === null) ? $mailsep : ''; |
| 166: | |
| 167: | if (! $translation) { |
| 168: | $boundary = $bounds . '_default'; |
| 169: | foreach ($headers as $key => $val) if ($val !== null) $messages.= $key . ': ' . $val . "\n"; |
| 170: | $messages.= "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"\n\nThis is a multi-part template in MIME format.\n"; |
| 171: | if (\in_array('TEXT', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/plain; charset=\"UTF8\"\n\n" . $this->bodyText; |
| 172: | if (\in_array('HTML', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/html; charset=\"UTF8\"\n\n" . $this->bodyHtml; |
| 173: | if (\in_array('SMS', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/sms; charset=\"UTF8\"\n\n" . $this->bodySms; |
| 174: | $messages.= "\n--" . $boundary . "--\n\n"; |
| 175: | } else { |
| 176: | $boundary = $bounds . '_' . $translation->getLocale(); |
| 177: | $headers['Subject'] = $translation->getSubject(); |
| 178: | $headers['Content-Language'] = $translation->getLocale(); |
| 179: | foreach ($headers as $key => $val) if ($val !== null) $messages.= $key . ': ' . $val . "\n"; |
| 180: | $messages.= "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"\n\nThis is a multi-part template in MIME format.\n"; |
| 181: | if (\in_array('TEXT', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/plain; charset=\"UTF8\"\n\n" . $translation->getBodyText(); |
| 182: | if (\in_array('HTML', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/html; charset=\"UTF8\"\n\n" . $translation->getBodyHtml(); |
| 183: | if (\in_array('SMS', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/sms; charset=\"UTF8\"\n\n" . $translation->getBodySms(); |
| 184: | $messages.= "\n--" . $boundary . "--\n\n"; |
| 185: | } |
| 186: | if ($locale === null) foreach ($this->translations as $translation) { |
| 187: | $messages.= $mailsep; |
| 188: | $boundary = $bounds . '_' . $translation->getLocale(); |
| 189: | $headers['Subject'] = $translation->getSubject(); |
| 190: | $headers['Content-Language'] = $translation->getLocale(); |
| 191: | foreach ($headers as $key => $val) if ($val !== null) $messages.= $key . ': ' . $val . "\n"; |
| 192: | $messages.= "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"\n\nThis is a multi-part template in MIME format.\n"; |
| 193: | if (\in_array('TEXT', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/plain; charset=\"UTF8\"\n\n" . $translation->getBodyText(); |
| 194: | if (\in_array('HTML', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/html; charset=\"UTF8\"\n\n" . $translation->getBodyHtml(); |
| 195: | if (\in_array('SMS', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/sms; charset=\"UTF8\"\n\n" . $translation->getBodySms(); |
| 196: | $messages.= "\n--" . $boundary . "--\n\n"; |
| 197: | } |
| 198: | return $messages; |
| 199: | } |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | public function isEmpty(): bool |
| 209: | { |
| 210: | if ($this->bodyHtml !== NULL) return false; |
| 211: | if ($this->bodyText !== NULL) return false; |
| 212: | if ($this->bodySms !== NULL) return false; |
| 213: | return true; |
| 214: | } |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | public function listFormats(): array |
| 222: | { |
| 223: | $formats = array(); |
| 224: | if ($this->bodyHtml !== NULL) $formats[] = 'HTML'; |
| 225: | if ($this->bodyText !== NULL) $formats[] = 'TEXT'; |
| 226: | if ($this->bodySms !== NULL) $formats[] = 'SMS'; |
| 227: | return $formats; |
| 228: | } |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | public function getContents(string $locale = null): array |
| 238: | { |
| 239: | $contents = array( |
| 240: | 'subject' => $this->subject, |
| 241: | 'bodyText' => $this->bodyText, |
| 242: | 'bodyHtml' => $this->bodyHtml, |
| 243: | 'bodySms' => $this->bodySms, |
| 244: | ); |
| 245: | |
| 246: | if (($locale !== null) && ($translation = $this->getTranslation($locale))) { |
| 247: | if (($data = $translation->getSubject() ) !== null) $contents['subject'] = $data; |
| 248: | if (($data = $translation->getBodyText()) !== null) $contents['bodyText'] = $data; |
| 249: | if (($data = $translation->getBodyHtml()) !== null) $contents['bodyHtml'] = $data; |
| 250: | if (($data = $translation->getBodySms() ) !== null) $contents['bodySms'] = $data; |
| 251: | } |
| 252: | |
| 253: | return $contents; |
| 254: | } |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | |
| 262: | |
| 263: | public function getId(): ?string |
| 264: | { |
| 265: | return $this->ref; |
| 266: | } |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | public function getTranslation(string $locale): ?TemplateTranslation |
| 274: | { |
| 275: | foreach ($this->translations as $translation) { |
| 276: | if ($translation->getLocale() === $locale) return $translation; |
| 277: | } |
| 278: | return null; |
| 279: | } |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | public function __isset(string $property) |
| 289: | { |
| 290: | return \preg_match('/^(subject|bodyText|bodyHtml|bodySms|createdAt|updatedAt)_(.+)$/', $property); |
| 291: | } |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | public function __get(string $property) |
| 299: | { |
| 300: | if (\preg_match('/^(subject|bodyText|bodyHtml|bodySms|createdAt|updatedAt)_(.+)$/', $property, $match)) { |
| 301: | $accessor = 'get' . \ucfirst($match[1]); |
| 302: | $locale = \strtolower($match[2]); |
| 303: | |
| 304: | |
| 305: | $translation = $this->getTranslation($locale); |
| 306: | if ($translation) return \call_user_func(array($translation, $accessor)); |
| 307: | return null; |
| 308: | } |
| 309: | throw new \LogicException(\sprintf('No property named `%s` exists', $property)); |
| 310: | } |
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: | public function __set(string $property, mixed $value) |
| 319: | { |
| 320: | if (\preg_match('/^(subject|bodyText|bodyHtml|bodySms|createdAt|updatedAt)_(.+)$/', $property, $match)) { |
| 321: | $mutator = 'set' . \ucfirst($match[1]); |
| 322: | $locale = \strtolower($match[2]); |
| 323: | |
| 324: | |
| 325: | $translation = $this->getTranslation($locale); |
| 326: | if ($translation === null) { |
| 327: | $translation = new TemplateTranslation(); |
| 328: | $translation->setMaster($this); |
| 329: | $translation->setLocale($locale); |
| 330: | $this->translations[] = $translation; |
| 331: | } |
| 332: | |
| 333: | \call_user_func(array($translation, $mutator), $value); |
| 334: | return $this; |
| 335: | } |
| 336: | throw new \LogicException(\sprintf('No property named `%s` exists', $property)); |
| 337: | } |
| 338: | |
| 339: | |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | public function setRef($ref) |
| 350: | { |
| 351: | $this->ref = $ref; |
| 352: | |
| 353: | return $this; |
| 354: | } |
| 355: | |
| 356: | |
| 357: | |
| 358: | |
| 359: | |
| 360: | |
| 361: | public function getRef() |
| 362: | { |
| 363: | return $this->ref; |
| 364: | } |
| 365: | |
| 366: | |
| 367: | |
| 368: | |
| 369: | |
| 370: | |
| 371: | |
| 372: | |
| 373: | public function setLabel($label) |
| 374: | { |
| 375: | $this->label = $label; |
| 376: | |
| 377: | return $this; |
| 378: | } |
| 379: | |
| 380: | |
| 381: | |
| 382: | |
| 383: | |
| 384: | |
| 385: | public function getLabel() |
| 386: | { |
| 387: | return $this->label; |
| 388: | } |
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | public function setTarget($target) |
| 398: | { |
| 399: | $this->target = $target; |
| 400: | |
| 401: | return $this; |
| 402: | } |
| 403: | |
| 404: | |
| 405: | |
| 406: | |
| 407: | |
| 408: | |
| 409: | public function getTarget() |
| 410: | { |
| 411: | return $this->target; |
| 412: | } |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | |
| 419: | |
| 420: | |
| 421: | public function setSender($sender) |
| 422: | { |
| 423: | $this->sender = $sender; |
| 424: | |
| 425: | return $this; |
| 426: | } |
| 427: | |
| 428: | |
| 429: | |
| 430: | |
| 431: | |
| 432: | |
| 433: | public function getSender() |
| 434: | { |
| 435: | return $this->sender; |
| 436: | } |
| 437: | |
| 438: | |
| 439: | |
| 440: | |
| 441: | |
| 442: | |
| 443: | |
| 444: | |
| 445: | public function setReturn($return) |
| 446: | { |
| 447: | $this->return = $return; |
| 448: | |
| 449: | return $this; |
| 450: | } |
| 451: | |
| 452: | |
| 453: | |
| 454: | |
| 455: | |
| 456: | |
| 457: | public function getReturn() |
| 458: | { |
| 459: | return $this->return; |
| 460: | } |
| 461: | |
| 462: | |
| 463: | |
| 464: | |
| 465: | |
| 466: | |
| 467: | |
| 468: | |
| 469: | public function setBccAdmins($bccAdmins) |
| 470: | { |
| 471: | $this->bccAdmins = $bccAdmins; |
| 472: | |
| 473: | return $this; |
| 474: | } |
| 475: | |
| 476: | |
| 477: | |
| 478: | |
| 479: | |
| 480: | |
| 481: | public function getBccAdmins() |
| 482: | { |
| 483: | return $this->bccAdmins; |
| 484: | } |
| 485: | |
| 486: | |
| 487: | |
| 488: | |
| 489: | |
| 490: | |
| 491: | |
| 492: | |
| 493: | public function setBccMore($bccMore) |
| 494: | { |
| 495: | $this->bccMore = $bccMore; |
| 496: | |
| 497: | return $this; |
| 498: | } |
| 499: | |
| 500: | |
| 501: | |
| 502: | |
| 503: | |
| 504: | |
| 505: | public function getBccMore() |
| 506: | { |
| 507: | return $this->bccMore; |
| 508: | } |
| 509: | |
| 510: | |
| 511: | |
| 512: | |
| 513: | |
| 514: | |
| 515: | |
| 516: | |
| 517: | public function setSubject($subject) |
| 518: | { |
| 519: | $this->subject = $subject; |
| 520: | |
| 521: | return $this; |
| 522: | } |
| 523: | |
| 524: | |
| 525: | |
| 526: | |
| 527: | |
| 528: | |
| 529: | public function getSubject() |
| 530: | { |
| 531: | return $this->subject; |
| 532: | } |
| 533: | |
| 534: | |
| 535: | |
| 536: | |
| 537: | |
| 538: | |
| 539: | |
| 540: | |
| 541: | public function setBodyText($bodyText) |
| 542: | { |
| 543: | $this->bodyText = $bodyText; |
| 544: | |
| 545: | return $this; |
| 546: | } |
| 547: | |
| 548: | |
| 549: | |
| 550: | |
| 551: | |
| 552: | |
| 553: | public function getBodyText() |
| 554: | { |
| 555: | return $this->bodyText; |
| 556: | } |
| 557: | |
| 558: | |
| 559: | |
| 560: | |
| 561: | |
| 562: | |
| 563: | |
| 564: | |
| 565: | public function setBodyHtml($bodyHtml) |
| 566: | { |
| 567: | $this->bodyHtml = $bodyHtml; |
| 568: | |
| 569: | return $this; |
| 570: | } |
| 571: | |
| 572: | |
| 573: | |
| 574: | |
| 575: | |
| 576: | |
| 577: | public function getBodyHtml() |
| 578: | { |
| 579: | return $this->bodyHtml; |
| 580: | } |
| 581: | |
| 582: | |
| 583: | |
| 584: | |
| 585: | |
| 586: | |
| 587: | |
| 588: | |
| 589: | public function setBodySms($bodySms) |
| 590: | { |
| 591: | $this->bodySms = $bodySms; |
| 592: | |
| 593: | return $this; |
| 594: | } |
| 595: | |
| 596: | |
| 597: | |
| 598: | |
| 599: | |
| 600: | |
| 601: | public function getBodySms() |
| 602: | { |
| 603: | return $this->bodySms; |
| 604: | } |
| 605: | |
| 606: | |
| 607: | |
| 608: | |
| 609: | |
| 610: | |
| 611: | |
| 612: | |
| 613: | public function setEnabled($enabled) |
| 614: | { |
| 615: | $this->enabled = $enabled; |
| 616: | |
| 617: | return $this; |
| 618: | } |
| 619: | |
| 620: | |
| 621: | |
| 622: | |
| 623: | |
| 624: | |
| 625: | public function getEnabled() |
| 626: | { |
| 627: | return $this->enabled; |
| 628: | } |
| 629: | |
| 630: | |
| 631: | |
| 632: | |
| 633: | |
| 634: | |
| 635: | |
| 636: | |
| 637: | public function addTranslation(TemplateTranslation $translation) |
| 638: | { |
| 639: | $this->translations[] = $translation; |
| 640: | |
| 641: | return $this; |
| 642: | } |
| 643: | |
| 644: | |
| 645: | |
| 646: | |
| 647: | |
| 648: | |
| 649: | public function removeTranslation(TemplateTranslation $translation) |
| 650: | { |
| 651: | $this->translations->removeElement($translation); |
| 652: | } |
| 653: | |
| 654: | |
| 655: | |
| 656: | |
| 657: | |
| 658: | |
| 659: | public function getTranslations() |
| 660: | { |
| 661: | return $this->translations; |
| 662: | } |
| 663: | } |
| 664: | |