src/Controller/FrontOffice/BookingController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller\FrontOffice;
  3. use App\Controller\MaestroController;
  4. use App\Repository\MaterialRepository;
  5. use App\Repository\StudioRepository;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class BookingController extends MaestroController
  10. {
  11.     /**
  12.      * @Route ("/reservation", name="booking_form")
  13.      */
  14.     public function booking(Request $request)
  15.     {
  16.         if($request->isMethod("POST")){
  17.             /** Get informations **/
  18.             $post $this->getFormHTML($_POST);
  19.             $name "";
  20.             $firstname "";
  21.             $tel "";
  22.             $email "";
  23.             $address "";
  24.             $code "";
  25.             $city "";
  26.             $products = ["studio" => [],"material" => []];
  27.             foreach($post as $p => $value){
  28.                 $e explode("-",$p);
  29.                 if(count($e) > 1){
  30.                     /** Basics informations **/
  31.                     if($e[0] === "resa"){
  32.                         switch ($e[1]){
  33.                             case "name":
  34.                                 $name $value;
  35.                                 break;
  36.                             case "firstname":
  37.                                 $firstname $value;
  38.                                 break;
  39.                             case "tel":
  40.                                 $tel $value;
  41.                                 break;
  42.                             case "email":
  43.                                 $email $value;
  44.                                 break;
  45.                             case "address":
  46.                                 $address $value;
  47.                                 break;
  48.                             case "code":
  49.                                 $code $value;
  50.                                 break;
  51.                             case "city":
  52.                                 $city $value;
  53.                                 break;
  54.                         }
  55.                         /** Products informations 1/2 **/
  56.                     }elseif ($e[0] === "studio"){
  57.                         $products["studio"][$p] = [
  58.                             'name' => $value,
  59.                             'start' => "",
  60.                             'end' => ""
  61.                         ];
  62.                         /** Get cookies to delete **/
  63.                         $cookiesToDelete[] = $p;
  64.                     }elseif ($e[0] === "material"){
  65.                         $products["material"][$p] = [
  66.                             'name' => $value,
  67.                             'start' => "",
  68.                             'end' => ""
  69.                         ];
  70.                         /** Delete cookies **/
  71.                         $cookiesToDelete[]=$p;
  72.                     }
  73.                 }
  74.             }
  75.             /** Products informations 2/2 **/
  76.             foreach ($products['studio'] as $key => $product){
  77.                 $products["studio"][$key]["start"] = $post["date-start-day-".$key]."/".$post["date-start-month-".$key]."/".$post["date-start-year-".$key];
  78.                 $products["studio"][$key]['end'] = $post["date-end-day-".$key]."/".$post["date-end-month-".$key]."/".$post["date-end-year-".$key];
  79.             }
  80.             foreach ($products['material'] as $key => $product){
  81.                 $products["material"][$key]["start"] = $post["date-start-day-".$key]."/".$post["date-start-month-".$key]."/".$post["date-start-year-".$key];
  82.                 $products["material"][$key]['end'] = $post["date-end-day-".$key]."/".$post["date-end-month-".$key]."/".$post["date-end-year-".$key];
  83.             }
  84.             /** Send Email to admin **/
  85.             $from $this->variableConfig()['noreply'];
  86.             $to $this->variableConfig()['admin'];
  87.             $object "Nouvelle demande de devis";
  88.             $view "email/booking.html.twig";
  89.             $viewParam = [
  90.                 "name" => $name,
  91.                 "firstname" => $firstname,
  92.                 "tel" => $tel,
  93.                 "email" => $email,
  94.                 "address" => $address,
  95.                 "code" => $code,
  96.                 "city" => $city,
  97.                 "products" => $products
  98.             ];
  99.             $this->sendEmail($object,$from,$to,$view,$viewParam);
  100.             return $this->redirectToRoute("booking_thank");
  101.         }
  102.         return $this->maestroRender("front-office/booking/form.html.twig");
  103.     }
  104.     /**
  105.      * @Route ("/remerciement", name="booking_thank")
  106.      */
  107.     public function thank()
  108.     {
  109.         return $this->maestroRender("front-office/booking/thank.html.twig");
  110.     }
  111.     /*******************************************************
  112.      *                      AJAX                           *
  113.      ********************************************************/
  114.     /**
  115.      * @Route ("/ajax/remove-all-cookies", name="ajax_remove_all_cookies")
  116.      */
  117.     public function removeAllCookies()
  118.     {
  119.         $cookies $this->getFormHTML($_COOKIE);
  120.         foreach($cookies as $key => $c){
  121.             $keyE explode("-",$key);
  122.             if(count($keyE) == 2){
  123.                 if($keyE[0] == "material" || $keyE[0] == "studio"){
  124.                     $this->removeCookie($key);
  125.                 }
  126.             }
  127.         }
  128.         return new Response(true);
  129.     }
  130.     /**
  131.      * @Route ("/ajax/reservation/{product}/{id}", name="ajax_reservation")
  132.      *
  133.      * $product is "studio" or "material"
  134.      * $_POST = $id is Studio's ID or Material's ID
  135.      */
  136.     public function ajaxReservation(StudioRepository $studioRepositoryMaterialRepository $materialRepository,
  137.     $product,$id)
  138.     {
  139.         $productType $this->secureInput($product);
  140.         if($productType === "studio"){
  141.             [$id,$prod] = $this->secureAndFind($id,$studioRepository);
  142.         }elseif ($productType === "material"){
  143.             [$id,$prod] = $this->secureAndFind($id,$materialRepository);
  144.         }
  145.         if(isset($prod)){
  146.             $response = [
  147.                 'id' => $id,
  148.                 'price' => $prod->getPriceHt(),
  149.                 'tva' => $prod->getTva(),
  150.                 'name' => $prod->getName(),
  151.                 'type' => $productType
  152.             ];
  153.         }else{
  154.             $response false;
  155.         }
  156.         return new Response(json_encode($response));
  157.     }
  158.     /**
  159.      * @Route ("/ajax/cookie/remove/{product}/{id}",name="ajax_cookie_remove")
  160.      * $product is "studio" or "material"
  161.      * $_POST = $id is Studio's ID or Material's ID
  162.      */
  163.     public function ajaxCookieRemove($product,$id)
  164.     {
  165.         $product $this->secureInput($product);
  166.         $id $this->secureInput($id);
  167.         $this->newCookie($product."-".$id,"0",time()-3600);
  168.         return new Response(true);
  169.     }
  170. }