src/Controller/WebserviceController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use FOS\RestBundle\Controller\FOSRestController;
  4. use FOS\RestBundle\Controller\Annotations\Post;
  5. use FOS\RestBundle\Controller\Annotations\Get;
  6. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  7. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  8. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Bundle\FrameworkBundle\Console\Application;
  11. use Symfony\Component\Console\Input\ArrayInput;
  12. use Symfony\Component\Console\Output\NullOutput;
  13. use App\Entity\User;
  14. use App\Entity\Category;
  15. use App\Entity\Product;
  16. use App\Entity\Slider;
  17. use App\Entity\AccessToken;
  18. use App\Entity\RefreshToken;
  19. use App\Entity\Report;
  20. use Symfony\Component\Asset\Package;
  21. use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
  22. use App\Services\MailerService;
  23. use Symfony\Component\HttpFoundation\Request;
  24. class WebserviceController extends FOSRestController
  25. {
  26.     private $mailerService;
  27.     public function __construct(MailerService $mailerService)
  28.     {
  29.         $this->mailerService $mailerService;
  30.     }
  31.     /**
  32.      * POST Route annotation.
  33.      * @Post("/login", defaults={"_format"="json"})
  34.      */
  35.     public function loginAction()
  36.     {
  37.         $request Request::createFromGlobals();
  38.         $email $request->request->get('email');
  39.         $password $request->request->get('password');
  40.         $client_id=$this->container->getParameter('client_id');
  41.         $client_secret=$this->container->getParameter('client_secret');
  42.         $grant_type="password";
  43.         $url=$this->container->getParameter('base_url').$this->generateUrl('fos_oauth_server_token', array('client_id' => $client_id,'client_secret' => $client_secret,'grant_type' => $grant_type,'username' => $email,'password' => $password));
  44.         $ch curl_init();
  45.         curl_setopt($chCURLOPT_URL$url);
  46.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  47.  
  48.         $output curl_exec($ch);
  49.         if ($output === false) {
  50.             throw new \Exception(curl_error($ch), curl_errno($ch));
  51.         }
  52.         curl_close($ch);
  53.         $obj=json_decode($output);
  54.         if (isset($obj->error)) {
  55.             $data = array(
  56.                 'error' => $obj->error_description
  57.             );
  58.         } else {
  59.             $em $this->getDoctrine()->getEntityManager();
  60.             $user=$em->getRepository(User::class)->findOneBy(array("email"=>$email));
  61.             $data = array(
  62.                 'access_token' => $obj->access_token,
  63.                 'refresh_token' => $obj->refresh_token
  64.             );
  65.         }
  66.         
  67.         $view $this->view($data);
  68.         return $this->handleView($view);
  69.     }
  70.     /**
  71.      * POST Route annotation.
  72.      * @Post("/refresh", defaults={"_format"="json"})
  73.      */
  74.     public function refreshAction()
  75.     {
  76.         $request Request::createFromGlobals();
  77.         $refresh_token $request->request->get('refresh_token');
  78.         $client_id=$this->container->getParameter('client_id');
  79.         $client_secret=$this->container->getParameter('client_secret');
  80.         $grant_type="refresh_token";
  81.         $url=$this->container->getParameter('base_url').$this->generateUrl('fos_oauth_server_token', array('client_id' => $client_id,'client_secret' => $client_secret,'grant_type' => $grant_type,'refresh_token' => $refresh_token));
  82.         $ch curl_init();
  83.         curl_setopt($chCURLOPT_URL$url);
  84.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  85.  
  86.         $output curl_exec($ch);
  87.  
  88.         curl_close($ch);
  89.         $obj=json_decode($output);
  90.         if (isset($obj->error)) {
  91.             $data = array(
  92.                 'error' => $obj->error
  93.             );
  94.             $view $this->view($data);
  95.             return $this->handleView($view);
  96.         }
  97.         $data = array(
  98.             'access_token' => $obj->access_token,
  99.             'refresh_token' => $obj->refresh_token
  100.         );
  101.         
  102.         $view $this->view($data);
  103.         return $this->handleView($view);
  104.     }
  105.     /**
  106.      * GET Route annotation.
  107.      * @Get("/getdata", defaults={"_format"="json"})
  108.      */
  109.     public function getdataAction()
  110.     {       
  111.         $em $this->getDoctrine()->getEntityManager();
  112.         $data $this->getAllData();
  113.         $view $this->view($data);
  114.         return $this->handleView($view);
  115.     }
  116.     private function getAllData(){
  117.         $BASE_URL $this->container->getParameter('base_url').'/';
  118.         $package = new Package(new EmptyVersionStrategy());
  119.         $em $this->getDoctrine()->getEntityManager();
  120.         $MainCategories $em->getRepository(Category::class)->findBy(array('categoryParent' => null));
  121.         $categories = array();
  122.         foreach ($MainCategories as $MainCategory) {
  123.             $subcategories = array();
  124.             $SubCats $em->getRepository(Category::class)->findBy(array('categoryParent' => $MainCategory));
  125.             foreach ($SubCats as $SubCat) {
  126.                 $products = array();
  127.                 //$Prods = $em->getRepository(Product::class)->findBy(array('category' => $SubCat));
  128.                 $Prods $SubCat->getProducts();
  129.                 foreach ($Prods as $Prod) {
  130.                     $detailed_plans = [];
  131.                     $operation_images = [];
  132.                     if(!is_null($Prod->getDetailedPlans())) {
  133.                         foreach ($Prod->getDetailedPlans() as $dp) {
  134.                             array_push($detailed_plans, [ "title" => is_null($dp->getTitle()) ? '' $dp->getTitle(), "file" => ($dp->getFile() ? $BASE_URL.$package->getUrl('media/product/pdfPlanBatiments/'$Prod->getId().'/'.$dp->getFile()) : '')]);
  135.                         }
  136.                     }
  137.                     if(!is_null($Prod->getOperationImages())) {
  138.                         foreach ($Prod->getOperationImages() as $img) {
  139.                             array_push($operation_images, ($img->getFile()) ? $BASE_URL.$package->getUrl('media/product/image/'.$Prod->getId().'/'.$img->getFile()) : '');
  140.                         }
  141.                     }
  142.                     if ($Prod->getLat() != '' && $Prod->getLng() != '') {
  143.                         $video 'NON';
  144.                         if ($Prod->getVideo()) {
  145.                             $video 'OUI';
  146.                         }
  147.                         $products[] = array(
  148.                             "id" => $Prod->getId(),
  149.                             "name" => $Prod->getTitle(),
  150.                             "description" => $Prod->getDescription(),
  151.                             "address" => $Prod->getAddress(),
  152.                             "zipcode" => $Prod->getZipcode(),
  153.                             "city" => $Prod->getCity()." (".substr($Prod->getZipcode(),0,2).")",
  154.                             "lat" => $Prod->getLat(),
  155.                             "lng" => $Prod->getLng(),
  156.                             "contact" => $Prod->getContact(),
  157.                             "video" => $video,
  158.                             "videoUrl" => is_null($Prod->getVideoSketch()) ? '' $Prod->getVideoSketch(),
  159.                             "pdf" => ($Prod->getPdf()) ? $BASE_URL.$package->getUrl('media/product/pdf/'.$Prod->getId().'/'.$Prod->getPdf()) : '',
  160.                             "pdfPlanMasse" => [
  161.                                 "title" => is_null($Prod->getPdfPlansDetaille()) ? '' $Prod->getPdfPlansDetaille(),
  162.                                 "file" => ($Prod->getPdfPlanMasse()) ? $BASE_URL.$package->getUrl('media/product/pdfPlanMasse/'.$Prod->getId().'/'.$Prod->getPdfPlanMasse()) : ''
  163.                             ],
  164.                             "pdfPlansBatiments" => $detailed_plans,
  165.                             "image" => ($Prod->getImage()) ? $BASE_URL.$package->getUrl('media/product/image/'.$Prod->getId().'/'.$Prod->getImage()) : '',
  166.                             "operation_images" => $operation_images
  167.                         );
  168.                     }
  169.                 }
  170.                 $subcategories[] = array(
  171.                     "id" => $SubCat->getId(),
  172.                     "name" => $SubCat->getTitle(),
  173.                     "pdf" => ($SubCat->getPdf()) ? $BASE_URL.$package->getUrl('media/category/pdf/'.$SubCat->getId().'/'.$SubCat->getPdf()) : '',
  174.                     "image" => ($SubCat->getImage()) ? $BASE_URL.$package->getUrl('media/category/image/'.$SubCat->getId().'/'.$SubCat->getImage()) : '',
  175.                     "marker" => ($SubCat->getMarker()) ? $this->imageToBase64($BASE_URL.$package->getUrl('media/category/marker/'.$SubCat->getId().'/'.$SubCat->getMarker())) : '',
  176.                     "products" => $products
  177.                 );
  178.             }
  179.             $categories[] = array(
  180.                 "id" => $MainCategory->getId(),
  181.                 "name" => $MainCategory->getTitle(),
  182.                 "pdf" => ($MainCategory->getPdf()) ? $BASE_URL.$package->getUrl('media/category/pdf/'.$MainCategory->getId().'/'.$MainCategory->getPdf()) : '',
  183.                 "subcategories" => $subcategories
  184.             );
  185.         }
  186.         $MainSliders $em->getRepository(Slider::class)->findBy(array(),array('sort'=>'ASC'));
  187.         $sliders = array();
  188.         foreach ($MainSliders as $MainSlider) {
  189.             $sliders[] = array(
  190.                 "image" => ($MainSlider->getImage()) ? $BASE_URL.$package->getUrl('media/slider/image/'.$MainSlider->getId().'/'.$MainSlider->getImage()) : '',
  191.                 "category_id" => ($MainSlider->getCategory()) ? $MainSlider->getCategory()->getId() : ''
  192.             );
  193.         }
  194.         
  195.         $data = array(
  196.             "sliders" => $sliders,
  197.             "categories" => $categories
  198.         );
  199.         return $data;
  200.     }
  201.     /**
  202.      * POST Route annotation.
  203.      * @Post("/report", defaults={"_format"="json"})
  204.      */
  205.     public function reportAction()
  206.     {
  207.         $request Request::createFromGlobals();
  208.         $em $this->getDoctrine()->getEntityManager();
  209.         $product_id $request->request->get('product');
  210.         $product $em->getRepository(Product::class)->find($product_id);
  211.         $email $request->request->get('email');
  212.         $activity $request->request->get('activity');
  213.         $visit $request->request->get('visit');
  214.         $agent $request->request->get('agent');
  215.         $surface $request->request->get('surface');
  216.         $deadline $request->request->get('deadline');
  217.         $location = ($request->request->get('location') == 'true') ? true false;
  218.         $vente = ($request->request->get('vente') == 'true') ? true false;
  219.         $visited = ($request->request->get('visited') == 'true') ? true false;
  220.         $visitedDate $request->request->get('visitedDate');
  221.         $client $request->request->get('client');
  222.         $visitor $request->request->get('visitor');
  223.         $report = new Report();
  224.         $report->setProduct($product);
  225.         $report->setEmail($email);
  226.         $report->setActivity($activity);
  227.         $report->setVisit($visit);
  228.         $report->setAgent($agent);
  229.         $report->setSurface($surface);
  230.         $report->setDeadline($deadline);
  231.         $report->setLocation($location);
  232.         $report->setVente($vente);
  233.         $report->setClient($client);
  234.         $report->setVisitor($visitor);
  235.         $report->setVisited($visited);
  236.         $report->setVisitedDate($visitedDate);
  237.         $em->persist($report);
  238.         $em->flush();
  239.         $textlocation =  ($location) ? 'oui' 'non';
  240.         $textvente =  ($vente) ? 'oui' 'non';
  241.         $textvisited =  ($visited) ? 'oui' 'non';
  242.         $data "
  243.             Email : " $email "<br>
  244.             Bien : " $product->getTitle() . "<br>
  245.             Activité : " $activity "<br>
  246.             Bât / Lot visité : " $visit "<br>
  247.             Agence / Agent : " $agent "<br>
  248.             Surfaces recherchées : " $surface "<br>
  249.             Echéance : " $deadline "<br>
  250.             Location: " $textlocation "<br>
  251.             Vente: " $textvente "<br>
  252.             Nom de la société / client: " $client "<br>
  253.             Nom du visiteur: " $visitor "<br>
  254.             Visité: " $textvisited "<br>
  255.             Date de la visite: " $visitedDate "<br>
  256.         ";
  257.         $email "contact.ie@spirit.net";
  258.         //$email = "rprunier@spirit.net";
  259.         // $email = "mel.martineau@gmail.com";
  260.         // $email = "a.marin@rezoloco.com";
  261.         $result $this->mailerService->sendEmailForTemplate(array($email), ''1, array('DATA' => $data));
  262.         $view $this->view(array('message' => $result));
  263.         return $this->handleView($view);
  264.     }
  265.     private function base64_encode_image ($filename) {
  266.         if ($filename) {
  267.             $b64_url 'php://filter/read=convert.base64-encode/resource='.$filename;
  268.             $curl_handle=curl_init();
  269.             curl_setopt($curl_handleCURLOPT_URL,$b64_url);
  270.             curl_setopt($curl_handleCURLOPT_CONNECTTIMEOUT2);
  271.             curl_setopt($curl_handleCURLOPT_RETURNTRANSFER1);
  272.             $query curl_exec($curl_handle);
  273.             curl_close($curl_handle);
  274.             return $query;
  275.         }
  276.     }
  277.     private function curl_get_contents($url)
  278.     {
  279.         $ch curl_init();
  280.         curl_setopt($chCURLOPT_HEADER0);
  281.         curl_setopt($chCURLOPT_RETURNTRANSFER1);
  282.         curl_setopt($chCURLOPT_URL$url);
  283.         $data curl_exec($ch);
  284.         curl_close($ch);
  285.         return $data;
  286.     }
  287.     private function imageToBase64($image){
  288.         $imageData base64_encode($this->curl_get_contents($image));
  289.         $mime_types = array(
  290.         'pdf' => 'application/pdf',
  291.         'doc' => 'application/msword',
  292.         'odt' => 'application/vnd.oasis.opendocument.text ',
  293.         'docx'    => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  294.         'gif' => 'image/gif',
  295.         'jpg' => 'image/jpg',
  296.         'jpeg' => 'image/jpeg',
  297.         'png' => 'image/png',
  298.         'bmp' => 'image/bmp'
  299.         );
  300.         $ext pathinfo($imagePATHINFO_EXTENSION);
  301.         $a false;
  302.         if (array_key_exists($ext$mime_types)) {
  303.             $a $mime_types[$ext];
  304.         }
  305.         if (!$a) {
  306.             $ext substr($image, -3);
  307.             if (array_key_exists($ext$mime_types)) {
  308.                 $a $mime_types[$ext];
  309.             }
  310.         }
  311.         if (!$a) {
  312.             $a 'image/jpg';
  313.         }
  314.         return 'data: '.$a.';base64,'.$imageData;
  315.     }
  316. }