You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FrontendTwigExtension.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Lc\ShopBundle\Twig;
  3. use App\Repository\HubRepository;
  4. use App\Services\GlobalParam;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Lc\ShopBundle\Context\GlobalParamInterface;
  7. use Lc\ShopBundle\Context\MerchantInterface;
  8. use Lc\ShopBundle\Context\ProductCategoryInterface;
  9. use Lc\ShopBundle\Context\ProductFamilyInterface;
  10. use Lc\ShopBundle\Form\Frontend\NewsletterType;
  11. use Lc\ShopBundle\Repository\ProductCategoryRepository;
  12. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use Symfony\Component\Form\FormFactoryInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Security\Core\Security;
  17. use Twig\Extension\AbstractExtension;
  18. use Twig\TwigFilter;
  19. use Twig\TwigFunction;
  20. class FrontendTwigExtension extends AbstractExtension
  21. {
  22. protected $em ;
  23. protected $security ;
  24. protected $globalParam ;
  25. protected $formFactory ;
  26. protected $requestStack ;
  27. protected $productCategoryRepository ;
  28. protected $merchantRepository ;
  29. protected $productFamilyRepository ;
  30. protected $liipCacheHelper ;
  31. protected $parameterBag ;
  32. public function __construct(EntityManagerInterface $em, Security $security, GlobalParamInterface $globalParam,
  33. FormFactoryInterface $formFactory, RequestStack $requestStack, CacheManager $liipCacheHelper,
  34. ParameterBagInterface $parameterBag)
  35. {
  36. $this->em = $em ;
  37. $this->security = $security ;
  38. $this->globalParam = $globalParam ;
  39. $this->formFactory = $formFactory ;
  40. $this->requestStack = $requestStack ;
  41. $this->productCategoryRepository = $this->em->getRepository($this->em->getClassMetadata(ProductCategoryInterface::class)->getName()) ;
  42. $this->merchantRepository = $this->em->getRepository($this->em->getClassMetadata(MerchantInterface::class)->getName()) ;
  43. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetadata(ProductFamilyInterface::class)->getName()) ;
  44. $this->liipCacheHelper = $liipCacheHelper;
  45. $this->parameterBag = $parameterBag;
  46. }
  47. public function getFunctions()
  48. {
  49. return array(
  50. new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
  51. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  52. new TwigFunction('get_merchants', [$this, 'getMerchants']),
  53. new TwigFunction('lc_liip', [$this, 'lcLiip']),
  54. new TwigFunction('get_file_manager_folder', [$this, 'getFileManagerFolder']),
  55. );
  56. }
  57. public function getFilters()
  58. {
  59. return [
  60. new TwigFilter('format_price', [$this, 'formatPrice']),
  61. new TwigFilter('lc_liip', [$this, 'lcLiip']),
  62. ];
  63. }
  64. public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
  65. {
  66. if(strpos($path, $this->getFileManagerFolder())===false){
  67. $path = $this->getFileManagerFolder() .'/'. $path;
  68. }
  69. if ($path) {
  70. return $this->liipCacheHelper->getBrowserPath($path, $thumb);
  71. } else {
  72. return $this->liipCacheHelper->getBrowserPath('assets/img/frontend/'.$default, $thumb);
  73. }
  74. }
  75. /**
  76. * Retourne le chemin vers le dossier d'uploads de responsiveFilemanager
  77. *
  78. * @return string
  79. */
  80. public function getFileManagerFolder(){
  81. return $this->parameterBag->get('app.path.images');
  82. }
  83. public function getProductCategories()
  84. {
  85. $categories = $this->productCategoryRepository->findAllParents($this->globalParam->getCurrentMerchant()) ;
  86. return $categories ;
  87. }
  88. public function getFormNewsletter()
  89. {
  90. $form = $this->formFactory->create(NewsletterType::class);
  91. return $form->createView() ;
  92. }
  93. public function formatPrice($price)
  94. {
  95. $price = number_format($price, 2, ',', ' ') ;
  96. $price = $price .'&nbsp;€' ;
  97. return $price ;
  98. }
  99. public function getMerchants()
  100. {
  101. return $this->merchantRepository->findAll() ;
  102. }
  103. }