Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FrontendTwigExtension.php 4.7KB

4 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Security\Core\Security;
  14. use Twig\Extension\AbstractExtension;
  15. use Twig\TwigFilter;
  16. use Twig\TwigFunction;
  17. class FrontendTwigExtension extends AbstractExtension
  18. {
  19. protected $em;
  20. protected $security;
  21. protected $merchantUtils;
  22. protected $formFactory;
  23. protected $requestStack;
  24. protected $productCategoryRepository;
  25. protected $merchantRepository;
  26. protected $productFamilyRepository;
  27. protected $liipCacheHelper;
  28. protected $parameterBag;
  29. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
  30. FormFactoryInterface $formFactory, RequestStack $requestStack, CacheManager $liipCacheHelper,
  31. ParameterBagInterface $parameterBag)
  32. {
  33. $this->em = $em;
  34. $this->security = $security;
  35. $this->merchantUtils = $merchantUtils;
  36. $this->formFactory = $formFactory;
  37. $this->requestStack = $requestStack;
  38. $this->productCategoryRepository = $this->em->getRepository($this->em->getClassMetadata(ProductCategoryInterface::class)->getName());
  39. $this->merchantRepository = $this->em->getRepository($this->em->getClassMetadata(MerchantInterface::class)->getName());
  40. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetadata(ProductFamilyInterface::class)->getName());
  41. $this->liipCacheHelper = $liipCacheHelper;
  42. $this->parameterBag = $parameterBag;
  43. }
  44. public function getFunctions()
  45. {
  46. return array(
  47. new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
  48. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  49. new TwigFunction('get_merchants', [$this, 'getMerchants']),
  50. new TwigFunction('lc_liip', [$this, 'lcLiip']),
  51. new TwigFunction('get_file_manager_folder', [$this, 'getFileManagerFolder']),
  52. );
  53. }
  54. public function getFilters()
  55. {
  56. return [
  57. new TwigFilter('format_price', [$this, 'formatPrice']),
  58. new TwigFilter('lc_liip', [$this, 'lcLiip']),
  59. ];
  60. }
  61. public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
  62. {
  63. if (substr($path, 0, 1) === '/') $path = substr($path, 1);
  64. if ($path) {
  65. $fileManagerFolder = substr($this->getFileManagerFolder(), 1) ;
  66. if (strpos($path, $fileManagerFolder) === false) {
  67. $path = $fileManagerFolder . '/' . $path;
  68. }
  69. if (file_exists($path)) {
  70. return $this->liipCacheHelper->getBrowserPath($path, $thumb);
  71. }
  72. }
  73. return $this->liipCacheHelper->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  74. }
  75. /**
  76. * Retourne le chemin vers le dossier d'uploads de responsiveFilemanager
  77. *
  78. * @return string
  79. */
  80. public function getFileManagerFolder()
  81. {
  82. return $this->parameterBag->get('app.path.images');
  83. }
  84. public function getProductCategories()
  85. {
  86. $categories = $this->productCategoryRepository->findAllParents(false);
  87. return $categories;
  88. }
  89. public function getFormNewsletter()
  90. {
  91. $form = $this->formFactory->create(NewsletterType::class);
  92. return $form->createView();
  93. }
  94. public function formatPrice($price)
  95. {
  96. $price = number_format($price, 2, ',', ' ');
  97. $price = $price . '&nbsp;€';
  98. return $price;
  99. }
  100. public function getMerchants()
  101. {
  102. return $this->merchantRepository->findAll();
  103. }
  104. }