Add configuration pages for machines

master
Julien Rosset 2 months ago
parent fae1d06e93
commit d98e3f0dfc

@ -0,0 +1,96 @@
<?php
namespace App\Controller\Config;
use App\Entity\Machine;
use App\Form\Config\MachineEditForm;
use App\Misc\FlashType;
use App\Repository\MachineRepository;
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/Machine')]
class MachineController extends AbstractController {
private readonly EntityManagerInterface $entityManager;
/**
* @var MachineRepository The machine repository
*/
private readonly MachineRepository $machineRepository;
/**
* Initialization
*
* @param EntityManagerInterface $entityManager The entity manager
* @param MachineRepository $machineRepository The machine repository
*/
public function __construct (EntityManagerInterface $entityManager, MachineRepository $machineRepository) {
$this->entityManager = $entityManager;
$this->machineRepository = $machineRepository;
}
/**
* List of machines
*
* @return Response The response
*/
#[Route('/', name: 'config_machine_list', alias: 'config_machine')]
public function list (): Response {
return $this->render(
'Config/Machine/List.html.twig',
[
'machines' => $this->machineRepository->findAll(),
]
);
}
/**
* Edit/Create a machine
*
* @param Request $request The request
* @param Machine|null $machine The machine to edit
*
* @return Response The response
*/
#[Route('/Create', name: 'config_machine_create')]
#[Route('/Edit-{id}', name: 'config_machine_edit')]
public function edit (Request $request, ?Machine $machine = null): Response {
$machine ??= new Machine();
$form = $this->createForm(MachineEditForm::class, $machine);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($machine);
$this->entityManager->flush();
$this->addFlash(FlashType::SUCCESS, 'La machine a bien été enregistré.');
return $this->redirectToRoute('config_machine_list');
}
return $this->render('Config/Machine/Edit.html.twig', [
'machine' => $machine,
'form' => $form->createView(),
]);
}
/**
* Delete a machine
*
* @param Machine $machine The machine to delete
*
* @return Response The response
*/
#[Route('/Delete-{id}', name: 'config_machine_delete')]
public function delete (Machine $machine): Response {
$this->entityManager->remove($machine);
$this->entityManager->flush();
$this->addFlash(FlashType::SUCCESS, 'La machine a bien été supprimé.');
return $this->redirectToRoute('config_machine_list');
}
}

@ -0,0 +1,46 @@
<?php
namespace App\Form\Config;
use App\Entity\Machine;
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 MachineEditForm extends AbstractType {
/**
* @inheritDoc
*/
public function configureOptions (OptionsResolver $resolver): void {
$resolver->setDefaults(
[
'data_class' => Machine::class,
]
);
}
/**
* @inheritDoc
*/
public function buildForm (FormBuilderInterface $builder, array $options): void {
$builder
->add('name', null, [
'label' => 'Nom',
])
->add('labelExtraInfo1', null, [
'label' => 'Nom d\'information complémentaire n° 1',
'required' => false,
])
->add('labelExtraInfo2', null, [
'label' => 'Nom d\'information complémentaire n° 2',
'required' => false,
])
->add('submit', SubmitType::class, [
'label' => 'Enregistrer',
]);
}
}

@ -0,0 +1,15 @@
{% extends 'base.html.twig' %}
{% block title %}{% if machine.id is null %}Création {% else %}Modification{% endif %} machine - {{ parent() }}{% endblock %}
{% block mainContent %}
<h1>
{% if machine.id is null %}
Création nouvelle machine
{% else %}
Modification du la machine "{{ machine.name }}"
{% endif %}
</h1>
{{ form(form) }}
<a href="{{ path('config_machine_list') }}" class="btn btn-danger">Annuler</a>
{% endblock %}

@ -0,0 +1,62 @@
{% extends '/base.html.twig' %}
{% block title %}Liste des machines - {{ parent() }}{% endblock %}
{% block importmap %}{{ importmap(['app', 'datatables2']) }}{% endblock %}
{% block mainContent %}
<h1>Liste des machines</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_machine_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 machine in machines %}
<tr>
<td>{{ machine.name }}</td>
<td class="fit-content">
<a href="{{ path('config_machine_edit', {id: machine.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_machine_delete', {id: machine.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 cette machine ?
</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 %}

@ -13,6 +13,7 @@
</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>
<li><a href="{{ path('config_machine_list') }}" class="dropdown-item">Machines</a></li>
</ul>
</li>
</ul>

Loading…
Cancel
Save