|
- <?php
-
- namespace Lc\ShopBundle\Twig;
-
- use App\Services\NotificationUtils;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\MerchantInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\ProductCategoryInterface;
- use Lc\ShopBundle\Context\ProductFamilyInterface;
- use Lc\ShopBundle\Form\Frontend\NewsletterType;
- use Liip\ImagineBundle\Imagine\Cache\CacheManager;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\Form\FormFactoryInterface;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\HttpKernel\KernelInterface;
- use Symfony\Component\Routing\RouterInterface;
- use Symfony\Component\Security\Core\Security;
- use Twig\Extension\AbstractExtension;
- use Twig\TwigFilter;
- use Twig\TwigFunction;
-
- class FrontendTwigExtension extends AbstractExtension
- {
- protected $em;
- protected $security;
- protected $merchantUtils;
- protected $formFactory;
- protected $requestStack;
- protected $productCategoryRepository;
- protected $merchantRepository;
- protected $productFamilyRepository;
- protected $liipCacheHelper;
- protected $parameterBag;
- protected $kernel;
- protected $router ;
-
- public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
- FormFactoryInterface $formFactory, RequestStack $requestStack, ParameterBagInterface $parameterBag,
- KernelInterface $kernel, RouterInterface $router)
- {
- $this->em = $em;
- $this->security = $security;
- $this->merchantUtils = $merchantUtils;
- $this->formFactory = $formFactory;
- $this->requestStack = $requestStack;
- $this->productCategoryRepository = $this->em->getRepository($this->em->getClassMetadata(ProductCategoryInterface::class)->getName());
- $this->merchantRepository = $this->em->getRepository($this->em->getClassMetadata(MerchantInterface::class)->getName());
- $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetadata(ProductFamilyInterface::class)->getName());
- $this->parameterBag = $parameterBag;
- $this->kernel = $kernel;
- $this->router = $router ;
- }
-
- public function getFunctions()
- {
- return array(
- new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
- new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
- new TwigFunction('get_merchants', [$this, 'getMerchants']),
- new TwigFunction('get_file_manager_folder', [$this, 'getFileManagerFolder']),
- new TwigFunction('lc_format_price', [$this, 'formatPrice']),
- new TwigFunction('get_form_manage_notifications', [$this, 'getFormManageNotifications']),
- );
- }
-
- public function getFilters()
- {
- return [
- new TwigFilter('format_price', [$this, 'formatPrice']),
- new TwigFilter('lc_cache', [$this, 'lcCache']),
- ];
- }
-
- public function lcCache($file)
- {
- $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
- if ($cacheTime) {
- return $file . '?c=' . $cacheTime;
- } else {
- return $file . "?c=0";
- }
- }
-
- public function getProductCategories()
- {
- $categories = $this->productCategoryRepository->findAllParents(false);
- return $categories;
- }
-
- public function getFormNewsletter()
- {
- $form = $this->formFactory->create(NewsletterType::class);
- return $form->createView();
- }
-
- public function formatPrice($price, $unbreakableSpace = true)
- {
- $price = number_format($price, 2, ',', ' ');
- $price .= $unbreakableSpace ? ' ' : ' ' ;
- $price .= '€' ;
- return $price;
- }
-
- public function getMerchants()
- {
- return $this->merchantRepository->findAll();
- }
- }
|