Skip to content

Commit

Permalink
fix: Attach documents to custom-form notification emails
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jul 20, 2022
1 parent 9f1c8ed commit c213e87
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Controller/CustomFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
use Limenius\Liform\LiformInterface;
use Psr\Log\LoggerInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\Core\Models\DocumentInterface;
use RZ\Roadiz\CoreBundle\Bag\Settings;
use RZ\Roadiz\CoreBundle\CustomForm\CustomFormHelperFactory;
use RZ\Roadiz\CoreBundle\Entity\CustomForm;
use RZ\Roadiz\CoreBundle\Entity\CustomFormAnswer;
use RZ\Roadiz\CoreBundle\Exception\EntityAlreadyExistsException;
use RZ\Roadiz\CoreBundle\Form\Error\FormErrorSerializerInterface;
use RZ\Roadiz\CoreBundle\Mailer\EmailManager;
use RZ\Roadiz\Utils\Asset\Packages;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -45,6 +49,7 @@ final class CustomFormController extends AbstractController
private FormErrorSerializerInterface $formErrorSerializer;
private ManagerRegistry $registry;
private RateLimiterFactory $customFormLimiter;
private Packages $packages;

public function __construct(
EmailManager $emailManager,
Expand All @@ -56,7 +61,8 @@ public function __construct(
SerializerInterface $serializer,
FormErrorSerializerInterface $formErrorSerializer,
ManagerRegistry $registry,
RateLimiterFactory $customFormLimiter
RateLimiterFactory $customFormLimiter,
Packages $packages
) {
$this->emailManager = $emailManager;
$this->settingsBag = $settingsBag;
Expand All @@ -68,6 +74,7 @@ public function __construct(
$this->formErrorSerializer = $formErrorSerializer;
$this->registry = $registry;
$this->customFormLimiter = $customFormLimiter;
$this->packages = $packages;
}

protected function getTranslation(string $_locale = 'fr'): TranslationInterface
Expand Down Expand Up @@ -238,12 +245,14 @@ public function sentAction(Request $request, int $customFormId): Response
/**
* Send an answer form by Email.
*
* @param CustomFormAnswer $answer
* @param array $assignation
* @param string|array|null $receiver
* @return bool
* @throws Exception
*/
public function sendAnswer(
CustomFormAnswer $answer,
array $assignation,
$receiver
): bool {
Expand All @@ -255,6 +264,15 @@ public function sendAnswer(
$this->emailManager->setSubject($assignation['title']);
$this->emailManager->setEmailTitle($assignation['title']);
$this->emailManager->setSender($defaultSender);
$files = [];

foreach ($answer->getAnswers() as $customFormAnswerAttr) {
$files = array_merge($files, $customFormAnswerAttr->getDocuments()->map(function (DocumentInterface $document) {
return new File($this->packages->getDocumentFilePath($document));
})->toArray());
}

$this->emailManager->setFiles($files);

if (empty($receiver)) {
$this->emailManager->setReceiver($defaultSender);
Expand Down Expand Up @@ -355,6 +373,7 @@ public function prepareAndHandleCustomFormAssignation(
return new Address($email);
}, $receiver);
$this->sendAnswer(
$answer,
[
'mailContact' => $assignation['mailContact'],
'fields' => $assignation["emailFields"],
Expand Down
28 changes: 28 additions & 0 deletions src/Mailer/EmailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RZ\Roadiz\Core\Models\DocumentInterface;
use RZ\Roadiz\CoreBundle\Bag\Settings;
use RZ\Roadiz\Utils\UrlGenerators\DocumentUrlGeneratorInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mailer\MailerInterface;
Expand Down Expand Up @@ -42,6 +43,8 @@ class EmailManager
protected ?Email $message;
protected ?Settings $settingsBag;
protected ?DocumentUrlGeneratorInterface $documentUrlGenerator;
/** @var File[] */
protected array $files = [];

/**
* @param RequestStack $requestStack
Expand Down Expand Up @@ -138,6 +141,9 @@ public function appendWebsiteIcon()

/**
* @return Email
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function createMessage(): Email
{
Expand Down Expand Up @@ -184,6 +190,10 @@ public function send()
$this->message = $this->createMessage();
}

foreach ($this->files as $file) {
$this->message->attachFromPath($file->getRealPath(), $file->getFilename());
}

// Send the message
$this->mailer->send($this->message);
}
Expand Down Expand Up @@ -552,4 +562,22 @@ public function setEmailType(?string $emailType)
$this->emailType = $emailType;
return $this;
}

/**
* @return File[]
*/
public function getFiles(): array
{
return $this->files;
}

/**
* @param File[] $files
* @return EmailManager
*/
public function setFiles(array $files): EmailManager
{
$this->files = $files;
return $this;
}
}

0 comments on commit c213e87

Please sign in to comment.