Add controller for material types

master
Julien Rosset 2 months ago
parent 067786a3ea
commit 4e299630b6

@ -0,0 +1,89 @@
<?php
namespace App\Controller\Config;
use App\Entity\MaterialType;
use App\Form\Config\MaterialTypeEditForm;
use App\Misc\FlashType;
use App\Repository\MaterialTypeRepository;
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;
/**
* Controllers for the configuration pages
*/
#[Route('/Config/MaterialType')]
class MaterialTypeController extends AbstractController {
private readonly EntityManagerInterface $entityManager;
/**
* @var MaterialTypeRepository The material type repository
*/
private readonly MaterialTypeRepository $materialTypeRepository;
/**
* Initialisation
*
* @param MaterialTypeRepository $materialTypeRepository The material type repository
*/
public function __construct (EntityManagerInterface $entityManager, MaterialTypeRepository $materialTypeRepository) {
$this->entityManager = $entityManager;
$this->materialTypeRepository = $materialTypeRepository;
}
/**
* List of material types
*
* @return Response The response
*/
#[Route('/', name: 'config_materialType_list', alias: 'config_materialType')]
public function list (): Response {
return $this->render(
'Config/MaterialType/List.html.twig',
[
'materialTypes' => $this->materialTypeRepository->findAll(),
]
);
}
/**
* Edit/Create a material type
*
* @return Response The response
*/
#[Route('/Create', name: 'config_materialType_create')]
#[Route('/Edit-{id}', name: 'config_materialType_edit')]
public function edit (Request $request, ?MaterialType $materialType = null): Response {
$materialType ??= new MaterialType();
$form = $this->createForm(MaterialTypeEditForm::class, $materialType);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($materialType);
$this->entityManager->flush();
$this->addFlash(FlashType::SUCCESS, 'Le type de matériau a bien été enregistré.');
return $this->redirectToRoute('config_materialType_list');
}
return $this->render('Config/MaterialType/Edit.html.twig', [
'form' => $form->createView(),
]);
}
/**
* Delete a material type
*
* @return Response The response
*/
#[Route('/Delete-{id}', name: 'config_materialType_delete')]
public function delete (MaterialType $materialType): Response {
$this->entityManager->remove($materialType);
$this->entityManager->flush();
$this->addFlash(FlashType::SUCCESS, 'Le type de matériau a bien été supprimé.');
return $this->redirectToRoute('config_materialType_list');
}
}

@ -0,0 +1,45 @@
<?php
namespace App\Form\Config;
use App\Entity\MaterialType;
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 type
*/
class MaterialTypeEditForm extends AbstractType {
/**
* @inheritDoc
*/
public function configureOptions (OptionsResolver $resolver): void {
$resolver->setDefaults(
[
'data_class' => MaterialType::class,
]
);
}
/**
* @inheritDoc
*/
public function buildForm (FormBuilderInterface $builder, array $options): void {
$builder
->add('name', null, [
'label' => 'Nom',
])
->add('stackName', null, [
'label' => 'Nom de la pile',
])
->add('stackSize', null, [
'label' => 'Taille de la pile',
])
->add('submit', SubmitType::class, [
'label' => 'Enregistrer',
]);
}
}

@ -0,0 +1,8 @@
{% extends 'base.html.twig' %}
{% block title %}Modification type de matériau - {{ parent() }}{% endblock %}
{% block mainContent %}
<h1>Modification type de matériau</h1>
{{ form(form) }}
{% endblock %}

@ -0,0 +1,62 @@
{% extends '/base.html.twig' %}
{% block title %}Liste des types de matériaux - {{ parent() }}{% endblock %}
{% block importmap %}{{ importmap(['app', 'datatables2']) }}{% endblock %}
{% block mainContent %}
<h1>Liste des types de 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" data-sort="false" class="fit-content align-middle">
<a href="{{ path('config_materialType_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 materialType in materialTypes %}
<tr>
<td>{{ materialType.name }}</td>
<td class="fit-content">
<a href="{{ path('config_materialType_edit', {id: materialType.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_materialType_delete', {id: materialType.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 type de 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 %}

@ -5,7 +5,18 @@
{% block mainContent %}
<h1>Recipe Manager</h1>
{% if app.user %}
Bienvenu {{ app.user }}
<nav class="navbar navbar-expand-lg py-0">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle py-0" id="dropdown-config" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Configuration
</a>
<ul class="dropdown-menu" aria-labelledby="dropdown-config">
<li><a href="{{ path('config_materialType_list') }}" class="dropdown-item">Types de matériaux</a></li>
</ul>
</li>
</ul>
</nav>
{% else %}
<p>Bienvenu sur Recipe Manager, le gestionnaire de recette de jeux vidéos.</p>
<p>Merci de vous <a href="{{ path('user_signIn') }}">connecter</a> ou <a href="{{ path('user_signOut') }}">créer un compte</a> pour commencer.</p>

Loading…
Cancel
Save