src/Controller/HobitController.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. //Load core
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use \DateTime;
  6. //Load json rest
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use FOS\RestBundle\Controller\Annotations as Rest// alias pour toutes les annotations
  12. use Geocoder\Query\GeocodeQuery;
  13. use Geocoder\Query\ReverseQuery;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use App\Services\Utils;
  16. use App\Services\Tools;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. //Load entity
  20. use App\Entity\PositionGps;
  21. use App\Entity\Hobit;
  22. use App\Entity\User;
  23. use App\Entity\HobitDate;
  24. class HobitController extends AbstractController
  25. {
  26.     protected $em;
  27.     protected $utils;
  28.     protected $tools;
  29.     public function __construct(EntityManagerInterface $entityManagerUtils $utilsTranslatorInterface $translatorTools $tools) {
  30.         $this->em $entityManager;
  31.         $this->utils $utils;
  32.         $this->translator $translator;
  33.         $this->tools $tools;
  34.     }
  35.     /**
  36.      * @Rest\View(statusCode=201)
  37.      * @Rest\Get("/api/hobit/sync")
  38.      */
  39.     public function syncHobitApiAction(Request $request)
  40.     {    
  41.         $authTokenHeader $request->headers->get('X-Auth-Token');
  42.         $auth $this->em
  43.                          ->getRepository(AuthToken::class)
  44.                          ->findOneBy(array("value" => $authTokenHeader));
  45.         $tabHobit = [];
  46.         //For the distance between current position and last position default 5 (in metter))
  47.         $limitWidthHobit 10
  48.         //Time minimum between current position and last position, in second
  49.         $limitTimeHobit 300
  50.         //default : 20
  51.         $limitForCreateHobit 2;
  52.         //For create hobit, remove error and driver 
  53.         $limitStable 1
  54.         //For sum in XX address (Good points in target)
  55.         $maxSum 100;
  56.         //last value, interval (date printed and date server) for get last data. The new value work with th background. (warning, change the number in position save controller). This value can stop $limitForCreateHobit and $limitStable.
  57.         $numberIntervalPrintedAndServer 6;
  58.         //Debug
  59.         $debug true;
  60.         $debugAndFlush false;
  61.         //Write 'null' for disable
  62.         $dateStartForce null;
  63.         $dateEndForce null;
  64.         //$dateEndForce = "20220528170000";
  65.         $tabHobit $this->tools->dreamHobit($debug$debugAndFlush$limitWidthHobit$limitTimeHobit$limitForCreateHobit$limitStable$maxSum$numberIntervalPrintedAndServer$auth->getUser(), $dateEndForce$dateStartForce);
  66.         $hobits $this->tools->createHobit($tabHobit$limitWidthHobit$limitTimeHobit$limitForCreateHobit$maxSum$auth->getUser());
  67.         return true;
  68.     }
  69.     /**
  70.      * @Route("/hobit/sync", name="hobit_sync")
  71.      */
  72.     public function syncHobitAction(Request $request)
  73.     {   
  74.         $this->syncHobitDebugAction($requesttrue);
  75.         return $this->redirectToRoute('information_live_debug');
  76.     }
  77.     /**
  78.      * @Route("/debug/hobit", name="hobit_debug")
  79.      */
  80.     public function syncHobitDebugAction(Request $request$flush false)
  81.     {    
  82.         //Config
  83.         /*
  84.         $lat1 = "45.759465500637";
  85.         $lng1 = "4.8491488866881";
  86.         $lat2 = "45.759445726474";
  87.         $lng2 = "4.8491441637739";
  88.         $earth_radius = 6378137;   
  89.         $rlo1 = deg2rad($lng1);
  90.         $rla1 = deg2rad($lat1);
  91.         $rlo2 = deg2rad($lng2);
  92.         $rla2 = deg2rad($lat2);
  93.         $dlo = ($rlo2 - $rlo1) / 2;
  94.         $dla = ($rla2 - $rla1) / 2;
  95.         $a = (sin($dla) * sin($dla)) + cos($rla1) * cos($rla2) * (sin($dlo) * sin($dlo));
  96.         $d = 2 * atan2(sqrt($a), sqrt(1 - $a));
  97.         $valeur = ($earth_radius * $d);
  98.         echo $valeur;
  99.     die;
  100.     */
  101.         $tabHobit = [];
  102.         //For the distance between current position and last position default 5 (in metter))
  103.         $limitWidthHobit 10
  104.         //Time minimum between current position and last position, in second
  105.         $limitTimeHobit 120
  106.         //default : 20
  107.         $limitForCreateHobit 2;
  108.         //For create hobit, remove error and driver 
  109.         $limitStable 1
  110.         //For sum in XX address (Good points in target)
  111.         $maxSum 100;
  112.         //last value, interval (date printed and date server) for get last data. The new value work with th background. (warning, change the number in position save controller). This value can stop $limitForCreateHobit and $limitStable.
  113.         $numberIntervalPrintedAndServer 6;
  114.         //Debug
  115.         $debug true;
  116.         $debugAndFlush $flush;
  117.         $session = new Session();
  118.         $userId $session->get('currentUser');
  119.         if(!$userId){
  120.             $userId 4;
  121.         }
  122.         $userCurrent $this->em->getRepository(User::class)->find($userId);
  123.         //Write 'null' for disable
  124.         $dateStartForce "20220622080000";
  125.         $dateEndForce null;
  126.         //$dateEndForce = "20220528170000";
  127.         $tabHobit $this->tools->dreamHobit($debug$debugAndFlush$limitWidthHobit$limitTimeHobit$limitForCreateHobit$limitStable$maxSum$numberIntervalPrintedAndServer$userCurrent$dateEndForce$dateStartForce);
  128.         if($debugAndFlush){
  129.             $hobits $this->tools->createHobit($tabHobit$limitWidthHobit$limitTimeHobit$limitForCreateHobit$maxSum$userCurrent);
  130.             $this->addFlash('success''User has been sync.');
  131.             
  132.             return true;
  133.         }
  134.         else{
  135.             $hobits $tabHobit;
  136.         }
  137.         //var_dump($tabHobit);
  138.         //die;
  139.         
  140. /*
  141.         $curl = curl_init();
  142. // Set some options - we are passing in a useragent too here
  143. curl_setopt_array($curl, [
  144.     CURLOPT_RETURNTRANSFER => 1,
  145.     CURLOPT_URL => 'https://maps.googleapis.com/maps/api/streetview?size=600x300&location=45.763451584654,4.8423398764755&heading=151.78&pitch=-0.76&key=AIzaSyCq_-9DHJzN2z1wPx4O_v5PvVFvLhZlkmI',
  146.     CURLOPT_USERAGENT => 'Codular Sample cURL Request'
  147. ]);
  148. // Send the request & save response to $resp
  149. $resp = curl_exec($curl);
  150. // Close request to clear up some resources
  151. curl_close($curl);
  152. return $resp;
  153.         // close curl resource to free up system resources 
  154.     
  155.         die;
  156. */
  157.         return $this->render('debug/hobit.html.twig', Array(
  158.            "tabHobit" => $hobits,
  159.            "debugAndFlush" => $debugAndFlush
  160.         ));
  161.     }
  162.     /**
  163.      * @Rest\View(statusCode=201)
  164.      * @Rest\Get("/api/hobit/list")
  165.      */
  166.     public function getHobitAction(Request $request)
  167.     {    
  168.         //Load base
  169.         $em $this->em;
  170.         
  171.         //All without "NOW" 
  172.         /*
  173.         $hobits = $this->em
  174.                          ->getRepository(Hobit::class)
  175.                          ->findHobitAllWithoutNow();
  176.         */
  177.         $hobits $this->em
  178.                          ->getRepository(Hobit::class)
  179.                          ->findHobitAll();
  180.         return $hobits;
  181.     }
  182.     /**
  183.      * @Rest\View(statusCode=201)
  184.      * @Rest\Get("/api/hobit/hobit-date-list")
  185.      */
  186.     public function getHobitDateAction(Request $request)
  187.     {   
  188.         //Load base
  189.         $em $this->em;
  190.         
  191.         //All without "NOW" 
  192.         /*
  193.         $hobits = $this->em
  194.                          ->getRepository(Hobit::class)
  195.                          ->findHobitAllWithoutNow();
  196.         */
  197.         $hobitsDate $this->em
  198.                          ->getRepository(HobitDate::class)
  199.                          ->findAll();
  200.         $hobitsClean = [];
  201.         foreach ($hobitsDate as $key => $hobitDate) {
  202.             $hobitsClean[$key]['id'] = $hobitDate->getHobit()->getId();
  203.             $hobitsClean[$key]['address'] = $hobitDate->getHobit()->getAddress();
  204.             $hobitsClean[$key]['latitude'] = $hobitDate->getHobit()->getLatitude();
  205.             $hobitsClean[$key]['longitude'] = $hobitDate->getHobit()->getLongitude();
  206.             $hobitsClean[$key]['dateStartHobit'] = $hobitDate->getDateStartHobit();
  207.             $hobitsClean[$key]['dateEndHobit'] = $hobitDate->getDateEndHobit();
  208.             $hobitsClean[$key]['groupName'] = $hobitDate->getHobit()->getGroupName();
  209.             $hobitsClean[$key]['groupNameTranslate'] = $hobitDate->getHobit()->getGroupNameTranslate();
  210.             $hobitsClean[$key]['groupVerif'] = $hobitDate->getHobit()->getGroupVerif();
  211.         }
  212.         $hobitsClean array_reverse($hobitsClean);
  213.         return $hobitsClean;
  214.     }
  215.     /**
  216.      * @Rest\View(statusCode=201)
  217.      * @Rest\Get("/api/hobit/current")
  218.      */
  219.     public function getHobitCurrentAction(Request $request)
  220.     {    
  221.         //Load base
  222.         $em $this->em;
  223.         //Load get
  224.         $hobit $this->em
  225.                          ->getRepository(Hobit::class)
  226.                          ->findHobitCurrent();
  227.         return $hobit;
  228.     }
  229.     /**
  230.      * @Rest\View(statusCode=201)
  231.      * @Rest\Get("/api/hobit/history/{idHobit}")
  232.      */
  233.     public function getHobitHistoryAction(Request $request)
  234.     {    
  235.         //Load base
  236.         $em $this->em;
  237.         //Load get
  238.         $idHobit $request->get('idHobit');
  239.         
  240.         $hobits $this->em
  241.                          ->getRepository(Hobit::class)
  242.                          ->findAllHobitdateByHobit($idHobit);
  243.                          /*
  244.         $tabHobits = [];
  245.         foreach ($hobits as $key -> $hobit) {
  246.             $tabHobits[$key] = 
  247.         }
  248.         */
  249.         return $hobits;
  250.     }
  251.     /**
  252.      * @Rest\View(statusCode=201)
  253.      * @Rest\Get("/api/hobit/group")
  254.      */
  255.     public function getHobitGroupAction(Request $request)
  256.     {   
  257.         //Load base
  258.         $em $this->em;
  259.         //Load get
  260.         $hobits $this->em
  261.                          ->getRepository(Hobit::class)
  262.                          ->findHobitGroup();
  263.         return $hobits;
  264.     }
  265.     /**
  266.      * @Rest\View(statusCode=201)
  267.      * @Rest\Get("/api/hobit/group-no-verif")
  268.      */
  269.     public function getHobitGroupVerifAction(Request $request)
  270.     {   
  271.         //Load base
  272.         $em $this->em;
  273.         //Load get
  274.         $hobits $this->em
  275.                          ->getRepository(Hobit::class)
  276.                          ->findHobitGroupNoVerif();
  277.         $hobitsClean = [];
  278.         foreach ($hobits as $key => $hobit) {
  279.             $hobitsClean[$key]['id'] = $hobit->getId();
  280.             $hobitsClean[$key]['address'] = $hobit->getAddress();
  281.             $hobitsClean[$key]['latitude'] = $hobit->getLatitude();
  282.             $hobitsClean[$key]['longitude'] = $hobit->getLongitude();
  283.             $hobitsClean[$key]['groupName'] = $hobit->getGroupName();
  284.             $hobitsClean[$key]['groupNameTranslate'] = $hobit->getGroupNameTranslate();
  285.             $hobitsClean[$key]['groupVerif'] = $hobit->getGroupVerif();
  286.         }
  287.         return $hobitsClean;
  288.     }
  289.     /**
  290.      * @Rest\View(statusCode=201)
  291.      * @Rest\Get("/api/hobit/city")
  292.      */
  293.     public function getHobitCityAction(Request $request)
  294.     {   
  295.         //Load base
  296.         $em $this->em;
  297.         //Load get
  298.         $hobits $this->em
  299.                          ->getRepository(Hobit::class)
  300.                          ->findHobitCity();
  301.         $hobitsClean = [];
  302.         foreach ($hobits as $key => $hobit) {
  303.             $hobitsClean[$key]['city'] = $hobit['city'];
  304.             /*
  305.             $hobitsClean[$key]['id'] = $hobit['id'];
  306.             $hobitsClean[$key]['address'] = $hobit['address'];
  307.             $hobitsClean[$key]['latitude'] = $hobit['latitude'];
  308.             $hobitsClean[$key]['longitude'] = $hobit['longitude'];
  309.             $hobitsClean[$key]['groupName'] = $hobit['group_name'];
  310.             $hobitsClean[$key]['groupVerif'] = $hobit['group_verif'];
  311.             */
  312.         }
  313.         return $hobitsClean;
  314.     }
  315.     /**
  316.      * @Rest\View(statusCode=201)
  317.      * @Rest\Get("/api/hobit/group-change-verif/{hobitId}/{type}")
  318.      */
  319.     public function getHobitGroupChangeVerifAction(Request $request$hobitId$type)
  320.     {   
  321.         //Load base
  322.         $em $this->em;
  323.         //Load get
  324.         $hobit $this->em
  325.                          ->getRepository(Hobit::class)
  326.                          ->findOneBy(array("id" => $hobitId));
  327.         if($hobit){
  328.             if($type == "yes"){
  329.                 $hobit->setGroupVerif(1);  
  330.             }
  331.             else if($type == "no"){
  332.                 $hobit->setGroupVerif(1);  
  333.                 $hobit->setGroupName(null);  
  334.             }
  335.             else if($type == "never"){
  336.                 $hobit->setGroupVerif(1);  
  337.                 $hobit->setGroupName("never");  
  338.             }
  339.             
  340.             $em->persist($hobit);
  341.             $em->flush();
  342.         } 
  343.         return true;
  344.     }
  345. }