From 6e3ce06fcf9faaf9c50c1953e9150cebcca55b06 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Fri, 17 Sep 2021 14:33:42 +0200 Subject: [PATCH] new/show/edit/delete/list functionality added for accompanyingperiod task --- .../Entity/AccompanyingPeriod.php | 10 ++ .../Controller/SingleTaskController.php | 143 ++++++++++++------ .../ChillTaskBundle/Entity/AbstractTask.php | 12 ++ .../ChillTaskBundle/Form/SingleTaskType.php | 7 +- ...{PersonMenuBuilder.php => MenuBuilder.php} | 0 .../views/SingleTask/_edit.html.twig | 25 +++ .../Resources/views/SingleTask/_new.html.twig | 30 ++++ .../views/SingleTask/_show.html.twig | 4 +- .../confirm_deleteCourseTask.html.twig | 19 +++ .../Resources/views/SingleTask/edit.html.twig | 39 +---- .../views/SingleTask/editCourseTask.html.twig | 14 ++ .../Resources/views/SingleTask/new.html.twig | 33 +--- .../views/SingleTask/newCourseTask.html.twig | 12 ++ .../Security/Authorization/TaskVoter.php | 9 +- .../translations/messages.fr.yml | 1 + 15 files changed, 242 insertions(+), 116 deletions(-) rename src/Bundle/ChillTaskBundle/Menu/{PersonMenuBuilder.php => MenuBuilder.php} (100%) create mode 100644 src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_edit.html.twig create mode 100644 src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_new.html.twig create mode 100644 src/Bundle/ChillTaskBundle/Resources/views/SingleTask/confirm_deleteCourseTask.html.twig create mode 100644 src/Bundle/ChillTaskBundle/Resources/views/SingleTask/editCourseTask.html.twig create mode 100644 src/Bundle/ChillTaskBundle/Resources/views/SingleTask/newCourseTask.html.twig diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 14fb26f1a..82b2ee10c 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -28,6 +28,7 @@ use Chill\MainBundle\Entity\HasCentersInterface; use Chill\MainBundle\Entity\HasScopesInterface; use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\Address; +use Chill\MainBundle\Entity\Center; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork; use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment; @@ -977,6 +978,15 @@ class AccompanyingPeriod implements TrackCreationInterface, TrackUpdateInterface return $this->addressLocation; } + public function getCenter(): ?Center + { + if (count($this->getPersons()) === 0){ + return null; + } else { + return $this->getPersons()->first()->getCenter(); + } + } + /** * @Groups({"write"}) */ diff --git a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php index e98688aa6..456699091 100644 --- a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php +++ b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php @@ -6,7 +6,6 @@ use Chill\PersonBundle\Privacy\PrivacyEvent; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; -use Doctrine\ORM\EntityManager; use Chill\PersonBundle\Entity\Person; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -22,7 +21,6 @@ use Chill\TaskBundle\Repository\SingleTaskRepository; use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Security\Authorization\PersonVoter; use Chill\PersonBundle\Repository\PersonRepository; -use Chill\MainBundle\Entity\UserRepository; use Chill\TaskBundle\Event\TaskEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Translation\TranslatorInterface; @@ -32,8 +30,6 @@ use Chill\MainBundle\Timeline\TimelineBuilder; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Repository\AccompanyingPeriodRepository; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; -use Chill\TaskBundle\Form\SingleTaskCourseType; -use Chill\TaskBundle\Repository\AbstractTaskRepository; use Symfony\Component\HttpFoundation\RequestStack; /** @@ -205,6 +201,7 @@ class SingleTaskController extends AbstractController 'task' => $task, 'accompanyingCourse' => $course, )); + break; } } @@ -281,8 +278,9 @@ class SingleTaskController extends AbstractController $timeline = $this->timelineBuilder ->getTimelineHTML('task', array('task' => $task)); + - if($this->getEntityContext() === 'person'){ + if($task->getContext() instanceof Person){ return $this->render('ChillTaskBundle:SingleTask:show.html.twig', array( 'task' => $task, 'timeline' => $timeline @@ -311,7 +309,7 @@ class SingleTaskController extends AbstractController $em = $this->getDoctrine()->getManager(); $task = $em->getRepository(SingleTask::class)->find($id); - if ($task->getPerson() !== null) { + if ($task->getContext() instanceof Person) { $personId = $task->getPerson()->getId(); if ($personId === null) { return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST); @@ -324,7 +322,21 @@ class SingleTaskController extends AbstractController if ($person === null) { throw $this->createNotFoundException("Invalid person id"); } + } else { + $courseId = $task->getCourse()->getId(); + if ($courseId === null) { + return new Response("You must provide a course_id", Response::HTTP_BAD_REQUEST); + } + + $course = $this->getDoctrine()->getManager() + ->getRepository(AccompanyingPeriod::class) + ->find($courseId); + + if ($course === null) { + throw $this->createNotFoundException("Invalid accompanying period id"); + } } + $this->denyAccessUnlessGranted(TaskVoter::UPDATE, $task, 'You are not ' . 'allowed to edit this task'); @@ -351,19 +363,25 @@ class SingleTaskController extends AbstractController $this->addFlash('success', $translator ->trans("The task has been updated")); - - $event = new PrivacyEvent($person, array( + + if($task->getContext() instanceof Person){ + $event = new PrivacyEvent($person, array( 'element_class' => SingleTask::class, 'element_id' => $task->getId(), 'action' => 'update' - )); - $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); - - return $this->redirectToRoute( - 'chill_task_singletask_list', - $this->request->query->get('list_params', []) - ); + )); + $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); + return $this->redirectToRoute( + 'chill_task_singletask_list', + $this->request->query->get('list_params', []) + ); + } else { + return $this->redirectToRoute( + 'chill_task_singletask_courselist', + $this->request->query->get('list_params', []) + ); + } } else { $this->addFlash('error', $translator->trans("This form contains errors")); } @@ -375,17 +393,26 @@ class SingleTaskController extends AbstractController return $event->getResponse(); } - $event = new PrivacyEvent($person, array( - 'element_class' => SingleTask::class, - 'element_id' => $task->getId(), - 'action' => 'edit' - )); - $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); - - return $this->render('ChillTaskBundle:SingleTask:edit.html.twig', array( - 'task' => $task, - 'form' => $form->createView() - )); + if($task->getContext() instanceof Person){ + $event = new PrivacyEvent($person, array( + 'element_class' => SingleTask::class, + 'element_id' => $task->getId(), + 'action' => 'edit' + )); + $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); + + return $this->render('ChillTaskBundle:SingleTask:edit.html.twig', array( + 'task' => $task, + 'form' => $form->createView() + )); + } else { + return $this->render('ChillTaskBundle:SingleTask:editCourseTask.html.twig', array( + 'task' => $task, + 'form' => $form->createView(), + 'accompanyingCourse' => $course + )); + } + } @@ -422,8 +449,22 @@ class SingleTaskController extends AbstractController throw $this->createNotFoundException("Invalid person id"); } + } else { + $courseId = $task->getCourse()->getId(); + if ($courseId === null){ + return new Response("You must provide a course_id", Response::HTTP_BAD_REQUEST); + } + + $course = $this->getDoctrine()->getManager() + ->getRepository(AccompanyingPeriod::class) + ->find($courseId); + + if($course === null){ + throw $this->createNotFoundException("Invalid accompanying period id"); + } } + $this->denyAccessUnlessGranted(TaskVoter::DELETE, $task, 'You are not ' . 'allowed to delete this task'); @@ -452,19 +493,35 @@ class SingleTaskController extends AbstractController $this->addFlash('success', $translator ->trans("The task has been successfully removed.")); - return $this->redirect($this->generateUrl( - 'chill_task_singletask_list', - $request->query->get('list_params', [ - 'person_id' => $person->getId() - ]))); + if($task->getContext() instanceof Person){ + return $this->redirect($this->generateUrl( + 'chill_task_singletask_list', + $request->query->get('list_params', [ + 'person_id' => $person->getId() + ]))); + } else { + return $this->redirect($this->generateUrl( + 'chill_task_singletask_courselist', + $request->query->get('list_params', [ + 'course_id' => $course->getId() + ]))); + } } } + if($task->getContext() instanceof Person){ + return $this->render('ChillTaskBundle:SingleTask:confirm_delete.html.twig', array( + 'task' => $task, + 'delete_form' => $form->createView() + )); + } else { + return $this->render('ChillTaskBundle:SingleTask:confirm_deleteCourseTask.html.twig', array( + 'task' => $task, + 'delete_form' => $form->createView(), + 'accompanyingCourse' => $course + )); + } - return $this->render('ChillTaskBundle:SingleTask:confirm_delete.html.twig', array( - 'task' => $task, - 'delete_form' => $form->createView() - )); } /** @@ -475,18 +532,10 @@ class SingleTaskController extends AbstractController */ protected function setCreateForm(SingleTask $task, Role $role) { - if($this->getEntityContext() === 'person'){ - $form = $this->createForm(SingleTaskType::class, $task, [ - 'center' => $task->getCenter(), - 'role' => $role, - ]); - } - - if($this->getEntityContext() === 'course'){ - $form = $this->createForm(SingleTaskCourseType::class, $task, [ - 'center' => $task->getCenter(), - ]); - } + $form = $this->createForm(SingleTaskType::class, $task, [ + 'center' => $task->getCenter(), + 'role' => $role, + ]); $form->add('submit', SubmitType::class); diff --git a/src/Bundle/ChillTaskBundle/Entity/AbstractTask.php b/src/Bundle/ChillTaskBundle/Entity/AbstractTask.php index 649de0422..57a194ef8 100644 --- a/src/Bundle/ChillTaskBundle/Entity/AbstractTask.php +++ b/src/Bundle/ChillTaskBundle/Entity/AbstractTask.php @@ -248,11 +248,23 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface { if ($this->getPerson() instanceof Person) { return $this->getPerson()->getCenter(); + } else { + return $this->getCourse()->getCenter(); } return null; } + + public function getContext() + { + // if ($this->getCourse() instanceof AccompanyingPeriod){ + // return $this->getCourse(); + // } else { + // return $this->getPerson(); + // } + return $this->getPerson() ?? $this->getCourse(); + } public function getScope(): ?\Chill\MainBundle\Entity\Scope { diff --git a/src/Bundle/ChillTaskBundle/Form/SingleTaskType.php b/src/Bundle/ChillTaskBundle/Form/SingleTaskType.php index 16f000aa2..5802512b7 100644 --- a/src/Bundle/ChillTaskBundle/Form/SingleTaskType.php +++ b/src/Bundle/ChillTaskBundle/Form/SingleTaskType.php @@ -48,11 +48,11 @@ class SingleTaskType extends AbstractType 'center' => $options['center'], 'role' => $options['role'], 'placeholder' => 'Not assigned' - ]) + ]) ->add('circle', ScopePickerType::class, [ 'center' => $options['center'], 'role' => $options['role'] - ]) + ]) ->add('startDate', ChillDateType::class, [ 'required' => false ]) @@ -61,8 +61,7 @@ class SingleTaskType extends AbstractType ]) ->add('warningInterval', DateIntervalType::class, [ 'required' => false - ]) - ; + ]); } public function configureOptions(OptionsResolver $resolver) diff --git a/src/Bundle/ChillTaskBundle/Menu/PersonMenuBuilder.php b/src/Bundle/ChillTaskBundle/Menu/MenuBuilder.php similarity index 100% rename from src/Bundle/ChillTaskBundle/Menu/PersonMenuBuilder.php rename to src/Bundle/ChillTaskBundle/Menu/MenuBuilder.php diff --git a/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_edit.html.twig b/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_edit.html.twig new file mode 100644 index 000000000..98b070114 --- /dev/null +++ b/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_edit.html.twig @@ -0,0 +1,25 @@ +
+ +

{{ 'Edit task'|trans }}

+ + {{ form_start(form) }} + + {{ form_row(form.title) }} + {{ form_row(form.description) }} + {{ form_row(form.assignee) }} + {{ form_row(form.circle) }} + {{ form_row(form.startDate) }} + {{ form_row(form.endDate) }} + {{ form_row(form.warningInterval) }} + + + {{ form_end(form) }} +
diff --git a/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_new.html.twig b/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_new.html.twig new file mode 100644 index 000000000..7cb19b19f --- /dev/null +++ b/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_new.html.twig @@ -0,0 +1,30 @@ +
+ +

{{ 'New task'|trans }}

+ + {{ form_start(form) }} + + {{ form_errors(form) }} + + {{ form_row(form.title) }} + {{ form_row(form.description) }} + {{ form_row(form.assignee) }} + {{ form_row(form.circle) }} + {{ form_row(form.startDate) }} + {{ form_row(form.endDate) }} + {{ form_row(form.warningInterval) }} + + + + {{ form_end(form) }} + +
diff --git a/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_show.html.twig b/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_show.html.twig index a8905285a..f73f2740d 100644 --- a/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_show.html.twig +++ b/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/_show.html.twig @@ -68,8 +68,8 @@