calendarRepository = $calendarRepository; $this->calendarACLAwareRepository = $calendarACLAwareRepository; $this->filterOrderHelperFactory = $filterOrderHelperFactory; $this->logger = $logger; $this->paginator = $paginator; $this->remoteCalendarConnector = $remoteCalendarConnector; $this->serializer = $serializer; $this->userRepository = $userRepository; } /** * Delete a calendar item. * * @Route("/{_locale}/calendar/{id}/delete", name="chill_calendar_calendar_delete") */ public function deleteAction(Request $request, Calendar $entity) { $view = null; $em = $this->getDoctrine()->getManager(); $accompanyingPeriod = $entity->getAccompanyingPeriod(); $user = null; // TODO legacy code ? remove it ? if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = '@ChillCalendar/Calendar/confirm_deleteByAccompanyingCourse.html.twig'; } elseif ($user instanceof User) { $view = '@ChillCalendar/Calendar/confirm_deleteByUser.html.twig'; } $form = $this->createDeleteForm($entity->getId(), $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_by_period', $params); } } if (null === $view) { throw $this->createNotFoundException('Template not found'); } return $this->render($view, [ 'calendar' => $entity, 'delete_form' => $form->createView(), 'accompanyingCourse' => $accompanyingPeriod, ]); } /** * Edit a calendar item. * * @Route("/{_locale}/calendar/calendar/{id}/edit", name="chill_calendar_calendar_edit") */ public function editAction(Calendar $entity, Request $request): Response { if (!$this->remoteCalendarConnector->isReady()) { return $this->remoteCalendarConnector->getMakeReadyResponse($request->getUri()); } $view = null; $em = $this->getDoctrine()->getManager(); [$user, $accompanyingPeriod] = $this->getEntity($request); if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = '@ChillCalendar/Calendar/editByAccompanyingCourse.html.twig'; } elseif ($user instanceof User) { throw new Exception('to analyze'); $view = '@ChillCalendar/Calendar/editByUser.html.twig'; } $form = $this->createForm(CalendarType::class, $entity); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $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_by_period', $params); } if ($form->isSubmitted() && !$form->isValid()) { $this->addFlash('error', $this->get('translator')->trans('This form contains errors')); } $deleteForm = $this->createDeleteForm($entity->getId(), $user, $accompanyingPeriod); if (null === $view) { 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, ]); } /** * Lists all Calendar entities. * * @Route("/{_locale}/calendar/calendar/by-period/{id}", name="chill_calendar_calendar_list_by_period") */ public function listActionByCourse(AccompanyingPeriod $accompanyingPeriod): Response { $filterOrder = $this->buildListFilterOrder(); ['from' => $from, 'to' => $to] = $filterOrder->getDateRangeData('startDate'); $total = $this->calendarACLAwareRepository ->countByAccompanyingPeriod($accompanyingPeriod, $from, $to); $paginator = $this->paginator->create($total); $calendarItems = $this->calendarACLAwareRepository->findByAccompanyingPeriod( $accompanyingPeriod, $from, $to, ['startDate' => 'DESC'], $paginator->getCurrentPageFirstItemNumber(), $paginator->getItemsPerPage() ); return $this->render('@ChillCalendar/Calendar/listByAccompanyingCourse.html.twig', [ 'calendarItems' => $calendarItems, 'accompanyingCourse' => $accompanyingPeriod, 'paginator' => $paginator, 'filterOrder' => $filterOrder, ]); } /** * @Route("/{_locale}/calendar/calendar/my", name="chill_calendar_calendar_list_my") */ public function myCalendar(Request $request): Response { $this->denyAccessUnlessGranted('ROLE_USER'); if (!$this->remoteCalendarConnector->isReady()) { return $this->remoteCalendarConnector->getMakeReadyResponse($request->getUri()); } if (!$this->getUser() instanceof User) { throw new UnauthorizedHttpException('you are not an user'); } $view = '@ChillCalendar/Calendar/listByUser.html.twig'; return $this->render($view, [ 'user' => $this->getUser(), ]); } /** * Create a new calendar item. * * @Route("/{_locale}/calendar/calendar/new", name="chill_calendar_calendar_new") */ public function newAction(Request $request): Response { if (!$this->remoteCalendarConnector->isReady()) { return $this->remoteCalendarConnector->getMakeReadyResponse($request->getUri()); } $view = null; $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(); if ($request->query->has('mainUser')) { $entity->setMainUser($this->userRepository->find($request->query->getInt('mainUser'))); } // if ($user instanceof User) { // $entity->setPerson($user); // } if ($accompanyingPeriod instanceof AccompanyingPeriod) { $entity->setAccompanyingPeriod($accompanyingPeriod); } $form = $this->createForm(CalendarType::class, $entity); $form->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_by_period', $params); } if ($form->isSubmitted() && !$form->isValid()) { $this->addFlash('error', $this->get('translator')->trans('This form contains errors')); } if (null === $view) { 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, ]); } /** * @Route("/{_locale}/calendar/calendar/{id}/to-activity", name="chill_calendar_calendar_to_activity") */ public function toActivity(Request $request, Calendar $calendar): RedirectResponse { $personsId = array_map( static fn (Person $p): int => $p->getId(), $calendar->getPersons()->toArray() ); $professionalsId = array_map( static fn (ThirdParty $thirdParty): ?int => $thirdParty->getId(), $calendar->getProfessionals()->toArray() ); $usersId = array_map( static fn (User $user): ?int => $user->getId(), array_merge($calendar->getUsers()->toArray(), [$calendar->getMainUser()]) ); $durationTime = $calendar->getEndDate()->diff($calendar->getStartDate()); $durationTimeInMinutes = $durationTime->days * 1440 + $durationTime->h * 60 + $durationTime->i; $activityData = [ 'calendarId' => $calendar->getId(), 'personsId' => $personsId, 'professionalsId' => $professionalsId, 'usersId' => $usersId, 'date' => $calendar->getStartDate()->format('Y-m-d'), 'durationTime' => $durationTimeInMinutes, 'location' => $calendar->getLocation() ? $calendar->getLocation()->getId() : null, 'comment' => $calendar->getComment()->getComment(), ]; return $this->redirectToRoute( 'chill_activity_activity_new', [ 'accompanying_period_id' => $calendar->getAccompanyingPeriod()->getId(), 'activityData' => $activityData, 'returnPath' => $request->query->get('returnPath', null), ] ); } /** * Show a calendar item. * * @Route("/{_locale}/calendar/calendar/{id}/show", name="chill_calendar_calendar_show") */ public function showAction(Request $request, int $id): Response { $view = null; $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'; } if (null === $view) { throw $this->createNotFoundException('Template not found'); } /** @var Calendar $entity */ $entity = $em->getRepository(\Chill\CalendarBundle\Entity\Calendar::class)->find($id); if (null === $entity) { throw $this->createNotFoundException('Unable to find Calendar entity.'); } if (null !== $accompanyingPeriod) { // @TODO: These properties are declared dynamically. // It must be removed. // @See https://wiki.php.net/rfc/deprecate_dynamic_properties $entity->personsAssociated = $entity->getPersonsAssociated(); $entity->personsNotAssociated = $entity->getPersonsNotAssociated(); } // $deleteForm = $this->createDeleteForm($id, $accompanyingPeriod); $personsId = array_map( static fn (Person $p): int => $p->getId(), $entity->getPersons()->toArray() ); $professionalsId = array_map( static fn (ThirdParty $thirdParty): ?int => $thirdParty->getId(), $entity->getProfessionals()->toArray() ); $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() ? $entity->getLocation()->getId() : null, 'comment' => $entity->getComment()->getComment(), ]; return $this->render($view, [ 'accompanyingCourse' => $accompanyingPeriod, 'entity' => $entity, 'user' => $user, 'activityData' => $activityData, //'delete_form' => $deleteForm->createView(), ]); } private function buildListFilterOrder(): FilterOrderHelper { $filterOrder = $this->filterOrderHelperFactory->create(self::class); $filterOrder->addDateRange('startDate', 'chill_calendar.start date filter', new DateTimeImmutable('3 days ago'), null); return $filterOrder->build(); } private function buildParamsToUrl(?User $user, ?AccompanyingPeriod $accompanyingPeriod): array { $params = []; if (null !== $user) { $params['user_id'] = $user->getId(); } if (null !== $accompanyingPeriod) { $params['id'] = $accompanyingPeriod->getId(); } return $params; } /** * Creates a form to delete a Calendar entity by id. */ private function createDeleteForm(int $id, ?User $user, ?AccompanyingPeriod $accompanyingPeriod): FormInterface { $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 (null === $user) { 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 (null === $accompanyingPeriod) { 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, ]; } }