, * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace Chill\CalendarBundle\Controller; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Privacy\PrivacyEvent; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Form; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Role\Role; use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Form\CalendarType; use Chill\CalendarBundle\Repository\CalendarRepository; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Pagination\PaginatorFactory; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Routing\Annotation\Route; /** * Class CalendarController * * @package Chill\CalendarBundle\Controller */ class CalendarController extends AbstractController { protected EventDispatcherInterface $eventDispatcher; protected AuthorizationHelper $authorizationHelper; protected LoggerInterface $logger; protected SerializerInterface $serializer; protected PaginatorFactory $paginator; private CalendarRepository $calendarRepository; public function __construct( EventDispatcherInterface $eventDispatcher, AuthorizationHelper $authorizationHelper, LoggerInterface $logger, SerializerInterface $serializer, PaginatorFactory $paginator, CalendarRepository $calendarRepository ) { $this->eventDispatcher = $eventDispatcher; $this->authorizationHelper = $authorizationHelper; $this->logger = $logger; $this->serializer = $serializer; $this->paginator = $paginator; $this->calendarRepository = $calendarRepository; } /** * Lists all Calendar entities. * @Route("/{_locale}/calendar/calendar/", name="chill_calendar_calendar_list") */ public function listAction(Request $request): Response { $view = null; [$user, $accompanyingPeriod] = $this->getEntity($request); if ($user instanceof User) { $calendarItems = $this->calendarRepository->findByUser($user); $view = '@ChillCalendar/Calendar/listByUser.html.twig'; return $this->render($view, [ 'calendarItems' => $calendarItems, 'user' => $user ]); } if ($accompanyingPeriod instanceof AccompanyingPeriod) { $total = $this->calendarRepository->countByAccompanyingPeriod($accompanyingPeriod); $paginator = $this->paginator->create($total); $calendarItems = $this->calendarRepository->findBy( ['accompanyingPeriod' => $accompanyingPeriod], ['startDate' => 'DESC'], $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber() ); $view = '@ChillCalendar/Calendar/listByAccompanyingCourse.html.twig'; return $this->render($view, [ 'calendarItems' => $calendarItems, 'accompanyingCourse' => $accompanyingPeriod, 'paginator' => $paginator ]); } throw new \Exception('Unable to list actions.'); } /** * Create a new calendar item * @Route("/{_locale}/calendar/calendar/new", name="chill_calendar_calendar_new") */ public function newAction(Request $request): Response { $em = $this->getDoctrine()->getManager(); [$user, $accompanyingPeriod] = $this->getEntity($request); if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = '@ChillCalendar/Calendar/newByAccompanyingCourse.html.twig'; } // elseif ($user instanceof User) { // $view = '@ChillCalendar/Calendar/newUser.html.twig'; // } $entity = new Calendar(); $entity->setUser($this->getUser()); $entity->setStatus($entity::STATUS_VALID); // if ($user instanceof User) { // $entity->setPerson($user); // } if ($accompanyingPeriod instanceof AccompanyingPeriod) { $entity->setAccompanyingPeriod($accompanyingPeriod); } $form = $this->createForm(CalendarType::class, $entity, [ 'accompanyingPeriod' => $accompanyingPeriod, ])->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->persist($entity); $em->flush(); $this->addFlash('success', $this->get('translator')->trans('Success : calendar item created!')); $params = $this->buildParamsToUrl($user, $accompanyingPeriod); return $this->redirectToRoute('chill_calendar_calendar_list', $params); } elseif ($form->isSubmitted() and !$form->isValid()) { $this->addFlash('error', $this->get('translator')->trans('This form contains errors')); } if ($view === null) { throw $this->createNotFoundException('Template not found'); } $entity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']); return $this->render($view, [ 'user' => $user, 'accompanyingCourse' => $accompanyingPeriod, 'entity' => $entity, 'form' => $form->createView(), 'entity_json' => $entity_array ]); } /** * Show a calendar item * @Route("/{_locale}/calendar/calendar/{id}/show", name="chill_calendar_calendar_show") */ public function showAction(Request $request, $id): Response { $em = $this->getDoctrine()->getManager(); [$user, $accompanyingPeriod] = $this->getEntity($request); if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = '@ChillCalendar/Calendar/showByAccompanyingCourse.html.twig'; } elseif ($user instanceof User) { $view = '@ChillCalendar/Calendar/showByUser.html.twig'; } $entity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id); if (!$entity) { throw $this->createNotFoundException('Unable to find Calendar entity.'); } if (null !== $accompanyingPeriod) { $entity->personsAssociated = $entity->getPersonsAssociated(); $entity->personsNotAssociated = $entity->getPersonsNotAssociated(); } // $deleteForm = $this->createDeleteForm($id, $accompanyingPeriod); if ($view === null) { throw $this->createNotFoundException('Template not found'); } $personsId = []; foreach ($entity->getPersons() as $p) { array_push($personsId, $p->getId()); } $professionalsId = []; foreach ($entity->getProfessionals() as $p) { array_push($professionalsId, $p->getId()); } $durationTime = $entity->getEndDate()->diff($entity->getStartDate()); $durationTimeInMinutes = $durationTime->days*1440 + $durationTime->h*60 + $durationTime->i; $activityData = [ 'calendarId' => $id, 'personsId' => $personsId, 'professionalsId' => $professionalsId, 'date' => $entity->getStartDate()->format('Y-m-d'), 'durationTime' => $durationTimeInMinutes, 'location' => $entity->getLocation()->getId(), 'comment' => $entity->getComment()->getComment(), ]; return $this->render($view, [ 'accompanyingCourse' => $accompanyingPeriod, 'entity' => $entity, 'user' => $user, 'activityData' => $activityData //'delete_form' => $deleteForm->createView(), ]); } /** * Edit a calendar item * @Route("/{_locale}/calendar/calendar/{id}/edit", name="chill_calendar_calendar_edit") */ public function editAction($id, Request $request): Response { $em = $this->getDoctrine()->getManager(); [$user, $accompanyingPeriod] = $this->getEntity($request); if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = '@ChillCalendar/Calendar/editByAccompanyingCourse.html.twig'; } elseif ($user instanceof User) { $view = '@ChillCalendar/Calendar/editByUser.html.twig'; } $entity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id); if (!$entity) { throw $this->createNotFoundException('Unable to find Calendar entity.'); } $form = $this->createForm(CalendarType::class, $entity, [ 'accompanyingPeriod' => $accompanyingPeriod, ])->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->persist($entity); $em->flush(); $this->addFlash('success', $this->get('translator')->trans('Success : calendar item updated!')); $params = $this->buildParamsToUrl($user, $accompanyingPeriod); return $this->redirectToRoute('chill_calendar_calendar_list', $params); } elseif ($form->isSubmitted() and !$form->isValid()) { $this->addFlash('error', $this->get('translator')->trans('This form contains errors')); } $deleteForm = $this->createDeleteForm($id, $user, $accompanyingPeriod); if ($view === null) { throw $this->createNotFoundException('Template not found'); } $entity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']); return $this->render($view, [ 'entity' => $entity, 'form' => $form->createView(), 'delete_form' => $deleteForm->createView(), 'accompanyingCourse' => $accompanyingPeriod, 'user' => $user, 'entity_json' => $entity_array ]); } /** * Delete a calendar item * @Route("/{_locale}/calendar/{id}/delete", name="chill_calendar_calendar_delete") */ public function deleteAction(Request $request, $id) { $em = $this->getDoctrine()->getManager(); [$user, $accompanyingPeriod] = $this->getEntity($request); if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = '@ChillCalendar/Calendar/confirm_deleteByAccompanyingCourse.html.twig'; } elseif ($user instanceof User) { $view = '@ChillCalendar/Calendar/confirm_deleteByUser.html.twig'; } /* @var $entity Calendar */ $entity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id); if (!$entity) { throw $this->createNotFoundException('Unable to find Calendar entity.'); } $form = $this->createDeleteForm($id, $user, $accompanyingPeriod); if ($request->getMethod() === Request::METHOD_DELETE) { $form->handleRequest($request); if ($form->isValid()) { $this->logger->notice("A calendar event has been removed", [ 'by_user' => $this->getUser()->getUsername(), 'calendar_id' => $entity->getId() ]); $em->remove($entity); $em->flush(); $this->addFlash('success', $this->get('translator') ->trans("The calendar item has been successfully removed.")); $params = $this->buildParamsToUrl($user, $accompanyingPeriod); return $this->redirectToRoute('chill_calendar_calendar_list', $params); } } if ($view === null) { throw $this->createNotFoundException('Template not found'); } return $this->render($view, [ 'calendar' => $entity, 'delete_form' => $form->createView(), 'accompanyingCourse' => $accompanyingPeriod, ]); } /** * Creates a form to delete a Calendar entity by id. */ private function createDeleteForm(int $id, ?User $user, ?AccompanyingPeriod $accompanyingPeriod): Form { $params = $this->buildParamsToUrl($user, $accompanyingPeriod); $params['id'] = $id; return $this->createFormBuilder() ->setAction($this->generateUrl('chill_calendar_calendar_delete', $params)) ->setMethod('DELETE') ->add('submit', SubmitType::class, ['label' => 'Delete']) ->getForm() ; } private function getEntity(Request $request): array { $em = $this->getDoctrine()->getManager(); $user = $accompanyingPeriod = null; if ($request->query->has('user_id')) { $user_id = $request->get('user_id'); $user = $em->getRepository(User::class)->find($user_id); if ($user === null) { throw $this->createNotFoundException('User not found'); } // TODO Add permission // $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $user); } elseif ($request->query->has('accompanying_period_id')) { $accompanying_period_id = $request->get('accompanying_period_id'); $accompanyingPeriod = $em->getRepository(AccompanyingPeriod::class)->find($accompanying_period_id); if ($accompanyingPeriod === null) { throw $this->createNotFoundException('Accompanying Period not found'); } // TODO Add permission // $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); } else { throw $this->createNotFoundException("Person or Accompanying Period not found"); } return [ $user, $accompanyingPeriod ]; } private function buildParamsToUrl( ?User $user, ?AccompanyingPeriod $accompanyingPeriod ): array { $params = []; if ($user) { $params['user_id'] = $user->getId(); } if ($accompanyingPeriod) { $params['accompanying_period_id'] = $accompanyingPeriod->getId(); } return $params; } }