src/Controller/ServicesController.php line 37

  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 Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use App\Entity\Services;
  12. use App\Form\ServicesType;
  13. use App\Repository\ServicesRepository;
  14. use OlaSoft\Common;
  15. /**
  16.  * @Route("/os-admin/services", name="services-")
  17.  */
  18. class ServicesController extends AbstractController
  19. {
  20.     public function __construct(ManagerRegistry $doctrine){
  21.         $this->doctrine =  $doctrine;
  22.     }
  23.     
  24.     private $doctrine;
  25.     
  26.     public function getDoctrine()
  27.     {
  28.         return $this->doctrine;
  29.     }
  30.     /**
  31.      * @Route("/",name="admin")
  32.      */
  33.     public function indexAction(Request $request)
  34.     {
  35.         $em $this->getDoctrine()->getManager();
  36.         $list $em->getRepository(Services::class)->findBy(array(),array('id'=>'DESC'));
  37.         $ajax $request->isXmlHttpRequest();
  38.         $response $this->render('Admin\index.html.twig',[
  39.             'list'=>$list,
  40.             'ajax'=>$ajax,
  41.             'noResearch'=>true,
  42.             "title"=> "Gestion des services",
  43.             "subtitle"=> "Liste des services"
  44.         ]);
  45.         return $ajax ?
  46.             new Response(\json_encode([
  47.                 'content'=>$response->getContent(),
  48.                 "title"=> "Gestion des services",
  49.                 "subtitle"=> "Liste des services"
  50.             ]))
  51.         : $response;
  52.     }
  53.     /**
  54.      * @Route("/add", name="new")
  55.      * @Route("/{id}/edit", name="edit")
  56.      */
  57.     public function edit(Request $requestServices $item null)
  58.     {
  59.         if(!$item)
  60.             $item = new Services;
  61.         $em $this->getDoctrine()->getManager();
  62.         $form $this->createForm(ServicesType::class, $item);
  63.         $form->handleRequest($request);
  64.         if ($form->isSubmitted() && $form->isValid()){
  65.             $item->setSlug(Common::slug($item->getTitle()));
  66.             $em->persist($item);
  67.             $em->flush();
  68.             Common::trackChange($em$item$this->getUser(), 'add'"Édition d'un service");
  69.             $this->addFlash("notice","Enregistrement effectué avec succès.");
  70.             return $this->redirectToRoute('services-admin');
  71.         }
  72.         $ajax $request->isXmlHttpRequest();
  73.         $response $this->render('Admin\edit.html.twig',[
  74.             'form'=>$form->createView(),
  75.             'ajax'=>$ajax,
  76.             "title"=> "Gestion des services",
  77.             "subtitle"=>"Edition d'un service "
  78.         ]);
  79.         return $ajax ?
  80.             new Response(\json_encode([
  81.                 'content'=>$response->getContent(),
  82.                 "title"=> "Gestion des services",
  83.                 "subtitle"=>"Edition d'un service "
  84.             ]))
  85.         : $response;
  86.      }
  87.     /**
  88.      * @Route("/{id}/enable/", name="enable")
  89.      */
  90.     public function enable(Request $requestServices $item)
  91.     {
  92.         $em $this->getDoctrine()->getManager();
  93.         $item->setIsEnabled(!$item->getIsEnabled());
  94.         $em->persist($item);
  95.         $em->flush();
  96.         Common::trackChange($em$item$this->getUser(), $item->getIsEnabled() ? 'enable':'disable'"Activation / Désactivation d'un service");
  97.         $ajax $request->isXmlHttpRequest();
  98.         if(!$ajax)
  99.             $this->addFlash("notice","Activation / Désactivation effectuée avec succès.");
  100.         return $ajax
  101.             ? new Response(\json_encode(['status'=>$item->getIsEnabled(), 'notice'=>'Activation / Désactivation effectuée avec succès.']))
  102.             : $this->redirectToRoute('services-admin');
  103.     }
  104. }