From 1f5c34e451759c90a6ab212ecea8990cb68fb801 Mon Sep 17 00:00:00 2001 From: rs2487 Date: Mon, 30 Jan 2017 17:56:34 +0100 Subject: [PATCH] Added additional receivers for sending notification as CC or BCC (#20) * Added additional receivers for sending notification as CC or BCC (#20) --- CHANGELOG.md | 2 +- Controller/FormController.php | 16 +- DependencyInjection/Configuration.php | 13 +- DependencyInjection/SuluFormExtension.php | 6 +- Entity/Dynamic.php | 12 + Entity/FormTranslation.php | 25 ++ Entity/FormTranslationReceiver.php | 151 ++++++++++++ Event/DynFormSavedEvent.php | 6 +- Event/MailSubscriber.php | 231 ++++++++++++++++++ Form/Type/DynamicFormType.php | 28 ++- Mail/Helper.php | 27 +- Mail/HelperInterface.php | 19 +- Mail/NullHelper.php | 12 +- Manager/FormManager.php | 116 ++++++--- Repository/FormRepository.php | 3 +- Resources/config/doctrine/FormField.orm.xml | 3 - .../doctrine/FormFieldTranslation.orm.xml | 3 - .../config/doctrine/FormTranslation.orm.xml | 11 +- .../doctrine/FormTranslationReceiver.orm.xml | 22 ++ Resources/config/services.xml | 10 + Resources/doc/dynamic.md | 33 +-- Resources/doc/index.md | 6 +- Resources/translations/sulu/backend.de.xlf | 18 ++ Resources/translations/sulu/backend.en.xlf | 18 ++ Resources/translations/sulu/backend.fr.xlf | 18 ++ .../forms/blocks/email-receiver.html.twig | 69 ++++++ Resources/views/forms/template.html.twig | 175 ++++++++----- Resources/views/mails/customer.html.twig | 1 + Resources/views/mails/notify.html.twig | 25 ++ 29 files changed, 919 insertions(+), 160 deletions(-) create mode 100644 Entity/FormTranslationReceiver.php create mode 100644 Event/MailSubscriber.php create mode 100644 Resources/config/doctrine/FormTranslationReceiver.orm.xml create mode 100644 Resources/views/forms/blocks/email-receiver.html.twig create mode 100644 Resources/views/mails/customer.html.twig create mode 100644 Resources/views/mails/notify.html.twig diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e381ee9..242d5fb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Changelog ## dev-develop - + - FEATURE #20 Added additional receivers for sending notification as CC or BCC - ENHANCEMENT #23 Fixed composer json links and description - BUGFIX #16 Fixed setting of entity value in dynamic which represents an array - ENHANCEMENT #13 Added dynamic type pool service for enabling custom form field types diff --git a/Controller/FormController.php b/Controller/FormController.php index c0023062..c6e3ac24 100644 --- a/Controller/FormController.php +++ b/Controller/FormController.php @@ -5,8 +5,8 @@ use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Routing\ClassResourceInterface; use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeInterface; -use Sulu\Bundle\FormBundle\Entity\Dynamic; use Sulu\Bundle\FormBundle\Entity\Form; +use Sulu\Bundle\FormBundle\Mail\HelperInterface; use Sulu\Bundle\FormBundle\Manager\FormManager; use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactory; use Sulu\Component\Rest\ListBuilder\Doctrine\FieldDescriptor\DoctrineCaseFieldDescriptor; @@ -189,16 +189,18 @@ public function cgetTemplateAction(Request $request) ]; $types = $this->get('sulu_form.dynamic.form_field_type_pool')->all(); - - if (class_exists(\EWZ\Bundle\RecaptchaBundle\Form\Type\RecaptchaType::class)) { - $types[] = Dynamic::TYPE_RECAPTCHA; - } + $receiverTypes = [ + 'to' => HelperInterface::MAIL_RECEIVER_TO, + 'cc' => HelperInterface::MAIL_RECEIVER_CC, + 'bcc' => HelperInterface::MAIL_RECEIVER_BCC, + ]; return $this->render( $this->getBundleName() . ':' . $this->getListName() . ':template.html.twig', [ 'types' => $this->getSortedTypes($types), 'widths' => $widths, + 'receiverTypes' => $receiverTypes, ] ); } @@ -212,13 +214,14 @@ public function getSortedTypes($types = []) { /** @var Translator $translator */ $translator = $this->get('translator'); + $locale = $this->getUser()->getLocale(); $sortedTypes = []; $returnTypes = []; $i = 0; foreach ($types as $alias => $type) { - $translation = $translator->trans($type->getConfiguration()->getTitle(), [], 'backend'); + $translation = $translator->trans($type->getConfiguration()->getTitle(), [], 'backend', $locale); $sortedTypes[$translation . $i] = ['alias' => $alias, 'type' => $type]; ++$i; } @@ -515,6 +518,7 @@ private function getApiEntity(Form $entity, $locale) 'sendAttachments' => $translation->getSendAttachments(), 'deactivateNotifyMails' => $translation->getDeactivateNotifyMails(), 'deactivateCustomerMails' => $translation->getDeactivateCustomerMails(), + 'receivers' => $translation->getReceivers(), ]; } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 32605a38..c15f4b4e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -22,10 +22,21 @@ public function getConfigTreeBuilder() $rootNode->children() ->scalarNode('mailchimp_api_key')->defaultValue(null)->end() - ->arrayNode('mail_helper') + ->arrayNode('mail') ->children() ->scalarNode('from')->end() ->scalarNode('to')->end() + ->arrayNode('templates') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('notify') + ->defaultValue('SuluFormBundle:mails:notify.html.twig') + ->end() + ->scalarNode('customer') + ->defaultValue('SuluFormBundle:mails:customer.html.twig') + ->end() + ->end() + ->end() ->end() ->end() ->arrayNode('ajax_templates') diff --git a/DependencyInjection/SuluFormExtension.php b/DependencyInjection/SuluFormExtension.php index 7dfe88dd..cfe4df97 100644 --- a/DependencyInjection/SuluFormExtension.php +++ b/DependencyInjection/SuluFormExtension.php @@ -47,8 +47,10 @@ public function load(array $configs, ContainerBuilder $container) $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $container->setParameter('sulu_form.mail.from', $config['mail_helper']['from']); - $container->setParameter('sulu_form.mail.to', $config['mail_helper']['to']); + $container->setParameter('sulu_form.mail.from', $config['mail']['from']); + $container->setParameter('sulu_form.mail.to', $config['mail']['to']); + $container->setParameter('sulu_form.mail.template.notify', $config['mail']['templates']['notify']); + $container->setParameter('sulu_form.mail.template.customer', $config['mail']['templates']['customer']); $container->setParameter('sulu_form.ajax_templates', $config['ajax_templates']); $container->setParameter('sulu_form.mailchimp_api_key', $config['mailchimp_api_key']); diff --git a/Entity/Dynamic.php b/Entity/Dynamic.php index c055a99e..bd56e2a6 100644 --- a/Entity/Dynamic.php +++ b/Entity/Dynamic.php @@ -237,6 +237,18 @@ public function __isset($name) * {@inheritdoc} */ public function __get($name) + { + return $this->getField($name); + } + + /** + * Returns the fields value identified by its name. + * + * @param string $name + * + * @return mixed + */ + public function getField($name) { if (property_exists($this, $name)) { if (in_array($name, self::$arrayTypes)) { diff --git a/Entity/FormTranslation.php b/Entity/FormTranslation.php index debb593e..128afba1 100644 --- a/Entity/FormTranslation.php +++ b/Entity/FormTranslation.php @@ -97,6 +97,11 @@ class FormTranslation implements AuditableInterface */ private $changed; + /** + * @var FormTranslationReceiver[] + */ + private $receivers; + /** * @param string $title * @@ -452,4 +457,24 @@ public function setChanged($changed) return $this; } + + /** + * Returns receivers. + * + * @return FormTranslationReceiver[] + */ + public function getReceivers() + { + return $this->receivers; + } + + /** + * Sets receivers. + * + * @param FormTranslationReceiver[] $receivers + */ + public function setReceivers($receivers) + { + $this->receivers = $receivers; + } } diff --git a/Entity/FormTranslationReceiver.php b/Entity/FormTranslationReceiver.php new file mode 100644 index 00000000..1275b307 --- /dev/null +++ b/Entity/FormTranslationReceiver.php @@ -0,0 +1,151 @@ +id; + } + + /** + * Sets id. + * + * @param int $id + * + * @return FormTranslationReceiver + */ + public function setId($id) + { + $this->id = $id; + + return $this; + } + + /** + * Returns type. + * + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * Sets type. + * + * @param string $type + * + * @return FormTranslationReceiver + */ + public function setType($type) + { + $this->type = $type; + + return $this; + } + + /** + * Returns email. + * + * @return string + */ + public function getEmail() + { + return $this->email; + } + + /** + * Sets email. + * + * @param string $email + * + * @return FormTranslationReceiver + */ + public function setEmail($email) + { + $this->email = $email; + + return $this; + } + + /** + * Returns name. + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Sets name. + * + * @param string $name + * + * @return FormTranslationReceiver + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Returns formTranslation. + * + * @return FormTranslation + */ + public function getFormTranslation() + { + return $this->formTranslation; + } + + /** + * Sets formTranslation. + * + * @param FormTranslation $formTranslation + * + * @return FormTranslationReceiver + */ + public function setFormTranslation($formTranslation) + { + $this->formTranslation = $formTranslation; + + return $this; + } +} diff --git a/Event/DynFormSavedEvent.php b/Event/DynFormSavedEvent.php index 09b76f70..df0072b4 100644 --- a/Event/DynFormSavedEvent.php +++ b/Event/DynFormSavedEvent.php @@ -23,7 +23,7 @@ class DynFormSavedEvent extends Event * DynFormSavedEvent constructor. * * @param array $data - * @param Dynamic $dynamic will be required in the future + * @param Dynamic $dynamic */ public function __construct($data, $dynamic = null) { @@ -44,7 +44,7 @@ public function getFormSelect() } /** - * Get data. + * Returns data. * * @return array */ @@ -54,7 +54,7 @@ public function getData() } /** - * Get dynamic. + * Returns dynamic. * * @return Dynamic|null */ diff --git a/Event/MailSubscriber.php b/Event/MailSubscriber.php new file mode 100644 index 00000000..3f92e076 --- /dev/null +++ b/Event/MailSubscriber.php @@ -0,0 +1,231 @@ +mailHelper = $mailHelper; + $this->templating = $templating; + $this->mediaManager = $mediaManager; + $this->storageManager = $storageManager; + $this->notifyTemplate = $notifyTemplate; + $this->customerTemplate = $customerTemplate; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() + { + return [ + DynFormSavedEvent::NAME => 'sendMails', + ]; + } + + /** + * Handles the mail delivery on saving a dynamic form. + * + * @param DynFormSavedEvent $event + */ + public function sendMails(DynFormSavedEvent $event) + { + $dynamic = $event->getDynamic(); + $form = $dynamic->form; + $formEntity = $form->serializeForLocale($dynamic->locale, $dynamic); + $translation = $form->getTranslation($dynamic->locale); + + if (!$translation->getDeactivateCustomerMails() && !empty($dynamic->email)) { + $customerMail = $this->templating->render($this->customerTemplate, ['formEntity' => $formEntity]); + $this->mailHelper->sendMail( + $translation->getSubject(), + $customerMail, + $dynamic->email, + $this->getFromAddress($translation), + true + ); + } + + $this->sendNotifyMail($dynamic, $form, $formEntity, $translation); + } + + /** + * Sends the notification mails. + * + * @param Dynamic $dynamic + * @param Form $form + * @param array $formEntity + * @param FormTranslation $translation + */ + protected function sendNotifyMail(Dynamic $dynamic, Form $form, $formEntity, FormTranslation $translation) + { + if (!$translation->getDeactivateNotifyMails()) { + $allReceivers = $this->mailHelper->getReceiverTypes(); + + // Add main receiver of form. + $mainReceiver = $this->getNotifyToMailAddress($translation); + if ($mainReceiver) { + $allReceivers[MailHelperInterface::MAIL_RECEIVER_TO] = $mainReceiver; + } + + // Add additional receivers. + foreach ($translation->getReceivers() as $receiver) { + if (!empty($receiver->getEmail())) { + $allReceivers[$receiver->getType()][$receiver->getEmail()] = $receiver->getName(); + } + } + + $attachedMediaIds = $this->getAttachedMediaIds($form, $dynamic); + $attachments = $this->loadAttachments($attachedMediaIds, $dynamic->locale); + + $notifyMail = $this->templating->render($this->notifyTemplate, ['formEntity' => $formEntity]); + $this->mailHelper->sendMail( + $translation->getSubject(), + $notifyMail, + $allReceivers[MailHelperInterface::MAIL_RECEIVER_TO], + $this->getFromAddress($translation), + true, + null, + $attachments, + $allReceivers[MailHelperInterface::MAIL_RECEIVER_CC], + $allReceivers[MailHelperInterface::MAIL_RECEIVER_BCC] + ); + } + } + + /** + * Returns the sender email address. + * + * @param FormTranslation $translation + * + * @return array|null + */ + protected function getFromAddress(FormTranslation $translation) + { + $fromMail = $translation->getFromEmail(); + $fromName = $translation->getFromName(); + + if (!$fromMail || !$fromName) { + return; + } + + return [$fromMail => $fromName]; + } + + /** + * Returns the main email address for sending notification. + * + * @param FormTranslation $translation + * + * @return string + */ + protected function getNotifyToMailAddress(FormTranslation $translation) + { + $toMail = $translation->getToEmail(); + $toName = $translation->getToName(); + + if (!$toMail || !$toName) { + return; + } + + return [$toMail => $toName]; + } + + /** + * Returns the ids of medias which was uploaded in the form. + * + * @param Form $formEntity + * @param Dynamic $dynamic + * + * @return array + */ + protected function getAttachedMediaIds(Form $formEntity, Dynamic $dynamic) + { + $mediaIds = []; + + foreach ($formEntity->getFields() as $field) { + if ($field->getType() === Dynamic::TYPE_ATTACHMENT) { + $mediaIds = array_merge($mediaIds, $dynamic->getField($field->getKey())); + } + } + + return $mediaIds; + } + + /** + * Returns the files which should be attached to the mail. + * + * @param array $attachedMediaIds + * @param string $locale + * + * @return \SplFileInfo[] + */ + protected function loadAttachments($attachedMediaIds, $locale) + { + $attachments = []; + + $medias = $this->mediaManager->getByIds($attachedMediaIds, $locale); + foreach ($medias as $media) { + $path = $this->storageManager->load($media->getName(), $media->getVersion(), $media->getStorageOptions()); + $attachments[] = new \SplFileInfo($path); + } + + return $attachments; + } +} diff --git a/Form/Type/DynamicFormType.php b/Form/Type/DynamicFormType.php index b430bb5d..d7d7c91e 100644 --- a/Form/Type/DynamicFormType.php +++ b/Form/Type/DynamicFormType.php @@ -133,6 +133,8 @@ public function getName() /** * {@inheritdoc} + * + * @deprecated */ public function getCustomerSubject($formData = []) { @@ -141,6 +143,8 @@ public function getCustomerSubject($formData = []) /** * {@inheritdoc} + * + * @deprecated */ public function getNotifySubject($formData = []) { @@ -149,6 +153,8 @@ public function getNotifySubject($formData = []) /** * {@inheritdoc} + * + * @deprecated */ public function getCustomerFromMailAddress($formData = []) { @@ -164,6 +170,8 @@ public function getCustomerFromMailAddress($formData = []) /** * {@inheritdoc} + * + * @deprecated */ public function getNotifyFromMailAddress($formData = []) { @@ -179,6 +187,8 @@ public function getNotifyFromMailAddress($formData = []) /** * {@inheritdoc} + * + * @deprecated */ public function getNotifyToMailAddress($formData = []) { @@ -194,6 +204,8 @@ public function getNotifyToMailAddress($formData = []) /** * {@inheritdoc} + * + * @deprecated */ public function getCustomerMail($formData = []) { @@ -202,6 +214,8 @@ public function getCustomerMail($formData = []) /** * {@inheritdoc} + * + * @deprecated */ public function getNotifyMail($formData = []) { @@ -210,6 +224,8 @@ public function getNotifyMail($formData = []) /** * {@inheritdoc} + * + * @deprecated */ public function getNotifySendAttachments($formData = []) { @@ -220,24 +236,32 @@ public function getNotifySendAttachments($formData = []) * @param $formData * * @return bool + * + * @deprecated */ public function getNotifyDeactivateMails($formData = []) { - return $this->getTranslation()->getDeactivateNotifyMails(); + // Deactivated because of using MailSubscriber service. + return true; } /** * @param $formData * * @return bool + * + * @deprecated */ public function getCustomerDeactivateMails($formData = []) { - return $this->getTranslation()->getDeactivateCustomerMails(); + // Deactivated because of using MailSubscriber service. + return true; } /** * {@inheritdoc} + * + * @deprecated */ public function getMailText($formData = []) { diff --git a/Mail/Helper.php b/Mail/Helper.php index ef1dd37a..b81c579d 100644 --- a/Mail/Helper.php +++ b/Mail/Helper.php @@ -56,7 +56,9 @@ public function sendMail( $fromMail = null, $html = true, $replyTo = null, - $attachments = [] + $attachments = [], + $ccMail = [], + $bccMail = [] ) { $message = new \Swift_Message( $subject, @@ -99,18 +101,37 @@ public function sendMail( $message->setReplyTo($replyTo); } + $message->setCc($ccMail); + $message->setBcc($bccMail); + $this->logger->info(sprintf( 'Try register mail from SuluFormBundle: ' . PHP_EOL . ' From: %s' . PHP_EOL . ' To: %s' . PHP_EOL . ' Reply to: %s' . PHP_EOL . - ' Subject: %s' . PHP_EOL, + ' Subject: %s' . PHP_EOL . + ' CC: %s' . PHP_EOL . + ' BCC: %s' . PHP_EOL, is_string($fromMail) ? $fromMail : serialize($fromMail), is_string($toMail) ? $toMail : serialize($toMail), is_string($replyTo) ? $replyTo : serialize($toMail), - is_string($subject) ? $subject : serialize($subject) + is_string($subject) ? $subject : serialize($subject), + is_string($ccMail) ? $ccMail : serialize($ccMail), + is_string($bccMail) ? $bccMail : serialize($bccMail) )); return $this->mailer->send($message); } + + /** + * {@inheritdoc} + */ + public function getReceiverTypes() + { + return [ + self::MAIL_RECEIVER_TO => [], + self::MAIL_RECEIVER_CC => [], + self::MAIL_RECEIVER_BCC => [], + ]; + } } diff --git a/Mail/HelperInterface.php b/Mail/HelperInterface.php index 5aa4301c..c3aad24b 100644 --- a/Mail/HelperInterface.php +++ b/Mail/HelperInterface.php @@ -4,14 +4,20 @@ interface HelperInterface { + const MAIL_RECEIVER_TO = 'to'; + const MAIL_RECEIVER_CC = 'cc'; + const MAIL_RECEIVER_BCC = 'bcc'; + /** * @param string $subject * @param string $body - * @param string $toMail + * @param string|array $toMail * @param string $fromMail * @param bool $html * @param string $replyTo * @param \SplFileInfo[] $attachments + * @param string|array $ccMail + * @param string|array $bccMail * * @return int */ @@ -22,6 +28,15 @@ public function sendMail( $fromMail = null, $html = true, $replyTo = null, - $attachments = [] + $attachments = [], + $ccMail = [], + $bccMail = [] ); + + /** + * Returns an array for holding receivers divided by types. + * + * @return array + */ + public function getReceiverTypes(); } diff --git a/Mail/NullHelper.php b/Mail/NullHelper.php index 5b8f94f2..897d1fe5 100644 --- a/Mail/NullHelper.php +++ b/Mail/NullHelper.php @@ -31,18 +31,24 @@ public function sendMail( $fromMail = null, $html = true, $replyTo = null, - $attachments = [] + $attachments = [], + $ccMail = [], + $bccMail = [] ) { $this->logger->info(sprintf( 'SuluFormBundle NullMailHelper: ' . PHP_EOL . ' From: %s' . PHP_EOL . ' To: %s' . PHP_EOL . ' Reply to: %s' . PHP_EOL . - ' Subject: %s' . PHP_EOL, + ' Subject: %s' . PHP_EOL . + ' CC: %s' . PHP_EOL . + ' BCC: %s' . PHP_EOL, is_string($fromMail) ? $fromMail : serialize($fromMail), is_string($toMail) ? $toMail : serialize($toMail), is_string($replyTo) ? $replyTo : serialize($toMail), - is_string($subject) ? $subject : serialize($subject) + is_string($subject) ? $subject : serialize($subject), + is_string($ccMail) ? $ccMail : serialize($ccMail), + is_string($bccMail) ? $bccMail : serialize($bccMail) )); } } diff --git a/Manager/FormManager.php b/Manager/FormManager.php index 6fbd4978..d8e2cea1 100644 --- a/Manager/FormManager.php +++ b/Manager/FormManager.php @@ -5,6 +5,8 @@ use Doctrine\ORM\EntityManagerInterface; use Sulu\Bundle\FormBundle\Entity\Form; use Sulu\Bundle\FormBundle\Entity\FormField; +use Sulu\Bundle\FormBundle\Entity\FormTranslation; +use Sulu\Bundle\FormBundle\Entity\FormTranslationReceiver; use Sulu\Bundle\FormBundle\Repository\FormRepository; /** @@ -80,7 +82,7 @@ public function save($data, $locale = null, $id = null) { $form = new Form(); - // find exist or create new entity + // Find exist or create new entity. if ($id) { $form = $this->findById($id, $locale); } @@ -100,18 +102,92 @@ public function save($data, $locale = null, $id = null) $translation->setDeactivateCustomerMails(self::getValue($data, 'deactivateCustomerMails')); $translation->setChanged(new \DateTime()); - // Add Translation to Form + // Add Translation to Form. if (!$translation->getId()) { $translation->setForm($form); $form->addTranslation($translation); } - // Set Default Locale + // Set Default Locale. if (!$form->getId()) { $form->setDefaultLocale($locale); } - // Fields + // Update field of form and the receivers. + $this->updateFields($data, $form, $locale); + $this->updateReceivers($data, $translation); + + $this->entityManager->persist($form); + $this->entityManager->flush(); + + if (!$id) { + // To avoid lazy load of sub entities in the serializer reload whole object with sub entities from db + // remove this when you don`t join anything in `findById`. + $form = $this->findById($form->getId(), $locale); + } + + return $form; + } + + /** + * @param int $id + * @param string $locale + * + * @return Form|null + */ + public function delete($id, $locale = null) + { + $object = $this->findById($id, $locale); + + if (!$object) { + return; + } + + $this->entityManager->remove($object); + $this->entityManager->flush(); + + return $object; + } + + /** + * @param array $data + * @param FormTranslation $translation + */ + public function updateReceivers($data, $translation) + { + $translation->setReceivers([]); + $receiversRepository = $this->entityManager->getRepository('SuluFormBundle:FormTranslationReceiver'); + $receiverDatas = self::getValue($data, 'receivers', []); + + // Remove old receivers. + $oldReceivers = $receiversRepository->findBy(['formTranslation' => $translation]); + foreach ($oldReceivers as $oldReceiver) { + $this->entityManager->remove($oldReceiver); + } + + $receivers = []; + foreach ($receiverDatas as $receiverData) { + $receiver = new FormTranslationReceiver(); + $receiver->setType($receiverData['type']); + $receiver->setEmail($receiverData['email']); + $receiver->setName($receiverData['name']); + $receiver->setFormTranslation($translation); + + $receivers[] = $receiver; + $this->entityManager->persist($receiver); + } + $translation->setReceivers($receivers); + } + + /** + * Updates the contained fields in the form. + * + * @param array $data + * @param Form $form + * @param string $locale + */ + protected function updateFields($data, $form, $locale) + { $reservedKeys = array_column(self::getValue($data, 'fields', []), 'key'); $counter = 0; @@ -179,38 +255,6 @@ public function save($data, $locale = null, $id = null) $form->removeField($deletedField); $this->entityManager->remove($deletedField); } - - // Save - $this->entityManager->persist($form); - $this->entityManager->flush(); - - if (!$id) { - // to avoid lazy load of sub entities in the serializer reload whole object with sub entities from db - // remove this when you don`t join anything in `findById` - $form = $this->findById($form->getId(), $locale); - } - - return $form; - } - - /** - * @param int $id - * @param string $locale - * - * @return Form|null - */ - public function delete($id, $locale = null) - { - $object = $this->findById($id, $locale); - - if (!$object) { - return; - } - - $this->entityManager->remove($object); - $this->entityManager->flush(); - - return $object; } /** diff --git a/Repository/FormRepository.php b/Repository/FormRepository.php index d1e2f322..669200b3 100644 --- a/Repository/FormRepository.php +++ b/Repository/FormRepository.php @@ -17,7 +17,8 @@ public function findById($id, $locale = null) $queryBuilder = $this->createQueryBuilder('form') ->leftJoin('form.translations', 'translation')->addSelect('translation') ->leftJoin('form.fields', 'field')->addSelect('field') - ->leftJoin('field.translations', 'fieldTranslation')->addSelect('fieldTranslation'); + ->leftJoin('field.translations', 'fieldTranslation')->addSelect('fieldTranslation') + ->leftJoin('translation.receivers', 'receiver')->addSelect('receiver'); $queryBuilder->where($queryBuilder->expr()->eq('form.id', ':id')); $queryBuilder->setParameter('id', $id); diff --git a/Resources/config/doctrine/FormField.orm.xml b/Resources/config/doctrine/FormField.orm.xml index d2c2bd7b..ccfdecae 100644 --- a/Resources/config/doctrine/FormField.orm.xml +++ b/Resources/config/doctrine/FormField.orm.xml @@ -26,9 +26,6 @@ - - - diff --git a/Resources/config/doctrine/FormFieldTranslation.orm.xml b/Resources/config/doctrine/FormFieldTranslation.orm.xml index 7a79f863..e2e9a636 100644 --- a/Resources/config/doctrine/FormFieldTranslation.orm.xml +++ b/Resources/config/doctrine/FormFieldTranslation.orm.xml @@ -18,9 +18,6 @@ - - - diff --git a/Resources/config/doctrine/FormTranslation.orm.xml b/Resources/config/doctrine/FormTranslation.orm.xml index dd137206..9b06b384 100644 --- a/Resources/config/doctrine/FormTranslation.orm.xml +++ b/Resources/config/doctrine/FormTranslation.orm.xml @@ -38,12 +38,17 @@ - - - + + + + + + diff --git a/Resources/config/doctrine/FormTranslationReceiver.orm.xml b/Resources/config/doctrine/FormTranslationReceiver.orm.xml new file mode 100644 index 00000000..2ebcba7a --- /dev/null +++ b/Resources/config/doctrine/FormTranslationReceiver.orm.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 0e6ce64d..a6220200 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -81,5 +81,15 @@ + + + + + + + %sulu_form.mail.template.notify% + %sulu_form.mail.template.customer% + + diff --git a/Resources/doc/dynamic.md b/Resources/doc/dynamic.md index 2fb17f49..9db949e6 100644 --- a/Resources/doc/dynamic.md +++ b/Resources/doc/dynamic.md @@ -119,35 +119,20 @@ Please check to following issue before using it in production: To create a dynamic form (which is selectable in the property type `form_select`) simply click on the magic icon in the Sulu backend navigation and create a new form. -## Emails +## Custom emails -Create the emails template in the a folder . +For customizing the notification mail and the customer confirmation mail, adding the following lines to the configuration: -form-notify.html.twig - -```twig -{% for field in formEntity.fields|default([]) %} - {% set value = field.value %} - - {# get formatted value #} - {% if value is iterable %} - {% set value = value|json_encode %} - {% elseif value.timestamp is defined %} - {% set value = value|date('d.m.Y') %} - {% endif %} - - {% if value is not empty %} - {{ field.shortTitle|default(field.title)|raw }}: {{ value }}
- {% endif %} -{% endfor %} ``` - -form-notify.html.twig - -```twig -{{ formEntity.mailText|default('')|raw }} +sulu_form: + mail: + templates: + notify: 'AppBundle:mails:dynamic-notify.html.twig' + customer: 'AppBundle:mails:dynamic-customer.html.twig' ``` +Examples for the notification and costumer mail you can find [here](https://github.com/sulu/SuluFormBundle/tree/master/Resources/views/mails/). + ## List Tab - Export To visualise a tab in the Sulu template, simply add following lines to your Bundles `services.xml`: diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 00ec636c..e1ca6844 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -32,9 +32,9 @@ framework: esi: { enabled: true } # use to reload csrf token sulu_form: - mail_helper: - from: %parameter_recommended_for_from% - to: %parameter_recommended_for_to% + mail: + from: %sulu_admin.email% + to: %sulu_admin.email% ``` ## Create Database diff --git a/Resources/translations/sulu/backend.de.xlf b/Resources/translations/sulu/backend.de.xlf index e0053800..dd9483db 100644 --- a/Resources/translations/sulu/backend.de.xlf +++ b/Resources/translations/sulu/backend.de.xlf @@ -292,6 +292,24 @@ sulu_form.attachment_max Maximale Anzahl + + + + sulu_form.receivers + Empfänger + + + sulu_form.receiver.to + An + + + sulu_form.receiver.cc + CC + + + sulu_form.receiver.bcc + BCC + diff --git a/Resources/translations/sulu/backend.en.xlf b/Resources/translations/sulu/backend.en.xlf index bd84c098..938af6f3 100644 --- a/Resources/translations/sulu/backend.en.xlf +++ b/Resources/translations/sulu/backend.en.xlf @@ -292,6 +292,24 @@ sulu_form.attachment_max Maximale amount + + + + sulu_form.receivers + Receivers + + + sulu_form.receiver.to + To + + + sulu_form.receiver.cc + CC + + + sulu_form.receiver.bcc + BCC + diff --git a/Resources/translations/sulu/backend.fr.xlf b/Resources/translations/sulu/backend.fr.xlf index d8532526..5dd90490 100644 --- a/Resources/translations/sulu/backend.fr.xlf +++ b/Resources/translations/sulu/backend.fr.xlf @@ -291,6 +291,24 @@ sulu_form.attachment_max Maximale amount + + + + sulu_form.receivers + Récepteur + + + sulu_form.receiver.to + À + + + sulu_form.receiver.cc + CC + + + sulu_form.receiver.bcc + BCC + diff --git a/Resources/views/forms/blocks/email-receiver.html.twig b/Resources/views/forms/blocks/email-receiver.html.twig new file mode 100644 index 00000000..28e769ec --- /dev/null +++ b/Resources/views/forms/blocks/email-receiver.html.twig @@ -0,0 +1,69 @@ + diff --git a/Resources/views/forms/template.html.twig b/Resources/views/forms/template.html.twig index 5fcf2b60..f39c29eb 100644 --- a/Resources/views/forms/template.html.twig +++ b/Resources/views/forms/template.html.twig @@ -17,7 +17,6 @@ class="form-element input-bold husky-validate" data-mapper-property="title" data-validation-required="true"> - @@ -27,6 +26,78 @@
+
+
+
+
+

+ <%= translate('sulu_form.form_fields') %> +

+ + {% set blockId = 'fields' %} + +
+
{#class="text-blocks-header"#} +
+ <%= translate('sulu.content.collapse.blocks') %> +
+
+ <%= translate('sulu.content.expand.blocks') %> +
+
+
+ + + +
", + "tpl": "{{ alias }}-{{ blockId }}-tpl" + } + {% if not loop.last %},{% endif %} + {% endfor %} + ]' + data-type-min="0" + data-type-max="999" + data-type-default="{{ types|keys|first }}" + data-mapper-property="{{ blockId }}" + data-mapper-full-class="full" + data-mapper-empty-class="empty"> + + {% for alias, type in types %} + {% include type.configuration.template with { + 'viewData': type.configuration.attributes, + 'alias': alias + }%} + {% endfor %} +
+ + +
+ +
+
+

<%= translate('sulu_form.website_configuration') %> @@ -230,75 +301,51 @@

-
-
-
-
-

- <%= translate('sulu_form.form_fields') %> -

- - {% set blockId = 'fields' %} - -
-
{#class="text-blocks-header"#} -
- <%= translate('sulu.content.collapse.blocks') %> -
-
- <%= translate('sulu.content.expand.blocks') %> -
-
-
- - - -
", - "tpl": "{{ alias }}-{{ blockId }}-tpl" - }{% if not loop.last %},{% endif %} - {% endfor %} - ]' - data-type-min="0" - data-type-max="999" - data-type-default="{{ types|keys|first }}" - data-mapper-property="{{ blockId }}" - data-mapper-full-class="full" - data-mapper-empty-class="empty"> + +
+

+ <%= translate('sulu_form.receivers') %> +

- {% for alias, type in types %} - {% include type.configuration.template with { - 'viewData': type.configuration.attributes, - 'alias': alias - }%} - {% endfor %} -
+
+
+
<%= + translate('sulu.content.collapse.blocks') %> +
+
<%= + translate('sulu.content.expand.blocks') %> +
+
+
+
", "tpl": "to-receivers-tpl"},{"data": "cc", "title": "<%= translate('sulu_form.receiver.cc') %>", "tpl": "cc-receivers-tpl"},{"data": "bcc", "title": "<%= translate('sulu_form.receiver.bcc') %>", "tpl": "bcc-receivers-tpl"}]' + data-type-min="0" + data-type-max="999" + data-type-default="to" + data-mapper-property="receivers" + data-mapper-full-class="full" + data-mapper-empty-class="empty"> - -
-
+
+ +
diff --git a/Resources/views/mails/customer.html.twig b/Resources/views/mails/customer.html.twig new file mode 100644 index 00000000..0191ac20 --- /dev/null +++ b/Resources/views/mails/customer.html.twig @@ -0,0 +1 @@ +{{ formEntity.mailText|default('')|raw }} diff --git a/Resources/views/mails/notify.html.twig b/Resources/views/mails/notify.html.twig new file mode 100644 index 00000000..2299d43a --- /dev/null +++ b/Resources/views/mails/notify.html.twig @@ -0,0 +1,25 @@ +{% for field in formEntity.fields|default([]) %} + {% set value = field.value %} + + {# get formatted value. #} + {% if value is iterable %} + {% if field.type == 'attachment' %} + {% set mediaUrls = [] %} + {% for mediaId in field.value %} + {% set media = sulu_resolve_media(mediaId, app.request.locale) %} + {% set mediaUrls = mediaUrls|merge(['' ~ media.title ~ '']) %} + {% endfor %} + {% set value %} + {{ mediaUrls|join(', ')|raw }} + {% endset %} + {% else %} + {% set value = value|json_encode %} + {% endif %} + {% elseif value.timestamp is defined %} + {% set value = value|date('d.m.Y') %} + {% endif %} + + {% if value is not empty %} + {{ field.shortTitle|default(field.title)|raw }}: {{ value }}
+ {% endif %} +{% endfor %}