<?php
namespace App\Controller\FrontOffice;
use App\Controller\MaestroController;
use App\Repository\MaterialRepository;
use App\Repository\StudioRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class BookingController extends MaestroController
{
/**
* @Route ("/reservation", name="booking_form")
*/
public function booking(Request $request)
{
if($request->isMethod("POST")){
/** Get informations **/
$post = $this->getFormHTML($_POST);
$name = "";
$firstname = "";
$tel = "";
$email = "";
$address = "";
$code = "";
$city = "";
$products = ["studio" => [],"material" => []];
foreach($post as $p => $value){
$e = explode("-",$p);
if(count($e) > 1){
/** Basics informations **/
if($e[0] === "resa"){
switch ($e[1]){
case "name":
$name = $value;
break;
case "firstname":
$firstname = $value;
break;
case "tel":
$tel = $value;
break;
case "email":
$email = $value;
break;
case "address":
$address = $value;
break;
case "code":
$code = $value;
break;
case "city":
$city = $value;
break;
}
/** Products informations 1/2 **/
}elseif ($e[0] === "studio"){
$products["studio"][$p] = [
'name' => $value,
'start' => "",
'end' => ""
];
/** Get cookies to delete **/
$cookiesToDelete[] = $p;
}elseif ($e[0] === "material"){
$products["material"][$p] = [
'name' => $value,
'start' => "",
'end' => ""
];
/** Delete cookies **/
$cookiesToDelete[]=$p;
}
}
}
/** Products informations 2/2 **/
foreach ($products['studio'] as $key => $product){
$products["studio"][$key]["start"] = $post["date-start-day-".$key]."/".$post["date-start-month-".$key]."/".$post["date-start-year-".$key];
$products["studio"][$key]['end'] = $post["date-end-day-".$key]."/".$post["date-end-month-".$key]."/".$post["date-end-year-".$key];
}
foreach ($products['material'] as $key => $product){
$products["material"][$key]["start"] = $post["date-start-day-".$key]."/".$post["date-start-month-".$key]."/".$post["date-start-year-".$key];
$products["material"][$key]['end'] = $post["date-end-day-".$key]."/".$post["date-end-month-".$key]."/".$post["date-end-year-".$key];
}
/** Send Email to admin **/
$from = $this->variableConfig()['noreply'];
$to = $this->variableConfig()['admin'];
$object = "Nouvelle demande de devis";
$view = "email/booking.html.twig";
$viewParam = [
"name" => $name,
"firstname" => $firstname,
"tel" => $tel,
"email" => $email,
"address" => $address,
"code" => $code,
"city" => $city,
"products" => $products
];
$this->sendEmail($object,$from,$to,$view,$viewParam);
return $this->redirectToRoute("booking_thank");
}
return $this->maestroRender("front-office/booking/form.html.twig");
}
/**
* @Route ("/remerciement", name="booking_thank")
*/
public function thank()
{
return $this->maestroRender("front-office/booking/thank.html.twig");
}
/*******************************************************
* AJAX *
********************************************************/
/**
* @Route ("/ajax/remove-all-cookies", name="ajax_remove_all_cookies")
*/
public function removeAllCookies()
{
$cookies = $this->getFormHTML($_COOKIE);
foreach($cookies as $key => $c){
$keyE = explode("-",$key);
if(count($keyE) == 2){
if($keyE[0] == "material" || $keyE[0] == "studio"){
$this->removeCookie($key);
}
}
}
return new Response(true);
}
/**
* @Route ("/ajax/reservation/{product}/{id}", name="ajax_reservation")
*
* $product is "studio" or "material"
* $_POST = $id is Studio's ID or Material's ID
*/
public function ajaxReservation(StudioRepository $studioRepository, MaterialRepository $materialRepository,
$product,$id)
{
$productType = $this->secureInput($product);
if($productType === "studio"){
[$id,$prod] = $this->secureAndFind($id,$studioRepository);
}elseif ($productType === "material"){
[$id,$prod] = $this->secureAndFind($id,$materialRepository);
}
if(isset($prod)){
$response = [
'id' => $id,
'price' => $prod->getPriceHt(),
'tva' => $prod->getTva(),
'name' => $prod->getName(),
'type' => $productType
];
}else{
$response = false;
}
return new Response(json_encode($response));
}
/**
* @Route ("/ajax/cookie/remove/{product}/{id}",name="ajax_cookie_remove")
* $product is "studio" or "material"
* $_POST = $id is Studio's ID or Material's ID
*/
public function ajaxCookieRemove($product,$id)
{
$product = $this->secureInput($product);
$id = $this->secureInput($id);
$this->newCookie($product."-".$id,"0",time()-3600);
return new Response(true);
}
}