createMailingForm($request); if ($form->isSubmitted()) { $this->draftHandler->saveDraft($form->getData()); } if ($form->isSubmitted() && $form->isValid()) { $this->mailingService->sendPreview($form->getData(), $this->getUser()); $this->addFlash('success', sprintf( 'Die Vorschau wurde an %s gesendet', $this->getUser()->getUserIdentifier() )); } $filterDto = $this->filterHandler->getFilterSettings(); // no redirect after the preview, the composed mail has to survive it return $this->render('admin/teamer/mailing.html.twig', [ 'form' => $form->createView(), 'filterDto' => $filterDto, 'recipients' => $this->mailingService->resolveRecipients($filterDto), 'placeholders' => TeamerMailingService::PLACEHOLDERS, 'hasDraft' => $this->draftHandler->hasDraft(), 'returnUrl' => $this->getReturnUrl($request, 'app_administrative_teamer_index'), ]); } /** * Park what has been typed so far, so that a trip to the filter and back does not * throw the composed mail away. Answers nothing, the page stays as it is. */ #[Route('/admin/teamer/mailing/draft', name: 'app_admin_teamer_mailing_draft', methods: ['POST'])] #[IsGranted('ROLE_ADMIN')] public function draft(Request $request): Response { $this->draftHandler->saveDraft($this->createMailingForm($request)->getData()); return new Response(null, Response::HTTP_NO_CONTENT); } #[Route('/admin/teamer/mailing/discard', name: 'app_admin_teamer_mailing_discard')] #[IsGranted('ROLE_ADMIN')] public function discard(): Response { $this->draftHandler->resetDraft(); return $this->redirectToRoute('app_admin_teamer_mailing'); } #[Route('/admin/teamer/mailing/confirm', name: 'app_admin_teamer_mailing_confirm', methods: ['POST'])] #[IsGranted('ROLE_ADMIN')] public function confirm(Request $request): Response { $form = $this->createMailingForm($request); $filterDto = $this->filterHandler->getFilterSettings(); // the composed mail is carried on in hidden fields so no half written mailing has // to be parked in the session between the confirmation and the send return $this->render('admin/teamer/modal_mailing_confirm.html.twig', [ 'form' => $form->createView(), 'mailingDto' => $form->getData(), 'valid' => $form->isSubmitted() && $form->isValid(), 'recipients' => $this->mailingService->resolveRecipients($filterDto), ]); } #[Route('/admin/teamer/mailing/send', name: 'app_admin_teamer_mailing_send', methods: ['POST'])] #[IsGranted('ROLE_ADMIN')] public function send(Request $request): Response { $form = $this->createMailingForm($request); if (false === $form->isSubmitted() || false === $form->isValid()) { $this->addFlash('error', 'Die Mail konnte nicht gesendet werden, Betreff oder Nachricht fehlen'); return new HxRedirectResponse($this->generateUrl('app_admin_teamer_mailing')); } $recipients = $this->mailingService->resolveRecipients($this->filterHandler->getFilterSettings()); $count = $this->mailingService->send($form->getData(), $recipients); // it went out, the next mailing starts on a blank page $this->draftHandler->resetDraft(); $this->addFlash('success', sprintf('Die Mail wurde an %d Teamer:innen gesendet', $count)); return new HxRedirectResponse($this->generateUrl('app_administrative_teamer_index')); } /** * A GET starts from the parked draft, a POST always carries the current one itself. */ private function createMailingForm(Request $request): FormInterface { $form = $this->createForm(TeamerMailingType::class, $this->draftHandler->getDraft()); $form->handleRequest($request); return $form; } }