public function setUp() { $this->emailTemplate = new EmailTemplate('update_entity.html.twig', "@subject = sdfdsf\n abc"); $this->assertEquals('abc', $this->emailTemplate->getContent()); $this->assertFalse($this->emailTemplate->getIsSystem()); $this->assertEquals('html', $this->emailTemplate->getType()); }
/** * @param EmailTemplate $value * @param Constraint|EmailTemplateSyntax $constraint */ public function validate($value, Constraint $constraint) { // prepare templates to be validated $itemsToValidate = [['field' => 'subject', 'locale' => null, 'template' => $value->getSubject()], ['field' => 'content', 'locale' => null, 'template' => $value->getContent()]]; $translations = $value->getTranslations(); foreach ($translations as $trans) { if (in_array($trans->getField(), ['subject', 'content'])) { $itemsToValidate[] = ['field' => $trans->getField(), 'locale' => $trans->getLocale(), 'template' => $trans->getContent()]; } } /** @var \Twig_Extension_Sandbox $sandbox */ $sandbox = $this->twig->getExtension('sandbox'); $sandbox->enableSandbox(); // validate templates' syntax $errors = []; foreach ($itemsToValidate as &$item) { try { $this->twig->parse($this->twig->tokenize($item['template'])); } catch (\Twig_Error_Syntax $e) { $errors[] = ['field' => $item['field'], 'locale' => $item['locale'], 'error' => $e->getMessage()]; } } $sandbox->disableSandbox(); // add violations for found errors if (!empty($errors)) { foreach ($errors as $error) { $this->context->addViolation($constraint->message, ['{{ field }}' => $this->getFieldLabel(ClassUtils::getClass($value), $error['field']), '{{ locale }}' => $this->getLocaleName($error['locale']), '{{ error }}' => $error['error']]); } } }
/** * Compile preview content * * @param EmailTemplate $entity * @param null|string $locale * * @return string */ public function compilePreview(EmailTemplate $entity, $locale = null) { $content = $entity->getContent(); if ($locale) { foreach ($entity->getTranslations() as $translation) { /** @var EmailTemplateTranslation $translation */ if ($translation->getLocale() === $locale && $translation->getField() === 'content') { $content = $translation->getContent(); } } } return $this->render('{% verbatim %}' . $content . '{% endverbatim %}', []); }
/** * @param EmailModel $emailModel * @param EmailTemplate $template * @param Email $email */ protected function applyTemplate(EmailModel $emailModel, EmailTemplate $template, Email $email) { $locales = array_merge($email->getAcceptedLocales(), [$this->defaultLocale]); $flippedLocales = array_flip($locales); $translatedSubjects = []; $translatedContents = []; foreach ($template->getTranslations() as $translation) { switch ($translation->getField()) { case 'content': $translatedContents[$translation->getLocale()] = $translation->getContent(); break; case 'subject': $translatedSubjects[$translation->getLocale()] = $translation->getContent(); break; } } $comparator = ArrayUtil::createOrderedComparator($flippedLocales); uksort($translatedSubjects, $comparator); uksort($translatedContents, $comparator); $validContents = array_intersect_key($translatedContents, $flippedLocales); $validsubjects = array_intersect_key($translatedSubjects, $flippedLocales); $content = reset($validContents); $subject = reset($validsubjects); $emailModel->setSubject($subject === false ? $template->getSubject() : $subject)->setBody($content === false ? $template->getContent() : $content)->setType($template->getType()); }
/** * Compile preview content * * @param EmailTemplate $entity * * @return string */ public function compilePreview(EmailTemplate $entity) { // ensure we have no html tags in txt template $content = $entity->getContent(); $content = $entity->getType() == 'txt' ? strip_tags($content) : $content; $templateParams['user'] = $this->user; $templateRendered = $this->render('{% verbatim %}' . $content . '{% endverbatim %}', $templateParams); return $templateRendered; }
/** * Compile preview content * * @param EmailTemplate $entity * * @return string */ public function compilePreview(EmailTemplate $entity) { return $this->render('{% verbatim %}' . $entity->getContent() . '{% endverbatim %}', []); }