src/Controller/DefaultController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Cassonetti\Alarm;
  4. use App\Entity\Cassonetti\AutomezziOggetti;
  5. use App\Entity\Cassonetti\Cassonetto;
  6. use App\Entity\Cassonetti\Comune;
  7. use App\Entity\Cassonetti\OrdineServizio;
  8. use App\Entity\Cassonetti\Rilevazione;
  9. use App\Form\GpsAutomezziType;
  10. use App\Form\GpsOggettiType;
  11. use App\Form\MapsComuneType;
  12. use App\Services\AppManager;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Contracts\HttpClient\HttpClientInterface;
  21. use Twig\Environment;
  22. class DefaultController extends AbstractController
  23. {
  24.     private $params;
  25.     private $em;
  26.     private $client;
  27.     private $manager;
  28.     /**
  29.      * DropboxUploaderCommand constructor.
  30.      * @param $params
  31.      */
  32.     public function __construct(ParameterBagInterface $params, EntityManagerInterface $em, HttpClientInterface $client, AppManager $manager)
  33.     {
  34.         $this->params=$params;
  35.         $this->em=$em;
  36.         $this->client=$client;
  37.         $this->manager = $manager;
  38.     }
  39.     #[Route(path: '/', name: 'home')]
  40.     public function index():Response{
  41.         $idComune = 0;
  42.         $user = $this->getUser();
  43.         if (in_array("ROLE_COMUNE", $user->getRoles()) ) {
  44.             $idComune   = $user->getComune()->getId();
  45.         }
  46.         $aRilevazioni = [];
  47.         if ($idComune > 0){
  48.             $odss = $this->em->getRepository(OrdineServizio::class)->findOdsByComune($idComune);
  49.             $rilevazioni = $this->em->getRepository(Rilevazione::class)->findRilevazioniByComune($idComune);
  50.             $alarms = $this->em->getRepository(Alarm::class)->findAlarmByComune($idComune);
  51.         } else {
  52.             $odss = $this->em->getRepository(OrdineServizio::class)->findEnabled();
  53.             $rilevazioni = $this->em->getRepository(Rilevazione::class)->findEnabled();
  54.             $alarms = $this->em->getRepository(Alarm::class)->findAll();
  55.         }
  56.         foreach ($rilevazioni as $rilevazione) {
  57.             $des_comune = '';
  58.             if ($rilevazione->getComune()) {
  59.                 $des_comune = $rilevazione->getComune()->getDescrizione();
  60.             }
  61.             foreach ($rilevazione->getJson() as $json){
  62.                 $rilevazione = json_decode($json, true);
  63.                 $rilevazione['comune'] = $des_comune;
  64.                 $aRilevazioni[] = $rilevazione;
  65.             }
  66.         }
  67.         return $this->render('default/index.html.twig',['odss'=>$odss, 'rilevazioni'=>$aRilevazioni, 'alarms'=>$alarms]);
  68.     }
  69.     #[Route(path: '/gpscassonetti', name: 'gpsCassonetti')]
  70.     public function gpsCassonetti(Request $request): Response
  71.     {
  72.         $idComune = 0;
  73.         $user = $this->getUser();
  74.         if (in_array("ROLE_COMUNE", $user->getRoles()) ) {
  75.             $idComune   = $user->getComune()->getId();
  76.         }
  77.         $form = $this->createForm(MapsComuneType::class, [$idComune]);
  78.         $form->handleRequest($request);
  79.         $locations=[];
  80.         if ($form->isSubmitted()) {
  81.             if ($form->get('comune')->getData() instanceof Comune){
  82.                 $cassonetti = $this->em->getRepository(Cassonetto::class)->findCassonettiByComune($form->get('comune')->getData()->getId());
  83.                 $locations=[];
  84.                 foreach ($cassonetti as $cassonetto) {
  85.                     $loc=[];
  86.                     $loc['id']=$cassonetto->getId();
  87.                     $loc['indirizzo']=str_replace("'","\u0027", $cassonetto->getIndirizzo());
  88.                     $loc['localita']=str_replace("'","\u0027",$cassonetto->getLocalita());
  89.                     $loc['descrizione']=str_replace("'","\u0027",$cassonetto->getDescrizione());
  90.                     $loc['lat']=$cassonetto->getLatitudine();
  91.                     $loc['long']=$cassonetto->getLongitudine();
  92.                     $locations[]=$loc;
  93.                 }
  94.                 // dd($locations);
  95.             }
  96.             return $this->render('default/gpsCassonetti.html.twig', ['form'=>$form->createView(),'locations'=>$locations]);
  97.         }
  98.         return $this->render('default/gpsCassonetti.html.twig', ['form'=>$form->createView(),'locations'=>$locations]);
  99.     }
  100.     #[Route(path: '/gpsautomezzi', name: 'gpsAutomezzi')]
  101.     public function gpsAutomezzi(Request $request): Response
  102.     {
  103.         $idComune = 0;
  104.         $user = $this->getUser();
  105.         if (in_array("ROLE_COMUNE", $user->getRoles()) ) {
  106.             $idComune   = $user->getComune()->getId();
  107.         }
  108.         $form = $this->createForm(GpsAutomezziType::class, [$idComune]);
  109.         $form->handleRequest($request);
  110.         $locations=[];
  111.         if ($form->isSubmitted()) {
  112.             try {
  113.                 if ($form->get('automezzo')->getData() instanceof AutomezziOggetti && $form->get('data')->getData()){
  114.                     $data = $form->get('data')->getData()->format('Ymd');
  115.                     $response = $this->client->request('GET', $this->getParameter('api_endpoint').'trackbymezzo/'.$form->get('automezzo')->getData()->getIdTrackIdea().'/'.$data);
  116.                     $punti = $response->toArray();
  117.                     $locations=[];
  118.                     foreach ($punti as $punto) {
  119.                         $loc=[];
  120.                         $loc['id']=$punto['IdGPSTrack'];
  121.                         $loc['lat']=$punto['Lat'];
  122.                         $loc['long']=$punto['Lon'];
  123.                         $locations[]=$loc;
  124.                     }
  125.                 }
  126.                 return $this->render('default/gpsAutomezzi.html.twig', ['form'=>$form->createView(),'locations'=>$locations]);
  127.             } catch (\Exception $ex){
  128.                 $this->addFlash('error', 'Non è possibile reperire dati GPS dal mezzo selezionato.');
  129.                 return $this->render('default/gpsAutomezzi.html.twig', ['form'=>$form->createView(),'locations'=>$locations]);
  130.             }
  131.         }
  132.         return $this->render('default/gpsAutomezzi.html.twig', ['form'=>$form->createView(),'locations'=>$locations]);
  133.     }
  134.     #[Route(path: '/gpsoggetti', name: 'gpsOggetti')]
  135.     public function gpsOggetti(Request $request): Response
  136.     {
  137.         $idComune = 0;
  138.         $user = $this->getUser();
  139.         if (in_array("ROLE_COMUNE", $user->getRoles()) ) {
  140.             $idComune   = $user->getComune()->getId();
  141.         }
  142.         $form = $this->createForm(GpsOggettiType::class, [$idComune]);
  143.         $form->handleRequest($request);
  144.         $rilevazioni=[];
  145.         if ($form->isSubmitted()) {
  146.             try {
  147.                 if ($form->get('oggetto')->getData() instanceof AutomezziOggetti && $form->get('data')->getData()){
  148.                     $data = $form->get('data')->getData()->format('Ymd');
  149.                     $response = $this->client->request('GET', $this->getParameter('api_endpoint').'livellosensori/'.$form->get('oggetto')->getData()->getCodiceSensore().'/'.$data);
  150.                     $rilevazioni = $response->toArray();
  151.                     $rilevazione = end($rilevazioni);
  152.                 }
  153.                 return $this->render('default/gpsOggetti.html.twig', ['form'=>$form->createView(),'rilevazionei'=>$rilevazioni, 'rilevazione' => $rilevazione]);
  154.             } catch (\Exception $ex){
  155.                 $this->addFlash('error', 'Non è possibile reperire dati GPS dall\' oggetto selezionato.');
  156.                 return $this->render('default/gpsOggetti.html.twig', ['form'=>$form->createView(),'rilevazioni'=>$rilevazioni, 'rilevazione' => []]);
  157.             }
  158.         }
  159.         return $this->render('default/gpsOggetti.html.twig', ['form'=>$form->createView(),'rilevazioni'=>$rilevazioni, 'rilevazione' => []]);
  160.     }
  161.     #[Route(path: '/reloadmarkers', name: 'reloadMarkers')]
  162.     public function reloadMarkers()
  163.     {
  164.         $cassonetti = [];
  165.         $locations=[];
  166.         foreach ($cassonetti as $cassonetto) {
  167.             $loc=[];
  168.             $loc['id']=$cassonetto->getId();
  169.             $loc['indirizzo']=$cassonetto->getIndirizzo();
  170.             $loc['localita']=$cassonetto->getLocalita();
  171.             $loc['descrizione']=$cassonetto->getDescrizione();
  172.             $loc['lat']=$cassonetto->getLongitudine();
  173.             $loc['long']=$cassonetto->getLatitudine();
  174.             $locations[]=$loc;
  175.         }
  176.         return  new JsonResponse(json_encode($locations),200,[],true);
  177.     }
  178.     #[Route(path: '/reloadmarkersautomezzi', name: 'reloadMarkersAutomezzi')]
  179.     public function reloadMarkersAutomezzi()
  180.     {
  181.         $punti = [];
  182.         $locations=[];
  183.         foreach ($punti as $punto) {
  184.             $loc=[];
  185.             $loc['id']=$punto['IdGPSTrack'];
  186.             $loc['lat']=$punto['Lat'];
  187.             $loc['long']=$punto['Lon'];
  188.             $locations[]=$loc;
  189.         }
  190.         return  new JsonResponse(json_encode($locations),200,[],true);
  191.     }
  192.     #[Route(path: '/reloadpostazioni', name: 'reloadPostazioni')]
  193.     public function reloadPostazioni(Environment  $environment)
  194.     {
  195.         $cassonetti = [];
  196.         $locations=[];
  197.         foreach ($cassonetti as $cassonetto) {
  198.             $loc=[];
  199.             $loc['id']=$cassonetto->getId();
  200.             $loc['indirizzo']=$cassonetto->getIndirizzo();
  201.             $loc['localita']=$cassonetto->getLocalita();
  202.             $loc['descrizione']=$cassonetto->getDescrizione();
  203.             $loc['lat']=$cassonetto->getLongitudine();
  204.             $loc['long']=$cassonetto->getLatitudine();
  205.             $locations[]=$loc;
  206.         }
  207.         $template = $environment->load("default/gpsCassonetti.html.twig");
  208.         $newLocations = $template->renderBlock('locationBlock', array('locations' => $locations));
  209.         return new Response(
  210.             json_encode(array(
  211.                     'result' => 'ok',
  212.                     'newLocations' => $newLocations
  213.                 )
  214.             ));
  215.     }
  216.     #[Route(path: '/reloadpostazioniautmezzi', name: 'reloadPostazioniAutomezzi')]
  217.     public function reloadPostazioniAutomezzi(Environment  $environment)
  218.     {
  219.         $punti = [];
  220.         $locations=[];
  221.         foreach ($punti as $punto) {
  222.             $loc=[];
  223.             $loc['id']=$punto['IdGPSTrack'];
  224.             $loc['lat']=$punto['Lat'];
  225.             $loc['long']=$punto['Lon'];
  226.             $locations[]=$loc;
  227.         }
  228.         $template = $environment->load("default/gpsAutomezzi.html.twig");
  229.         $newLocations = $template->renderBlock('locationBlock', array('locations' => $locations));
  230.         return new Response(
  231.             json_encode(array(
  232.                     'result' => 'ok',
  233.                     'newLocations' => $newLocations
  234.                 )
  235.             ));
  236.     }
  237. }