diff --git a/src/Controller/TaskController.php b/src/Controller/TaskController.php index c5d844a..299f856 100644 --- a/src/Controller/TaskController.php +++ b/src/Controller/TaskController.php @@ -8,6 +8,7 @@ use App\Repository\ActorRepository; use App\Repository\TaskRepository; use App\Form\Actors\TaskAddForm; +use App\Form\Activity\TaskDeleteForm; class TaskController extends AbstractExtendedController { @@ -54,12 +55,40 @@ } } + // Task delete form + /** @var TaskDeleteForm $taskDeleteForm */ + $taskDeleteForm = $this->createNamedCustomForm('taskDelete', TaskDeleteForm::class); + $taskDeleteForm->handleRequest($request); + if ($taskDeleteForm->isSubmitted() && $taskDeleteForm->isValid()) { + $errors = $taskDeleteForm->validate(); + if (empty($errors)) { + $task = $taskDeleteForm->getTask($taskRepository); + if (!is_null($task)) { + $entityManager->remove($task); + $entityManager->flush(); + return $this->ajaxFormAnswer([ + 'refresh' => true, + ]); + } + return $this->ajaxFormAnswer([ + 'error' => 'Tâche non trouvé', + 'code' => Response::HTTP_BAD_REQUEST, + ]); + } else { + return $this->ajaxFormAnswer([ + 'error' => join('
', $errors), + 'code' => Response::HTTP_BAD_REQUEST, + ]); + } + } + // Get tasks $tasks = $taskRepository->findAll(); return $this->render('task/tasks.html.twig', [ 'tasks' => $tasks, 'taskAddForm' => $taskAddForm, + 'taskDeleteForm' => $taskDeleteForm, ]); } } \ No newline at end of file diff --git a/src/Form/Task/TaskDeleteForm.php b/src/Form/Task/TaskDeleteForm.php new file mode 100644 index 0000000..1e208e0 --- /dev/null +++ b/src/Form/Task/TaskDeleteForm.php @@ -0,0 +1,40 @@ +form->getData(); + return $taskRepository->find($data['id']); + } + + /** + * {@inheritdoc} + */ + public function getTemplate(): string + { + return '_includes/html/form/task/delete.html.twig'; + } + + /** + * {@inheritdoc} + */ + protected function addFields($formBuilder, $options): void + { + $formBuilder->add('id', HiddenType::class)->add('submit', SubmitType::class); + } +} \ No newline at end of file diff --git a/templates/_includes/html/arrays/task.html.twig b/templates/_includes/html/arrays/task.html.twig index a1c4353..d77e688 100644 --- a/templates/_includes/html/arrays/task.html.twig +++ b/templates/_includes/html/arrays/task.html.twig @@ -16,6 +16,7 @@ {% endif %} Planifié Executé + Actions @@ -38,6 +39,8 @@ {{ dateTools.euro(task.planifiedDate) }} {% endif %} {% if task.executionDate is not null %} {{ dateTools.euro(task.executionDate) }} {% endif %} + {% if taskDeleteForm is defined %} {% include '_includes/html/genericForm.html.twig' with {'form': taskDeleteForm, 'data': {task: task, variant: 'icon'}} only %} + {% endif %} {% endfor %} diff --git a/templates/_includes/html/form/task/delete.html.twig b/templates/_includes/html/form/task/delete.html.twig new file mode 100644 index 0000000..773fe57 --- /dev/null +++ b/templates/_includes/html/form/task/delete.html.twig @@ -0,0 +1,15 @@ +{{ form_start(form, {attr: {class: 'ajax-form', 'data-form-confirmation': "Voulez-vous supprimer la tâche "~data.task.name~" ?"}}) }} +{{ form_widget(form.id, {value: data.task.id}) }} +{% if data.variant|default('icon') == 'icon' %} +{{ form_widget(form.submit, { + label: 'Supprimer', + label_html: true, + attr: { + class: 'icon-button', + title: 'Supprimer la tâche', + } +})}} +{% elseif data.variant == 'full' %} +{{ form_widget(form.submit, {label: 'Supprimer la tâche', attr: {class: "w-100"}})}} +{% endif %} +{{ form_end(form) }} \ No newline at end of file diff --git a/templates/task/tasks.html.twig b/templates/task/tasks.html.twig index 4e82824..decb68b 100644 --- a/templates/task/tasks.html.twig +++ b/templates/task/tasks.html.twig @@ -10,7 +10,7 @@

Tâches

- {% include "_includes/html/arrays/task.html.twig" with {tasks: tasks} only %} + {% include "_includes/html/arrays/task.html.twig" with {tasks: tasks, taskDeleteForm: taskDeleteForm} only %}