<?php
namespace App\Controller;
use App\Service\Cache\Cache;
use App\Model\Cart as ModelCart;
use App\Model\Prod as ModelProd;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use App\RepositoryInterface\PageRepositoryInterface;
use App\RepositoryInterface\ProdRepositoryInterface;
use App\RepositoryInterface\PhotoRepositoryInterface;
use App\RepositoryInterface\BannerRepositoryInterface;
class MainController extends AbstractASController
{
protected EntityManagerInterface $em;
protected ModelProd $ModelProd;
protected ModelCart $ModelCart;
//Repositories
protected PageRepositoryInterface $Pages;
protected BannerRepositoryInterface $Banners;
protected ProdRepositoryInterface $Prods;
protected PhotoRepositoryInterface $Photos;
public function __construct(ModelProd $ModelProd, PageRepositoryInterface $Pages, BannerRepositoryInterface $Banners, ProdRepositoryInterface $Prods, PhotoRepositoryInterface $Photos, ModelCart $ModelCart, RequestStack $requestStack)
{
$this->requestStack = $requestStack;
$this->ModelProd = $ModelProd;
$this->ModelCart = $ModelCart;
$this->Pages = $Pages;
$this->Banners = $Banners;
$this->Prods = $Prods;
$this->Photos = $Photos;
}
#[Route('/', name: 'home_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route('/{_locale}', name: 'home', requirements: ['_locale' => '%app.langs%'])]
public function index(): Response
{
$page = $this->Pages->getMainPage();
$capage = $this->Pages->getByName('catalog/action');
$banners = $this->Banners->getForMainPage();
$actions = $this->Pages->getActions(12);
$newactions = $this->Pages->getNewActions(12);
$prods = $this->Prods->getNewProds(12);
$prodphotos = [];
foreach ($prods as $k => $prod) {
$prods[$k]->setPrices($this->ModelProd->getPrices($prod));
$prodphotos[$prod->getId()] = $this->Photos->getProdPhotos($prod->getId());
}
$response = $this->render('main/index.html.twig', [
'time' => time(),
'page' => $page,
'isVisibleActions' => $capage->isVisible(),
'pageblocks' => $page->getBlocks(),
'photos' => $page->getPhotos(),
'banners' => $banners,
'actions' => $actions,
'newactions' => $newactions,
'action_prods' => $prods,
'action_prods_photos' => $prodphotos,
'cart_items' => $this->ModelCart->getCart(),
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
}