Files
kupshop/bundles/KupShop/ContentBundle/View/HomeView.php
2025-08-02 16:30:27 +02:00

152 lines
4.1 KiB
PHP

<?php
namespace KupShop\ContentBundle\View;
use KupShop\ComponentsBundle\View\ComponentsViewInterface;
use KupShop\ComponentsBundle\View\ComponentsViewTrait;
use KupShop\ContentBundle\Exception\PageException;
use KupShop\ContentBundle\Page\HomePage;
use KupShop\KupShopBundle\Context\CacheContext;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Views\Traits\RequestTrait;
use KupShop\KupShopBundle\Views\View;
use KupShop\MetricsBundle\PrometheusWrapper;
use KupShop\ResponseCacheBundle\Util\Cache\ResponseCache;
use Symfony\Component\HttpFoundation\Request;
class HomeView extends View implements ComponentsViewInterface
{
use RequestTrait;
use ComponentsViewTrait;
protected $template = 'home.tpl';
protected string $entrypoint = 'home';
/** @var LanguageContext */
private $languageContext;
/** @var CurrencyContext */
private $currencyContext;
/** @var HomePage */
private $homePage;
public function __construct()
{
$this->proxyCacheEnabled = findModule(\Modules::PROXY_CACHE, 'home');
}
/**
* @return $this
*
* @required
*/
public function setLanguageContext(LanguageContext $languageContext)
{
$this->languageContext = $languageContext;
return $this;
}
/**
* @return $this
*
* @required
*/
public function setCurrencyContext(CurrencyContext $currencyContext)
{
$this->currencyContext = $currencyContext;
return $this;
}
/**
* @required
*/
public function setHomePage(HomePage $homePage): void
{
$this->homePage = $homePage;
}
public function getHeaderVariables()
{
$dbcfg = \Settings::getDefault();
$header = parent::getHeaderVariables();
$header['pageTitle'] = $dbcfg['index_title'];
$header['meta_keywords'] = $dbcfg['index_keywords'];
$header['meta_description'] = $dbcfg['index_description'];
return $header;
}
public function getBodyVariables()
{
$vars = parent::getBodyVariables();
$this->homePage->setView($this);
try {
$page = $this->homePage->getPage();
} catch (PageException $e) {
$page = false;
}
$vars['html_page'] = $page['block'] ?? null;
$vars['productsList'] = [];
$vars['page'] = $page;
return $vars;
}
public function getResponse(?Request $request = null)
{
if (findModule(\Modules::RESPONSE_CACHE, 'home')
&& !isFunctionalTests()
&& !getAdminUser()
&& $this->request->get('skip_gtm') === null
) {
$cacheContext = Contexts::get(CacheContext::class);
$cacheKeyContext = $cacheContext->getKey([CacheContext::TYPE_FULL_PAGE, CacheContext::TYPE_PRICE, CacheContext::TYPE_TEXT]);
$isHit = false;
if (!is_null($cacheKeyContext)) {
$responseCache = ServiceContainer::getService(ResponseCache::class);
$this->responseCacheEnabled = true;
$cacheKey = ['home'];
$cacheKey[] = $cacheKeyContext;
$cacheKey[] = 'rc';
$cacheKey = join('-', $cacheKey);
$response = $responseCache->wrap(function () use ($request) {
return parent::getResponse($request);
}, $cacheKey, 15 * 60, null, $isHit);
}
$metrics = ServiceContainer::getService(PrometheusWrapper::class);
$metrics->setCounter('kupshop', 'home_cache_used', '', 1, ['used' => $isHit ? '1' : '0']);
}
if (!isset($response)) {
$response = parent::getResponse($request);
}
return $response;
}
public function getWpjToolbar()
{
$arr = [
'url' => getAdminUrl('PagesSystem', ['pageType' => 'HOME']),
'title' => 'Upravit stránku',
];
return array_merge(parent::getWpjToolbar(), $arr);
}
}