Add configuration pages for material

master
Julien Rosset 2 months ago
parent d98e3f0dfc
commit c5730cc3bd

@ -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 {

@ -0,0 +1,99 @@
<?php
namespace App\Controller\Config;
use App\Entity\Machine;
use App\Entity\Material;
use App\Form\Config\MachineEditForm;
use App\Form\Config\MaterialEditForm;
use App\Misc\FlashType;
use App\Repository\MachineRepository;
use App\Repository\MaterialRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
/**
* Controller for the configuration pages of material
*/
#[Route('/Config/Material')]
class MaterialController extends AbstractController {
private readonly EntityManagerInterface $entityManager;
/**
* @var MaterialRepository The material repository
*/
private readonly MaterialRepository $materialRepository;
/**
* Initialization
*
* @param EntityManagerInterface $entityManager The entity manager
* @param MaterialRepository $materialRepository The material repository
*/
public function __construct (EntityManagerInterface $entityManager, MaterialRepository $materialRepository) {
$this->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');
}
}

@ -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 {

@ -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;

@ -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;

@ -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;

@ -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;

@ -36,4 +36,11 @@ trait TNamedEntity {
return $this;
}
/**
* @inheritDoc
*/
public function __toString (): string {
return $this->getName();
}
}

@ -0,0 +1,44 @@
<?php
namespace App\Form\Config;
use App\Entity\Material;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* The form for editing a material
*/
class MaterialEditForm extends AbstractType {
/**
* @inheritDoc
*/
public function configureOptions (OptionsResolver $resolver): void {
$resolver->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',
]);
}
}

@ -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 %}
<h1>
{% if material.id is null %}
Création nouveau matériau
{% else %}
Modification du matériau "{{ material.name }}"
{% endif %}
</h1>
{{ form(form) }}
<a href="{{ path('config_material_list') }}" class="btn btn-danger">Annuler</a>
{% endblock %}

@ -0,0 +1,64 @@
{% extends '/base.html.twig' %}
{% block title %}Liste des matériaux - {{ parent() }}{% endblock %}
{% block importmap %}{{ importmap(['app', 'datatables2']) }}{% endblock %}
{% block mainContent %}
<h1>Liste des matériaux</h1>
<div class="d-flex">
<div class="table-responsive mnw-25">
<table class="table table-sm table-striped table-hover table-bordered table-datatable2">
<thead>
<tr>
<th scope="col" data-sort-onLoad="1" class="align-middle">Nom</th>
<th scope="col" class="align-middle">Type</th>
<th scope="col" data-sort="false" class="fit-content align-middle">
<a href="{{ path('config_material_create') }}" class="btn btn-primary" data-bs-toggle="tooltip" data-bs-title="Ajouter">
<i class="fa-solid fa-square-plus"></i>
</a>
</th>
</tr>
</thead>
<tbody>
{% for material in materials %}
<tr>
<td>{{ material.name }}</td>
<td>{{ material.type.name }}</td>
<td class="fit-content">
<a href="{{ path('config_material_edit', {id: material.id}) }}"
class="text-primary me-2"
data-bs-toggle="tooltip"
data-bs-title="Éditer"
><i class="fa-solid fa-pen"></i></a>
<a href="#" class="text-danger" id="btDelete" data-bs-toggle="tooltip" data-bs-title="Supprimer">
<span data-bs-toggle="modal"
data-bs-target="#deleteConfirmation"
data-modal-dynamic-link-url="{{ path('config_material_delete', {id: material.id}) }}"
><i class="fa-solid fa-xmark"></i></span>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="modal modal-dynamic fade" id="deleteConfirmation" tabindex="-1" aria-label="btDelete" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5">Suppression</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Êtes-vous sûr de vouloir supprimer ce matériau ?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Non</button>
<a href="#" class="modal-confirm-link btn btn-danger">Oui</a>
</div>
</div>
</div>
</div>
{% endblock %}

@ -14,6 +14,7 @@
<ul class="dropdown-menu" aria-labelledby="dropdown-config">
<li><a href="{{ path('config_materialType_list') }}" class="dropdown-item">Types de matériaux</a></li>
<li><a href="{{ path('config_machine_list') }}" class="dropdown-item">Machines</a></li>
<li><a href="{{ path('config_material_list') }}" class="dropdown-item">Matériaux</a></li>
</ul>
</li>
</ul>

Loading…
Cancel
Save