, * * 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\MainBundle\Entity\User; 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; public function __construct( EventDispatcherInterface $eventDispatcher, AuthorizationHelper $authorizationHelper, LoggerInterface $logger, SerializerInterface $serializer ) { $this->eventDispatcher = $eventDispatcher; $this->authorizationHelper = $authorizationHelper; $this->logger = $logger; $this->serializer = $serializer; } /** * Lists all Calendar entities. * @Route("/{_locale}/calendar/", name="chill_calendar_calendar") */ public function listAction(Request $request): Response { $em = $this->getDoctrine()->getManager(); $view = null; [$user, $accompanyingPeriod] = $this->getEntity($request); if ($user instanceof User) { // $calendar = $em->getRepository(Calendar::class) // ->findByUser($user) // ; // $view = 'ChillCalendarBundle:Calendar:listByUser.html.twig'; } elseif ($accompanyingPeriod instanceof AccompanyingPeriod) { $calendarItems = $em->getRepository(Calendar::class)->findBy( ['accompanyingPeriod' => $accompanyingPeriod] ); $view = 'ChillCalendarBundle:Calendar:listByAccompanyingCourse.html.twig'; } return $this->render($view, array( 'calendarItems' => $calendarItems, 'user' => $user, 'accompanyingCourse' => $accompanyingPeriod, )); } // public function selectTypeAction(Request $request): Response // { // $em = $this->getDoctrine()->getManager(); // $view = null; // [$person, $accompanyingPeriod] = $this->getEntity($request); // if ($accompanyingPeriod instanceof AccompanyingPeriod) { // $view = 'ChillCalendarBundle:Calendar:selectTypeAccompanyingCourse.html.twig'; // } elseif ($person instanceof Person) { // $view = 'ChillCalendarBundle:Calendar:selectTypePerson.html.twig'; // } // $data = []; // $activityTypeCategories = $em->getRepository(\Chill\CalendarBundle\Entity\ActivityTypeCategory::class) // ->findBy(['active' => true], ['ordering' => 'ASC']); // foreach ($activityTypeCategories as $activityTypeCategory) { // $activityTypes = $em->getRepository(\Chill\ActivityBundle\Entity\ActivityType::class) // ->findBy(['active' => true, 'category' => $activityTypeCategory], ['ordering' => 'ASC']); // $data[] = [ // 'activityTypeCategory' => $activityTypeCategory, // 'activityTypes' => $activityTypes, // ]; // } // if ($view === null) { // throw $this->createNotFoundException('Template not found'); // } // return $this->render($view, [ // 'person' => $person, // 'accompanyingCourse' => $accompanyingPeriod, // 'data' => $data, // ]); // } /** * Create a new calendar item * @Route("/{_locale}/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 = 'ChillCalendarBundle:Calendar:newAccompanyingCourse.html.twig'; } // elseif ($user instanceof User) { // $view = 'ChillCalendarBundle: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); //TODO useful? $params['id'] = $entity->getId(); return $this->redirectToRoute('chill_calendar_calendar_show', $params); } 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 ]); } // public function showAction(Request $request, $id): Response // { // $em = $this->getDoctrine()->getManager(); // [$person, $accompanyingPeriod] = $this->getEntity($request); // if ($accompanyingPeriod instanceof AccompanyingPeriod) { // $view = 'ChillCalendarBundle:Calendar:showAccompanyingCourse.html.twig'; // } elseif ($person instanceof Person) { // $view = 'ChillCalendarBundle:Calendar:showPerson.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(); // } // // TODO revoir le Voter de Activity pour tenir compte qu'une activité peut appartenir a une période // // $this->denyAccessUnlessGranted('CHILL_ACTIVITY_SEE', $entity); // $deleteForm = $this->createDeleteForm($id, $person, $accompanyingPeriod); // // TODO // /* // $event = new PrivacyEvent($person, array( // 'element_class' => Activity::class, // 'element_id' => $entity->getId(), // 'action' => 'show' // )); // $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); // */ // if ($view === null) { // throw $this->createNotFoundException('Template not found'); // } // return $this->render($view, array( // 'person' => $person, // 'accompanyingCourse' => $accompanyingPeriod, // 'entity' => $entity, // 'delete_form' => $deleteForm->createView(), // )); // } // /** // * Displays a form to edit an existing Activity entity. // * // */ // public function editAction($id, Request $request): Response // { // $em = $this->getDoctrine()->getManager(); // [$person, $accompanyingPeriod] = $this->getEntity($request); // if ($accompanyingPeriod instanceof AccompanyingPeriod) { // $view = 'ChillCalendarBundle:Calendar:editAccompanyingCourse.html.twig'; // } elseif ($person instanceof Person) { // $view = 'ChillCalendarBundle:Calendar:editPerson.html.twig'; // } // $entity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id); // if (!$entity) { // throw $this->createNotFoundException('Unable to find Calendar entity.'); // } // // TODO // // $this->denyAccessUnlessGranted('CHILL_ACTIVITY_UPDATE', $entity); // $form = $this->createForm(CalendarType::class, $entity, [ // 'center' => $entity->getCenter(), // 'role' => new Role('CHILL_ACTIVITY_UPDATE'), // 'activityType' => $entity->getType(), // 'accompanyingPeriod' => $accompanyingPeriod, // ])->handleRequest($request); // if ($form->isSubmitted() && $form->isValid()) { // $em->persist($entity); // $em->flush(); // $this->addFlash('success', $this->get('translator')->trans('Success : activity updated!')); // $params = $this->buildParamsToUrl($person, $accompanyingPeriod); // $params['id'] = $id; // return $this->redirectToRoute('chill_activity_activity_show', $params); // } // $deleteForm = $this->createDeleteForm($id, $person, $accompanyingPeriod); // /* // * TODO // $event = new PrivacyEvent($person, array( // 'element_class' => Calendar::class, // 'element_id' => $entity->getId(), // 'action' => 'edit' // )); // $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); // */ // if ($view === null) { // throw $this->createNotFoundException('Template not found'); // } // $activity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']); // return $this->render($view, array( // 'entity' => $entity, // 'edit_form' => $form->createView(), // 'delete_form' => $deleteForm->createView(), // 'person' => $person, // 'accompanyingCourse' => $accompanyingPeriod, // 'activity_json' => $activity_array // )); // } // /** // * Deletes a Calendar entity. // * // */ // public function deleteAction(Request $request, $id) // { // $em = $this->getDoctrine()->getManager(); // [$person, $accompanyingPeriod] = $this->getEntity($request); // if ($accompanyingPeriod instanceof AccompanyingPeriod) { // $view = 'ChillCalendarBundle:Calendar:confirm_deleteAccompanyingCourse.html.twig'; // } elseif ($person instanceof Person) { // $view = 'ChillCalendarBundle:Calendar:confirm_deletePerson.html.twig'; // } // /* @var $activity Calendar */ // $activity = $em->getRepository('ChillCalendarBundle:Calendar')->find($id); // if (!$activity) { // throw $this->createNotFoundException('Unable to find Calendar entity.'); // } // // TODO // // $this->denyAccessUnlessGranted('CHILL_ACTIVITY_DELETE', $activity); // $form = $this->createDeleteForm($id, $person, $accompanyingPeriod); // if ($request->getMethod() === Request::METHOD_DELETE) { // $form->handleRequest($request); // if ($form->isValid()) { // $this->logger->notice("An activity has been removed", array( // 'by_user' => $this->getUser()->getUsername(), // 'activity_id' => $activity->getId(), // 'person_id' => $activity->getPerson() ? $activity->getPerson()->getId() : null, // 'comment' => $activity->getComment()->getComment(), // 'scope_id' => $activity->getScope() ? $activity->getScope()->getId() : null, // 'reasons_ids' => $activity->getReasons() // ->map(function ($ar) { return $ar->getId(); }) // ->toArray(), // 'type_id' => $activity->getType()->getId(), // 'duration' => $activity->getDurationTime() ? $activity->getDurationTime()->format('U') : null, // 'date' => $activity->getDate()->format('Y-m-d'), // 'attendee' => $activity->getAttendee() // )); // $em->remove($activity); // $em->flush(); // $this->addFlash('success', $this->get('translator') // ->trans("The activity has been successfully removed.")); // $params = $this->buildParamsToUrl($person, $accompanyingPeriod); // return $this->redirectToRoute('chill_activity_activity_list', $params); // } // } // if ($view === null) { // throw $this->createNotFoundException('Template not found'); // } // return $this->render($view, array( // 'activity' => $activity, // 'delete_form' => $form->createView(), // 'person' => $person, // 'accompanyingCourse' => $accompanyingPeriod, // )); // } // /** // * Creates a form to delete a Calendar entity by id. // */ // private function createDeleteForm(int $id, ?Person $person, ?AccompanyingPeriod $accompanyingPeriod): Form // { // $params = $this->buildParamsToUrl($person, $accompanyingPeriod); // $params['id'] = $id; // return $this->createFormBuilder() // ->setAction($this->generateUrl('chill_activity_activity_delete', $params)) // ->setMethod('DELETE') // ->add('submit', SubmitType::class, array('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'); } $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; } }