<?php
namespace App\Twig;
use App\Entity\Gos\Cart;
use App\Entity\Gos\Country;
use App\Entity\Gos\CountrySettings;
use App\Entity\Gos\Language;
use App\Entity\Gos\PortalSettings;
use App\Entity\Gos\ProductCart;
use App\Entity\Gos\StaticPage;
use App\Entity\Gos\User;
use App\Utils\Cart\CartPriceCalculator;
use App\Utils\CartServices;
use App\Utils\Uniqskills\FrontendCategoryTree;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class UniqskillsPartialsRenderer extends AbstractExtension
{
private $em;
private $request;
private $categoryTree;
private $cartServices;
private CartPriceCalculator $cartPriceCalculator;
/** @var PortalSettings */
private $portalSettings;
private $languages;
private $user;
/** @var Cart|null */
private $cart;
private $cartPrice;
public function __construct(
EntityManagerInterface $_em,
RequestStack $requestStack,
FrontendCategoryTree $_categoryTree,
Security $security,
CartServices $_cartServices,
CartPriceCalculator $cartPriceCalculator
) {
$this->em = $_em;
$this->request = $requestStack->getCurrentRequest();
$this->categoryTree = $_categoryTree;
$this->user = $security->getUser();
$this->cartServices = $_cartServices;
$this->cartPriceCalculator = $cartPriceCalculator;
}
public function getFunctions(): array
{
return array(
new TwigFunction('renderUniqskillsScripts', [$this, 'renderUniqskillsScripts']),
new TwigFunction('getUniqskillsStaticPages', [$this, 'getUniqskillsStaticPages']),
new TwigFunction('getUniqskillsCategories', [$this, 'getUniqskillsCategories']),
new TwigFunction('getUniqskillsLanguages', [$this, 'getUniqskillsLanguages']),
new TwigFunction('getUniqskillsCartProductsAmount', [$this, 'getUniqskillsCartProductsAmount']),
new TwigFunction('getUniqskillsCartPrice', [$this, 'getUniqskillsCartPrice']),
new TwigFunction('getUniqskillsCartCountry', [$this, 'getUniqskillsCartCountry']),
new TwigFunction('getUniqskillsLogo', [$this, 'getUniqskillsLogo'])
);
}
public function renderUniqskillsScripts($type = 'Body')
{
$this->getPortalSettings();
if ($this->portalSettings)
{
return $this->portalSettings->{'getPortal' . $type . 'Scripts'}() ?: '';
}
return '';
}
public function getUniqskillsStaticPages()
{
$this->getPortalSettings();
$staticPages = [];
if ($this->portalSettings)
{
$locale = $this->request ? $this->request->getSession()->get('userLocale', 'pl') : 'pl';
$staticPages = $this->em->getRepository(StaticPage::class)
->findAllByPortalSettingsAndLanguage($this->portalSettings, $locale);
}
return $staticPages;
}
public function getUniqskillsCategories()
{
$this->getPortalSettings();
$categories = [];
if ($this->portalSettings)
{
$locale = $this->request ? $this->request->getSession()->get('userLocale', 'pl') : 'pl';
$categories = $this->categoryTree->getCategoryTree(
$locale,
[],
$this->portalSettings->getId(),
true
);
}
return $categories;
}
public function getUniqskillsLanguages()
{
if (!empty($this->languages))
{
return $this->languages;
}
$this->getPortalSettings();
if ($this->portalSettings)
{
$this->languages = $this->em->getRepository(Language::class)
->findSupportedLanguage($this->portalSettings);
}
return $this->languages;
}
public function getUniqskillsCartProductsAmount()
{
if ($this->user instanceof User && $this->user->getCart())
{
$cartHash = $this->user->getCart()->getHash();
}
elseif ($this->request)
{
$cartHash = $this->request->cookies->get('cartHash');
}
if (!isset($cartHash) || !$cartHash)
{
return '0';
}
$productsAmount = $this->em->getRepository(ProductCart::class)->sumProductCart($cartHash);
return $productsAmount ?: '0';
}
public function getUniqskillsCartPrice()
{
if ($this->cartPrice !== null)
{
return $this->cartPrice;
}
if (!$this->cart instanceof Cart)
{
$findCart = $this->cartServices->findCart($this->user, null, false);
$this->cart = $findCart['cart'];
}
if ($this->cart instanceof Cart && !$this->cart->getProductCart()->isEmpty())
{
$price = $this->cartPriceCalculator->getCartPrice($this->cart);
$this->cartPrice = $price['totalPriceWithShippingGross'];
return $this->cartPrice;
}
return '0';
}
public function getUniqskillsCartCountry()
{
if ($this->cart instanceof Cart)
{
return $this->cart->getCountry();
}
else
{
$findCart = $this->cartServices->findCart($this->user, null, false);
$this->cart = $findCart['cart'];
if ($this->cart instanceof Cart)
{
return $this->cart->getCountry();
}
else
{
return null;
}
}
}
public function getUniqskillsLogo($locale = 'pl')
{
/** @var Country $country */
$country = $this->em->getRepository(Country::class)->findOneByAlpha2($locale);
if($country && $country->getCountrySettings() && $country->getCountrySettings()->getLogoFilename())
{
return '/uploads/logos/'.$country->getCountrySettings()->getLogoFilename();
}
// this can be removed when they add german logo to countrySettings
if($locale == 'de')
{
return 'uniqskills/img/german_logo.png';
}
return 'uniqskills/img/white_logo.svg';
}
private function getPortalSettings()
{
if ($this->portalSettings === null)
{
$this->portalSettings = $this->em->getRepository(PortalSettings::class)
->findOneByHash($_ENV['US_PORTAL_SETTINGS']);
}
}
}