Skip to content
Snippets Groups Projects
Commit b80b46e6 authored by POINTUD AXEL's avatar POINTUD AXEL
Browse files

Merge branch 'feat/admin' into 'dev'

Feat/admin

See merge request !5
parents 3fab2cd2 37b5d8c6
Branches
2 merge requests!6Dev,!5Feat/admin
......@@ -10,16 +10,19 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/category')]
#[Route('/admin/category')]
class CategoryController extends AbstractController
{
#[Route('/', name: 'app_category_index', methods: ['GET'])]
public function index(CategoryRepository $categoryRepository): Response
{
$categories = $categoryRepository->findBy([], ['id' => 'DESC']);
return $this->render('category/index.html.twig', [
'categories' => $categoryRepository->findAll(),
'categories' => $categories,
]);
}
#[Route('/new', name: 'app_category_new', methods: ['GET', 'POST'])]
public function new(Request $request, CategoryRepository $categoryRepository): Response
......@@ -48,7 +51,7 @@ class CategoryController extends AbstractController
]);
}
#[Route('/{id}/edit', name: 'app_category_edit', methods: ['GET', 'POST'])]
#[Route('/edit/{id}', name: 'app_category_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Category $category, CategoryRepository $categoryRepository): Response
{
$form = $this->createForm(CategoryType::class, $category);
......@@ -69,11 +72,19 @@ class CategoryController extends AbstractController
#[Route('/{id}', name: 'app_category_delete', methods: ['POST'])]
public function delete(Request $request, Category $category, CategoryRepository $categoryRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$category->getId(), $request->request->get('_token'))) {
$categoryRepository->remove($category, true);
try {
if ($this->isCsrfTokenValid('delete'.$category->getId(), $request->request->get('_token'))) {
$categoryRepository->remove($category, true);
}
} catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) {
$errorMessage = 'Impossible de supprimer la catégorie car elle est liée à un ou plusieurs produits.';
return $this->render('category/error.html.twig', [
'errorMessage' => $errorMessage,
]);
}
return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER);
}
}
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_start(form, {'attr': {'class': 'flex flex-col gap-y-6 bg-neutral-100 max-w-[950px] rounded-lg px-6 py-12'
}}) }}
{{ form_row(form.name, {'attr': {'class': 'p-2 rounded-lg border flex w-full text-black'
}}) }}
{{ form_widget(form) }}
<div>
<button class="bg-blue-400 text-white py-2 px-6 rounded-md">
{{ button_label|default('Save') }}
</button>
<a
class="bg-gray-600 text-white py-2 px-6 rounded-md"
href="{{ path('app_category_index') }}"
>Annuler</a
>
</div>
{{ form_end(form) }}
{% 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 %}
{% extends 'base.html.twig' %} {% block title %}Edit Category{% endblock %} {%
block body %}
<h2 class="text-4xl md:text-5xl">Modifier une catégorie</h2>
<section class="mt-[50px]">
{{ include("category/_form.html.twig", { button_label: "Modifier" }) }}
</section>
{% endblock %}
\ No newline at end of file
{% extends 'base.html.twig' %} {% block body %}
<h1 class="text-5xl">Erreur !</h1>
<p class="text-red-500 mt-6">{{ errorMessage }}</p>
<a
href="{{ path('app_category_index') }}"
class="inline-block mt-12 text-center bg-red-500 text-white py-2 px-6 rounded-md"
>Ok</a
>
{% endblock %}
{% extends 'base.html.twig' %}
{% extends 'base.html.twig' %} {% block body %}
<div class="mb-[50px]">
<div class="flex flex-col gap-6 lg:flex-row lg:items-center">
<h2 class="text-4xl md:text-5xl">Gestion des catégories</h2>
<div class="flex gap-4">
<a
href="{{ path('app_category_new') }}"
class="text-center bg-blue-400 text-white py-2 px-6 rounded-md"
>Ajouter une catégorie</a
>
<a
href="{{ path('app_product_index_admin') }}"
class="text-center bg-red-500 text-white py-2 px-6 rounded-md"
>Retour à l'admin</a
>
</div>
</div>
</div>
{% block title %}Category index{% endblock %}
<section class="flex flex-col gap-y-6">
{% for category in categories %}
<article
class="flex flex-col justify-between border-b border-[#DCDCDC] pb-6 md:flex-row md:items-center"
>
<div class="flex flex-col md:flex-row md:items-center">
<p class="text-xl">{{ category.id }}</p>
<p class="md:ml-12">{{ category.name }}</p>
</div>
{% block body %}
<h1>Category index</h1>
<div class="mt-6 md:m-0">
<a
class="bg-blue-400 text-white py-2 px-6 rounded-md"
href="{{ path('app_category_edit', { id: category.id }) }}"
>Modifier</a
>
<a
class="bg-red-400 text-white py-2 px-6 rounded-md"
href="{{ path('app_category_delete', { id: category.id }) }}"
>Supprimer</a
>
</div>
</article>
{% else %}
<p>Aucune catégorie à afficher</p>
{% endfor %}
</section>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for category in categories %}
<tr>
<td>{{ category.id }}</td>
<td>{{ category.name }}</td>
<td>
<a href="{{ path('app_category_show', {'id': category.id}) }}">show</a>
<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>
{% endblock %} {# {% extends 'base.html.twig' %} {% block title %}Category
index{% endblock %} {% block body %}
<h1>Category index</h1>
<a href="{{ path('app_category_new') }}">Create new</a>
{% endblock %}
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for category in categories %}
<tr>
<td>{{ category.id }}</td>
<td>{{ category.name }}</td>
<td>
<a href="{{ path('app_category_show', { id: category.id }) }}">show</a>
<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 %} #}
{% 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 %}
{% extends 'base.html.twig' %} {% block title %}New Category{% endblock %} {%
block body %}
<h2 class="text-4xl md:text-5xl">Ajouter une catégorie</h2>
<section class="mt-[50px]">
{{ include("category/_form.html.twig", { button_label: "Ajouter" }) }}
</section>
{% endblock %}
\ No newline at end of file
......@@ -2,11 +2,19 @@
<div class="mb-[50px]">
<div class="flex flex-col gap-6 lg:flex-row lg:items-center">
<h2 class="text-4xl md:text-5xl">Administration du site</h2>
<a
<div class="flex gap-4">
<a
href="{{ path('app_product_new') }}"
class="text-center bg-blue-400 text-white py-2 px-6 rounded-md"
>Ajouter un item</a
>
<a
href="{{ path('app_category_index') }}"
class="text-center bg-gray-400 text-white py-2 px-6 rounded-md"
>Gérer les catégories</a
>
</div>
</div>
<form class="mt-4" action="{{ path('app_product_index_admin') }}" method="GET">
<label for="category-select">Trier les produits par catégorie :</label>
......@@ -30,7 +38,7 @@
<p class="md:ml-12">{{ product.category.getName() }}</p>
</div>
<div class="mt-6 md:m-0">
<div class="flex gap-[20px] mt-6 md:m-0">
<a
class="text-white bg-gray-400 py-2 px-6 rounded-md"
href="{{ path('app_product_show', { id: product.id }) }}"
......@@ -41,11 +49,12 @@
href="{{ path('app_product_edit', { id: product.id }) }}"
>Modifier</a
>
<a
class="bg-red-400 text-white py-2 px-6 rounded-md"
href="{{ path('app_product_delete', { id: product.id }) }}"
>Supprimer</a
>
<form action="{{ path('app_product_delete', { id: product.id }) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ product.id) }}">
<button type="submit" class="bg-red-400 text-white py-2 px-6 rounded-md">Supprimer</button>
</form>
</div>
</article>
{% else %}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment