Files
myep-team/src/Controller/Administrative/System/MailingTemplate/EditController.php
T

86 lines
3.4 KiB
PHP

<?php
namespace App\Controller\Administrative\System\MailingTemplate;
use App\Controller\Traits\AuthenticatedUserTrait;
use App\Entity\MailingTemplate;
use App\Entity\Upload;
use App\Form\MailingTemplateType;
use App\Service\Teamer\MailingAttachmentHandler;
use App\Service\Teamer\TeamerMailingService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class EditController extends AbstractController
{
use AuthenticatedUserTrait;
public function __construct(
private readonly MailingAttachmentHandler $attachmentHandler,
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger,
) {
}
#[Route('/administrative/system/mailing-template/edit/{uuid}', name: 'app_administrative_system_mailing_template_edit')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(
#[MapEntity(mapping: ['uuid' => 'uuid'])]
MailingTemplate $template,
Request $request,
): Response {
if (true === $template->isDeleted()) {
throw $this->createNotFoundException('Die Mailvorlage wurde gelöscht');
}
$form = $this->createForm(MailingTemplateType::class, $template);
$form->handleRequest($request);
// files left over from a template form that was never saved must not end up on this one
if (false === $form->isSubmitted()) {
$this->attachmentHandler->discardPending(Upload::TYPE_MAILING_TEMPLATE);
}
/** @var Upload[] $remove */
$remove = $form->has('removeAttachments')
? iterator_to_array($form->get('removeAttachments')->getData() ?? [], false)
: [];
if ($form->isSubmitted() && $form->isValid()) {
if (null !== $error = $this->attachmentHandler->validateTemplateAttachments($template, $remove)) {
$form->addError(new FormError($error));
}
}
if ($form->isSubmitted() && $form->isValid()) {
$this->attachmentHandler->saveTemplateAttachments($template, $remove, $this->getAuthenticatedUser());
$this->entityManager->flush();
$this->addFlash('success', 'Die Mailvorlage wurde aktualisiert');
$this->logger->info('Edit mailing template', [
'mailing_template_id' => $template->getId(),
'mailing_template_name' => $template->getName(),
'attachments' => $template->getAttachments()->count(),
'attachments_removed' => count($remove),
]);
return $this->redirectToRoute('app_administrative_system_mailing_template_index');
}
return $this->render('administrative/system/mailing_template/edit.html.twig', [
'form' => $form->createView(),
'template' => $template,
'pending' => $this->attachmentHandler->getPending(Upload::TYPE_MAILING_TEMPLATE),
'placeholders' => TeamerMailingService::PLACEHOLDERS,
'maxAttachmentBytes' => MailingAttachmentHandler::MAX_TOTAL_BYTES,
]);
}
}