<?php
namespace App\Controller\Booking;
use App\Service\BookingService;
use Pimcore\Model\DataObject\BookingConfig;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Pimcore\Model\DataObject\Region;
use Pimcore\Model\DataObject\Governorate;
use Pimcore\Model\DataObject\Category;
use Pimcore\Model\DataObject\Products;
use Pimcore\Model\DataObject\Brands;
class BookingController extends AbstractController
{
private $bookingService;
private $session;
private $user;
private $translator;
public function __construct(BookingService $bookingService, RequestStack $requestStack, TranslatorInterface $translator)
{
$this->bookingService = $bookingService;
$this->session = $requestStack->getSession();
$this->user = $this->session->get('user');
$this->translator = $translator;
}
/**
* @Route("/{locale}/booking/index", name="booking_index")
*/
public function index(Request $request)
{
$locale = $request->get("locale", "en");
$url = $request->get('turl');
$ref = $request->server->get('REDIRECT_URL');
$this->get('session')->set('refURL', $ref);
$email = $this->get('session')->get('loginUserId');
if ($email) {
$config = BookingConfig::getByPath("/Booking/Config/Config"); // Assume 1 is the ID of the config
$regions = new Region\Listing();
$regions->load();
$brands = $this->getBrands();
return $this->render('booking/index.html.twig', [
'config' => $config,
'brands' => $brands,
'regions'=> $regions,
'locale' => $locale
]);
} else {
return $this->redirect("/" . $locale . "/signin");
}
}
/**
* @Route("/{locale}/booking/slots", name="booking_slots")
*/
public function getBookingSlots(Request $request)
{
$date = $request->get('date');
$slots = $this->bookingService->getAvailableSlots($date);
return new JsonResponse($slots);
}
/**
* @Route("/{locale}/booking/create", name="create_booking")
*/
public function createBooking(Request $request)
{
$data = $request->request->all();
$result = $this->bookingService->createBooking($data, $this->user);
if ($result) {
return new JsonResponse(["success" => true, "message" => $this->translator->trans("booking_confirmation_message", ['booking_id' => $result])]);
} else {
return new JsonResponse(["success" => false, "message" => $this->translator->trans("booking_failure_message")]);
}
}
/**
* @Route("/get-all-governorates-by-region", name="get-all-governorates-by-region")
*/
public function getGovernoratesByRegion(Request $request): JsonResponse
{
$governoratesData = [];
// Retrieve and validate the single region ID from the request
$content = json_decode($request->getContent(), true);
$regionId = $content['region_id'] ?? null;
if (!empty($regionId)) {
// Fetch the region by the given region ID
$region = Region::getByRegionId($regionId, true);
if ($region instanceof Region) {
// Fetch the governorates related to the given region
$governorates = new Governorate\Listing();
$governorates->filterByRegion($region);
foreach ($governorates as $governorate) {
// Build governorates data array
$governoratesData[] = [
"id" => $governorate->getGovId(),
"nameEn" => $governorate->getName('en'),
"nameAr" => $governorate->getName('ar')
];
}
}
}
// Return the governorates data in a JSON response
return new JsonResponse([
'data' => $governoratesData
]);
}
public function getBrands($brand_id = 0){
$brands = new Brands\Listing();
$brands->setOrderKey('order');
$brands->setOrder('asc');
$brands->load();
return $brands;
}
/**
* @Route("/get-category-by-brand", name="get-category-by-brand")
*/
public function getCategoryByBrand(Request $request): JsonResponse
{
$categoryData = [];
// Retrieve and validate the single region ID from the request
$content = json_decode($request->getContent(), true);
$brandId = $content['brand_id'] ?? null;
if (!empty($brandId)) {
// Fetch the region by the given region ID
$brand = Brands::getById($brandId, true);
$products = new Products\Listing();
$products = $products->filterbyBrands($brand);
$products->load();
foreach ($products as $product) {
if ($product->getCategories()) {
foreach ($product->getCategories() as $category) {
$categoryData[$category->getId()] = [
'name_en' => $category->getName('en'),
'name_ar' => $category->getName('ar'),
];
}
}
}
}
// Return the governorates data in a JSON response
return new JsonResponse([
'data' => $categoryData
]);
}
}