137 lines
5.2KB

  1. <?php
  2. namespace Lc\ShopBundle\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\MerchantInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\ProductCategoryInterface;
  7. use Lc\ShopBundle\Context\ProductFamilyInterface;
  8. use Lc\ShopBundle\Form\Frontend\NewsletterType;
  9. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFilter;
  17. use Twig\TwigFunction;
  18. class FrontendTwigExtension extends AbstractExtension
  19. {
  20. protected $em;
  21. protected $security;
  22. protected $merchantUtils;
  23. protected $formFactory;
  24. protected $requestStack;
  25. protected $productCategoryRepository;
  26. protected $merchantRepository;
  27. protected $productFamilyRepository;
  28. protected $liipCacheHelper;
  29. protected $parameterBag;
  30. protected $kernel;
  31. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
  32. FormFactoryInterface $formFactory, RequestStack $requestStack, CacheManager $liipCacheHelper,
  33. ParameterBagInterface $parameterBag, KernelInterface $kernel)
  34. {
  35. $this->em = $em;
  36. $this->security = $security;
  37. $this->merchantUtils = $merchantUtils;
  38. $this->formFactory = $formFactory;
  39. $this->requestStack = $requestStack;
  40. $this->productCategoryRepository = $this->em->getRepository($this->em->getClassMetadata(ProductCategoryInterface::class)->getName());
  41. $this->merchantRepository = $this->em->getRepository($this->em->getClassMetadata(MerchantInterface::class)->getName());
  42. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetadata(ProductFamilyInterface::class)->getName());
  43. $this->liipCacheHelper = $liipCacheHelper;
  44. $this->parameterBag = $parameterBag;
  45. $this->kernel = $kernel;
  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. new TwigFilter('lc_cache', [$this, 'lcCache']),
  63. ];
  64. }
  65. public function lcCache($file){
  66. $cacheTime = filemtime($this->kernel->getProjectDir().'/public'.$file);
  67. if($cacheTime){
  68. return $file.'?c='.$cacheTime;
  69. }else{
  70. return $file."?c=0";
  71. }
  72. }
  73. public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
  74. {
  75. if (substr($path, 0, 1) === '/') $path = substr($path, 1);
  76. if ($path) {
  77. $fileManagerFolder = substr($this->getFileManagerFolder(), 1) ;
  78. if (strpos($path, $fileManagerFolder) === false) {
  79. $path = $fileManagerFolder . '/' . $path;
  80. }
  81. if (file_exists($path)) {
  82. return $this->liipCacheHelper->getBrowserPath($path, $thumb);
  83. }
  84. }
  85. return $this->liipCacheHelper->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  86. }
  87. /**
  88. * Retourne le chemin vers le dossier d'uploads de responsiveFilemanager
  89. *
  90. * @return string
  91. */
  92. public function getFileManagerFolder()
  93. {
  94. return $this->parameterBag->get('app.path.images');
  95. }
  96. public function getProductCategories()
  97. {
  98. $categories = $this->productCategoryRepository->findAllParents(false);
  99. return $categories;
  100. }
  101. public function getFormNewsletter()
  102. {
  103. $form = $this->formFactory->create(NewsletterType::class);
  104. return $form->createView();
  105. }
  106. public function formatPrice($price)
  107. {
  108. $price = number_format($price, 2, ',', ' ');
  109. $price = $price . '&nbsp;€';
  110. return $price;
  111. }
  112. public function getMerchants()
  113. {
  114. return $this->merchantRepository->findAll();
  115. }
  116. }