Menu Chef
This commit is contained in:
ExostFlash 2023-11-26 21:14:56 +01:00
parent 6c07fa278f
commit 4efbc3c680
17 changed files with 515 additions and 0 deletions

View file

@ -32,6 +32,7 @@ security:
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/admin, roles: ROLE_Root }
- { path: ^/chef, roles: "ROLE_Root, ROLE_Chef" }
# - { path: ^/profile, roles: ROLE_USER }
when@test:

View file

@ -0,0 +1,139 @@
<?php
namespace App\Controller;
use App\Entity\Menu;
use App\Form\MenuType;
use App\Repository\MenuRepository;
use App\Repository\UserRepository;
use App\Repository\RestoRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
#[Route('/chef/menu')]
class MenuChefController extends AbstractController
{
private $id_resto;
private $id_user;
private $userRepository;
private $restoRepository;
public function __construct(userRepository $userRepository, restoRepository $restoRepository, Security $security)
{
$utilisateur = $security->getUser();
$email_user = $utilisateur->getUserIdentifier();
$user = $userRepository->findOneBy(['email' => $email_user]);
$id_resto = $user->getIdResto();
$id_user = $user->getId();
$this->id_resto = $id_resto;
$this->id_user = $id_user;
$this->userRepository = $userRepository;
$this->restoRepository = $restoRepository;
}
#[Route('/', name: 'app_menu_chef_index', methods: ['GET'])]
public function index(MenuRepository $menuRepository): Response
{
if ($this->id_resto != '') {
$menus = $menuRepository->findBy(['id_resto' => $this->id_resto]);
} else {
$this->id_resto = 0;
$menus = $menuRepository->findAll();
}
$users = [];
foreach ($menus as $menu) {
$userId = $menu->getIdUsers();
$user = $this->userRepository->find($userId);
if ($user !== null) {
$users[$userId] = $user;
}
}
$restos = [];
foreach ($menus as $menu) {
$restoId = $menu->getIdResto();
$resto = $this->restoRepository->find($restoId);
if ($resto !== null) {
$restos[$restoId] = $resto;
}
}
return $this->render('menu_chef/index.html.twig', [
'menus' => $menus,
'users' => $users,
'restos' => $restos,
'id_resto' => $this->id_resto,
]);
}
#[Route('/new', name: 'app_menu_chef_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$id_resto = $this->id_resto;
$id_user = $this->id_user;
$menu = new Menu();
$menu->setIdResto($id_resto);
$menu->setIdUsers($id_user);
$form = $this->createForm(MenuType::class, $menu);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($menu);
$entityManager->flush();
return $this->redirectToRoute('app_menu_chef_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('menu_chef/new.html.twig', [
'menu' => $menu,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_menu_chef_show', methods: ['GET'])]
public function show(Menu $menu): Response
{
return $this->render('menu_chef/show.html.twig', [
'menu' => $menu,
]);
}
#[Route('/{id}/edit', name: 'app_menu_chef_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Menu $menu, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(MenuType::class, $menu);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_menu_chef_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('menu_chef/edit.html.twig', [
'menu' => $menu,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_menu_chef_delete', methods: ['POST'])]
public function delete(Request $request, Menu $menu, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete' . $menu->getId(), $request->request->get('_token'))) {
$entityManager->remove($menu);
$entityManager->flush();
}
return $this->redirectToRoute('app_menu_chef_index', [], Response::HTTP_SEE_OTHER);
}
}

View file

@ -0,0 +1,81 @@
<?php
namespace App\Controller;
use App\Entity\Resto;
use App\Form\RestoType;
use App\Repository\RestoRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/admin/resto')]
class RestoAdminController extends AbstractController
{
#[Route('/', name: 'app_resto_admin_index', methods: ['GET'])]
public function index(RestoRepository $restoRepository): Response
{
return $this->render('resto_admin/index.html.twig', [
'restos' => $restoRepository->findAll(),
]);
}
#[Route('/new', name: 'app_resto_admin_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$resto = new Resto();
$form = $this->createForm(RestoType::class, $resto);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($resto);
$entityManager->flush();
return $this->redirectToRoute('app_resto_admin_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('resto_admin/new.html.twig', [
'resto' => $resto,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_resto_admin_show', methods: ['GET'])]
public function show(Resto $resto): Response
{
return $this->render('resto_admin/show.html.twig', [
'resto' => $resto,
]);
}
#[Route('/{id}/edit', name: 'app_resto_admin_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Resto $resto, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(RestoType::class, $resto);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_resto_admin_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('resto_admin/edit.html.twig', [
'resto' => $resto,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_resto_admin_delete', methods: ['POST'])]
public function delete(Request $request, Resto $resto, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$resto->getId(), $request->request->get('_token'))) {
$entityManager->remove($resto);
$entityManager->flush();
}
return $this->redirectToRoute('app_resto_admin_index', [], Response::HTTP_SEE_OTHER);
}
}

35
src/Form/MenuType.php Normal file
View file

