Form login, WIP

This commit is contained in:
besbota 2024-12-05 16:44:09 +01:00
parent fab5249ac5
commit 5cde70a7b3
9 changed files with 227 additions and 0 deletions

View File

@ -17,4 +17,42 @@ class AffectationController extends AbstractController
'controller_name' => 'AffectationController', '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);
}
} }

View File

@ -0,0 +1,74 @@
<?php
namespace App\Controller;
use App\Entity\Category;
use App\Form\CategoryType;
use App\Repository\CategoryRepository;
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;
final class CategoryController extends AbstractController
{
#[Route(path:'/category', name: 'category')]
#[Route(name: 'app_category_index', methods: ['GET'])]
public function index(CategoryRepository $categoryRepository): Response
{
return $this->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);
}
}

28
src/Form/CategoryType.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace App\Form;
use App\Entity\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class CategoryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('label', TextType::class, [
'label' => 'Category label',
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Category::class,
]);
}
}

View File

@ -0,0 +1,21 @@
{% extends 'base.html.twig' %}
{% block title %}Affectation{% endblock %}
{% block body %}
<div class="wrapper">
<h1>Affectation des utilisateurs.</h1>
{% for employee in employees %}
<tr>
<td>{{ employee.id }}</td>
</tr>
{% else %}
<tr>
<td colspan="7">no records found</td>
</tr>
</div>
{% endblock %}

View File

@ -0,0 +1,4 @@
<form method="post" action="{{ path('app_category_delete', {'id': category.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ category.id) }}">
<button class="btn">Delete</button>
</form>

View File

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

View File

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Category{% endblock %}
{% block body %}
<h1>Edit Category</h1>
{{ include('category/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_category_index') }}">back to list</a>
{{ include('category/_delete_form.html.twig') }}
{% endblock %}

View File

@ -0,0 +1,34 @@
{% extends 'base.html.twig' %}
{% block title %}Category index{% endblock %}
{% block body %}
<h1>Liste des catégories disponibles :</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Label</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for category in categories %}
<tr>
<td>{{ category.id }}</td>
<td>{{ category.label }}</td>
<td>
<a href="{{ path('app_category_edit', {'id': category.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="3">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_category_new') }}">Create new</a>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New Category{% endblock %}
{% block body %}
<h1>Create new Category</h1>
{{ include('category/_form.html.twig') }}
<a href="{{ path('app_category_index') }}">back to list</a>
{% endblock %}