100 lines
3.7 KiB
PHP
100 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Administrative\AssignmentMailing;
|
|
|
|
use App\Controller\Traits\AuthenticatedUserTrait;
|
|
use App\Email\MailBodyRenderer;
|
|
use App\Entity\Upload;
|
|
use App\Form\AssignmentMailingAddType;
|
|
use App\Form\AssignmentMailingType;
|
|
use App\Model\AssignmentMailingDto;
|
|
use App\Service\Teamer\AssignmentMailingDraftHandler;
|
|
use App\Service\Teamer\AssignmentMailingService;
|
|
use App\Service\Teamer\MailingAttachmentHandler;
|
|
use App\Service\Teamer\TeamerMailingService;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
/**
|
|
* What the actions of the assignment mailing share: the draft, the form over its wording and the
|
|
* pieces of the compose page that htmx swaps on their own.
|
|
*/
|
|
abstract class AbstractAssignmentMailingController extends AbstractController
|
|
{
|
|
use AuthenticatedUserTrait;
|
|
|
|
protected AssignmentMailingDraftHandler $draftHandler;
|
|
protected AssignmentMailingService $mailingService;
|
|
protected MailingAttachmentHandler $attachmentHandler;
|
|
protected TeamerMailingService $teamerMailingService;
|
|
protected MailBodyRenderer $mailBodyRenderer;
|
|
|
|
#[Required]
|
|
public function setMailingDependencies(
|
|
AssignmentMailingDraftHandler $draftHandler,
|
|
AssignmentMailingService $mailingService,
|
|
MailingAttachmentHandler $attachmentHandler,
|
|
TeamerMailingService $teamerMailingService,
|
|
MailBodyRenderer $mailBodyRenderer,
|
|
): void {
|
|
$this->draftHandler = $draftHandler;
|
|
$this->mailingService = $mailingService;
|
|
$this->attachmentHandler = $attachmentHandler;
|
|
$this->teamerMailingService = $teamerMailingService;
|
|
$this->mailBodyRenderer = $mailBodyRenderer;
|
|
}
|
|
|
|
/**
|
|
* The form over the draft: a GET shows the parked wording, a POST carries the current one.
|
|
* Assignments and template stay what the draft says, the form has no fields for them.
|
|
*/
|
|
protected function createMailingForm(Request $request, ?AssignmentMailingDto $mailingDto = null): FormInterface
|
|
{
|
|
$form = $this->createForm(AssignmentMailingType::class, $mailingDto ?? $this->draftHandler->getDraft());
|
|
$form->handleRequest($request);
|
|
|
|
return $form;
|
|
}
|
|
|
|
/**
|
|
* What the composing user would receive, see the teamer mailing's renderPreview().
|
|
*
|
|
* @return array{subject: string, bodyHtml: string}
|
|
*/
|
|
protected function renderPreview(AssignmentMailingDto $mailingDto): array
|
|
{
|
|
$values = $this->teamerMailingService->placeholderValuesForUser($this->getAuthenticatedUser());
|
|
|
|
return [
|
|
'subject' => $this->teamerMailingService->render($mailingDto->getSubject(), $values),
|
|
'bodyHtml' => $this->mailBodyRenderer->render($mailingDto->getMessage(), $values),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* For administrative/assignment_mailing/_assignments.html.twig.
|
|
*/
|
|
protected function assignmentsContext(AssignmentMailingDto $mailingDto): array
|
|
{
|
|
return [
|
|
'mailingDto' => $mailingDto,
|
|
'recipients' => $this->mailingService->resolveRecipients($mailingDto),
|
|
'addForm' => $this->createForm(AssignmentMailingAddType::class)->createView(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* For administrative/assignment_mailing/_attachments.html.twig.
|
|
*/
|
|
protected function attachmentsContext(AssignmentMailingDto $mailingDto): array
|
|
{
|
|
return [
|
|
'mailingDto' => $mailingDto,
|
|
'pending' => $this->attachmentHandler->getPending(Upload::TYPE_MAILING_ATTACHMENT),
|
|
'maxAttachmentBytes' => MailingAttachmentHandler::MAX_TOTAL_BYTES,
|
|
];
|
|
}
|
|
}
|