diff --git a/src/Controller/TaskController.php b/src/Controller/TaskController.php index 004f3f5..5b9f2df 100644 --- a/src/Controller/TaskController.php +++ b/src/Controller/TaskController.php @@ -3,8 +3,11 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use App\Entity\ActorEntity; use App\Entity\TaskEntity; +use App\Repository\ActorRepository; use App\Repository\TaskRepository; +use App\Form\Actors\TaskAddForm; class TaskController extends AbstractExtendedController { @@ -19,13 +22,43 @@ { // Get repositories $entityManager = $this->getDoctrine()->getManager(); + /** @var ActorRepository $actorRepository */ + $actorRepository = $entityManager->getRepository(ActorEntity::class); /** @var TaskRepository $taskRepository */ $taskRepository = $entityManager->getRepository(TaskEntity::class); + // Get actors + $actors = $actorRepository->findAll(); + + // Task add form + /** @var TaskAddForm $taskAddForm */ + $taskAddForm = $this->createNamedCustomForm('taskAdd', TaskAddForm::class, [ + 'actors' => $actors, + ]); + $taskAddForm->handleRequest($request); + if ($taskAddForm->isSubmitted() && $taskAddForm->isValid()) { + $errors = $taskAddForm->validate(); + if (empty($errors)) { + $task = $taskAddForm->getTask(); + $entityManager->persist($task); + $entityManager->flush(); + return $this->ajaxFormAnswer([ + 'refresh' => true, + 'reset' => true, + ]); + } else { + return $this->ajaxFormAnswer([ + 'error' => join('
', $errors), + 'code' => Response::HTTP_BAD_REQUEST, + ]); + } + } + // Get tasks $tasks = $taskRepository->findAll(); return $this->render('task/tasks.html.twig', [ + 'taskAddForm' => $taskAddForm, 'tasks' => $tasks, ]); } diff --git a/src/Form/Task/TaskAddForm.php b/src/Form/Task/TaskAddForm.php new file mode 100644 index 0000000..49e5dfe --- /dev/null +++ b/src/Form/Task/TaskAddForm.php @@ -0,0 +1,97 @@ +actors = $data['actors']; + parent::__construct($formBuilder, $data, $usesToken); + } + + /** + * Create and return taks entity from data + * + * @return TaskEntity + */ + public function getTask(): TaskEntity + { + $data = $this->form->getData(); + $task = new TaskEntity(); + $task->generateId(); + $task->setName($data['name']); + $task->setStatus($data['status']); + $task->setActor($data['actor']); + $task->setPlanifiedDate($data['planifiedDate']); + $task->setExecutionDate($data['executionDate']); + + return $task; + } + + /** + * {@inheritdoc} + */ + public function getTemplate(): string + { + return '_includes/html/form/task/add.html.twig'; + } + + /** + * {@inheritdoc} + */ + protected function addFields($formBuilder, $options): void + { + $formBuilder->add('name', TextType::class, [ + 'required' => true + ]) + ->add('actor', EntityType::class, [ + 'class' => ActorEntity::class, + 'choices' => $this->actors, + 'group_by' => function (ActorEntity $choice) { + if ($choice->getFollowed()) { + return 'Suivi'; + } + if ($choice->getActive()) { + return 'Actif'; + } + + return 'Inactif'; + }, + 'choice_label' => 'displayName', + 'required' => false, + ]) + ->add('status', ChoiceType::class, [ + 'choices' => TaskEntity::VALID_STATUS, + 'choice_label' => function ($choice, $key, $value) { + return StatusEnum::STATUS_NAME[$choice]; + }, + ]) + ->add('planifiedDate', DateType::class, [ + 'required' => false, + 'widget' => 'single_text', + ]) + ->add('executionDate', DateType::class, [ + 'required' => false, + 'widget' => 'single_text', + ]) + ->add('submit', SubmitType::class); + } +} \ No newline at end of file diff --git a/templates/_includes/html/form/task/add.html.twig b/templates/_includes/html/form/task/add.html.twig new file mode 100644 index 0000000..fa9a1f8 --- /dev/null +++ b/templates/_includes/html/form/task/add.html.twig @@ -0,0 +1,33 @@ +{% import '_includes/macros/status.html.twig' as statusTools %} + +{{ form_start(form, {attr: {class: 'ajax-form'}}) }} +
+
+
+ {{ form_label(form.name, 'Nom', {label_attr: {class: 'input-text-label'}}) }} + {{ form_widget(form.name, {attr: {class: 'w-100'}}) }} +
+
+ {{ form_label(form.status, 'Status', {label_attr: {class: 'input-text-label'}}) }} + {{ form_widget(form.status, {attr: {class: 'w-100'}}) }} +
+
+ {{ form_label(form.actor, 'Acteur', {label_attr: {class: 'input-text-label'}}) }} + {{ form_widget(form.actor, {attr: {class: 'w-100'}}) }} +
+
+
+
+ {{ form_label(form.planifiedDate, 'Planifié', {label_attr: {class: 'input-text-label'}}) }} + {{ form_widget(form.planifiedDate, {attr: {class: 'w-100'}}) }} +
+
+ {{ form_label(form.executionDate, 'Executé', {label_attr: {class: 'input-text-label'}}) }} + {{ form_widget(form.executionDate, {attr: {class: 'w-100'}}) }} +
+
+
+

+

+{{ form_widget(form.submit, {attr: {class: 'w-100'}, label: 'Nouvelle activité'}) }} +{{ form_end(form) }} \ No newline at end of file diff --git a/templates/task/tasks.html.twig b/templates/task/tasks.html.twig index fafa0ba..4e82824 100644 --- a/templates/task/tasks.html.twig +++ b/templates/task/tasks.html.twig @@ -14,4 +14,14 @@ +
+
+
+

Ajouter une tâche

+
+
+ {% include '_includes/html/genericForm.html.twig' with {form: taskAddForm} only %} +
+
+
{% endblock %} \ No newline at end of file