diff --git a/src/Controller/Config/MachineController.php b/src/Controller/Config/MachineController.php index 16b604a..425dc3f 100644 --- a/src/Controller/Config/MachineController.php +++ b/src/Controller/Config/MachineController.php @@ -13,7 +13,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; /** - * Controllers for the configuration pages + * Controller for the configuration pages of machines */ #[Route('/Config/Machine')] class MachineController extends AbstractController { diff --git a/src/Controller/Config/MaterialController.php b/src/Controller/Config/MaterialController.php new file mode 100644 index 0000000..c79f9b9 --- /dev/null +++ b/src/Controller/Config/MaterialController.php @@ -0,0 +1,99 @@ +entityManager = $entityManager; + $this->materialRepository = $materialRepository; + } + + /** + * List of materials + * + * @return Response The response + */ + #[Route('/', name: 'config_material_list', alias: 'config_material')] + public function list (): Response { + return $this->render( + 'Config/Material/List.html.twig', + [ + 'materials' => $this->materialRepository->findAll(), + ] + ); + } + + /** + * Edit/Create a material + * + * @param Request $request The request + * @param Material|null $material The material to edit + * + * @return Response The response + */ + #[Route('/Create', name: 'config_material_create')] + #[Route('/Edit-{id}', name: 'config_material_edit')] + public function edit (Request $request, ?Material $material = null): Response { + $material ??= new Material(); + + $form = $this->createForm(MaterialEditForm::class, $material); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $this->entityManager->persist($material); + $this->entityManager->flush(); + + $this->addFlash(FlashType::SUCCESS, 'Le matériau a bien été enregistré.'); + return $this->redirectToRoute('config_material_list'); + } + + return $this->render('Config/Material/Edit.html.twig', [ + 'material' => $material, + 'form' => $form->createView(), + ]); + } + + /** + * Delete a material + * + * @param Material $material The material to delete + * + * @return Response The response + */ + #[Route('/Delete-{id}', name: 'config_material_delete')] + public function delete (Material $material): Response { + $this->entityManager->remove($material); + $this->entityManager->flush(); + + $this->addFlash(FlashType::SUCCESS, 'Le matériau a bien été supprimé.'); + return $this->redirectToRoute('config_material_list'); + } +} diff --git a/src/Controller/Config/MaterialTypeController.php b/src/Controller/Config/MaterialTypeController.php index 5a8811d..085e89c 100644 --- a/src/Controller/Config/MaterialTypeController.php +++ b/src/Controller/Config/MaterialTypeController.php @@ -13,7 +13,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; /** - * Controllers for the configuration pages + * Controller for the configuration pages of material types */ #[Route('/Config/MaterialType')] class MaterialTypeController extends AbstractController { diff --git a/src/Entity/Machine.php b/src/Entity/Machine.php index c95c17f..13aa92d 100644 --- a/src/Entity/Machine.php +++ b/src/Entity/Machine.php @@ -6,6 +6,7 @@ use App\Repository\MachineRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Stringable; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Validator\Constraints as Assert; @@ -14,7 +15,7 @@ use Symfony\Component\Validator\Constraints as Assert; */ #[ORM\Entity(repositoryClass: MachineRepository::class)] #[UniqueEntity(fields: ['name'], message: 'Il existe déjà une machine avec ce nom')] -class Machine { +class Machine implements Stringable { use TBaseEntity; use TNamedEntity; diff --git a/src/Entity/Material.php b/src/Entity/Material.php index fa7a832..285f9bb 100644 --- a/src/Entity/Material.php +++ b/src/Entity/Material.php @@ -6,6 +6,7 @@ use App\Repository\MaterialRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Stringable; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Validator\Constraints as Assert; @@ -14,7 +15,7 @@ use Symfony\Component\Validator\Constraints as Assert; */ #[ORM\Entity(repositoryClass: MaterialRepository::class)] #[UniqueEntity(fields: ['name'], message: 'Il existe déjà un matériau avec ce nom')] -class Material { +class Material implements Stringable { use TBaseEntity; use TNamedEntity; diff --git a/src/Entity/MaterialType.php b/src/Entity/MaterialType.php index 9337525..58d97a4 100644 --- a/src/Entity/MaterialType.php +++ b/src/Entity/MaterialType.php @@ -4,6 +4,7 @@ namespace App\Entity; use App\Repository\MaterialTypeRepository; use Doctrine\ORM\Mapping as ORM; +use Stringable; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Validator\Constraints as Assert; @@ -12,7 +13,7 @@ use Symfony\Component\Validator\Constraints as Assert; */ #[ORM\Entity(repositoryClass: MaterialTypeRepository::class)] #[UniqueEntity(fields: ['name'], message: 'Il existe déjà un type de matériau avec ce nom')] -class MaterialType { +class MaterialType implements Stringable { use TBaseEntity; use TNamedEntity; diff --git a/src/Entity/Recipe.php b/src/Entity/Recipe.php index ef7ed3f..9ec76a0 100644 --- a/src/Entity/Recipe.php +++ b/src/Entity/Recipe.php @@ -6,13 +6,14 @@ use App\Repository\RecipeRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Stringable; use Symfony\Component\Validator\Constraints as Assert; /** * A crafting recipe */ #[ORM\Entity(repositoryClass: RecipeRepository::class)] -class Recipe { +class Recipe implements Stringable { use TBaseEntity; use TNamedEntity; diff --git a/src/Entity/TNamedEntity.php b/src/Entity/TNamedEntity.php index bf4335f..4629a5d 100644 --- a/src/Entity/TNamedEntity.php +++ b/src/Entity/TNamedEntity.php @@ -36,4 +36,11 @@ trait TNamedEntity { return $this; } + + /** + * @inheritDoc + */ + public function __toString (): string { + return $this->getName(); + } } \ No newline at end of file diff --git a/src/Form/Config/MaterialEditForm.php b/src/Form/Config/MaterialEditForm.php new file mode 100644 index 0000000..e45025e --- /dev/null +++ b/src/Form/Config/MaterialEditForm.php @@ -0,0 +1,44 @@ +setDefaults( + [ + 'data_class' => Material::class, + ] + ); + } + + /** + * @inheritDoc + */ + public function buildForm (FormBuilderInterface $builder, array $options): void { + $builder + ->add('name', null, [ + 'label' => 'Nom', + ]) + ->add('type', null, [ + 'label' => 'Type', + ]) + ->add('isCraftableByDefault', null, [ + 'label' => 'Est-ce que ce matériel est craftable par défaut ?', + ]) + ->add('submit', SubmitType::class, [ + 'label' => 'Enregistrer', + ]); + } +} \ No newline at end of file diff --git a/templates/Config/Material/Edit.html.twig b/templates/Config/Material/Edit.html.twig new file mode 100644 index 0000000..b4d3a2f --- /dev/null +++ b/templates/Config/Material/Edit.html.twig @@ -0,0 +1,15 @@ +{% extends 'base.html.twig' %} + +{% block title %}{% if material.id is null %}Création {% else %}Modification{% endif %} matériau - {{ parent() }}{% endblock %} + +{% block mainContent %} +

+ {% if material.id is null %} + Création nouveau matériau + {% else %} + Modification du matériau "{{ material.name }}" + {% endif %} +

+ {{ form(form) }} + Annuler +{% endblock %} diff --git a/templates/Config/Material/List.html.twig b/templates/Config/Material/List.html.twig new file mode 100644 index 0000000..b17ebc2 --- /dev/null +++ b/templates/Config/Material/List.html.twig @@ -0,0 +1,64 @@ +{% extends '/base.html.twig' %} + +{% block title %}Liste des matériaux - {{ parent() }}{% endblock %} +{% block importmap %}{{ importmap(['app', 'datatables2']) }}{% endblock %} + +{% block mainContent %} +

Liste des matériaux

+
+
+ + + + + + + + + + {% for material in materials %} + + + + + + {% endfor %} + +
NomType + + + +
{{ material.name }}{{ material.type.name }} + + + + +
+
+
+ + +{% endblock %} \ No newline at end of file diff --git a/templates/Core/Main.html.twig b/templates/Core/Main.html.twig index 89639f5..4816f1c 100644 --- a/templates/Core/Main.html.twig +++ b/templates/Core/Main.html.twig @@ -14,6 +14,7 @@