From 5cde70a7b39d3a05a4165a3aa5fc616fd80f4e00 Mon Sep 17 00:00:00 2001 From: besbota Date: Thu, 5 Dec 2024 16:44:09 +0100 Subject: [PATCH] Form login, WIP --- src/Controller/AffectationController.php | 38 ++++++++++++ src/Controller/CategoryController.php | 74 +++++++++++++++++++++++ src/Form/CategoryType.php | 28 +++++++++ templates/affectation/index.html.twig | 21 +++++++ templates/category/_delete_form.html.twig | 4 ++ templates/category/_form.html.twig | 4 ++ templates/category/edit.html.twig | 13 ++++ templates/category/index.html.twig | 34 +++++++++++ templates/category/new.html.twig | 11 ++++ 9 files changed, 227 insertions(+) create mode 100644 src/Controller/CategoryController.php create mode 100644 src/Form/CategoryType.php create mode 100644 templates/affectation/index.html.twig create mode 100644 templates/category/_delete_form.html.twig create mode 100644 templates/category/_form.html.twig create mode 100644 templates/category/edit.html.twig create mode 100644 templates/category/index.html.twig create mode 100644 templates/category/new.html.twig diff --git a/src/Controller/AffectationController.php b/src/Controller/AffectationController.php index db70093..c6c0047 100644 --- a/src/Controller/AffectationController.php +++ b/src/Controller/AffectationController.php @@ -17,4 +17,42 @@ class AffectationController extends AbstractController 'controller_name' => 'AffectationController', ]); } + + public function add(Request $request) + { + $validated = $request->validate([ + 'name' => 'required|string|max:255', + 'description' => 'nullable|string', + ]); + + $affectation = Affectation::create($validated); + + return response()->json([ + 'message' => 'Affectation réussie', + 'data' => $affectation + ], 201); + } + + public function list() + { + $affectations = Affectation::all(); + + return response()->json([ + 'data' => $affectations + ], 200); + } + + public function delete($id) + { + $affectation = Affectation::find($id); + + if (!$affectation) { + return response()->json(['message' => ''], 404); + } + + $affectation->delete(); + + return response()->json(['message' => ''], 200); + } + } diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php new file mode 100644 index 0000000..af71d09 --- /dev/null +++ b/src/Controller/CategoryController.php @@ -0,0 +1,74 @@ +render('category/index.html.twig', [ + 'categories' => $categoryRepository->findAll(), + ]); + } + + #[Route('/new', name: 'app_category_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $category = new Category(); + $form = $this->createForm(CategoryType::class, $category); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($category); + $entityManager->flush(); + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('category/new.html.twig', [ + 'category' => $category, + 'form' => $form, + ]); + } + + + #[Route('/{id}/edit', name: 'app_category_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Category $category, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(CategoryType::class, $category); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('category/edit.html.twig', [ + 'category' => $category, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: 'app_category_delete', methods: ['POST'])] + public function delete(Request $request, Category $category, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$category->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($category); + $entityManager->flush(); + } + + return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); + } +} diff --git a/src/Form/CategoryType.php b/src/Form/CategoryType.php new file mode 100644 index 0000000..4d68103 --- /dev/null +++ b/src/Form/CategoryType.php @@ -0,0 +1,28 @@ +add('label', TextType::class, [ + 'label' => 'Category label', + ]); + + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Category::class, + ]); + } +} diff --git a/templates/affectation/index.html.twig b/templates/affectation/index.html.twig new file mode 100644 index 0000000..2db6701 --- /dev/null +++ b/templates/affectation/index.html.twig @@ -0,0 +1,21 @@ +{% extends 'base.html.twig' %} + +{% block title %}Affectation{% endblock %} + + +{% block body %} + +
+

Affectation des utilisateurs.

+ + {% for employee in employees %} + + {{ employee.id }} + + + {% else %} + + no records found + +
+{% endblock %} diff --git a/templates/category/_delete_form.html.twig b/templates/category/_delete_form.html.twig new file mode 100644 index 0000000..535a698 --- /dev/null +++ b/templates/category/_delete_form.html.twig @@ -0,0 +1,4 @@ +
+ + +
diff --git a/templates/category/_form.html.twig b/templates/category/_form.html.twig new file mode 100644 index 0000000..bf20b98 --- /dev/null +++ b/templates/category/_form.html.twig @@ -0,0 +1,4 @@ +{{ form_start(form) }} + {{ form_widget(form) }} + +{{ form_end(form) }} diff --git a/templates/category/edit.html.twig b/templates/category/edit.html.twig new file mode 100644 index 0000000..2be3489 --- /dev/null +++ b/templates/category/edit.html.twig @@ -0,0 +1,13 @@ +{% extends 'base.html.twig' %} + +{% block title %}Edit Category{% endblock %} + +{% block body %} +

Edit Category

+ + {{ include('category/_form.html.twig', {'button_label': 'Update'}) }} + + back to list + + {{ include('category/_delete_form.html.twig') }} +{% endblock %} diff --git a/templates/category/index.html.twig b/templates/category/index.html.twig new file mode 100644 index 0000000..311d058 --- /dev/null +++ b/templates/category/index.html.twig @@ -0,0 +1,34 @@ +{% extends 'base.html.twig' %} + +{% block title %}Category index{% endblock %} + +{% block body %} +

Liste des catégories disponibles :

+ + + + + + + + + + + {% for category in categories %} + + + + + + {% else %} + + + + {% endfor %} + +
IdLabelactions
{{ category.id }}{{ category.label }} + edit +
no records found
+ + Create new +{% endblock %} diff --git a/templates/category/new.html.twig b/templates/category/new.html.twig new file mode 100644 index 0000000..bab5f3c --- /dev/null +++ b/templates/category/new.html.twig @@ -0,0 +1,11 @@ +{% extends 'base.html.twig' %} + +{% block title %}New Category{% endblock %} + +{% block body %} +

Create new Category

+ + {{ include('category/_form.html.twig') }} + + back to list +{% endblock %}