<?php
namespace App\Twig;
use App\Entity\Gos\PortalSettings;
use App\Utils\FrontendSCSSGenerator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class FrontendPartialsRenderer extends AbstractExtension
{
private $em;
private $request;
private $SCSSGenerator;
/** @var PortalSettings|null */
private $portalSettings;
public function __construct(EntityManagerInterface $_em,
RequestStack $requestStack,
FrontendSCSSGenerator $_SCSSGenerator)
{
$this->em = $_em;
$this->request = $requestStack->getCurrentRequest();
$this->SCSSGenerator = $_SCSSGenerator;
}
public function getFunctions(): array
{
return array(
new TwigFunction('getFrontendSCSS', [$this, 'getFrontendSCSS']),
new TwigFunction('getPortalCSS', [$this, 'getPortalCSS']),
new TwigFunction('getStyleForNewsletter', [$this, 'getStyleForNewsletter']),
new TwigFunction('getPasswordAdditionalContent', [$this, 'getPasswordAdditionalContent']),
);
}
public function getFrontendSCSS(): string
{
$this->getPortalSettings();
return $this->SCSSGenerator->getPortalSCSS($this->portalSettings);
}
public function getPortalCSS(): string
{
$this->getPortalSettings();
if ($this->portalSettings)
{
return $this->portalSettings->getPortalCss() ?: '';
}
return '';
}
public function getStyleForNewsletter(): string
{
$this->getPortalSettingsFromCookies();
if ($this->portalSettings)
{
$leadingColor = $this->portalSettings->getLeadingColor4();
if(!empty($leadingColor))
{
return "
.newsletter .newsletter__button {
background-color: {$leadingColor} !important;
}
.newsletter_title > span { color: {$leadingColor} !important; }
.otp-container .btn { background-color: {$leadingColor}; }
.otp-password__save__btn {background: {$leadingColor} !important; border-color: {$leadingColor} !important;}
.otp-container a, .newsletter__message-text, .otp-message--success, .otp-message--phone-success, .otp-message--mail-success { color: {$leadingColor}; }
.otp-password .btn-primary:not(:disabled):not(.disabled).active, .otp-container .btn-primary:not(:disabled):not(.disabled):active { background-color: {$leadingColor}; border-color: {$leadingColor};}
.login-content-submit { background-color: {$leadingColor}; }
.login-content-register > a, .login-show-password, .login-content-forgot-password > span a { color: {$leadingColor}; }
.newsletter__message-text { color: {$leadingColor}; }
";
}
}
return '';
}
public function getPasswordAdditionalContent(): ?string
{
$this->getPortalSettingsFromCookies();
if($this->portalSettings)
{
$additionalContent = $this->portalSettings->getPasswordAdditionalContent();
if($additionalContent) {
return $this->portalSettings->getPasswordAdditionalContent();
}
}
// return '<img src="/frontend/img/otp-korzysci.png" style="width: 100%;">';
return null;
}
private function getPortalSettingsFromCookies(): void
{
$portalSettingHash = $this->request->getSession()->has('portalSettingsHash')
? $this->request->getSession()->get('portalSettingsHash')
: $this->request->cookies->get('gos_portal_settings', null);
if ($portalSettingHash !== null)
{
$this->portalSettings = $this->em->getRepository(PortalSettings::class)->findOneByHash($portalSettingHash);
}
}
private function getPortalSettings(): void
{
if ($this->portalSettings === null && $this->request)
{
$this->portalSettings = $this->em->getRepository(PortalSettings::class)
->findOneByHash($this->request->getSession()->get('portalSettingsHash'));
}
}
}