src/Controller/HomeController.php line 20

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\File;
  4. use App\Repository\PropertyRepository;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. class HomeController extends BaseController
  9. {
  10.     public function __construct(
  11.         private \App\Repository\MenuRepository $menuRepository,
  12.         private PropertyRepository $propertyRepository,
  13.     ) {
  14.     }
  15.     #[Route('/')]
  16.     public function indexNoLocale(Request $request): Response
  17.     {
  18.         $parameters $request->query->all();
  19.         $parameters['_locale'] = 'bg';
  20.         $url $this->generateUrl('home'$parameters);
  21.         return $this->redirect($url);
  22.     }
  23.     #[Route('/{_locale<%app.supported_locales%>}'name'home')]
  24.     #[Route('/{_locale<%app.supported_locales%>}/menus'name'menus')]
  25.     public function index(Request $request): Response
  26.     {
  27.         $properties = [];
  28.         $returnUrl $request->get('returnUrl');
  29.         try {
  30.             $propertyEntities $this->propertyRepository->findBy([
  31.             ], [
  32.                 'sort' => 'ASC',
  33.             ]);
  34.             foreach ($propertyEntities as $propertyEntity) {
  35.                 $images array_map(function (File $image) {
  36.                     return [
  37.                         'id' => $image->getId(),
  38.                         'sort' => $image->getSort(),
  39.                     ];
  40.                 }, $propertyEntity->getFiles()->toArray());
  41.                 usort($images, function ($a$b) {
  42.                     return $a['sort'] <=> $b['sort'];
  43.                 });
  44.                 $mainMenu $this->menuRepository->findMainMenu($propertyEntity);
  45.                 $cateringMenu $this->menuRepository->findCateringMenu($propertyEntity);
  46.                 $lunchMenu $this->menuRepository->findCurrentLunch($propertyEntity);
  47.                 if (empty($lunchMenu)) {
  48.                     $lunchMenu $this->menuRepository->findNextLunch($propertyEntity);
  49.                 }
  50.                 $properties[] = [
  51.                     'id' => $propertyEntity->getId(),
  52.                     'slug' => $propertyEntity->getSlug(),
  53.                     'name' => $propertyEntity->getName(),
  54.                     'address' => $propertyEntity->getAddress(),
  55.                     'phone' => $propertyEntity->getPhone(),
  56.                     'email' => $propertyEntity->getEmail(),
  57.                     'workingHours' => $propertyEntity->getWorkingHours(),
  58.                     'images' => $images,
  59.                     'hasMainMenu' => !empty($mainMenu),
  60.                     'hasCateringMenu' => !empty($cateringMenu),
  61.                     'hasLunchMenu' => !empty($lunchMenu),
  62.                     'city' => $propertyEntity->getCity(),
  63.                     'whitelabel' => $propertyEntity->getWhitelabel(),
  64.                 ];
  65.             }
  66.             return $this->render($this->getTemplatePath('index/home.html.twig'), [
  67.                 'returnUrl' => $returnUrl,
  68.                 'properties' => $properties,
  69.             ]);
  70.         } catch (\ErrorException $e) {
  71.             return $this->render($this->getTemplatePath('index/home.html.twig'), [
  72.                 'returnUrl' => $returnUrl,
  73.                 'error' => [
  74.                     'message' => $e->getMessage(),
  75.                 ],
  76.             ]);
  77.         }
  78.     }
  79. }