src/Controller/Booking/BookingController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Booking;
  3. use App\Service\BookingService;
  4. use Pimcore\Model\DataObject\BookingConfig;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Pimcore\Model\DataObject\Region;
  13. use Pimcore\Model\DataObject\Governorate;
  14. use Pimcore\Model\DataObject\Category;
  15. use Pimcore\Model\DataObject\Products;
  16. use Pimcore\Model\DataObject\Brands;
  17. class BookingController extends AbstractController
  18. {
  19.     private $bookingService;
  20.     private $session;
  21.     private $user;
  22.     private $translator;
  23.     public function __construct(BookingService $bookingServiceRequestStack $requestStackTranslatorInterface $translator)
  24.     {
  25.         $this->bookingService $bookingService;
  26.         $this->session $requestStack->getSession();
  27.         $this->user $this->session->get('user');
  28.         $this->translator $translator;
  29.     }
  30.     /**
  31.      * @Route("/{locale}/booking/index", name="booking_index")
  32.      */
  33.     public function index(Request $request)
  34.     {
  35.         $locale $request->get("locale""en");
  36.         $url =  $request->get('turl');
  37.         $ref $request->server->get('REDIRECT_URL');
  38.         $this->get('session')->set('refURL'$ref);
  39.         $email $this->get('session')->get('loginUserId');
  40.         if ($email) {
  41.             $config BookingConfig::getByPath("/Booking/Config/Config"); // Assume 1 is the ID of the config
  42.             $regions = new Region\Listing();
  43.             $regions->load();
  44.             $brands $this->getBrands();
  45.             return $this->render('booking/index.html.twig', [
  46.                 'config' => $config,
  47.                 'brands' => $brands,
  48.                 'regions'=> $regions,
  49.                 'locale' => $locale
  50.             ]);
  51.         } else {
  52.              return $this->redirect("/" $locale "/signin");
  53.         }
  54.         
  55.         
  56.     }
  57.     /**
  58.      * @Route("/{locale}/booking/slots", name="booking_slots")
  59.      */
  60.     public function getBookingSlots(Request $request)
  61.     {
  62.         $date $request->get('date');
  63.         $slots $this->bookingService->getAvailableSlots($date);
  64.         return new JsonResponse($slots);
  65.     }
  66.     /**
  67.      * @Route("/{locale}/booking/create", name="create_booking")
  68.      */
  69.     public function createBooking(Request $request)
  70.     {
  71.         $data $request->request->all();
  72.         $result $this->bookingService->createBooking($data$this->user);
  73.         if ($result) {
  74.             return new JsonResponse(["success" => true"message" => $this->translator->trans("booking_confirmation_message", ['booking_id' => $result])]);
  75.         } else {
  76.             return new JsonResponse(["success" => false"message" => $this->translator->trans("booking_failure_message")]);
  77.         }
  78.     }
  79.     /**
  80.      * @Route("/get-all-governorates-by-region", name="get-all-governorates-by-region")
  81.      */
  82.     public function getGovernoratesByRegion(Request $request): JsonResponse
  83.     {
  84.         $governoratesData = [];
  85.         // Retrieve and validate the single region ID from the request
  86.         $content json_decode($request->getContent(), true);
  87.         $regionId $content['region_id'] ?? null;
  88.         if (!empty($regionId)) {
  89.             // Fetch the region by the given region ID
  90.             $region Region::getByRegionId($regionIdtrue);
  91.             if ($region instanceof Region) {
  92.                 // Fetch the governorates related to the given region
  93.                 $governorates = new Governorate\Listing();
  94.                 $governorates->filterByRegion($region);
  95.                 foreach ($governorates as $governorate) {
  96.                     // Build governorates data array
  97.                     $governoratesData[] = [
  98.                         "id" => $governorate->getGovId(),
  99.                         "nameEn" => $governorate->getName('en'),
  100.                         "nameAr" => $governorate->getName('ar')
  101.                     ];
  102.                 }
  103.             }
  104.         }
  105.         // Return the governorates data in a JSON response
  106.         return new JsonResponse([
  107.             'data' => $governoratesData
  108.         ]);
  109.     }
  110.     public function getBrands($brand_id 0){
  111.         $brands = new Brands\Listing();
  112.         $brands->setOrderKey('order');
  113.         $brands->setOrder('asc');
  114.         $brands->load();
  115.         return $brands;
  116.     }
  117.     /**
  118.      * @Route("/get-category-by-brand", name="get-category-by-brand")
  119.     */
  120.     public function getCategoryByBrand(Request $request): JsonResponse
  121.     {
  122.         $categoryData = [];
  123.         // Retrieve and validate the single region ID from the request
  124.         $content json_decode($request->getContent(), true);
  125.         $brandId $content['brand_id'] ?? null;
  126.         if (!empty($brandId)) {
  127.             // Fetch the region by the given region ID
  128.             $brand Brands::getById($brandIdtrue);
  129.             $products = new Products\Listing();
  130.             $products $products->filterbyBrands($brand);
  131.             $products->load();
  132.             foreach ($products as $product) {
  133.                 if ($product->getCategories()) {
  134.                     foreach ($product->getCategories() as $category) {
  135.                         $categoryData[$category->getId()] = [
  136.                             'name_en' => $category->getName('en'),
  137.                             'name_ar' => $category->getName('ar'),
  138.                         ];
  139.                     }
  140.                     
  141.                 }
  142.             }
  143.             
  144.         }
  145.         // Return the governorates data in a JSON response
  146.         return new JsonResponse([
  147.             'data' => $categoryData
  148.         ]);
  149.     }   
  150. }