<?php
namespace App\Controller\bilanControllers;
use App\Entity\bilan\Entity;
use App\Entity\bilan\Location;
use App\Entity\bilan\Zone;
use App\Entity\Processus;
use App\Repository\bilan\ZoneRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/zone")
*/
class ZoneController extends AbstractController
{
/**
* @Route("/", name="listEvrpZone", methods={"GET"})
*/
public function listEvrpZone(Request $request, ZoneRepository $zoneRepository): Response
{
$zonesJSON = $zoneRepository->transformAll();
return new JsonResponse($zonesJSON);
}
/**
* @Route("/new", name="new_zone",methods={"POST"})
*/
public function new(Request $request, ZoneRepository $zoneRepository): Response
{
if (!$request->isXmlHttpRequest()) {
return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
}
if(isset($request->request))
{
$user = $this->getUser();
$entityManager = $this->getDoctrine()->getManager();
$entity_id = $request->get('entity_id');
$newEntity = $request->get('newEntity');
$location_id = $request->get('location_id');
$newLocation = $request->get('newLocation');
$zone_title = $request->get('zone');
$processus_id = $request->get('processus');
if($newEntity) {
$entity= new Entity();
$entity->setCreatedBy($user);
$entity->setTitle($newEntity);
$entityManager->persist($entity);
} else {
$entity = $entityManager->getRepository(Entity::class)->find($entity_id);
}
$processus = $entityManager->getRepository(Processus::class)->find($processus_id);
if ($newLocation) {
$location= new Location();
$location->setCreatedBy($user);
$location->setEntity($entity);
$location->setName($newLocation);
$location->setProcessus($processus);
$entityManager->persist($location);
} else {
$location = $entityManager->getRepository(Location::class)->find($location_id);
$location->setEntity($entity);
$location->setProcessus($processus);
}
$zone = new Zone();
$zone->setName($zone_title);
$zone->setCreatedBy($user);
$zone->setLocation($location);
$entityManager->persist($zone);
try {
$entityManager->flush();
$data = $zoneRepository->transform($zone);
$response = $data;
} catch (\Exception $e) {
$response = array('status' => 'error', 'message' => $e->getMessage());
}
}
return new JsonResponse($response,200);
}
/**
* @Route("/{id}/edit", name="edit_zone",methods={"POST"})
*/
public function edit(Request $request, Zone $zone, ZoneRepository $zoneRepository): Response
{
if (!$request->isXmlHttpRequest()) {
return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
}
if(isset($request->request))
{
$user = $this->getUser();
$entityManager = $this->getDoctrine()->getManager();
$entity_id = $request->get('entity_id');
$newEntity = $request->get('newEntity');
$location_id = $request->get('location_id');
$newLocation = $request->get('newLocation');
$zone_title = $request->get('zone_title');
$processus_id= $request->get('processus');
$processus = $entityManager->getRepository(Processus::class)->find($processus_id);
if($newEntity) {
$entity= new Entity();
$entity->setCreatedBy($user);
$entity->setTitle($newEntity);
$entityManager->persist($entity);
}else {
$entity = $entityManager->getRepository(Entity::class)->find($entity_id);
}
if($newLocation) {
$location= new Location();
$location->setCreatedBy($user);
$location->setEntity($entity);
$location->setName($newLocation);
$location->setProcessus($processus);
$entityManager->persist($location);
}else {
$location = $entityManager->getRepository(Location::class)->find($location_id);
$location->setEntity($entity);
$location->setProcessus($processus);
}
$zone->setName($zone_title);
$zone->setLocation($location);
try {
$entityManager->flush();
$data = $zoneRepository->transform($zone);
$response =$data;
} catch (\Exception $e) {
$response = array('status' => 'error', 'message' => $e->getMessage());
}
}
return new JsonResponse($response,200);
}
/**
* @Route("/delete/{id}", name="delete_zone", methods={"GET"})
*/
public function delete(Request $request, Zone $zone): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($zone);
$entityManager->flush();
$response =['deleted'=>1];
return new JsonResponse($response,200);
}
/**
* @Route("/{id}/list", name="zones_list_by_location", methods={"GET"})
*/
public function getZonesByLocation(Location $location): Response
{
$zones = $this->getDoctrine()->getRepository(Zone::class)->findBy(['location'=>$location]);
$processus = $location->getProcessus();
$responsible = $processus->getPilote();
$data['responsible']=$responsible->getFullName();
$data['fonction']=$responsible->getFonction();
$data['processus']=$processus->getTitle();
$data['zones'] = [];
foreach($zones as $zone) {
$data['zones'][] = [
'id'=>$zone->getId(),
'name'=>$zone->getName()
];
}
return new JsonResponse($data,200);
}
}