src/Controller/Menus/MainController.php line 22

  1. <?php
  2. namespace App\Controller\Menus;
  3. use App\Controller\BaseController;
  4. use App\Repository\PropertyRepository;
  5. use Detection\MobileDetect;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class MainController extends BaseController
  10. {
  11.     public function __construct(
  12.         private \App\Repository\MenuRepository $menuRepository,
  13.         private PropertyRepository $propertyRepository,
  14.         private \Symfony\Contracts\Translation\TranslatorInterface $translator
  15.     ) {
  16.     }
  17.     #[Route('/{_locale<%app.supported_locales%>}/restaurants/{slug}/menus/main'name'restaurants.menus.main')]
  18.     public function index(Request $request): Response
  19.     {
  20.         $currentMenu = [];
  21.         $products = [];
  22.         $isMobile false;
  23.         if (!empty($_SERVER['HTTP_USER_AGENT'])) {
  24.             $mobileDetect = new MobileDetect();
  25.             $mobileDetect->setUserAgent($_SERVER['HTTP_USER_AGENT']);
  26.             $isMobile $mobileDetect->isMobile();
  27.         }
  28.         $returnUrl $request->get('returnUrl');
  29.         $returnUrlHost null;
  30.         if (!empty($returnUrl)) {
  31.             $returnUrlHost parse_url($returnUrlPHP_URL_HOST);
  32.         }
  33.         $capellaPlayHosts = ['www.capellaplay.bg''capellaplay.bg'];
  34.         try {
  35.             $propertyEntity $this->propertyRepository->findOneBy(['slug' => $request->get('slug')]);
  36.             if (empty($propertyEntity)) {
  37.                 throw new \ErrorException($this->translator->trans('Property not found'));
  38.             }
  39.             $currentMenuEntity $this->menuRepository->findOneBy([
  40.                 'property' => $propertyEntity,
  41.                 'type' => 2,
  42.             ]);
  43.             if (empty($currentMenuEntity)) {
  44.                 throw new \ErrorException($this->translator->trans('Menu not found'));
  45.             }
  46.             $customMenuDataDto $this->menuRepository->getCustomMenuData($currentMenuEntity);
  47.             $currentMenu = [
  48.                 'id' => $currentMenuEntity->getId(),
  49.                 'name' => $currentMenuEntity->getName(),
  50.                 'fromDate' => $currentMenuEntity->getFromDate()->format('d-m-Y'),
  51.                 'toDate' => $currentMenuEntity->getToDate()->format('d-m-Y'),
  52.             ];
  53.             foreach ($customMenuDataDto->getProductsArray() as $product) {
  54.                 $productName = ('en' == $this->translator->getLocale()) ? (!empty($product['nameEn']) ? $product['nameEn'] : $product['name']) : $product['name'];
  55.                 $arrTemp = [
  56.                     'id' => $product['id'],
  57.                     'name' => $productName,
  58.                     'nameEn' => $product['nameEn'],
  59.                     'price' => number_format($product['price'], 2'.'''),
  60.                     'createDate' => $product['createDate']->format('Y-m-d H:i:s'),
  61.                     'weight' => $product['weight'],
  62.                     'category' => $product['category'],
  63.                     'components' => [],
  64.                     'allergens' => [],
  65.                 ];
  66.                 $componentsArr = [];
  67.                 $allergensArr = [];
  68.                 foreach ($customMenuDataDto->getComponentsArray() as $component) {
  69.                     if ($component['toProductID'] == $product['id']) {
  70.                         $componentsArr[] = [
  71.                             'id' => $component[0]['id'],
  72.                             'name' => ('en' == $this->translator->getLocale()) ? (!empty($component[0]['nameEn']) ? $component[0]['nameEn'] : $component[0]['name']) : $component[0]['name'],
  73.                         ];
  74.                     }
  75.                 }
  76.                 foreach ($customMenuDataDto->getAllergensArray() as $allergen) {
  77.                     if ($allergen['toProductID'] == $product['id']) {
  78.                         $allergensArr[] = [
  79.                             'id' => $allergen[0]['id'],
  80.                             'name' => ('en' == $this->translator->getLocale()) ? (!empty($allergen[0]['nameEn']) ? $allergen[0]['nameEn'] : $allergen[0]['name']) : $allergen[0]['name'],
  81.                         ];
  82.                     }
  83.                 }
  84.                 $arrTemp['components'] = $componentsArr;
  85.                 $arrTemp['allergens'] = $allergensArr;
  86.                 $products[] = $arrTemp;
  87.             }
  88.             $currentMenuEntityArray = [
  89.                 'menu' => [
  90.                     'id' => $currentMenuEntity->getId(),
  91.                     'name' => $currentMenuEntity->getName(),
  92.                     'fromDate' => $currentMenuEntity->getFromDate()->format('d.m.Y'),
  93.                     'toDate' => $currentMenuEntity->getToDate()->format('d.m.Y'),
  94.                     'type' => $currentMenuEntity->getType(),
  95.                 ],
  96.                 'property' => [
  97.                     'id' => $propertyEntity->getId(),
  98.                     'name' => $propertyEntity->getName(),
  99.                     'address' => $propertyEntity->getAddress(),
  100.                     'phone' => $propertyEntity->getPhone(),
  101.                     'slug' => $propertyEntity->getSlug(),
  102.                 ],
  103.                 'returnUrl' => $returnUrl,
  104.                 'returnUrlHost' => $returnUrlHost,
  105.                 'capellaPlayHosts' => $capellaPlayHosts,
  106.                 'currentMenu' => $currentMenu,
  107.                 'products' => $products,
  108.                 'isMobile' => $isMobile,
  109.                 'error' => null,
  110.             ];
  111.             return $this->render($this->getTemplatePath('menus/main.html.twig'), $currentMenuEntityArray);
  112.         } catch (\ErrorException $e) {
  113.             return $this->render($this->getTemplatePath('menus/main.html.twig'), [
  114.                 'menu' => null,
  115.                 'property' => null,
  116.                 'returnUrl' => $returnUrl,
  117.                 'returnUrlHost' => $returnUrlHost,
  118.                 'capellaPlayHosts' => $capellaPlayHosts,
  119.                 'error' => [
  120.                     'message' => $e->getMessage(),
  121.                 ],
  122.                 'currentMenu' => $currentMenu,
  123.                 'products' => $products,
  124.                 'isMobile' => $isMobile,
  125.             ]);
  126.         }
  127.     }
  128. }