|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
-
- namespace Lc\ShopBundle\Twig;
-
- 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\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;
-
- public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
- FormFactoryInterface $formFactory, RequestStack $requestStack, CacheManager $liipCacheHelper,
- ParameterBagInterface $parameterBag)
- {
- $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->liipCacheHelper = $liipCacheHelper;
- $this->parameterBag = $parameterBag;
- }
-
- 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('lc_liip', [$this, 'lcLiip']),
- new TwigFunction('get_file_manager_folder', [$this, 'getFileManagerFolder']),
- );
- }
-
- public function getFilters()
- {
- return [
- new TwigFilter('format_price', [$this, 'formatPrice']),
- new TwigFilter('lc_liip', [$this, 'lcLiip']),
- ];
- }
-
- public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
- {
- if (substr($path, 0, 1) === '/') $path = substr($path, 1);
-
- if ($path) {
- $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
-
- if (strpos($path, $fileManagerFolder) === false) {
- $path = $fileManagerFolder . '/' . $path;
- }
-
- if (file_exists($path)) {
- return $this->liipCacheHelper->getBrowserPath($path, $thumb);
- }
-
- }
-
- return $this->liipCacheHelper->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
-
- }
-
-
-
- public function getFileManagerFolder()
- {
- return $this->parameterBag->get('app.path.images');
- }
-
-
- 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)
- {
- $price = number_format($price, 2, ',', ' ');
- $price = $price . ' €';
- return $price;
- }
-
- public function getMerchants()
- {
- return $this->merchantRepository->findAll();
- }
-
- }
|