Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions src/Controller/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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", [
Expand All @@ -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');
}

}
37 changes: 36 additions & 1 deletion src/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

}
25 changes: 25 additions & 0 deletions src/Type/TaskFilterType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;

class TaskFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setMethod('GET')
->add('isCompleted', ChoiceType::class, [
'choices' => [
'Yes' => true,
'No' => false,
'Any' => null
]
]);
$builder->add('submit', SubmitType::class);
}

}
4 changes: 3 additions & 1 deletion src/Type/TaskType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
}
Expand Down
41 changes: 41 additions & 0 deletions templates/task/list.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% extends 'base.html.twig' %}

{% block body %}
<div class="container">
<div class="row">
<div class="col">
</div>
<div class="col">
{{ form(filterForm, {'action' : path('task_list'), 'method' : 'GET'}) }}
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Due date</th>
<th scope="col">Is completed</th>
<th scope="col">Ready</th>
</tr>
</thead>
<tbody>
{% for task in tasks %}
<tr>
<th scope="row">{{ task.id }}</th>
<td>{{ task.name }}</td>
<td>{{ task.description }}</td>
<td>{{ task.dueDate|date("d.m.Y") }}</td>
<td>{{ task.isCompleted ? 'Completed' : 'Not completed' }}</td>
<td><a href="{{ path("task_complete", {"id" : task.id}) }}"><input type="button" class="btn btn-danger" value="Ready"></a> </td>
</tr>
{% endfor %}
</tbody>
</table>

<a href="{{ path("task_create") }}">Create task</a>
</div>
<div class="col">
</div>
</div>
</div>
{% endblock %}