src\Controller\Admin\AdminFormationController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Formation;
  4. use App\Entity\User;
  5. use App\Form\FormationType;
  6. use Cocur\Slugify\Slugify;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. /**
  14.  * @Route("/admin/formations")
  15.  */
  16. class AdminFormationController extends AbstractController
  17. {
  18.     private $em$paginator;
  19.     public function __construct(EntityManagerInterface $em,PaginatorInterface $paginator)
  20.     {
  21.         $this->em=$em;
  22.         $this->paginator $paginator;
  23.     }
  24.     /**
  25.      * @Route("/{type?}", name="admin.formation.index",  methods={"GET"})
  26.      */
  27.     public function index(string $type="",Request $request){
  28.         $titre="Liste des formations ".$type;
  29.         $param=[];
  30.         if($type=="physiques"){
  31.             //$titre.=" physiques";
  32.             $param=['type'=>0];
  33.         }else if($type == "virtuelles"){
  34.             //$titre.=" virtuelles";
  35.             $param=['type'=>1];
  36.         }
  37.         $all=$this->em->getRepository(Formation::class)->findBy($param,['created_at'=>'DESC']);
  38.         $paginedData=$this->paginator->paginate($all,$request->query->getInt('page'1),20);
  39.         return $this->render('admin/formation/index.html.twig',[
  40.             'formations'=>$paginedData,
  41.                 'titre'=>$titre,
  42.         ]
  43.         );
  44.     }
  45.     /**
  46.      * @Route("-new", name="admin.formation.new",  methods={"GET","POST"})
  47.      */
  48.     public function create(Request $request){
  49.         //dd($request->request->all());
  50.         $formation=new Formation();
  51.         $form $this->createForm(FormationType::class, $formation);
  52.         $form->handleRequest($request);
  53.         if ($form->isSubmitted() && $form->isValid()) {
  54.             $imageFile $form->get('image')->getData();
  55.             if($imageFile){
  56.                 //$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
  57.                 $fileName md5(uniqid()).'.'.$imageFile->guessExtension();
  58.                 $imageFile->move(
  59.                     $this->getParameter('formation_image_dir'),
  60.                     $fileName
  61.                 );
  62.                 $formation->setImage($fileName);
  63.             }
  64.             $documentFile $form->get('document')->getData();
  65.             if($documentFile){
  66.                 //$originalFilename = pathinfo($documentFile->getClientOriginalName(), PATHINFO_FILENAME);
  67.                 $slug=new Slugify();
  68.                 $fileName='OF_'.$slug->slugify($documentFile->getClientOriginalName());
  69.                 $fileName.=time() .'.'$documentFile->guessExtension();
  70.                 //$fileName = md5(uniqid()).'.'.$documentFile->guessExtension();
  71.                 $documentFile->move(
  72.                     $this->getParameter('formation_document_dir'),
  73.                     $fileName
  74.                 );
  75.                 $formation->setDocument($fileName);
  76.             }
  77.             //$formation->setType(($type=='physique')?0:1);
  78.             /*$formation->setDateDebut($form->get('date_debut')->getData());
  79.             $formation->setDateFin($form->get('date_fin')->getData());
  80.             $formation->setLieu($form->get('lieu')->getData());*/
  81.             $formation->setUser($this->getUser());
  82.             $this->em->persist($formation);
  83.             $this->em->flush();
  84.             return $this->redirectToRoute('admin.formation.index');
  85.         }
  86.         return $this->render('admin/formation/new.html.twig',[
  87.             'formation' => $formation,
  88.                 'form'=>$form->createView()
  89.             ]
  90.         );
  91.     }
  92.     /**
  93.      * @Route("/{id}/show", name="admin.formation.show", methods={"GET"})
  94.      */
  95.     public function show(Formation $formation)
  96.     {
  97.         //dd($formation);
  98.         $chapitres=$formation->getChapitres()->get(0);
  99.         //dd($chapitres);
  100.             return $this->render('admin/formation/show.html.twig', [
  101.                 'formation' => $formation,
  102.                 'chapitres'=> $formation->getChapitres(),
  103.             ]);
  104.     }
  105.     /**
  106.      * @Route("{id}/edit/", name="admin.formation.edit",  methods={"GET","POST"})
  107.      */
  108.     public function edit(Formation $formationRequest $request){
  109.         $form $this->createForm(FormationType::class, $formation);
  110.         $form->handleRequest($request);
  111.         if ($form->isSubmitted() && $form->isValid()) {
  112.             $imageFile $form->get('image')->getData();
  113.             if($imageFile){
  114.                 //$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
  115.                 $fileName md5(uniqid()).'.'.$imageFile->guessExtension();
  116.                 $imageFile->move(
  117.                     $this->getParameter('formation_image_dir'),
  118.                     $fileName
  119.                 );
  120.                 $formation->setImage($fileName);
  121.             }
  122.             $documentFile $form->get('document')->getData();
  123.             if($documentFile){
  124.                 //$originalFilename = pathinfo($documentFile->getClientOriginalName(), PATHINFO_FILENAME);
  125.                 $slug=new Slugify();
  126.                 $fileName='OF_'.$slug->slugify($documentFile->getClientOriginalName());
  127.                 $fileName.=time() .'.'$documentFile->guessExtension();
  128.                 //$fileName = md5(uniqid()).'.'.$documentFile->guessExtension();
  129.                 $documentFile->move(
  130.                     $this->getParameter('formation_document_dir'),
  131.                     $fileName
  132.                 );
  133.                 $formation->setDocument($fileName);
  134.             }
  135.             $formation->setUpdatedAt(new \DateTime());
  136.             $this->em->persist($formation);
  137.             $this->em->flush();
  138.             return $this->redirectToRoute('admin.formation.index');
  139.         }
  140.         return $this->render('admin/formation/edit.html.twig',[
  141.                 'formation' => $formation,
  142.                 'form'=>$form->createView(),
  143.             ]
  144.         );
  145.     }
  146.     /**
  147.      * @Route("/{id}/delete", name="admin.formation.supprimer",  methods={"GET"})
  148.      * @IsGranted("ROLE_SUPER_ADMIN")
  149.      */
  150.     public function delete(Formation $formation,Request $request){
  151.         $this->em->remove($formation);
  152.         $this->em->flush();
  153.         $this->addFlash('success',"Formatin supprimée avec succès");
  154.         return $this->redirect($request->headers->get('referer'));
  155.     }
  156.     /**
  157.      * @Route("/{id}/disable", name="admin.formation.desactiver",  methods={"GET"})
  158.      * @IsGranted("ROLE_TECH")
  159.      */
  160.     public function disable(Formation $formation,Request $request){
  161.         $this->setStatus(false,$formation);
  162.         return $this->redirect($request->headers->get('referer'));
  163.     }
  164.     /**
  165.      * @Route("/{id}/enable", name="admin.formation.activer",  methods={"GET"})
  166.      * @IsGranted("ROLE_TECH")
  167.      */
  168.     public function enable(Formation $formation,Request $request){
  169.         $this->setStatus(true,$formation);
  170.         return $this->redirect($request->headers->get('referer'));
  171.     }
  172.     ////====> P R I V A T ES
  173.     private function setStatus(bool $status,Formation $formation){
  174.         $message= ($status) ? "activée":"Désactivée";
  175.         if($formation->getStatus() == $status)
  176.             $this->addFlash('warning',"Formation déjà $message");
  177.         else{
  178.             $formation->setStatus($status);
  179.             $this->em->flush();
  180.             $this->addFlash('success',"Formation $message avec succès");
  181.         }
  182.     }
  183. }