diff --git a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php index bc76dacd4..8c742c627 100644 --- a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php +++ b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php @@ -86,24 +86,52 @@ class SingleTaskController extends AbstractController ->setType('task_default') ; - if ($request->query->has('person_id')) { + if($request->query->has('person_id')){ + $entityType = 'person'; + } else if ($request->query->has('course_id')) { + $entityType = 'course'; + } else { + $entityType = null; + } + + if ($entityType !== null) { - $personId = $request->query->getInt('person_id', 0); // sf4 check: + $entityId = $request->query->getInt("{$entityType}_id", 0); // sf4 check: // prevent error: `Argument 2 passed to ::getInt() must be of the type int, null given` - if ($personId === null) { - return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST); + if ($entityId === null) { + return new Response("You must provide a {$entityType}_id", Response::HTTP_BAD_REQUEST); } - $person = $this->getDoctrine()->getManager() - ->getRepository(Person::class) - ->find($personId); + if($entityType === 'person') + { + $person = $this->getDoctrine()->getManager() + ->getRepository(Person::class) + ->find($entityId); - if ($person === null) { - $this->createNotFoundException("Invalid person id"); + if ($person === null) { + $this->createNotFoundException("Invalid person id"); + } + + $task->setPerson($person); } - $task->setPerson($person); + if($entityType === 'course') + { + + $course = $this->getDoctrine()->getManager() + ->getRepository(AccompanyingPeriod::class) + ->find($entityId); + + if($course === null) { + $this->createNotFoundException("Invalid accompanying course id"); + } + + $task->setCourse($course); + + } + + } $this->denyAccessUnlessGranted(TaskVoter::CREATE, $task, 'You are not ' @@ -124,9 +152,19 @@ class SingleTaskController extends AbstractController $this->addFlash('success', $translator->trans("The task is created")); - return $this->redirectToRoute('chill_task_singletask_list', [ - 'person_id' => $task->getPerson()->getId() - ]); + if($entityType === 'person') + { + return $this->redirectToRoute('chill_task_singletask_list', [ + 'person_id' => $task->getPerson()->getId() + ]); + } + + if($entityType === 'course') + { + return $this->redirectToRoute('chill_task_singletask_list', [ + 'course_id' => $task->getCourse()->getId() + ]); + } } else { $this->addFlash('error', $translator->trans("This form contains errors"));