← Back
Editing: ReportController.php
<?php namespace App\Controller; use App\Entity\Report; use App\Entity\ReportMail; use App\Enum\ReportStatusType; use App\Enum\ReportType; use App\Form\ReportDetailManagerType; use App\Form\ReportFilterType; use App\Report\Mail\Enum\MailTemplateType; use App\Report\Mail\ReportMailFetcher; use App\Report\Mail\ReportMailInterface; use App\Report\ReportExporter; use App\Repository\ReportItemRepository; use App\Repository\ReportRepository; use Doctrine\ORM\EntityManagerInterface; use Knp\Component\Pager\PaginatorInterface; use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\SerializerInterface; /** * Controller for manager report. */ #[Route(path: 'report', name: 'report_')] class ReportController extends AbstractController { public function __construct( private readonly ReportRepository $reportRepository, private readonly EntityManagerInterface $em ) { } #[Route(path: '/mail/{id}/template/{mailTemplateType}', name: 'mail_template')] public function emailPreview( Report $report, MailTemplateType $mailTemplateType, ReportMailFetcher $fetcher ): Response { $reportMail = ($fetcher)($mailTemplateType); return $this->render($reportMail->getTemplate(), ['report' => $report]); } /** * @param iterable<ReportMailInterface> $reportMailTemplates */ #[IsGranted('ROLE_MANAGER')] #[Route(path: '/{type}/{id}/{reportMail}/mail.html', name: 'mail_with_id', requirements: ['id' => '\d+', 'reportMail' => '\d*'])] #[Route(path: '/{type}/{id}/mail.html', name: 'mail', requirements: ['id' => '\d+'])] public function mail( ReportType $type, Report $report, iterable $reportMailTemplates, SerializerInterface $serializer, ?ReportMail $reportMail = null ): Response { $templates = []; foreach ($reportMailTemplates as $reportMailTemplate) { if ($reportMailTemplate->getReportType() === $type) { $templates[] = $reportMailTemplate; } } return $this->render('report/mail.html.twig', [ 'report' => $report, 'reportType' => $type, 'reportMail' => $serializer->serialize($reportMail, 'json', ['groups' => 'show_report_mail']), 'mailTemplates' => $serializer->serialize($templates, 'json'), ]); } #[IsGranted('ROLE_MANAGER')] #[Route(path: '/{type}/{page}', name: 'index', requirements: [ 'id' => '\d+', 'page' => '\d+', ], defaults: ['page' => 1])] public function index( string $type, PaginatorInterface $paginator, Request $request, ReportItemRepository $reportItemRepository, int $page = 1 ): Response { $reportType = ReportType::VALUES[$type]; $filter = $this->createForm(type: ReportFilterType::class, options: ['type' => $reportType]); $filter->handleRequest($request); $history = $this->reportRepository->countByMonth( reportType: $reportType, filter: $filter, manager: $this->getUser() ); $items = $reportItemRepository->countReportWithFilter( reportType: $reportType, filter: $filter, manager: $this->getUser() ); $pagination = $paginator->paginate( $this->reportRepository->getReportsQueryBuilder($this->getUser(), $filter, $reportType), $page, 25 ); $pagination->setCustomParameters(['align' => 'center']); return $this->render( 'report/index.html.twig', [ 'filter' => $filter->createView(), 'pagination' => $pagination, 'reportType' => $reportType, 'items' => $items, 'history' => $history, ] ); } #[IsGranted('ROLE_MANAGER')] #[Route(path: '/{type}/{id}/detail', name: 'detail', requirements: ['id' => '\d+'], options: ['expose' => true])] public function detail($type, Report $report): Response { $reportType = ReportType::VALUES[$type]; if ($report->getManager() != $this->getUser()) { $this->addFlash('danger', 'Vous n\'avez accès à ce signalement.'); return $this->redirectToRoute('report_index'); } $form = $this->createForm(ReportDetailManagerType::class, $report, [ 'type' => $reportType, ]); return $this->render('report/detail.html.twig', [ 'reportType' => $reportType, 'form' => $form->createView(), 'report' => $report, 'statusIncoming' => ReportStatusType::REPORT_STATUS_INCOMING->value, 'statusInProgress' => ReportStatusType::REPORT_STATUS_IN_PROGRESS->value, 'statusClosed' => ReportStatusType::REPORT_STATUS_CLOSED->value, ]); } #[IsGranted('ROLE_MANAGER')] #[Route(path: '/{type}/{id}/confirm', name: 'confirm', methods: ['POST'])] public function confirm($type, Report $report, Request $request): Response { $reportType = ReportType::VALUES[$type]; if ($report->getManager() != $this->getUser()) { $this->addFlash('danger', 'Vous n\'avez pas le droit de confirmer ce signalement.'); return $this->redirectToRoute('report_index'); } $report->setConfirmedAt(new \DateTime()); $form = $this->createForm(ReportDetailManagerType::class, $report, ['type' => $reportType]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $report->setStatus(ReportStatusType::REPORT_STATUS_IN_PROGRESS); $this->em->flush(); $this->addFlash('success', 'Le signalement a bien été confirmé.'); } return $this->redirectToRoute('report_detail', [ 'id' => $report->getId(), 'type' => $type, ]); } #[IsGranted('ROLE_MANAGER')] #[Route(path: '/{type}/{id}/close', name: 'close', requirements: ['id' => '\d+'])] public function close($type, Report $report): Response { if ($report->getManager() != $this->getUser()) { $this->addFlash('danger', 'Vous n\'avez pas le droit de confirmer ce signalement.'); return $this->redirectToRoute('report_index'); } $close = true; if (ReportStatusType::REPORT_STATUS_CLOSED === $report->getStatus()) { $this->addFlash('danger', 'Ce report est déjà clôturé.'); $close = false; } if ($close) { $report->setStatus(ReportStatusType::REPORT_STATUS_CLOSED); $report->setClosedAt(new \DateTime()); $this->em->flush(); $this->addFlash('success', 'Le signalement a bien été clôturé.'); } return $this->redirectToRoute('report_detail', [ 'id' => $report->getId(), 'type' => $type, ]); } #[Route(path: '/{type}/export.xls', name: 'export')] public function xls(ReportType $type, ReportExporter $reportExporter): Response { $file = ($reportExporter)($type); return $this->file($file, 'export-signalements.xls', ResponseHeaderBag::DISPOSITION_INLINE); } }
Save File
Cancel