From 6ae03806b4db49c8cd5a6afdf837f15d458b1991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 20 Oct 2022 22:33:23 +0200 Subject: [PATCH] Feature: [calendar][docgen] controller and UI to generate a document from a calendar --- .../Controller/CalendarController.php | 9 +- .../Controller/CalendarDocController.php | 82 +++++++++++++++++++ .../listByAccompanyingCourse.html.twig | 9 +- .../views/CalendarDoc/pick_template.html.twig | 24 ++++++ .../translations/messages.fr.yml | 2 + 5 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 src/Bundle/ChillCalendarBundle/Controller/CalendarDocController.php create mode 100644 src/Bundle/ChillCalendarBundle/Resources/views/CalendarDoc/pick_template.html.twig diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php index 595eceb36..ff470842c 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php @@ -15,7 +15,7 @@ use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Form\CalendarType; use Chill\CalendarBundle\RemoteCalendar\Connector\RemoteCalendarConnectorInterface; use Chill\CalendarBundle\Repository\CalendarACLAwareRepositoryInterface; -use Chill\CalendarBundle\Repository\CalendarRepository; +use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Repository\UserRepository; @@ -42,7 +42,7 @@ class CalendarController extends AbstractController { private CalendarACLAwareRepositoryInterface $calendarACLAwareRepository; - private CalendarRepository $calendarRepository; + private DocGeneratorTemplateRepository $docGeneratorTemplateRepository; private FilterOrderHelperFactoryInterface $filterOrderHelperFactory; @@ -57,8 +57,8 @@ class CalendarController extends AbstractController private UserRepository $userRepository; public function __construct( - CalendarRepository $calendarRepository, CalendarACLAwareRepositoryInterface $calendarACLAwareRepository, + DocGeneratorTemplateRepository $docGeneratorTemplateRepository, FilterOrderHelperFactoryInterface $filterOrderHelperFactory, LoggerInterface $logger, PaginatorFactory $paginator, @@ -66,8 +66,8 @@ class CalendarController extends AbstractController SerializerInterface $serializer, UserRepository $userRepository ) { - $this->calendarRepository = $calendarRepository; $this->calendarACLAwareRepository = $calendarACLAwareRepository; + $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; $this->filterOrderHelperFactory = $filterOrderHelperFactory; $this->logger = $logger; $this->paginator = $paginator; @@ -214,6 +214,7 @@ class CalendarController extends AbstractController 'accompanyingCourse' => $accompanyingPeriod, 'paginator' => $paginator, 'filterOrder' => $filterOrder, + 'hasDocs' => 0 < $this->docGeneratorTemplateRepository->countByEntity(Calendar::class), ]); } diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarDocController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarDocController.php new file mode 100644 index 000000000..2c9074488 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarDocController.php @@ -0,0 +1,82 @@ +security = $security; + $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; + $this->urlGenerator = $urlGenerator; + $this->engine = $engine; + } + + /** + * @Route("/{_locale}/calendar/docgen/pick/{id}", name="chill_calendar_calendardoc_pick_template") + */ + public function pickTemplate(Calendar $calendar): Response + { + if (!$this->security->isGranted(CalendarVoter::SEE, $calendar)) { + throw new AccessDeniedException('Not authorized to see this calendar'); + } + + if (0 === $number = $this->docGeneratorTemplateRepository->countByEntity(Calendar::class)) { + throw new RuntimeException('should not be redirected to this page if no template'); + } + + if (1 === $number) { + $templates = $this->docGeneratorTemplateRepository->findByEntity(Calendar::class); + + return new RedirectResponse( + $this->urlGenerator->generate( + 'chill_docgenerator_generate_from_template', + [ + 'template' => $templates[0]->getId(), + 'entityClassName' => Calendar::class, + 'entityId' => $calendar->getId(), + ] + ) + ); + } + + return new Response( + $this->engine->render('@ChillCalendar/CalendarDoc/pick_template.html.twig', [ + 'calendar' => $calendar, + 'accompanyingCourse' => $calendar->getAccompanyingPeriod(), + ]) + ); + } +} diff --git a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig index e71c3f7d4..03e4c85c2 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig +++ b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig @@ -128,7 +128,14 @@