<?php
namespace App\Controller\Admin;
use App\Entity\Formation;
use App\Entity\User;
use App\Form\FormationType;
use Cocur\Slugify\Slugify;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* @Route("/admin/formations")
*/
class AdminFormationController extends AbstractController
{
private $em, $paginator;
public function __construct(EntityManagerInterface $em,PaginatorInterface $paginator)
{
$this->em=$em;
$this->paginator = $paginator;
}
/**
* @Route("/{type?}", name="admin.formation.index", methods={"GET"})
*/
public function index(string $type="",Request $request){
$titre="Liste des formations ".$type;
$param=[];
if($type=="physiques"){
//$titre.=" physiques";
$param=['type'=>0];
}else if($type == "virtuelles"){
//$titre.=" virtuelles";
$param=['type'=>1];
}
$all=$this->em->getRepository(Formation::class)->findBy($param,['created_at'=>'DESC']);
$paginedData=$this->paginator->paginate($all,$request->query->getInt('page', 1),20);
return $this->render('admin/formation/index.html.twig',[
'formations'=>$paginedData,
'titre'=>$titre,
]
);
}
/**
* @Route("-new", name="admin.formation.new", methods={"GET","POST"})
*/
public function create(Request $request){
//dd($request->request->all());
$formation=new Formation();
$form = $this->createForm(FormationType::class, $formation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$imageFile = $form->get('image')->getData();
if($imageFile){
//$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
$fileName = md5(uniqid()).'.'.$imageFile->guessExtension();
$imageFile->move(
$this->getParameter('formation_image_dir'),
$fileName
);
$formation->setImage($fileName);
}
$documentFile = $form->get('document')->getData();
if($documentFile){
//$originalFilename = pathinfo($documentFile->getClientOriginalName(), PATHINFO_FILENAME);
$slug=new Slugify();
$fileName='OF_'.$slug->slugify($documentFile->getClientOriginalName());
$fileName.=time() .'.'. $documentFile->guessExtension();
//$fileName = md5(uniqid()).'.'.$documentFile->guessExtension();
$documentFile->move(
$this->getParameter('formation_document_dir'),
$fileName
);
$formation->setDocument($fileName);
}
//$formation->setType(($type=='physique')?0:1);
/*$formation->setDateDebut($form->get('date_debut')->getData());
$formation->setDateFin($form->get('date_fin')->getData());
$formation->setLieu($form->get('lieu')->getData());*/
$formation->setUser($this->getUser());
$this->em->persist($formation);
$this->em->flush();
return $this->redirectToRoute('admin.formation.index');
}
return $this->render('admin/formation/new.html.twig',[
'formation' => $formation,
'form'=>$form->createView()
]
);
}
/**
* @Route("/{id}/show", name="admin.formation.show", methods={"GET"})
*/
public function show(Formation $formation)
{
//dd($formation);
$chapitres=$formation->getChapitres()->get(0);
//dd($chapitres);
return $this->render('admin/formation/show.html.twig', [
'formation' => $formation,
'chapitres'=> $formation->getChapitres(),
]);
}
/**
* @Route("{id}/edit/", name="admin.formation.edit", methods={"GET","POST"})
*/
public function edit(Formation $formation, Request $request){
$form = $this->createForm(FormationType::class, $formation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$imageFile = $form->get('image')->getData();
if($imageFile){
//$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
$fileName = md5(uniqid()).'.'.$imageFile->guessExtension();
$imageFile->move(
$this->getParameter('formation_image_dir'),
$fileName
);
$formation->setImage($fileName);
}
$documentFile = $form->get('document')->getData();
if($documentFile){
//$originalFilename = pathinfo($documentFile->getClientOriginalName(), PATHINFO_FILENAME);
$slug=new Slugify();
$fileName='OF_'.$slug->slugify($documentFile->getClientOriginalName());
$fileName.=time() .'.'. $documentFile->guessExtension();
//$fileName = md5(uniqid()).'.'.$documentFile->guessExtension();
$documentFile->move(
$this->getParameter('formation_document_dir'),
$fileName
);
$formation->setDocument($fileName);
}
$formation->setUpdatedAt(new \DateTime());
$this->em->persist($formation);
$this->em->flush();
return $this->redirectToRoute('admin.formation.index');
}
return $this->render('admin/formation/edit.html.twig',[
'formation' => $formation,
'form'=>$form->createView(),
]
);
}
/**
* @Route("/{id}/delete", name="admin.formation.supprimer", methods={"GET"})
* @IsGranted("ROLE_SUPER_ADMIN")
*/
public function delete(Formation $formation,Request $request){
$this->em->remove($formation);
$this->em->flush();
$this->addFlash('success',"Formatin supprimée avec succès");
return $this->redirect($request->headers->get('referer'));
}
/**
* @Route("/{id}/disable", name="admin.formation.desactiver", methods={"GET"})
* @IsGranted("ROLE_TECH")
*/
public function disable(Formation $formation,Request $request){
$this->setStatus(false,$formation);
return $this->redirect($request->headers->get('referer'));
}
/**
* @Route("/{id}/enable", name="admin.formation.activer", methods={"GET"})
* @IsGranted("ROLE_TECH")
*/
public function enable(Formation $formation,Request $request){
$this->setStatus(true,$formation);
return $this->redirect($request->headers->get('referer'));
}
////====> P R I V A T ES
private function setStatus(bool $status,Formation $formation){
$message= ($status) ? "activée":"Désactivée";
if($formation->getStatus() == $status)
$this->addFlash('warning',"Formation déjà $message");
else{
$formation->setStatus($status);
$this->em->flush();
$this->addFlash('success',"Formation $message avec succès");
}
}
}