src/Controller/bilanControllers/ZoneController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller\bilanControllers;
  3. use App\Entity\bilan\Entity;
  4. use App\Entity\bilan\Location;
  5. use App\Entity\bilan\Zone;
  6. use App\Entity\Processus;
  7. use App\Repository\bilan\ZoneRepository;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/zone")
  15.  */
  16. class ZoneController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/", name="listEvrpZone", methods={"GET"})
  20.      */
  21.     public function listEvrpZone(Request $requestZoneRepository $zoneRepository): Response
  22.     {
  23.         
  24.         $zonesJSON $zoneRepository->transformAll();
  25.         return new JsonResponse($zonesJSON);
  26.     }
  27.     
  28.     /**
  29.      * @Route("/new", name="new_zone",methods={"POST"})
  30.      */
  31.     public function new(Request $requestZoneRepository $zoneRepository): Response
  32.     {
  33.         if (!$request->isXmlHttpRequest()) {
  34.             return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
  35.         }
  36.         if(isset($request->request))
  37.         {
  38.             $user $this->getUser();
  39.             $entityManager $this->getDoctrine()->getManager();
  40.             $entity_id $request->get('entity_id');
  41.             $newEntity $request->get('newEntity');
  42.             $location_id $request->get('location_id');
  43.             $newLocation $request->get('newLocation');
  44.             $zone_title $request->get('zone');
  45.             $processus_id $request->get('processus');
  46.             
  47.             if($newEntity) {
  48.                 $entity= new Entity();
  49.                 $entity->setCreatedBy($user);
  50.                 $entity->setTitle($newEntity);
  51.                 $entityManager->persist($entity);
  52.             } else {
  53.                 $entity $entityManager->getRepository(Entity::class)->find($entity_id);
  54.             }
  55.             
  56.             $processus $entityManager->getRepository(Processus::class)->find($processus_id);
  57.             
  58.             if ($newLocation) {
  59.                 $location= new Location();
  60.                 $location->setCreatedBy($user);
  61.                 $location->setEntity($entity);
  62.                 $location->setName($newLocation);
  63.                 $location->setProcessus($processus);
  64.                 $entityManager->persist($location);
  65.             } else {
  66.                 $location $entityManager->getRepository(Location::class)->find($location_id);
  67.                 $location->setEntity($entity);
  68.                 $location->setProcessus($processus);
  69.             }
  70.             $zone = new Zone();
  71.             $zone->setName($zone_title);
  72.             $zone->setCreatedBy($user);
  73.             $zone->setLocation($location);
  74.             
  75.             $entityManager->persist($zone);
  76.             try {
  77.                 $entityManager->flush();
  78.                 $data $zoneRepository->transform($zone);
  79.                 $response $data;
  80.             } catch (\Exception $e) {
  81.                 $response = array('status' => 'error''message' => $e->getMessage());
  82.             }
  83.             
  84.         }
  85.         return new JsonResponse($response,200);
  86.         
  87.     }
  88.     /**
  89.      * @Route("/{id}/edit", name="edit_zone",methods={"POST"})
  90.      */
  91.     public function edit(Request $requestZone $zoneZoneRepository $zoneRepository): Response
  92.     {
  93.         if (!$request->isXmlHttpRequest()) {
  94.             return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
  95.         }
  96.         if(isset($request->request))
  97.         {
  98.             $user $this->getUser();
  99.             $entityManager $this->getDoctrine()->getManager();
  100.             $entity_id $request->get('entity_id');
  101.             $newEntity $request->get('newEntity');
  102.             $location_id $request->get('location_id');
  103.             $newLocation $request->get('newLocation');
  104.             $zone_title $request->get('zone_title');
  105.             $processus_id$request->get('processus');
  106.             $processus $entityManager->getRepository(Processus::class)->find($processus_id);            
  107.             
  108.             if($newEntity) {
  109.                 $entity= new Entity();
  110.                 $entity->setCreatedBy($user);
  111.                 $entity->setTitle($newEntity);
  112.                 $entityManager->persist($entity);
  113.             }else {
  114.                 $entity $entityManager->getRepository(Entity::class)->find($entity_id);
  115.             }
  116.             
  117.             if($newLocation) {
  118.                 $location= new Location();
  119.                 $location->setCreatedBy($user);
  120.                 $location->setEntity($entity);
  121.                 $location->setName($newLocation);
  122.                 $location->setProcessus($processus);
  123.                 $entityManager->persist($location);
  124.             }else {
  125.                 $location $entityManager->getRepository(Location::class)->find($location_id);
  126.                 $location->setEntity($entity);
  127.                 $location->setProcessus($processus);
  128.             }
  129.             
  130.             $zone->setName($zone_title);
  131.             $zone->setLocation($location);
  132.             try {
  133.                 $entityManager->flush();
  134.                 $data $zoneRepository->transform($zone);
  135.                 $response =$data;
  136.             } catch (\Exception $e) {
  137.                 $response = array('status' => 'error''message' => $e->getMessage());
  138.             }
  139.             
  140.         }
  141.         return new JsonResponse($response,200);
  142.         
  143.     }
  144.     /**
  145.      * @Route("/delete/{id}", name="delete_zone", methods={"GET"})
  146.      */
  147.     public function delete(Request $requestZone $zone): Response
  148.     {
  149.         
  150.         $entityManager $this->getDoctrine()->getManager();
  151.         $entityManager->remove($zone);
  152.         $entityManager->flush();
  153.         $response =['deleted'=>1];
  154.         return new JsonResponse($response,200);
  155.         
  156.     }
  157.     /**
  158.      * @Route("/{id}/list", name="zones_list_by_location", methods={"GET"})
  159.      */
  160.     public function getZonesByLocation(Location $location): Response
  161.     {
  162.         $zones $this->getDoctrine()->getRepository(Zone::class)->findBy(['location'=>$location]);
  163.         $processus $location->getProcessus();
  164.         $responsible $processus->getPilote();
  165.         
  166.         $data['responsible']=$responsible->getFullName();
  167.         $data['fonction']=$responsible->getFonction();
  168.         $data['processus']=$processus->getTitle();
  169.         $data['zones'] = [];
  170.         foreach($zones as $zone) {
  171.             $data['zones'][] = [
  172.                 'id'=>$zone->getId(),
  173.                 'name'=>$zone->getName()
  174.             ];
  175.         }
  176.         
  177.         return new JsonResponse($data,200);
  178.         
  179.     }
  180. }