<?php
namespace App\Controller;
use App\Entity\Avocats;
use App\Form\AvocatsType;
use App\Repository\AvocatsRepository;
use App\Repository\JoursSemaineRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/avocats")
*/
class AvocatsController extends AbstractController
{
/**
* @Route("/", name="app_avocats_index", methods={"GET"})
*/
public function index(AvocatsRepository $avocatsRepository): Response
{
$user = $this->getUser();
if (in_array('ROLE_ADMIN', $this->getUser()->getRoles(), true))
{
$avc = $avocatsRepository->findBy(array('Deleted'=>0));
$role = 'admin';
}
else
{
$avc = $avocatsRepository->findBy(array('id'=>$user->getAvocats()));
$role = 'user';
}
return $this->render('avocats/index.html.twig', [
'avocats' => $avc,
'role'=>$role
]);
}
/**
* @Route("/new", name="app_avocats_new", methods={"GET", "POST"})
*/
public function new(Request $request, AvocatsRepository $avocatsRepository): Response
{
$avocat = new Avocats();
$form = $this->createForm(AvocatsType::class, $avocat);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $form->get('Photos')->getData();
if($file != 'ok') {
$path = '/';
$fileName = uniqid().'-'.$file->getClientOriginalName();
$file->move(
$this->getParameter('photos_directory').$path,
$fileName
);
$avocat->setPhotos($fileName);
}
$avocat->setDeleted(0);
$avocatsRepository->add($avocat, true);
return $this->redirectToRoute('app_avocats_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('avocats/new.html.twig', [
'avocat' => $avocat,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_avocats_show", methods={"GET"})
*/
public function show(Avocats $avocat): Response
{
return $this->render('avocats/show.html.twig', [
'avocat' => $avocat,
]);
}
/**
* @Route("/{id}/edit", name="app_avocats_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Avocats $avocat, AvocatsRepository $avocatsRepository, JoursSemaineRepository $joursSemaineRepo): Response
{
$js = $joursSemaineRepo->findBy(array('Avocats'=>$avocat));
$form = $this->createForm(AvocatsType::class, $avocat);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$avocatsRepository->add($avocat, true);
return $this->redirectToRoute('app_avocats_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('avocats/edit.html.twig', [
'avocat' => $avocat,
'form' => $form,
'js'=>$js
]);
}
/**
* @Route("/{id}", name="app_avocats_delete", methods={"POST"})
*/
public function delete(Request $request, Avocats $avocat, AvocatsRepository $avocatsRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$avocat->getId(), $request->request->get('_token'))) {
$avocat->setDeleted(1);
$avocatsRepository->add($avocat, true);
}
return $this->redirectToRoute('app_avocats_index', [], Response::HTTP_SEE_OTHER);
}
}