src/Controller/NoticeController.php line 226

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\Form\FormError;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use App\Entity\Notice;
  11. use App\Form\NoticeType;
  12. class NoticeController extends AbstractController
  13. {
  14.     private $nameentity  'Notice';
  15.     private $labelentity 'App\Entity\Notice';
  16.     private $labelroute  'app_portal_config_notice';
  17.     private $labeldata   'notice';
  18.     private $labeldatas  'notices';
  19.     public function list(ManagerRegistry $em)
  20.     {
  21.         $notices $em->getRepository($this->labelentity)->findAll();        
  22.         $groups $em->getRepository('App\Entity\Group')->findBy([], ['label' => 'asc']);
  23.         return $this->render($this->nameentity.'\list.html.twig', [
  24.             'useheader'         => true,
  25.             'usemenu'           => false,
  26.             'usesidebar'        => true,            
  27.             'notices'           => $notices,
  28.             'groups'            => $groups
  29.         ]);
  30.     }
  31.     public function ajaxseleclist(Request $request,ManagerRegistry $em)
  32.     {
  33.         // S'assurer que c'est un appel ajax
  34.         if (!$request->isXmlHttpRequest()) {
  35.             return new JsonResponse(array('message' => 'Interdit'), 400);
  36.         }
  37.         $output=array();
  38.         $page_limit=$request->query->get('page_limit');
  39.         $q=$request->query->get('q');
  40.         
  41.         $qb $em->getManager()->createQueryBuilder();
  42.         $qb->select('table')->from("App\Entity\Notice",'table')
  43.            ->where('table.title LIKE :value')
  44.            ->setParameter("value""%".$q."%")
  45.            ->orderBy('table.title');
  46.         
  47.         $datas=$qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
  48.         foreach($datas as $data) {
  49.             array_push($output,array("id"=>$data->getId(),"text"=>$data->getTitle()));
  50.         }
  51.         $response = new Response(json_encode($output));    
  52.         $response->headers->set('Content-Type''application/json');      
  53.         return $response;
  54.     } 
  55.     public function submit(Request $request,ManagerRegistry $em)
  56.     {
  57.         // Initialisation de l'enregistrement
  58.         $data = new Notice();
  59.         
  60.         // Création du formulaire
  61.         $form $this->createForm(NoticeType::class,$data,array("mode"=>"submit"));
  62.         // Récupération des data du formulaire
  63.         $form->handleRequest($request);
  64.         
  65.         // Sur erreur
  66.         $this->getErrorForm(null,$form,$request,$data,"submit");
  67.         
  68.         // Sur validation
  69.         if ($form->get('submit')->isClicked() && $form->isValid()) {  
  70.             $data $form->getData();  
  71.             // Sauvegarde
  72.             $em->getManager()->persist($data);
  73.             $em->getManager()->flush();
  74.             // Retour à la liste
  75.             return $this->redirectToRoute($this->labelroute);
  76.         }
  77.         
  78.         // Affichage du formulaire
  79.         return $this->render($this->nameentity.'\edit.html.twig', [
  80.             'useheader'         => true,
  81.             'usemenu'           => false,
  82.             'usesidebar'        => true,            
  83.             $this->labeldata    => $data,
  84.             'mode'              => 'submit',
  85.             'icons'             => $em->getRepository("App\Entity\Icon")->findAll(),
  86.             'form'              => $form->createView()
  87.         ]);
  88.     }  
  89.     public function update($id,Request $request,ManagerRegistry $em)
  90.     {
  91.         // Récupération de l'enregistrement courant 
  92.         $data=$this->getData($em,$id);
  93.         
  94.         // Vérifier que cet enregistrement est modifiable
  95.                                     
  96.         // Création du formulaire
  97.         $form $this->createForm(NoticeType::class,$data,array(
  98.             "mode"      => "update"
  99.         ));
  100.         // Récupération des data du formulaire
  101.         $form->handleRequest($request);
  102.     
  103.         // Sur erreur
  104.         $this->getErrorForm($id,$form,$request,$data,"update");
  105.         
  106.         // Sur validation
  107.         if ($form->get('submit')->isClicked() && $form->isValid()) {
  108.             $data $form->getData();
  109.             // Sauvegarde
  110.             $em->getManager()->flush();
  111.             // Retour à la liste
  112.             return $this->redirectToRoute($this->labelroute);
  113.         }
  114.         
  115.        
  116.         // Affichage du formulaire
  117.         return $this->render($this->nameentity.'\edit.html.twig', [
  118.             'useheader'         => true,
  119.             'usemenu'           => false,
  120.             'usesidebar'        => true,            
  121.             $this->labeldata    => $data,
  122.             'mode'              => 'update',
  123.             'form'              => $form->createView()
  124.         ]);
  125.     }
  126.     public function delete($id,Request $request,ManagerRegistry $em)
  127.     {
  128.         // Récupération de l'enregistrement courant 
  129.         $data=$this->getData($em,$id);
  130.         // Vérifier que cet enregistrement est supprimable
  131.         // Supprimer la donnée
  132.         $em->getManager()->remove($data);
  133.         $em->getManager()->flush();
  134.         
  135.         return $this->redirectToRoute($this->labelroute);
  136.     }
  137.     public function users($id,ManagerRegistry $em)
  138.     {
  139.         $notice $this->getData($em,$id);        
  140.         
  141.         return $this->render($this->nameentity.'\users.html.twig', [
  142.             'useheader'         => true,
  143.             'usemenu'           => false,
  144.             'usesidebar'        => true,            
  145.             'notice'            => $notice,
  146.         ]);
  147.     }    
  148.     public function usersdelete($id,$userid,ManagerRegistry $em)
  149.     {
  150.         $notice $this->getData($em,$id);      
  151.         
  152.         $user $em->getRepository("App\Entity\User")->find($userid);
  153.         $notice->removeUser($user);
  154.         $em->getManager()->flush();
  155.         return $this->render($this->nameentity.'\users.html.twig', [
  156.             'useheader'         => true,
  157.             'usemenu'           => false,
  158.             'usesidebar'        => true,            
  159.             'notice'            => $notice,
  160.         ]);
  161.     } 
  162.     public function usersdeleteall($id,ManagerRegistry $em)
  163.     {
  164.         $notice $this->getData($em,$id);  
  165.         foreach($notice->getUsers() as $user) {
  166.             $notice->removeUser($user);
  167.         }
  168.         $em->getManager()->flush();
  169.         return $this->render($this->nameentity.'\users.html.twig', [
  170.             'useheader'         => true,
  171.             'usemenu'           => false,
  172.             'usesidebar'        => true,            
  173.             'notice'            => $notice,
  174.         ]);
  175.     }
  176.     public function haveread(Request $request,ManagerRegistry $em)
  177.     {
  178.         $user=$this->getUser();
  179.         $notices=$em->getRepository("App\Entity\Notice")->getNoticeToRead($user);
  180.             
  181.         // Récupération redirection potentielle
  182.         $id $request->get('id');
  183.         foreach($notices as $notice) {
  184.             $users=$notice->getUsers();
  185.             $userCriteria Criteria::create()->where(Criteria::expr()->eq('id'$user->getId())); 
  186.             $userCollection $notice->getUsers()->matching($userCriteria);
  187.             if($userCollection->isEmpty()) {
  188.                 $notice->addUser($user);
  189.                 $em->getManager()->flush();
  190.             }
  191.         }
  192.         return $this->redirectToRoute("app_core_home",["id"=>$id]);
  193.     }
  194.     public function view(Request $request,ManagerRegistry $em) {
  195.         $user=$this->getUser();
  196.         $notices=$em->getRepository("App\Entity\Notice")->getNoticeUser($user,false);
  197.         return $this->render($this->nameentity.'\view.html.twig',[
  198.             'useheader'     => true,
  199.             'usemenu'       => false,
  200.             'usesidebar'    => false,  
  201.             'maxwidth'      => true,
  202.             'notices'       => $notices,
  203.         ]);
  204.     }
  205.     protected function getDatas($em)
  206.     {
  207.         $datas $em->getRepository($this->labelentity)->findAll();
  208.         return $datas;
  209.     } 
  210.             
  211.     protected function getData($em,$id)
  212.     {
  213.         $data $em->getRepository($this->labelentity)->find($id);
  214.         if (!$data) {
  215.             throw $this->createNotFoundException('Unable to find '.$this->labeldata);
  216.         }
  217.         return $data;
  218.     } 
  219.     protected function getErrorForm($id,$form,$request,$data,$mode) {
  220.         if ($form->get('submit')->isClicked()&&$mode=="delete") {
  221.         }
  222.         if ($form->get('submit')->isClicked() && $mode!="delete") {
  223.             if(is_null($data->getDescription())) {
  224.                 $form->addError(new FormError('Description obligatoire'));
  225.             }
  226.         }
  227.         if ($form->get('submit')->isClicked() && !$form->isValid()) {
  228.             $errors $form->getErrors();
  229.             foreach( $errors as $error ) {
  230.                 $request->getSession()->getFlashBag()->add("error"$error->getMessage());
  231.             }
  232.         }
  233.     }    
  234. }