activityACLAwareRepository = $activityACLAwareRepository; $this->activityTypeRepository = $activityTypeRepository; $this->activityTypeCategoryRepository = $activityTypeCategoryRepository; $this->personRepository = $personRepository; $this->thirdPartyRepository = $thirdPartyRepository; $this->locationRepository = $locationRepository; $this->activityRepository = $activityRepository; $this->accompanyingPeriodRepository = $accompanyingPeriodRepository; $this->entityManager = $entityManager; $this->eventDispatcher = $eventDispatcher; $this->logger = $logger; $this->serializer = $serializer; } /** * Deletes a Activity entity. * * @param mixed $id */ public function deleteAction(Request $request, $id) { $view = null; [$person, $accompanyingPeriod] = $this->getEntity($request); $activity = $this->activityRepository->find($id); if (!$activity) { throw $this->createNotFoundException('Unable to find Activity entity.'); } if ($activity->getAccompanyingPeriod() instanceof AccompanyingPeriod) { $view = 'ChillActivityBundle:Activity:confirm_deleteAccompanyingCourse.html.twig'; $accompanyingPeriod = $activity->getAccompanyingPeriod(); } else { $view = 'ChillActivityBundle:Activity:confirm_deletePerson.html.twig'; } // TODO // $this->denyAccessUnlessGranted('CHILL_ACTIVITY_DELETE', $activity); $form = $this->createDeleteForm($activity->getId(), $person, $accompanyingPeriod); if ($request->getMethod() === Request::METHOD_DELETE) { $form->handleRequest($request); if ($form->isValid()) { $this->logger->notice('An activity has been removed', [ '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( static fn (ActivityReason $ar): int => $ar->getId() ) ->toArray(), 'type_id' => $activity->getActivityType()->getId(), 'duration' => $activity->getDurationTime() ? $activity->getDurationTime()->format('U') : null, 'date' => $activity->getDate()->format('Y-m-d'), 'attendee' => $activity->getAttendee(), ]); $this->entityManager->remove($activity); $this->entityManager->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 (null === $view) { throw $this->createNotFoundException('Template not found'); } return $this->render($view, [ 'activity' => $activity, 'delete_form' => $form->createView(), 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, ]); } /** * Displays a form to edit an existing Activity entity. */ public function editAction(int $id, Request $request): Response { $view = null; [$person, $accompanyingPeriod] = $this->getEntity($request); $entity = $this->activityRepository->find($id); if (null === $entity) { throw $this->createNotFoundException('Unable to find Activity entity.'); } if ($entity->getAccompanyingPeriod() instanceof AccompanyingPeriod) { $view = 'ChillActivityBundle:Activity:editAccompanyingCourse.html.twig'; $accompanyingPeriod = $entity->getAccompanyingPeriod(); } else { $view = 'ChillActivityBundle:Activity:editPerson.html.twig'; } // TODO // $this->denyAccessUnlessGranted('CHILL_ACTIVITY_UPDATE', $entity); $form = $this->createForm(ActivityType::class, $entity, [ 'center' => $entity->getCenter(), 'role' => new Role('CHILL_ACTIVITY_UPDATE'), 'activityType' => $entity->getActivityType(), 'accompanyingPeriod' => $accompanyingPeriod, ])->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->entityManager->persist($entity); $this->entityManager->flush(); $this->addFlash('success', $this->get('translator')->trans('Success : activity updated!')); $params = $this->buildParamsToUrl($person, $accompanyingPeriod); $params['id'] = $entity->getId(); return $this->redirectToRoute('chill_activity_activity_show', $params); } $deleteForm = $this->createDeleteForm($entity->getId(), $person, $accompanyingPeriod); /* * TODO $event = new PrivacyEvent($person, array( 'element_class' => Activity::class, 'element_id' => $entity->getId(), 'action' => 'edit' )); $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); */ if (null === $view) { throw $this->createNotFoundException('Template not found'); } $activity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']); return $this->render($view, [ 'entity' => $entity, 'edit_form' => $form->createView(), 'delete_form' => $deleteForm->createView(), 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, 'activity_json' => $activity_array, ]); } /** * Lists all Activity entities. */ public function listAction(Request $request): Response { $view = null; $activities = []; // TODO: add pagination [$person, $accompanyingPeriod] = $this->getEntity($request); if ($person instanceof Person) { $this->denyAccessUnlessGranted(ActivityVoter::SEE, $person); $activities = $this->activityACLAwareRepository ->findByPerson($person, ActivityVoter::SEE, 0, null, ['date' => 'DESC', 'id' => 'DESC']); $event = new PrivacyEvent($person, [ 'element_class' => Activity::class, 'action' => 'list', ]); $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); $view = 'ChillActivityBundle:Activity:listPerson.html.twig'; } elseif ($accompanyingPeriod instanceof AccompanyingPeriod) { $this->denyAccessUnlessGranted(ActivityVoter::SEE, $accompanyingPeriod); $activities = $this->activityACLAwareRepository ->findByAccompanyingPeriod($accompanyingPeriod, ActivityVoter::SEE, 0, null, ['date' => 'DESC', 'id' => 'DESC']); $view = 'ChillActivityBundle:Activity:listAccompanyingCourse.html.twig'; } return $this->render( $view, [ 'activities' => $activities, 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, ] ); } public function newAction(Request $request): Response { $view = null; [$person, $accompanyingPeriod] = $this->getEntity($request); if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = 'ChillActivityBundle:Activity:newAccompanyingCourse.html.twig'; } elseif ($person instanceof Person) { $view = 'ChillActivityBundle:Activity:newPerson.html.twig'; } $activityType_id = $request->get('activityType_id', 0); $activityType = $this->activityTypeRepository->find($activityType_id); if (isset($activityType) && !$activityType->isActive()) { throw new InvalidArgumentException('Activity type must be active'); } $activityData = null; if ($request->query->has('activityData')) { $activityData = $request->query->get('activityData'); } if ( !$activityType instanceof \Chill\ActivityBundle\Entity\ActivityType || !$activityType->isActive() ) { $params = $this->buildParamsToUrl($person, $accompanyingPeriod); if (null !== $activityData) { $params['activityData'] = $activityData; } return $this->redirectToRoute('chill_activity_activity_select_type', $params); } $entity = new Activity(); $entity->setUser($this->getUser()); if ($person instanceof Person) { $entity->setPerson($person); } if ($accompanyingPeriod instanceof AccompanyingPeriod) { $entity->setAccompanyingPeriod($accompanyingPeriod); } $entity->setActivityType($activityType); $entity->setDate(new DateTime('now')); if ($request->query->has('activityData')) { $activityData = $request->query->get('activityData'); if (array_key_exists('durationTime', $activityData)) { $durationTimeInMinutes = $activityData['durationTime']; $hours = floor($durationTimeInMinutes / 60); $minutes = $durationTimeInMinutes % 60; $duration = DateTime::createFromFormat('H:i', $hours . ':' . $minutes); if ($duration) { $entity->setDurationTime($duration); } } if (array_key_exists('date', $activityData)) { $date = DateTime::createFromFormat('Y-m-d', $activityData['date']); if ($date) { $entity->setDate($date); } } if (array_key_exists('personsId', $activityData)) { foreach ($activityData['personsId'] as $personId) { $concernedPerson = $this->personRepository->find($personId); $entity->addPerson($concernedPerson); } } if (array_key_exists('professionalsId', $activityData)) { foreach ($activityData['professionalsId'] as $professionalsId) { $professional = $this->thirdPartyRepository->find($professionalsId); $entity->addThirdParty($professional); } } if (array_key_exists('location', $activityData)) { $location = $this->locationRepository->find($activityData['location']); $entity->setLocation($location); } if (array_key_exists('comment', $activityData)) { $comment = new CommentEmbeddable(); $comment->setComment($activityData['comment']); $comment->setUserId($this->getUser()->getid()); $comment->setDate(new DateTime('now')); $entity->setComment($comment); } } $this->denyAccessUnlessGranted(ActivityVoter::CREATE, $entity); $form = $this->createForm(ActivityType::class, $entity, [ 'center' => $entity->getCenter(), 'role' => new Role('CHILL_ACTIVITY_CREATE'), 'activityType' => $entity->getActivityType(), 'accompanyingPeriod' => $accompanyingPeriod, ]); if ($form->has('documents')) { $form->add('gendocTemplateId', HiddenType::class, [ 'mapped' => false, 'data' => null, 'attr' => [ // required for js 'data-template-id' => 'data-template-id', ], ]); } $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { if ($form->has('gendocTemplateId')) { dd($form->get('gendocTemplateId')->getData()); } $this->entityManager->persist($entity); $this->entityManager->flush(); $this->addFlash('success', $this->get('translator')->trans('Success : activity created!')); $params = $this->buildParamsToUrl($person, $accompanyingPeriod); $params['id'] = $entity->getId(); return $this->redirectToRoute('chill_activity_activity_show', $params); } if (null === $view) { throw $this->createNotFoundException('Template not found'); } $activity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']); $defaultLocation = $this->getUser()->getCurrentLocation(); return $this->render($view, [ 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, 'entity' => $entity, 'form' => $form->createView(), 'activity_json' => $activity_array, 'default_location' => $defaultLocation, ]); } public function selectTypeAction(Request $request): Response { $view = null; [$person, $accompanyingPeriod] = $this->getEntity($request); if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = 'ChillActivityBundle:Activity:selectTypeAccompanyingCourse.html.twig'; } elseif ($person instanceof Person) { $view = 'ChillActivityBundle:Activity:selectTypePerson.html.twig'; } $data = []; $activityTypeCategories = $this ->activityTypeCategoryRepository ->findBy(['active' => true], ['ordering' => 'ASC']); foreach ($activityTypeCategories as $activityTypeCategory) { $activityTypes = $this ->activityTypeRepository ->findBy( ['active' => true, 'category' => $activityTypeCategory], ['ordering' => 'ASC'] ); $data[] = [ 'activityTypeCategory' => $activityTypeCategory, 'activityTypes' => $activityTypes, ]; } if (null === $view) { throw $this->createNotFoundException('Template not found'); } return $this->render($view, [ 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, 'data' => $data, 'activityData' => $request->query->get('activityData', []), ]); } public function showAction(Request $request, int $id): Response { $entity = $this->activityRepository->find($id); if (null === $entity) { throw $this->createNotFoundException('Unable to find Activity entity.'); } $accompanyingPeriod = $entity->getAccompanyingPeriod(); $person = $entity->getPerson(); if ($accompanyingPeriod instanceof AccompanyingPeriod) { $view = 'ChillActivityBundle:Activity:showAccompanyingCourse.html.twig'; } elseif ($person instanceof Person) { $view = 'ChillActivityBundle:Activity:showPerson.html.twig'; } else { throw new RuntimeException('the activity should be linked with a period or person'); } if (null !== $accompanyingPeriod) { // @TODO: Properties created dynamically. $entity->personsAssociated = $entity->getPersonsAssociated(); $entity->personsNotAssociated = $entity->getPersonsNotAssociated(); } $this->denyAccessUnlessGranted(ActivityVoter::SEE, $entity); $deleteForm = $this->createDeleteForm($entity->getId(), $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 (null === $view) { throw $this->createNotFoundException('Template not found'); } return $this->render($view, [ 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, 'entity' => $entity, 'delete_form' => $deleteForm->createView(), ]); } private function buildParamsToUrl(?Person $person, ?AccompanyingPeriod $accompanyingPeriod): array { $params = []; if (null !== $person) { $params['person_id'] = $person->getId(); } if (null !== $accompanyingPeriod) { $params['accompanying_period_id'] = $accompanyingPeriod->getId(); } return $params; } /** * Creates a form to delete a Activity entity by id. */ private function createDeleteForm(int $id, ?Person $person, ?AccompanyingPeriod $accompanyingPeriod): FormInterface { $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, ['label' => 'Delete']) ->getForm(); } private function getEntity(Request $request): array { $person = $accompanyingPeriod = null; if ($request->query->has('person_id')) { $person_id = $request->get('person_id'); $person = $this->personRepository->find($person_id); if (null === $person) { throw $this->createNotFoundException('Person not found'); } $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); } elseif ($request->query->has('accompanying_period_id')) { $accompanying_period_id = $request->get('accompanying_period_id'); $accompanyingPeriod = $this->accompanyingPeriodRepository->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 [ $person, $accompanyingPeriod, ]; } }