112 lines
4.7 KiB
PHP
112 lines
4.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Controller;
|
||
|
|
||
|
use App\Entity\ProjectOption;
|
||
|
use App\Form\OptionType;
|
||
|
use App\Repository\ProjectOptionRepository;
|
||
|
use App\Repository\ProjectRepository;
|
||
|
use App\Security\ProjectVoter;
|
||
|
use Doctrine\ORM\EntityManagerInterface;
|
||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||
|
use Symfony\Component\Routing\Attribute\Route;
|
||
|
|
||
|
class ProjectOptionController extends AbstractController
|
||
|
{
|
||
|
#[Route('/admin/option/submit/{idproject}', name: 'app_admin_option_submit')]
|
||
|
#[Route('/user/option/submit/{idproject}', name: 'app_user_option_submit')]
|
||
|
public function submit(int $idproject, Request $request, ProjectRepository $projectRepository, EntityManagerInterface $em): Response
|
||
|
{
|
||
|
$project = $projectRepository->find($idproject);
|
||
|
if (!$project) {
|
||
|
throw new NotFoundHttpException('La ressource demandée est introuvable.');
|
||
|
}
|
||
|
$this->denyAccessUnlessGranted(ProjectVoter::UPDATE, $project);
|
||
|
|
||
|
$option = new ProjectOption();
|
||
|
$option->setProject($project);
|
||
|
|
||
|
$isAdmin = str_starts_with($request->attributes->get('_route'), 'app_admin');
|
||
|
|
||
|
$form = $this->createForm(OptionType::class, $option, ['mode' => 'submit']);
|
||
|
$form->handleRequest($request);
|
||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||
|
$em->persist($option);
|
||
|
$em->flush();
|
||
|
|
||
|
return $this->redirectToRoute($isAdmin ? 'app_admin_project_update' : 'app_user_project_update', ['id' => $idproject]);
|
||
|
}
|
||
|
|
||
|
return $this->render('option/edit.html.twig', [
|
||
|
'usemenu' => true,
|
||
|
'usesidebar' => $isAdmin,
|
||
|
'title' => 'Création Option',
|
||
|
'routecancel' => $isAdmin ? 'app_admin_project_update' : 'app_admin_project_update',
|
||
|
'routedelete' => $isAdmin ? 'app_admin_option_delete' : 'app_user_option_delete',
|
||
|
'mode' => 'submit',
|
||
|
'idproject' => $idproject,
|
||
|
'form' => $form,
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
#[Route('/admin/option/update/{idproject}/{id}', name: 'app_admin_option_update')]
|
||
|
#[Route('/user/option/update/{idproject}/{id}', name: 'app_user_option_update')]
|
||
|
public function update(int $idproject, int $id, Request $request, ProjectOptionRepository $projectOptionResitory, EntityManagerInterface $em): Response
|
||
|
{
|
||
|
$option = $projectOptionResitory->find($id);
|
||
|
if (!$option) {
|
||
|
throw new NotFoundHttpException('La ressource demandée est introuvable.');
|
||
|
}
|
||
|
|
||
|
$this->denyAccessUnlessGranted(ProjectVoter::UPDATE, $option->getProject());
|
||
|
|
||
|
$isAdmin = str_starts_with($request->attributes->get('_route'), 'app_admin');
|
||
|
$form = $this->createForm(OptionType::class, $option, ['mode' => 'update']);
|
||
|
$form->handleRequest($request);
|
||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||
|
$em->flush();
|
||
|
|
||
|
return $this->redirectToRoute($isAdmin ? 'app_admin_project_update' : 'app_user_project_update', ['id' => $idproject]);
|
||
|
}
|
||
|
|
||
|
return $this->render('option/edit.html.twig', [
|
||
|
'usemenu' => true,
|
||
|
'usesidebar' => $isAdmin,
|
||
|
'title' => 'Modification Option = '.$option->getTitle(),
|
||
|
'routecancel' => $isAdmin ? 'app_admin_project_update' : 'app_user_project_update',
|
||
|
'routedelete' => $isAdmin ? 'app_admin_option_delete' : 'app_user_option_delete',
|
||
|
'mode' => 'update',
|
||
|
'idproject' => $idproject,
|
||
|
'option' => $option,
|
||
|
'form' => $form,
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
#[Route('/admin/option/delete/{idproject}/{id}', name: 'app_admin_option_delete')]
|
||
|
#[Route('/user/option/delete/{idproject}/{id}', name: 'app_user_option_delete')]
|
||
|
public function delete(int $idproject, int $id, Request $request, ProjectOptionRepository $projectOptionResitory, EntityManagerInterface $em): Response
|
||
|
{
|
||
|
$option = $projectOptionResitory->find($id);
|
||
|
if (!$option) {
|
||
|
throw new NotFoundHttpException('La ressource demandée est introuvable.');
|
||
|
}
|
||
|
|
||
|
$this->denyAccessUnlessGranted(ProjectVoter::UPDATE, $option->getProject());
|
||
|
|
||
|
$isAdmin = str_starts_with($request->attributes->get('_route'), 'app_admin');
|
||
|
|
||
|
// Tentative de suppression
|
||
|
try {
|
||
|
$em->remove($option);
|
||
|
$em->flush();
|
||
|
} catch (\Exception $e) {
|
||
|
$this->addflash('error', $e->getMessage());
|
||
|
}
|
||
|
|
||
|
return $this->redirectToRoute($isAdmin ? 'app_admin_project_update' : 'app_user_project_update', ['id' => $idproject]);
|
||
|
}
|
||
|
}
|