<?php declare(strict_types = 1);
namespace App\Controller;
use NexCRM\BaseBundle\Controller\AbstractBaseController;
use NexCRM\BaseBundle\Entity\Lang;
use NexCRM\BaseBundle\Entity\Setting;
use NexCRM\ShopBundle\Entity\Category;
use NexCRM\ShopBundle\Repository\ProductRepository;
use NexCRM\ShopBundle\Service\CartManager;
use NexCRM\WebBundle\Entity\Block;
use NexCRM\WebBundle\Entity\Menu;
use NexCRM\WebBundle\Entity\Page;
use NexCRM\WebBundle\Entity\SEOTranslation;
use NexCRM\WebBundle\Entity\Slider;
use NexCRM\WebBundle\Service\MetaTagsService;
use NexCRM\WebBundle\Service\PageTemplateService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class BaseController extends AbstractBaseController
{
/** @var \NexCRM\ShopBundle\Service\CartManager */
public CartManager $cartManager;
/** @var \NexCRM\WebBundle\Service\PageTemplateService */
protected PageTemplateService $pageTemplateService;
/** @var \Symfony\Component\HttpFoundation\Request|null */
protected ?\Symfony\Component\HttpFoundation\Request $request;
/** @var \NexCRM\WebBundle\Service\MetaTagsService */
protected MetaTagsService $metaTagsService;
protected $data;
protected TranslatorInterface $translator;
/**
* @var \NexCRM\ShopBundle\Repository\ProductRepository
*/
private ProductRepository $productRepository;
public function __construct(
TranslatorInterface $translator,
RequestStack $requestStack,
MetaTagsService $metaTagsService,
PageTemplateService $pageTemplateService,
CartManager $cartManager,
ProductRepository $productRepository
)
{
$this->data = [];
$this->translator = $translator;
$this->request = $requestStack->getCurrentRequest();
$this->metaTagsService = $metaTagsService;
$this->pageTemplateService = $pageTemplateService;
$this->cartManager = $cartManager;
$this->productRepository = $productRepository;
$this->data["basket"] = $this->cartManager;
}
/**
* @Route("/{_locale}", name="front_homepage", defaults={"_locale": "cs"}, requirements={
* "_locale": "([a-z]){2}"
* })
*/
public function indexAction()
{
$message = $this->request->query->get("message");
$this->initialize();
$page = $this->getDoctrine()->getRepository(Page::class)->findOneBy(["homepage" => 1]);
$instagram = $this->forward('NexCRM\WebBundle\Controller\Front\FeedController::instagramAction');
return $this->render(
'front/index.html.twig', [
"meta" => $this->metaTagsService->getMetaTags($page),
"page" => $page,
"response" => $message,
"instagram" => json_decode($instagram->getContent(), true)
]
);
}
public function initialize()
{
$this->translator->setLocale($this->request->getLocale());
$settings = $this->getDoctrine()->getRepository(Setting::class)->findByToArray();
$pages = $this->getDoctrine()->getRepository(Page::class)->findBy([]);
$menus = $this->getDoctrine()->getRepository(Menu::class)->findByGroupOrdered();
$sliders = $this->getDoctrine()->getRepository(Slider::class)->findByGroupOrdered();
$blocks = $this->getDoctrine()->getRepository(Block::class)->findByToArray();
$categories = $this->getDoctrine()->getRepository(Category::class)->findBy([],["sort" => "ASC"]);
$page_templates = $this->pageTemplateService->getPagetemplate($pages);
$this->data["settings"] = $settings;
$this->data["pages"] = $pages;
$this->data["menus"] = $menus;
$this->data["blocks"] = $blocks;
$this->data["categories"] = $categories;
$this->data["sliders"] = $sliders;
$this->data["pt"] = $page_templates;
$this->data["RECAPTCHA_SITE_KEY"] = $_ENV['RECAPTCHA_SITE_KEY'];
}
public function render($view, array $parameters = [], Response $response = null): Response
{
return parent::render(
$view, array_merge(
$this->data, $parameters, [
'base_dir' => realpath($this->getParameter('kernel.project_dir') . '/..'),
],
), $response,
);
}
/**
* @Route("/sitemap.xml", name="sitemap", defaults={"_locale" = "cs"})
*/
public function sitemapAction(Request $request)
{
$items = [];
$langs = $this->getDoctrine()->getRepository(Lang::class)->findBy(['active' => 1]);
$default = $this->getDoctrine()->getRepository(Page::class)->findOneBy(['homepage' => 1])->getSEO()->getId();
foreach($langs as $lang) {
$seos = $this->getDoctrine()->getRepository(SEOTranslation::class)->findBy(['locale' => $lang->getIso2()]);
foreach ($seos as $key => $item) {
if ($item->getTranslatable()->isNoindex() == true || $item->getTranslatable()->getId()==$default) {
unset($seos[$key]);
}
}
$items[$lang->getIso2()] = $seos;
}
$response = new Response($this->renderView('front/sitemap.xml.twig', [
"items" => $items,
"langs" => $langs,
"url" => strtolower(explode('/',$request->server->get('SERVER_PROTOCOL'))[0]).'://'.$request->server->get('HTTP_HOST').'/'
]));
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');
return $response;
}
}