Source of file Template.php
Size: 17,438 Bytes - Last Modified: 2023-11-16T22:56:03+01:00
/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/mail-bundle/src/Entity/Template.php
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 | <?phpnamespace TIC\MailBundle\Entity; use TIC\DormBundle\Base\TICEntity as BaseEntity; use TIC\MailBundle\Repository\TemplateRepository as EntityRepo; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * Entité représentant un modèle de notification par email. * @ORM\Table(name="tic_mail_template") * @ORM\Entity(repositoryClass=EntityRepo::class) * @ORM\HasLifecycleCallbacks */class Template extends BaseEntity {use \TIC\DormBundle\Traits\EntityTimestampable; // require @ORM\HasLifecycleCallbacks // --------------------------------------------------------------------- Properties/** * @ORM\Column(type="string", length=100, unique=true) * @ORM\Id */private $ref; /** * @ORM\Column(type="string", length=250) */private $label; /** * @ORM\Column(type="string", length=100, nullable=true) */private $target; /** * @ORM\Column(type="string", length=250, nullable=true) */private $sender; /** * @ORM\Column(type="string", length=250, nullable=true, name="return_path") */private $return; /** * @ORM\Column(type="boolean", options={"default"=true}) */private $bccAdmins; /** * @ORM\Column(type="text", nullable=true) */private $bccMore; /** * @ORM\Column(type="string", length=250) */private $subject; /** * @ORM\Column(type="text", nullable=true) */private $bodyText; /** * @ORM\Column(type="text", nullable=true) */private $bodyHtml; /** * @ORM\Column(type="text", nullable=true) */private $bodySms; /** * @ORM\Column(type="boolean", options={"default"=true}) */private $enabled; /** * @ORM\OneToMany(targetEntity="TemplateTranslation", mappedBy="master", cascade={"persist", "remove"}, orphanRemoval=true) */private $translations; // --------------------------------------------------------------------- Custom methodspublic function __construct(?string $ref = null) { $this->ref = $ref; $this->bccAdmins = true; $this->enabled = true; $this->translations = new \Doctrine\Common\Collections\ArrayCollection(); } public function __toString() { return $this->label; } /** * Import des données de l'entité depuis un tableau associatif. */public function import(array $messages): void { \krsort($messages); // pour que la locale _DEFAULT_ passe en dernier (données prioritaires) foreach ($messages as $locale => $data) { if (! \is_array($data)) continue; $lang = ($locale == '_DEFAULT_') ? '' : '_' . $locale; foreach ($data as $key => $val) switch (\strtolower($key)) { # case 'x-tic-reference': $this->ref = \trim($val); break;case 'x-tic-enabled' : $this->enabled = ($val !== 'false'); break; case 'comments' : $this->label = \trim($val); break; case 'keywords' : $this->target = \trim($val); break; case 'return-path' : $this->return = \trim($val); break; case 'from' : $this->sender = \trim($val); break; # case 'to' : $this-> = \trim($val); break;case 'bcc' : $this->bccAdmins = \preg_match('/^Admins(,.*)?\s*$/', $val); $this->bccMore = \trim(\preg_replace('/^Admins(,(.*))?$/', '$2', $val)); break; case 'subject' : $this->{'subject' . $lang} = \trim($val); break; case 'body_plain' : $this->{'bodyText' . $lang} = \trim($val); break; case 'body_html' : $this->{'bodyHtml' . $lang} = \trim($val); break; case 'body_sms' : $this->{'bodySms' . $lang} = \trim($val); break; case 'date' : $this->{'createdAt' . $lang} = \DateTime::createFromFormat('j M Y H:i:s e', \trim($val)); break; case 'resent-date' : $this->{'updatedAt' . $lang} = \DateTime::createFromFormat('j M Y H:i:s e', \trim($val)); break; } } // désactivation du comportement timestampable automatique pour l'import foreach ($this->translations as $translation) $translation->doTimestampable(false); $this->doTimestampable(false); } /** * Export des données de l'entité au format Mail/Mbox. * (messages multipart MIME pour contenus text+html avec concaténation Mbox pour différentes locales) */public function exportMail(string $locale = null, array $formats = null): string { $translation = ($locale === null) ? false : $this->getTranslation($locale); if (empty($formats)) $formats = array('TEXT','HTML','SMS'); $headers = array( 'Message-ID' => 'template_' . $this->ref, 'Return-Path' => $this->return, 'From' => $this->sender, 'To' => null, 'Bcc' => \trim((($this->bccAdmins) ? 'Admins, ' : '') . $this->bccMore, " \t\r\n,"), 'Subject' => $this->subject, 'Content-Language' => null, 'Date' => ($this->createdAt) ? $this->createdAt->format('j M Y H:i:s e') : null, 'Resent-Date' => ($this->updatedAt) ? $this->updatedAt->format('j M Y H:i:s e') : null, 'Comments' => $this->label, 'Keywords' => $this->target, 'X-TIC-Reference' => $this->ref, 'X-TIC-Enabled' => $this->enabled ? 'true' : 'false', ); $mailsep = 'From tic.mail@bundle.teicee.fr ' . \date('D M j H:i:s Y') . "\n"; $bounds = '----TIC_TemplatePart_' . $this->ref; $messages= ($locale === null) ? $mailsep : ''; if (! $translation) { $boundary = $bounds . '_default'; foreach ($headers as $key => $val) if ($val !== null) $messages.= $key . ': ' . $val . "\n"; $messages.= "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"\n\nThis is a multi-part template in MIME format.\n"; if (\in_array('TEXT', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/plain; charset=\"UTF8\"\n\n" . $this->bodyText; if (\in_array('HTML', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/html; charset=\"UTF8\"\n\n" . $this->bodyHtml; if (\in_array('SMS', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/sms; charset=\"UTF8\"\n\n" . $this->bodySms; $messages.= "\n--" . $boundary . "--\n\n"; } else { $boundary = $bounds . '_' . $translation->getLocale(); $headers['Subject'] = $translation->getSubject(); $headers['Content-Language'] = $translation->getLocale(); foreach ($headers as $key => $val) if ($val !== null) $messages.= $key . ': ' . $val . "\n"; $messages.= "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"\n\nThis is a multi-part template in MIME format.\n"; if (\in_array('TEXT', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/plain; charset=\"UTF8\"\n\n" . $translation->getBodyText(); if (\in_array('HTML', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/html; charset=\"UTF8\"\n\n" . $translation->getBodyHtml(); if (\in_array('SMS', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/sms; charset=\"UTF8\"\n\n" . $translation->getBodySms(); $messages.= "\n--" . $boundary . "--\n\n"; } if ($locale === null) foreach ($this->translations as $translation) { $messages.= $mailsep; $boundary = $bounds . '_' . $translation->getLocale(); $headers['Subject'] = $translation->getSubject(); $headers['Content-Language'] = $translation->getLocale(); foreach ($headers as $key => $val) if ($val !== null) $messages.= $key . ': ' . $val . "\n"; $messages.= "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"\n\nThis is a multi-part template in MIME format.\n"; if (\in_array('TEXT', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/plain; charset=\"UTF8\"\n\n" . $translation->getBodyText(); if (\in_array('HTML', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/html; charset=\"UTF8\"\n\n" . $translation->getBodyHtml(); if (\in_array('SMS', $formats)) $messages.= "\n--" . $boundary . "\nContent-Type: text/sms; charset=\"UTF8\"\n\n" . $translation->getBodySms(); $messages.= "\n--" . $boundary . "--\n\n"; } return $messages; } // --------------------------------------------------------------------- Shortcut methods/** * Test si le contenu text/html ET text/plain ET sms sont vides. * @Assert\IsFalse(message = "ticmail.alert.emptyBody") */public function isEmpty(): bool { if ($this->bodyHtml !== NULL) return false; if ($this->bodyText !== NULL) return false; if ($this->bodySms !== NULL) return false; return true; } /** * Indique les formats (text/plain, text/html, sms) utilisés par ce modèle. * * @return array Liste pouvant contenir les indications : 'HTML', 'TEXT', 'SMS' */public function listFormats(): array { $formats = array(); if ($this->bodyHtml !== NULL) $formats[] = 'HTML'; if ($this->bodyText !== NULL) $formats[] = 'TEXT'; if ($this->bodySms !== NULL) $formats[] = 'SMS'; return $formats; } /** * Retourne la liste des contenus pour les 3 vues. * (utilisé pour initialiser un environnement Twig dans le service tic_mailer) * * @param string $locale Langue souhaitée pour les modèles (optionnel) * @return array Contenus pour les clés 'subject', 'bodyText', 'bodyHtml' et 'bodySms' */public function getContents(string $locale = null): array { $contents = array( 'subject' => $this->subject, 'bodyText' => $this->bodyText, 'bodyHtml' => $this->bodyHtml, 'bodySms' => $this->bodySms, ); if (($locale !== null) && ($translation = $this->getTranslation($locale))) { if (($data = $translation->getSubject() ) !== null) $contents['subject'] = $data; if (($data = $translation->getBodyText()) !== null) $contents['bodyText'] = $data; if (($data = $translation->getBodyHtml()) !== null) $contents['bodyHtml'] = $data; if (($data = $translation->getBodySms() ) !== null) $contents['bodySms'] = $data; } return $contents; } // --------------------------------------------------------------------- Tweaked methods/** * Get id * @return string */public function getId(): ?string { return $this->ref; } /** * Recherche l'instance TemplateTranslation associée pour la locale spécifiée. * @param string $locale * @return TemplateTranslation ou null si aucune correspondance */public function getTranslation(string $locale): ?TemplateTranslation { foreach ($this->translations as $translation) { if ($translation->getLocale() === $locale) return $translation; } return null; } // --------------------------------------------------------------------- Magic methods/** * @param string $property * @return boolean */public function __isset(string $property) { return \preg_match('/^(subject|bodyText|bodyHtml|bodySms|createdAt|updatedAt)_(.+)$/', $property); } /** * @param string $property * @return mixed * @throws \LogicException If no accessor/property exists by that name */public function __get(string $property) { if (\preg_match('/^(subject|bodyText|bodyHtml|bodySms|createdAt|updatedAt)_(.+)$/', $property, $match)) { $accessor = 'get' . \ucfirst($match[1]); $locale = \strtolower($match[2]); // récupération de la traduction existante sur cette locale ou résultat vide $translation = $this->getTranslation($locale); if ($translation) return \call_user_func(array($translation, $accessor)); return null; } throw new \LogicException(\sprintf('No property named `%s` exists', $property)); } /** * @param string $property * @param mixed $value * @return void * @throws \LogicException If no mutator/property exists by that name */public function __set(string $property, mixed $value) { if (\preg_match('/^(subject|bodyText|bodyHtml|bodySms|createdAt|updatedAt)_(.+)$/', $property, $match)) { $mutator = 'set' . \ucfirst($match[1]); $locale = \strtolower($match[2]); // récupération de la traduction existante sur cette locale ou création $translation = $this->getTranslation($locale); if ($translation === null) { $translation = new TemplateTranslation(); $translation->setMaster($this); $translation->setLocale($locale); $this->translations[] = $translation; } // mise à jour de la traduction \call_user_func(array($translation, $mutator), $value); return $this; } throw new \LogicException(\sprintf('No property named `%s` exists', $property)); } // --------------------------------------------------------------------- Auto-generated/** * Set ref * * @param string $ref * * @return Template */public function setRef($ref) { $this->ref = $ref; return $this; } /** * Get ref * * @return string */public function getRef() { return $this->ref; } /** * Set label * * @param string $label * * @return Template */public function setLabel($label) { $this->label = $label; return $this; } /** * Get label * * @return string */public function getLabel() { return $this->label; } /** * Set target * * @param string $target * * @return Template */public function setTarget($target) { $this->target = $target; return $this; } /** * Get target * * @return string */public function getTarget() { return $this->target; } /** * Set sender * * @param string $sender * * @return Template */public function setSender($sender) { $this->sender = $sender; return $this; } /** * Get sender * * @return string */public function getSender() { return $this->sender; } /** * Set return * * @param string $return * * @return Template */public function setReturn($return) { $this->return = $return; return $this; } /** * Get return * * @return string */public function getReturn() { return $this->return; } /** * Set bccAdmins * * @param boolean $bccAdmins * * @return Template */public function setBccAdmins($bccAdmins) { $this->bccAdmins = $bccAdmins; return $this; } /** * Get bccAdmins * * @return boolean */public function getBccAdmins() { return $this->bccAdmins; } /** * Set bccMore * * @param string $bccMore * * @return Template */public function setBccMore($bccMore) { $this->bccMore = $bccMore; return $this; } /** * Get bccMore * * @return string */public function getBccMore() { return $this->bccMore; } /** * Set subject * * @param string $subject * * @return Template */public function setSubject($subject) { $this->subject = $subject; return $this; } /** * Get subject * * @return string */public function getSubject() { return $this->subject; } /** * Set bodyText * * @param string $bodyText * * @return Template */public function setBodyText($bodyText) { $this->bodyText = $bodyText; return $this; } /** * Get bodyText * * @return string */public function getBodyText() { return $this->bodyText; } /** * Set bodyHtml * * @param string $bodyHtml * * @return Template */public function setBodyHtml($bodyHtml) { $this->bodyHtml = $bodyHtml; return $this; } /** * Get bodyHtml * * @return string */public function getBodyHtml() { return $this->bodyHtml; } /** * Set bodySms * * @param string $bodySms * * @return Template */public function setBodySms($bodySms) { $this->bodySms = $bodySms; return $this; } /** * Get bodySms * * @return string */public function getBodySms() { return $this->bodySms; } /** * Set enabled * * @param boolean $enabled * * @return Template */public function setEnabled($enabled) { $this->enabled = $enabled; return $this; } /** * Get enabled * * @return boolean */public function getEnabled() { return $this->enabled; } /** * Add translation * * @param TemplateTranslation $translation * * @return Template */public function addTranslation(TemplateTranslation $translation) { $this->translations[] = $translation; return $this; } /** * Remove translation * * @param TemplateTranslation $translation */public function removeTranslation(TemplateTranslation $translation) { $this->translations->removeElement($translation); } /** * Get translations * * @return \Doctrine\Common\Collections\Collection */public function getTranslations() { return $this->translations; } } |