diff --git a/src/Controller/TaskController.php b/src/Controller/TaskController.php index 54d9380..02b5ea8 100644 --- a/src/Controller/TaskController.php +++ b/src/Controller/TaskController.php @@ -3,7 +3,9 @@ namespace App\Controller; use App\Entity\Task; +use App\Type\TaskFilterType; use App\Type\TaskType; +use Psr\Container\ContainerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -27,7 +29,7 @@ public function create(Request $request): Response $this->getDoctrine()->getManager()->persist($task); $this->getDoctrine()->getManager()->flush(); - return $this->redirectToRoute('task_success'); + return $this->redirectToRoute('task_list'); } return $this->render("task/create.html.twig", [ @@ -42,7 +44,60 @@ public function create(Request $request): Response */ public function success(): Response { - return new Response(); } + + /** + * @Route ("/tasks", name="task_list") + * @return Response + */ + public function list(Request $request) :Response + { + $taskFilterForm = $this->createForm(TaskFilterType::class); + + $taskFilterForm->handleRequest($request); + + if ($taskFilterForm->isSubmitted() && $taskFilterForm->isValid()) { + $filter = $taskFilterForm->getData(); + if ($filter['isCompleted'] === null) { + unset($filter['isCompleted']); + } + + $tasks = $this->getDoctrine()->getRepository(Task::class) + ->findBy($filter, [ + 'dueDate' => 'DESC' + ]); + } else { + $tasks = $this->getDoctrine() + ->getManager() + ->getRepository(Task::class) + ->findBy([], [ + 'dueDate' => 'DESC', + ]); + } + + return $this->render('task/list.html.twig', [ + 'tasks' => $tasks, + 'filterForm' => $taskFilterForm->createView() + ]); + } + + /** + * @Route("/tasks/{id}/complete", name="task_complete") + * @return Response + */ + public function complete($id) + { + $task = $this->getDoctrine()->getManager()->find(Task::class, $id); + if ($task === null) { + return $this->createNotFoundException(sprintf("Task with id %s not found, $id")); + } + + $task->setIsCompleted(true); + $this->getDoctrine()->getManager()->persist($task); + $this->getDoctrine()->getManager()->flush(); + + return $this->redirectToRoute('task_list'); + } + } \ No newline at end of file diff --git a/src/Entity/Task.php b/src/Entity/Task.php index 683a266..cc9dfbf 100644 --- a/src/Entity/Task.php +++ b/src/Entity/Task.php @@ -36,10 +36,23 @@ class Task * @Assert\NotBlank * @Assert\Type("\DateTime") * @ORM\Column(type="date") - * @var + * @var \DateTime */ protected $dueDate; + /** + * @ORM\Column(type="boolean", nullable=false, options={"default" : 0}) + * @var boolean + */ + protected $isCompleted = false; + + + public function __construct() + { + $this->dueDate = new \DateTime('now'); + $this->isCompleted = false; + } + /** * @return mixed */ @@ -88,7 +101,29 @@ public function setDueDate($dueDate): void $this->dueDate = $dueDate; } + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + /** + * Return true if task is completed + * @return bool + */ + public function isCompleted(): bool + { + return $this->isCompleted; + } + /** + * @param bool $isCompleted + */ + public function setIsCompleted(bool $isCompleted): void + { + $this->isCompleted = $isCompleted; + } } \ No newline at end of file diff --git a/src/Type/TaskFilterType.php b/src/Type/TaskFilterType.php new file mode 100644 index 0000000..8d52bfa --- /dev/null +++ b/src/Type/TaskFilterType.php @@ -0,0 +1,25 @@ +setMethod('GET') + ->add('isCompleted', ChoiceType::class, [ + 'choices' => [ + 'Yes' => true, + 'No' => false, + 'Any' => null + ] + ]); + $builder->add('submit', SubmitType::class); + } + +} \ No newline at end of file diff --git a/src/Type/TaskType.php b/src/Type/TaskType.php index bf1f141..a1fcdd6 100644 --- a/src/Type/TaskType.php +++ b/src/Type/TaskType.php @@ -16,7 +16,9 @@ public function buildForm(FormBuilderInterface $builder, array $options) $builder ->add('name', TextType::class) ->add('description', TextareaType::class) - ->add('dueDate', DateType::class) + ->add('dueDate', DateType::class, [ + 'years' => range(2002,2020) + ]) ->add('save', SubmitType::class) ; } diff --git a/templates/task/list.html.twig b/templates/task/list.html.twig new file mode 100644 index 0000000..fe9a1b9 --- /dev/null +++ b/templates/task/list.html.twig @@ -0,0 +1,41 @@ +{% extends 'base.html.twig' %} + +{% block body %} +