src/Controller/BaseController.php line 84

Open in your IDE?
  1. <?php declare(strict_types 1);
  2. namespace App\Controller;
  3. use NexCRM\BaseBundle\Controller\AbstractBaseController;
  4. use NexCRM\BaseBundle\Entity\Lang;
  5. use NexCRM\BaseBundle\Entity\Setting;
  6. use NexCRM\ShopBundle\Entity\Category;
  7. use NexCRM\ShopBundle\Repository\ProductRepository;
  8. use NexCRM\ShopBundle\Service\CartManager;
  9. use NexCRM\WebBundle\Entity\Block;
  10. use NexCRM\WebBundle\Entity\Menu;
  11. use NexCRM\WebBundle\Entity\Page;
  12. use NexCRM\WebBundle\Entity\SEOTranslation;
  13. use NexCRM\WebBundle\Entity\Slider;
  14. use NexCRM\WebBundle\Service\MetaTagsService;
  15. use NexCRM\WebBundle\Service\PageTemplateService;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. class BaseController extends AbstractBaseController
  22. {
  23.     /** @var \NexCRM\ShopBundle\Service\CartManager */
  24.     public CartManager $cartManager;
  25.     /** @var \NexCRM\WebBundle\Service\PageTemplateService */
  26.     protected PageTemplateService $pageTemplateService;
  27.     /** @var \Symfony\Component\HttpFoundation\Request|null */
  28.     protected ?\Symfony\Component\HttpFoundation\Request $request;
  29.     /** @var \NexCRM\WebBundle\Service\MetaTagsService */
  30.     protected MetaTagsService $metaTagsService;
  31.     protected $data;
  32.     protected TranslatorInterface $translator;
  33.     /**
  34.      * @var \NexCRM\ShopBundle\Repository\ProductRepository
  35.      */
  36.     private ProductRepository $productRepository;
  37.     public function __construct(
  38.         TranslatorInterface $translator,
  39.         RequestStack $requestStack,
  40.         MetaTagsService $metaTagsService,
  41.         PageTemplateService $pageTemplateService,
  42.         CartManager $cartManager,
  43.         ProductRepository $productRepository
  44.     )
  45.     {
  46.         $this->data = [];
  47.         $this->translator $translator;
  48.         $this->request $requestStack->getCurrentRequest();
  49.         $this->metaTagsService $metaTagsService;
  50.         $this->pageTemplateService $pageTemplateService;
  51.         $this->cartManager $cartManager;
  52.         $this->productRepository $productRepository;
  53.         $this->data["basket"] = $this->cartManager;
  54.     }
  55.     /**
  56.      * @Route("/{_locale}", name="front_homepage", defaults={"_locale": "cs"}, requirements={
  57.      *     "_locale": "([a-z]){2}"
  58.      * })
  59.      */
  60.     public function indexAction()
  61.     {
  62.         $message $this->request->query->get("message");
  63.         $this->initialize();
  64.         $page $this->getDoctrine()->getRepository(Page::class)->findOneBy(["homepage" => 1]);
  65.         $instagram =  $this->forward('NexCRM\WebBundle\Controller\Front\FeedController::instagramAction');
  66.         return $this->render(
  67.             'front/index.html.twig', [
  68.                 "meta" => $this->metaTagsService->getMetaTags($page),
  69.                 "page" => $page,
  70.                 "response" => $message,
  71.                 "instagram" => json_decode($instagram->getContent(), true)
  72.             ]
  73.         );
  74.     }
  75.     public function initialize()
  76.     {
  77.         $this->translator->setLocale($this->request->getLocale());
  78.         $settings $this->getDoctrine()->getRepository(Setting::class)->findByToArray();
  79.         $pages $this->getDoctrine()->getRepository(Page::class)->findBy([]);
  80.         $menus $this->getDoctrine()->getRepository(Menu::class)->findByGroupOrdered();
  81.         $sliders $this->getDoctrine()->getRepository(Slider::class)->findByGroupOrdered();
  82.         $blocks $this->getDoctrine()->getRepository(Block::class)->findByToArray();
  83.         $categories $this->getDoctrine()->getRepository(Category::class)->findBy([],["sort" => "ASC"]);
  84.         $page_templates $this->pageTemplateService->getPagetemplate($pages);
  85.         $this->data["settings"] = $settings;
  86.         $this->data["pages"] = $pages;
  87.         $this->data["menus"] = $menus;
  88.         $this->data["blocks"] = $blocks;
  89.         $this->data["categories"] = $categories;
  90.         $this->data["sliders"] = $sliders;
  91.         $this->data["pt"] = $page_templates;
  92.         $this->data["RECAPTCHA_SITE_KEY"] = $_ENV['RECAPTCHA_SITE_KEY'];
  93.     }
  94.     public function render($view, array $parameters = [], Response $response null): Response
  95.     {
  96.         return parent::render(
  97.             $viewarray_merge(
  98.             $this->data$parameters, [
  99.             'base_dir' => realpath($this->getParameter('kernel.project_dir') . '/..'),
  100.         ],
  101.         ), $response,
  102.             );
  103.     }
  104.     /**
  105.      * @Route("/sitemap.xml", name="sitemap", defaults={"_locale" = "cs"})
  106.      */
  107.     public function sitemapAction(Request $request)
  108.     {
  109.         $items = [];
  110.         $langs $this->getDoctrine()->getRepository(Lang::class)->findBy(['active' => 1]);
  111.         $default $this->getDoctrine()->getRepository(Page::class)->findOneBy(['homepage' => 1])->getSEO()->getId();
  112.         foreach($langs as $lang) {
  113.             $seos $this->getDoctrine()->getRepository(SEOTranslation::class)->findBy(['locale' => $lang->getIso2()]);
  114.             foreach ($seos as $key => $item) {
  115.                 if ($item->getTranslatable()->isNoindex() == true || $item->getTranslatable()->getId()==$default) {
  116.                     unset($seos[$key]);
  117.                 }
  118.             }
  119.             $items[$lang->getIso2()] = $seos;
  120.         }
  121.         $response = new Response($this->renderView('front/sitemap.xml.twig', [
  122.             "items" => $items,
  123.             "langs" => $langs,
  124.             "url" => strtolower(explode('/',$request->server->get('SERVER_PROTOCOL'))[0]).'://'.$request->server->get('HTTP_HOST').'/'
  125.         ]));
  126.         $response->headers->set('Content-Type''application/xml; charset=utf-8');
  127.         return $response;
  128.     }
  129. }