improvements following review

This commit is contained in:
Julie Lenaerts 2021-10-01 16:32:05 +02:00
parent 461b96ea37
commit b3b54486b5
3 changed files with 124 additions and 169 deletions

View File

@ -2,6 +2,7 @@
namespace Chill\TaskBundle\Controller; namespace Chill\TaskBundle\Controller;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\Scope;
use Chill\PersonBundle\Privacy\PrivacyEvent; use Chill\PersonBundle\Privacy\PrivacyEvent;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -24,15 +25,16 @@ use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Repository\PersonRepository;
use Chill\TaskBundle\Event\TaskEvent; use Chill\TaskBundle\Event\TaskEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Chill\TaskBundle\Event\UI\UIEvent; use Chill\TaskBundle\Event\UI\UIEvent;
use Chill\MainBundle\Repository\CenterRepository; use Chill\MainBundle\Repository\CenterRepository;
use Chill\MainBundle\Repository\ScopeRepository;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Timeline\TimelineBuilder; use Chill\MainBundle\Timeline\TimelineBuilder;
use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository; use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Doctrine\ORM\EntityRepository;
use Symfony\Contracts\Translation\TranslatorInterface as TranslationTranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface as TranslationTranslatorInterface;
/** /**
@ -42,27 +44,23 @@ use Symfony\Contracts\Translation\TranslatorInterface as TranslationTranslatorIn
*/ */
class SingleTaskController extends AbstractController class SingleTaskController extends AbstractController
{ {
protected EventDispatcherInterface $eventDispatcher;
/** protected TimeLineBuilder $timelineBuilder;
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/** protected LoggerInterface $logger;
*
* @var TimelineBuilder
*/
protected $timelineBuilder;
/**
* @var LoggerInterface
*/
protected $logger;
/** protected EntityRepository $personRepository;
* @var RequestStack
*/ protected EntityRepository $courseRepository;
protected $request;
protected SingleTaskRepository $taskRepository;
protected EntityRepository $userRepository;
protected EntityRepository $centerRepository;
protected EntityRepository $scopeRepository;
/** /**
* SingleTaskController constructor. * SingleTaskController constructor.
@ -73,20 +71,25 @@ class SingleTaskController extends AbstractController
EventDispatcherInterface $eventDispatcher, EventDispatcherInterface $eventDispatcher,
TimelineBuilder $timelineBuilder, TimelineBuilder $timelineBuilder,
LoggerInterface $logger, LoggerInterface $logger,
RequestStack $requestStack EntityManagerInterface $em
) { ) {
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->timelineBuilder = $timelineBuilder; $this->timelineBuilder = $timelineBuilder;
$this->logger = $logger; $this->logger = $logger;
$this->request = $requestStack->getCurrentRequest(); $this->personRepository = $em->getRepository(Person::class);
$this->courseRepository = $em->getRepository(AccompanyingPeriod::class);
$this->taskRepository = $em->getRepository(SingleTask::class);
$this->userRepository = $em->getRepository(User::class);
$this->centerRepository = $em->getRepository(Center::class);
$this->scopeRepository = $em->getRepository(Scope::class);
} }
private function getEntityContext() private function getEntityContext(Request $request)
{ {
if($this->request->query->has('person_id')){ if($request->query->has('person_id')){
return 'person'; return 'person';
} else if ($this->request->query->has('course_id')) { } else if ($request->query->has('course_id')) {
return 'course'; return 'course';
} else { } else {
return null; return null;
@ -101,7 +104,8 @@ class SingleTaskController extends AbstractController
* ) * )
*/ */
public function newAction( public function newAction(
TranslationTranslatorInterface $translator TranslationTranslatorInterface $translator,
Request $request
) { ) {
$task = (new SingleTask()) $task = (new SingleTask())
@ -109,46 +113,41 @@ class SingleTaskController extends AbstractController
->setType('task_default') ->setType('task_default')
; ;
$entityType = $this->getEntityContext(); $entityType = $this->getEntityContext($request);
if ($entityType !== null) { if ($entityType !== null) {
$entityId = $this->request->query->getInt("{$entityType}_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` // prevent error: `Argument 2 passed to ::getInt() must be of the type int, null given`
if ($entityId === null) { switch($entityType){
return new Response("You must provide a {$entityType}_id", Response::HTTP_BAD_REQUEST); case 'person':
} $person = $this->personRepository
if($entityType === 'person')
{
$person = $this->getDoctrine()->getManager()
->getRepository(Person::class)
->find($entityId); ->find($entityId);
if ($person === null) { if ($person === null) {
$this->createNotFoundException("Invalid person id"); $this->createNotFoundException("Invalid person id");
} }
$task->setPerson($person); $task->setPerson($person);
break;
case 'course':
$course = $this->courseRepository
->find($entityId);
if($course === null) {
$this->createNotFoundException("Invalid accompanying course id");
}
$task->setCourse($course);
break;
default:
new Response("You must provide a {$entityType}_id", Response::HTTP_BAD_REQUEST);
break;
} }
if($entityType === 'course')
{
$course = $this->getDoctrine()->getManager()
->getRepository(AccompanyingPeriod::class)
->find($entityId);
if($course === null) {
$this->createNotFoundException("Invalid accompanying course id");
}
$task->setCourse($course);
}
} }
//TODO : resolve access rights //TODO : resolve access rights
@ -157,8 +156,7 @@ class SingleTaskController extends AbstractController
// . 'allowed to create this task'); // . 'allowed to create this task');
$form = $this->setCreateForm($task, new Role(TaskVoter::CREATE)); $form = $this->setCreateForm($task, new Role(TaskVoter::CREATE));
$form->handleRequest($request);
$form->handleRequest($this->request);
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
if ($form->isValid()) { if ($form->isValid()) {
@ -171,18 +169,17 @@ class SingleTaskController extends AbstractController
$this->addFlash('success', $translator->trans("The task is created")); $this->addFlash('success', $translator->trans("The task is created"));
if($entityType === 'person') switch($entityType){
{ case 'person':
return $this->redirectToRoute('chill_task_singletask_list', [ return $this->redirectToRoute('chill_task_singletask_list', [
'person_id' => $task->getPerson()->getId() 'person_id' => $task->getPerson()->getId()
]); ]);
} break;
case 'course':
if($entityType === 'course') return $this->redirectToRoute('chill_task_singletask_courselist', [
{
return $this->redirectToRoute('chill_task_singletask_courselist', [
'course_id' => $task->getCourse()->getId() 'course_id' => $task->getCourse()->getId()
]); ]);
break;
} }
} else { } else {
@ -190,7 +187,7 @@ class SingleTaskController extends AbstractController
} }
} }
switch($this->getEntityContext()){ switch($this->getEntityContext($request)){
case 'person': case 'person':
return $this->render('@ChillTask/SingleTask/Person/new.html.twig', array( return $this->render('@ChillTask/SingleTask/Person/new.html.twig', array(
'form' => $form->createView(), 'form' => $form->createView(),
@ -218,8 +215,8 @@ class SingleTaskController extends AbstractController
public function showAction($id) public function showAction($id)
{ {
$em = $this->getDoctrine()->getManager(); // $em = $this->getDoctrine()->getManager();
$task = $em->getRepository(SingleTask::class)->find($id); $task = $this->taskRepository->find($id);
if (!$task) { if (!$task) {
throw $this->createNotFoundException('Unable to find Task entity.'); throw $this->createNotFoundException('Unable to find Task entity.');
@ -227,15 +224,7 @@ class SingleTaskController extends AbstractController
if ($task->getPerson() !== null) { if ($task->getPerson() !== null) {
$personId = $task->getPerson()->getId(); $person = $task->getPerson();
if ($personId === null) {
return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST);
}
$person = $this->getDoctrine()->getManager()
->getRepository(Person::class)
->find($personId);
if ($person === null) { if ($person === null) {
throw $this->createNotFoundException("Invalid person id"); throw $this->createNotFoundException("Invalid person id");
@ -250,22 +239,9 @@ class SingleTaskController extends AbstractController
} }
if ($task->getCourse() !== null) if ($task->getCourse() === null)
{ {
$courseId = $task->getCourse()->getId(); return new Response("You must provide a course_id", Response::HTTP_BAD_REQUEST);
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 course id");
}
} }
@ -299,34 +275,23 @@ class SingleTaskController extends AbstractController
*/ */
public function editAction( public function editAction(
$id, $id,
TranslationTranslatorInterface $translator TranslationTranslatorInterface $translator,
Request $request
) { ) {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$task = $em->getRepository(SingleTask::class)->find($id); $task = $em->getRepository(SingleTask::class)->find($id);
if ($task->getContext() instanceof Person) { if ($task->getContext() instanceof Person) {
$personId = $task->getPerson()->getId();
if ($personId === null) { $person = $task->getPerson();
if ($person === null) {
return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST); return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST);
} }
$person = $this->getDoctrine()->getManager()
->getRepository(Person::class)
->find($personId);
if ($person === null) {
throw $this->createNotFoundException("Invalid person id");
}
} else { } 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() $course = $task->getCourse();
->getRepository(AccompanyingPeriod::class)
->find($courseId);
if ($course === null) { if ($course === null) {
throw $this->createNotFoundException("Invalid accompanying period id"); throw $this->createNotFoundException("Invalid accompanying period id");
@ -348,7 +313,7 @@ class SingleTaskController extends AbstractController
$form = $event->getForm(); $form = $event->getForm();
$form->handleRequest($this->request); $form->handleRequest($request);
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
if ($form->isValid()) { if ($form->isValid()) {
@ -370,12 +335,12 @@ class SingleTaskController extends AbstractController
return $this->redirectToRoute( return $this->redirectToRoute(
'chill_task_singletask_list', 'chill_task_singletask_list',
$this->request->query->get('list_params', []) $request->query->get('list_params', [])
); );
} else { } else {
return $this->redirectToRoute( return $this->redirectToRoute(
'chill_task_singletask_courselist', 'chill_task_singletask_courselist',
$this->request->query->get('list_params', []) $request->query->get('list_params', [])
); );
} }
} else { } else {
@ -423,9 +388,7 @@ class SingleTaskController extends AbstractController
$id, $id,
TranslationTranslatorInterface $translator TranslationTranslatorInterface $translator
) { ) {
$task = $this->taskRepository->find($id);
$em = $this->getDoctrine()->getManager();
$task = $em->getRepository(SingleTask::class)->find($id);
if (!$task) { if (!$task) {
throw $this->createNotFoundException('Unable to find Task entity.'); throw $this->createNotFoundException('Unable to find Task entity.');
@ -437,8 +400,7 @@ class SingleTaskController extends AbstractController
return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST); return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST);
} }
$person = $this->getDoctrine()->getManager() $person = $this->personRepository
->getRepository(Person::class)
->find($personId); ->find($personId);
if ($person === null) { if ($person === null) {
@ -451,8 +413,7 @@ class SingleTaskController extends AbstractController
return new Response("You must provide a course_id", Response::HTTP_BAD_REQUEST); return new Response("You must provide a course_id", Response::HTTP_BAD_REQUEST);
} }
$course = $this->getDoctrine()->getManager() $course = $this->courseRepository
->getRepository(AccompanyingPeriod::class)
->find($courseId); ->find($courseId);
if($course === null){ if($course === null){
@ -571,11 +532,12 @@ class SingleTaskController extends AbstractController
*/ */
public function listAction( public function listAction(
PaginatorFactory $paginatorFactory, PaginatorFactory $paginatorFactory,
SingleTaskRepository $taskRepository, // SingleTaskRepository $taskRepository,
PersonRepository $personRepository, // PersonRepository $personRepository,
AccompanyingPeriodRepository $courseRepository, // AccompanyingPeriodRepository $courseRepository,
CenterRepository $centerRepository, // CenterRepository $centerRepository,
FormFactoryInterface $formFactory FormFactoryInterface $formFactory,
Request $request
) { ) {
/* @var $viewParams array The parameters for the view */ /* @var $viewParams array The parameters for the view */
/* @var $params array The parameters for the query */ /* @var $params array The parameters for the query */
@ -589,10 +551,10 @@ class SingleTaskController extends AbstractController
$viewParams['accompanyingCourse'] = null; $viewParams['accompanyingCourse'] = null;
$params['accompanyingCourse'] = null; $params['accompanyingCourse'] = null;
if (!empty($this->request->query->get('person_id', NULL))) { if (!empty($request->query->get('person_id', NULL))) {
$personId = $this->request->query->getInt('person_id', 0); $personId = $request->query->getInt('person_id', 0);
$person = $personRepository->find($personId); $person = $this->personRepository->find($personId);
if ($person === null) { if ($person === null) {
throw $this->createNotFoundException("This person ' $personId ' does not exist."); throw $this->createNotFoundException("This person ' $personId ' does not exist.");
@ -603,10 +565,10 @@ class SingleTaskController extends AbstractController
$params['person'] = $person; $params['person'] = $person;
} }
if (!empty($this->request->query->get('course_id', NULL))) { if (!empty($request->query->get('course_id', NULL))) {
$courseId = $this->request->query->getInt('course_id', 0); $courseId = $request->query->getInt('course_id', 0);
$course = $courseRepository->find($courseId); $course = $this->courseRepository->find($courseId);
if ($course === null) { if ($course === null) {
throw $this->createNotFoundException("This accompanying course ' $courseId ' does not exist."); throw $this->createNotFoundException("This accompanying course ' $courseId ' does not exist.");
@ -617,27 +579,27 @@ class SingleTaskController extends AbstractController
$params['accompanyingCourse'] = $course; $params['accompanyingCourse'] = $course;
} }
if (!empty($this->request->query->get('center_id', NULL))) { if (!empty($request->query->get('center_id', NULL))) {
$center = $centerRepository->find($this->request->query->getInt('center_id')); $center = $this->centerRepository->find($this->request->query->getInt('center_id'));
if ($center === null) { if ($center === null) {
throw $this->createNotFoundException('center not found'); throw $this->createNotFoundException('center not found');
} }
$params['center'] = $center; $params['center'] = $center;
} }
if(!empty($this->request->query->get('types', []))) { if(!empty($request->query->get('types', []))) {
$types = $this->request->query->get('types', []); $types = $this->request->query->get('types', []);
if (count($types) > 0) { if (count($types) > 0) {
$params['types'] = $types; $params['types'] = $types;
} }
} }
if (!empty($this->request->query->get('user_id', null))) { if (!empty($request->query->get('user_id', null))) {
if ($this->request->query->get('user_id') === '_unassigned') { if ($request->query->get('user_id') === '_unassigned') {
$params['unassigned'] = true; $params['unassigned'] = true;
} else { } else {
$userId = $this->request->query->getInt('user_id', 0); $userId = $request->query->getInt('user_id', 0);
$user = $this->getDoctrine()->getManager() $user = $this->getDoctrine()->getManager()
->getRepository(User::class) ->getRepository(User::class)
->find($userId); ->find($userId);
@ -651,12 +613,11 @@ class SingleTaskController extends AbstractController
} }
} }
if (!empty($this->request->query->get('scope_id'))) { if (!empty($request->query->get('scope_id'))) {
$scopeId = $this->request->query->getInt('scope_id', 0); $scopeId = $request->query->getInt('scope_id', 0);
$scope = $this->getDoctrine()->getManager() $scope = $this->scopeRepository
->getRepository(Scope::class)
->find($scopeId); ->find($scopeId);
if ($scope === null) { if ($scope === null) {
@ -668,7 +629,7 @@ class SingleTaskController extends AbstractController
} }
$possibleStatuses = \array_merge(SingleTaskRepository::DATE_STATUSES, [ 'closed' ]); $possibleStatuses = \array_merge(SingleTaskRepository::DATE_STATUSES, [ 'closed' ]);
$statuses = $this->request->query->get('status', $possibleStatuses); $statuses = $request->query->get('status', $possibleStatuses);
$diff = \array_diff($statuses, $possibleStatuses); $diff = \array_diff($statuses, $possibleStatuses);
if (count($diff) > 0) { if (count($diff) > 0) {
@ -683,7 +644,7 @@ class SingleTaskController extends AbstractController
$tasks_count = 0; $tasks_count = 0;
foreach($statuses as $status) { foreach($statuses as $status) {
if($this->request->query->has('status') if($request->query->has('status')
&& FALSE === \in_array($status, $statuses)) { && FALSE === \in_array($status, $statuses)) {
continue; continue;
} }
@ -696,14 +657,14 @@ class SingleTaskController extends AbstractController
$params['is_closed'] = true; $params['is_closed'] = true;
} }
$count = $taskRepository $count = $this->taskRepository
->countByParameters($params, $this->getUser()) ->countByParameters($params, $this->getUser())
; ;
$paginator = $paginatorFactory->create($count); $paginator = $paginatorFactory->create($count);
$viewParams['single_task_'.$status.'_count'] = $count; $viewParams['single_task_'.$status.'_count'] = $count;
$viewParams['single_task_'.$status.'_paginator'] = $paginator; $viewParams['single_task_'.$status.'_paginator'] = $paginator;
$viewParams['single_task_'.$status.'_tasks'] = $taskRepository $viewParams['single_task_'.$status.'_tasks'] = $this->taskRepository
->findByParameters($params, $this->getUser(), ->findByParameters($params, $this->getUser(),
$singleStatus ? $paginator->getCurrentPage()->getFirstItemNumber() : 0, $singleStatus ? $paginator->getCurrentPage()->getFirstItemNumber() : 0,
$singleStatus ? $paginator->getItemsPerPage() : 10) $singleStatus ? $paginator->getItemsPerPage() : 10)
@ -729,7 +690,7 @@ class SingleTaskController extends AbstractController
'add_type' => true 'add_type' => true
]); ]);
$form->handleRequest($this->request); $form->handleRequest($request);
if (isset($person)) { if (isset($person)) {
$event = new PrivacyEvent($person, array( $event = new PrivacyEvent($person, array(
@ -744,10 +705,10 @@ class SingleTaskController extends AbstractController
} }
protected function getPersonParam(EntityManagerInterface $em) protected function getPersonParam(Request $request)
{ {
$person = $em->getRepository(Person::class) $person = $this->personRepository
->find($this->request->query->getInt('person_id')) ->find($request->query->getInt('person_id'))
; ;
if (NULL === $person) { if (NULL === $person) {
@ -760,10 +721,10 @@ class SingleTaskController extends AbstractController
return $person; return $person;
} }
protected function getUserParam(EntityManagerInterface $em) protected function getUserParam(Request $request)
{ {
$user = $em->getRepository(User::class) $user = $this->userRepository
->find($this->request->query->getInt('user_id')) ->find($request->query->getInt('user_id'))
; ;
if (NULL === $user) { if (NULL === $user) {
@ -799,17 +760,16 @@ class SingleTaskController extends AbstractController
*/ */
public function listCourseTasks( public function listCourseTasks(
AccompanyingPeriodRepository $courseRepository,
SingleTaskRepository $taskRepository,
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
TranslationTranslatorInterface $translator TranslationTranslatorInterface $translator,
Request $request
): Response ): Response
{ {
if (!empty($this->request->query->get('course_id', NULL))) { if (!empty($request->query->get('course_id', NULL))) {
$courseId = $this->request->query->getInt('course_id', 0); $courseId = $request->query->getInt('course_id', 0);
$course = $courseRepository->find($courseId); $course = $this->courseRepository->find($courseId);
if ($course === null) { if ($course === null) {
throw $this->createNotFoundException("This accompanying course ' $courseId ' does not exist."); throw $this->createNotFoundException("This accompanying course ' $courseId ' does not exist.");
@ -817,13 +777,11 @@ class SingleTaskController extends AbstractController
} }
$em = $this->getDoctrine()->getManager();
if($course === NULL) { if($course === NULL) {
throw $this->createNotFoundException('Accompanying course not found'); throw $this->createNotFoundException('Accompanying course not found');
} }
$tasks = $taskRepository $tasks = $this->taskRepository
->findBy( ->findBy(
array('course' => $course) array('course' => $course)
); );

View File

@ -8,5 +8,5 @@ services:
$eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface' $eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface'
$timelineBuilder: "@chill_main.timeline_builder" $timelineBuilder: "@chill_main.timeline_builder"
$logger: "@chill.main.logger" $logger: "@chill.main.logger"
$requestStack: '@Symfony\Component\HttpFoundation\RequestStack' $em: "@doctrine.orm.default_entity_manager"
tags: ["controller.service_arguments"] tags: ["controller.service_arguments"]

View File

@ -7,14 +7,11 @@ namespace Chill\Migrations\Task;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration; use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210909153533 extends AbstractMigration final class Version20210909153533 extends AbstractMigration
{ {
public function getDescription(): string public function getDescription(): string
{ {
return ''; return 'An accompanying period can be linked to a task. Key course_id is added to single task entity';
} }
public function up(Schema $schema): void public function up(Schema $schema): void