@ -0,0 +1,35 @@
<?php
namespace App\Form;
use App\Entity\Menu;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
class MenuType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('entre')
->add('plat')
->add('dessert')
->add('id_resto', HiddenType::class, [
'disabled' => true, // Désactive le champ
'data' => $options['data']->getIdResto() // Remplacez $valeurSpecifique par la valeur souhaitée
])
->add('id_users', HiddenType::class, [
'disabled' => true, // Désactive le champ
'data' => $options['data']->getIdUsers() // Remplacez $valeurSpecifique par la valeur souhaitée
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Menu::class,
]);
}
}

27
src/Form/RestoType.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace App\Form;
use App\Entity\Resto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class RestoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('pay')
->add('ville')
->add('address')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Resto::class,
]);
}
}

View file

@ -0,0 +1,4 @@
<form method="post" action="{{ path('app_menu_chef_delete', {'id': menu.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ menu.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 Menu{% endblock %}
{% block body %}
<h1>Edit Menu</h1>
{{ include('menu_chef/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_menu_chef_index') }}">back to list</a>
{{ include('menu_chef/_delete_form.html.twig') }}
{% endblock %}

View file

@ -0,0 +1,53 @@
{% extends 'base.html.twig' %}
{% block title %}Menu index{% endblock %}
{% block body %}
<h1>Menu du resto {{ id_resto }}</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Entre</th>
<th>Plat</th>
<th>Dessert</th>
<th>Adresse</th>
<th>Chef</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for menu in menus %}
<tr>
<td>{{ menu.id }}</td>
<td>{{ menu.entre }}</td>
<td>{{ menu.plat }}</td>
<td>{{ menu.dessert }}</td>
{% if restos[menu.idResto] is defined %}
<td>{{ restos[menu.idResto].address }}</td>
{% else %}
<td>Resto Not Found</td>
{% endif %}
{% if users[menu.idUsers] is defined %}
<td>{{ users[menu.idUsers].name }} {{ users[menu.idUsers].fullname }}</td>
{% else %}
<td>User Not Found</td>
{% endif %}
<td>
<a href="{{ path('app_menu_chef_show', {'id': menu.id}) }}">show</a>
<a href="{{ path('app_menu_chef_edit', {'id': menu.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="7">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_menu_chef_new') }}">Create new</a>
{% endblock %}

View file

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

View file

@ -0,0 +1,42 @@
{% extends 'base.html.twig' %}
{% block title %}Menu{% endblock %}
{% block body %}
<h1>Menu</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ menu.id }}</td>
</tr>
<tr>
<th>Entre</th>
<td>{{ menu.entre }}</td>
</tr>
<tr>
<th>Plat</th>
<td>{{ menu.plat }}</td>
</tr>
<tr>
<th>Dessert</th>
<td>{{ menu.dessert }}</td>
</tr>
<tr>
<th>Id_resto</th>
<td>{{ menu.idResto }}</td>
</tr>
<tr>
<th>Id_users</th>
<td>{{ menu.idUsers }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('app_menu_chef_index') }}">back to list</a>
<a href="{{ path('app_menu_chef_edit', {'id': menu.id}) }}">edit</a>
{{ include('menu_chef/_delete_form.html.twig') }}
{% endblock %}

View file

@ -0,0 +1,4 @@
<form method="post" action="{{ path('app_resto_admin_delete', {'id': resto.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ resto.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 Resto{% endblock %}
{% block body %}
<h1>Edit Resto</h1>
{{ include('resto_admin/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_resto_admin_index') }}">back to list</a>
{{ include('resto_admin/_delete_form.html.twig') }}
{% endblock %}

View file

@ -0,0 +1,39 @@
{% extends 'base.html.twig' %}
{% block title %}Resto index{% endblock %}
{% block body %}
<h1>Resto index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Pay</th>
<th>Ville</th>
<th>Address</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for resto in restos %}
<tr>
<td>{{ resto.id }}</td>
<td>{{ resto.pay }}</td>
<td>{{ resto.ville }}</td>
<td>{{ resto.address }}</td>
<td>
<a href="{{ path('app_resto_admin_show', {'id': resto.id}) }}">show</a>
<a href="{{ path('app_resto_admin_edit', {'id': resto.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="5">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_resto_admin_new') }}">Create new</a>
{% endblock %}

View file

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

View file

@ -0,0 +1,34 @@
{% extends 'base.html.twig' %}
{% block title %}Resto{% endblock %}
{% block body %}
<h1>Resto</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ resto.id }}</td>
</tr>
<tr>
<th>Pay</th>
<td>{{ resto.pay }}</td>
</tr>
<tr>
<th>Ville</th>
<td>{{ resto.ville }}</td>
</tr>
<tr>
<th>Address</th>
<td>{{ resto.address }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('app_resto_admin_index') }}">back to list</a>
<a href="{{ path('app_resto_admin_edit', {'id': resto.id}) }}">edit</a>
{{ include('resto_admin/_delete_form.html.twig') }}
{% endblock %}