src/Twig/UniqskillsPartialsRenderer.php line 102

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Entity\Gos\Cart;
  4. use App\Entity\Gos\Country;
  5. use App\Entity\Gos\CountrySettings;
  6. use App\Entity\Gos\Language;
  7. use App\Entity\Gos\PortalSettings;
  8. use App\Entity\Gos\ProductCart;
  9. use App\Entity\Gos\StaticPage;
  10. use App\Entity\Gos\User;
  11. use App\Utils\Cart\CartPriceCalculator;
  12. use App\Utils\CartServices;
  13. use App\Utils\Uniqskills\FrontendCategoryTree;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Security\Core\Security;
  17. use Twig\Extension\AbstractExtension;
  18. use Twig\TwigFunction;
  19. class UniqskillsPartialsRenderer extends AbstractExtension
  20. {
  21.     private $em;
  22.     private $request;
  23.     private $categoryTree;
  24.     private $cartServices;
  25.     private CartPriceCalculator $cartPriceCalculator;
  26.     /** @var PortalSettings */
  27.     private $portalSettings;
  28.     private $languages;
  29.     private $user;
  30.     /** @var Cart|null */
  31.     private $cart;
  32.     private $cartPrice;
  33.     public function __construct(
  34.         EntityManagerInterface $_em,
  35.         RequestStack $requestStack,
  36.         FrontendCategoryTree $_categoryTree,
  37.         Security $security,
  38.         CartServices $_cartServices,
  39.         CartPriceCalculator $cartPriceCalculator
  40.     ) {
  41.         $this->em $_em;
  42.         $this->request $requestStack->getCurrentRequest();
  43.         $this->categoryTree $_categoryTree;
  44.         $this->user $security->getUser();
  45.         $this->cartServices $_cartServices;
  46.         $this->cartPriceCalculator $cartPriceCalculator;
  47.     }
  48.     public function getFunctions(): array
  49.     {
  50.         return array(
  51.             new TwigFunction('renderUniqskillsScripts', [$this'renderUniqskillsScripts']),
  52.             new TwigFunction('getUniqskillsStaticPages', [$this'getUniqskillsStaticPages']),
  53.             new TwigFunction('getUniqskillsCategories', [$this'getUniqskillsCategories']),
  54.             new TwigFunction('getUniqskillsLanguages', [$this'getUniqskillsLanguages']),
  55.             new TwigFunction('getUniqskillsCartProductsAmount', [$this'getUniqskillsCartProductsAmount']),
  56.             new TwigFunction('getUniqskillsCartPrice', [$this'getUniqskillsCartPrice']),
  57.             new TwigFunction('getUniqskillsCartCountry', [$this'getUniqskillsCartCountry']),
  58.             new TwigFunction('getUniqskillsLogo', [$this'getUniqskillsLogo'])
  59.         );
  60.     }
  61.     public function renderUniqskillsScripts($type 'Body')
  62.     {
  63.         $this->getPortalSettings();
  64.         if ($this->portalSettings)
  65.         {
  66.             return $this->portalSettings->{'getPortal' $type 'Scripts'}() ?: '';
  67.         }
  68.         return '';
  69.     }
  70.     public function getUniqskillsStaticPages()
  71.     {
  72.         $this->getPortalSettings();
  73.         $staticPages = [];
  74.         if ($this->portalSettings)
  75.         {
  76.             $locale $this->request $this->request->getSession()->get('userLocale''pl') : 'pl';
  77.             $staticPages $this->em->getRepository(StaticPage::class)
  78.                 ->findAllByPortalSettingsAndLanguage($this->portalSettings$locale);
  79.         }
  80.         return $staticPages;
  81.     }
  82.     public function getUniqskillsCategories()
  83.     {
  84.         $this->getPortalSettings();
  85.         $categories = [];
  86.         if ($this->portalSettings)
  87.         {
  88.             $locale $this->request $this->request->getSession()->get('userLocale''pl') : 'pl';
  89.             $categories $this->categoryTree->getCategoryTree(
  90.                 $locale,
  91.                 [],
  92.                 $this->portalSettings->getId(),
  93.                 true
  94.             );
  95.         }
  96.         return $categories;
  97.     }
  98.     public function getUniqskillsLanguages()
  99.     {
  100.         if (!empty($this->languages))
  101.         {
  102.             return $this->languages;
  103.         }
  104.         $this->getPortalSettings();
  105.         if ($this->portalSettings)
  106.         {
  107.             $this->languages $this->em->getRepository(Language::class)
  108.                 ->findSupportedLanguage($this->portalSettings);
  109.         }
  110.         return $this->languages;
  111.     }
  112.     public function getUniqskillsCartProductsAmount()
  113.     {
  114.         if ($this->user instanceof User && $this->user->getCart())
  115.         {
  116.             $cartHash $this->user->getCart()->getHash();
  117.         }
  118.         elseif ($this->request)
  119.         {
  120.             $cartHash $this->request->cookies->get('cartHash');
  121.         }
  122.         if (!isset($cartHash) || !$cartHash)
  123.         {
  124.             return '0';
  125.         }
  126.         $productsAmount $this->em->getRepository(ProductCart::class)->sumProductCart($cartHash);
  127.         return $productsAmount ?: '0';
  128.     }
  129.     public function getUniqskillsCartPrice()
  130.     {
  131.         if ($this->cartPrice !== null)
  132.         {
  133.             return $this->cartPrice;
  134.         }
  135.         if (!$this->cart instanceof Cart)
  136.         {
  137.             $findCart $this->cartServices->findCart($this->usernullfalse);
  138.             $this->cart $findCart['cart'];
  139.         }
  140.         if ($this->cart instanceof Cart && !$this->cart->getProductCart()->isEmpty())
  141.         {
  142.             $price $this->cartPriceCalculator->getCartPrice($this->cart);
  143.             $this->cartPrice $price['totalPriceWithShippingGross'];
  144.             return $this->cartPrice;
  145.         }
  146.         return '0';
  147.     }
  148.     public function getUniqskillsCartCountry()
  149.     {
  150.         if ($this->cart instanceof Cart)
  151.         {
  152.             return $this->cart->getCountry();
  153.         }
  154.         else
  155.         {
  156.             $findCart $this->cartServices->findCart($this->usernullfalse);
  157.             $this->cart $findCart['cart'];
  158.             if ($this->cart instanceof Cart)
  159.             {
  160.                 return $this->cart->getCountry();
  161.             }
  162.             else
  163.             {
  164.                 return null;
  165.             }
  166.         }
  167.     }
  168.     public function getUniqskillsLogo($locale 'pl')
  169.     {
  170.         /** @var Country $country */
  171.         $country $this->em->getRepository(Country::class)->findOneByAlpha2($locale);
  172.         if($country && $country->getCountrySettings() && $country->getCountrySettings()->getLogoFilename())
  173.         {
  174.             return '/uploads/logos/'.$country->getCountrySettings()->getLogoFilename();
  175.         }
  176.         // this can be removed when they add german logo to countrySettings
  177.         if($locale == 'de')
  178.         {
  179.             return 'uniqskills/img/german_logo.png';
  180.         }
  181.         return 'uniqskills/img/white_logo.svg';
  182.     }
  183.     private function getPortalSettings()
  184.     {
  185.         if ($this->portalSettings === null)
  186.         {
  187.             $this->portalSettings $this->em->getRepository(PortalSettings::class)
  188.                 ->findOneByHash($_ENV['US_PORTAL_SETTINGS']);
  189.         }
  190.     }
  191. }