src/Controller/FormationsController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\Routing\Annotation\Method;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Routing\Annotation\Security;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Entity\Formations;
  11. use App\Form\FormationsType;
  12. use App\Repository\FormationsRepository;
  13. use OlaSoft\Common;
  14. use App\Entity\UsersActivities;
  15. /**
  16.  * @Route("/os-admin/formations", name="formations-")
  17.  */
  18. class FormationsController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/",name="admin")
  22.      */
  23.     public function indexAction()
  24.     {
  25.         $request $this->get('request_stack')->getCurrentRequest();
  26.         $em $this->getDoctrine()->getManager();
  27.         $list $em->getRepository(Formations::class)->findBy(array(),array('id'=>'DESC'));
  28.         $ajax $request->isXmlHttpRequest();
  29.         $response $this->render('Admin\index.html.twig',[
  30.             'list'=>$list,
  31.             'ajax'=>$ajax,
  32.             'noResearch'=>true,
  33.             "title"=> "Gestion des formations",
  34.             "subtitle"=> "Liste des formations",
  35.             "addBtnClass"=>"btn-popup semi"
  36.         ]);
  37.         return $ajax ?
  38.             new Response(\json_encode([
  39.                 'content'=>$response->getContent(),
  40.                 "title"=> "Gestion des formations",
  41.                 "subtitle"=> "Liste des formations"
  42.             ]))
  43.         : $response;
  44.     }
  45.     /**
  46.      * @Route("/add", name="new")
  47.      * @Route("/{id}/edit", name="edit")
  48.      */
  49.     public function edit(Formations $item null)
  50.     {
  51.         if(!$item){
  52.             $item = new Formations;
  53.             $item->setUser($this->getUser());
  54.             $item->setCreatedAt(new \DateTime);
  55.         }
  56.         $em $this->getDoctrine()->getManager();
  57.         $request $this->get('request_stack')->getCurrentRequest();
  58.         $form $this->createForm(FormationsType::class, $item);
  59.         $form->handleRequest($request);
  60.         if ($form->isSubmitted() && $form->isValid()){
  61.             $item->setSlug(Common::slug($item->getLabel()));
  62.             $em->persist($item);
  63.             $em->flush();
  64.             Common::trackChange(new UsersActivities$em$item$this->getUser(), 'edit'"Édition d'un projet");
  65.             $this->addFlash("notice","Enregistrement effectué avec succès.");
  66.             return $this->redirectToRoute('formations-admin');
  67.         }
  68.         $ajax $request->isXmlHttpRequest();
  69.         $response $this->render('Admin\edit.html.twig',[
  70.             'form'=>$form->createView(),
  71.             'ajax'=>$ajax,
  72.             "title"=> "Gestion des formations",
  73.             "subtitle"=>"Edition d'une formation "
  74.         ]);
  75.         return $ajax ?
  76.             new Response(\json_encode([
  77.                 'content'=>$response->getContent(),
  78.                 "title"=> "Gestion des formations",
  79.                 "subtitle"=>"Edition d'une formation "
  80.             ]))
  81.         : $response;
  82.      }
  83.     /**
  84.      * @Route("/{id}/enable/", name="enable")
  85.      */
  86.     public function enable(Formations $item)
  87.     {
  88.         $request $this->get('request_stack')->getCurrentRequest();
  89.         $em $this->getDoctrine()->getManager();
  90.         $item->setIsEnabled(!$item->getIsEnabled());
  91.         $em->persist($item);
  92.         $em->flush();
  93.         Common::trackChange(new UsersActivities$em$item$this->getUser(), 'enable'"Activation/désactivation d'une formation");
  94.         $ajax $request->isXmlHttpRequest();
  95.         if(!$ajax)
  96.             $this->addFlash("notice","Activation / Désactivation effectuée avec succès.");
  97.         return $ajax
  98.             ? new Response(\json_encode(['status'=>$item->getIsEnabled(), 'notice'=>'Activation / Désactivation effectuée avec succès.']))
  99.             : $this->redirectToRoute('formations-admin');
  100.     }
  101. }