Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FrontendTwigExtension.php 4.4KB

il y a 4 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Lc\ShopBundle\Twig;
  3. use App\Services\NotificationUtils;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantInterface;
  6. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  7. use Lc\ShopBundle\Context\ProductCategoryInterface;
  8. use Lc\ShopBundle\Context\ProductFamilyInterface;
  9. use Lc\ShopBundle\Form\Frontend\NewsletterType;
  10. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\Form\FormFactoryInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  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 $merchantUtils;
  25. protected $formFactory;
  26. protected $requestStack;
  27. protected $productCategoryRepository;
  28. protected $merchantRepository;
  29. protected $productFamilyRepository;
  30. protected $liipCacheHelper;
  31. protected $parameterBag;
  32. protected $kernel;
  33. protected $router ;
  34. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
  35. FormFactoryInterface $formFactory, RequestStack $requestStack, ParameterBagInterface $parameterBag,
  36. KernelInterface $kernel, RouterInterface $router)
  37. {
  38. $this->em = $em;
  39. $this->security = $security;
  40. $this->merchantUtils = $merchantUtils;
  41. $this->formFactory = $formFactory;
  42. $this->requestStack = $requestStack;
  43. $this->productCategoryRepository = $this->em->getRepository($this->em->getClassMetadata(ProductCategoryInterface::class)->getName());
  44. $this->merchantRepository = $this->em->getRepository($this->em->getClassMetadata(MerchantInterface::class)->getName());
  45. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetadata(ProductFamilyInterface::class)->getName());
  46. $this->parameterBag = $parameterBag;
  47. $this->kernel = $kernel;
  48. $this->router = $router ;
  49. }
  50. public function getFunctions()
  51. {
  52. return array(
  53. new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
  54. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  55. new TwigFunction('get_merchants', [$this, 'getMerchants']),
  56. new TwigFunction('get_file_manager_folder', [$this, 'getFileManagerFolder']),
  57. new TwigFunction('lc_format_price', [$this, 'formatPrice']),
  58. new TwigFunction('get_form_manage_notifications', [$this, 'getFormManageNotifications']),
  59. );
  60. }
  61. public function getFilters()
  62. {
  63. return [
  64. new TwigFilter('format_price', [$this, 'formatPrice']),
  65. new TwigFilter('lc_cache', [$this, 'lcCache']),
  66. ];
  67. }
  68. public function lcCache($file)
  69. {
  70. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  71. if ($cacheTime) {
  72. return $file . '?c=' . $cacheTime;
  73. } else {
  74. return $file . "?c=0";
  75. }
  76. }
  77. public function getProductCategories()
  78. {
  79. $categories = $this->productCategoryRepository->findAllParents(false);
  80. return $categories;
  81. }
  82. public function getFormNewsletter()
  83. {
  84. $form = $this->formFactory->create(NewsletterType::class);
  85. return $form->createView();
  86. }
  87. public function formatPrice($price, $unbreakableSpace = true)
  88. {
  89. $price = number_format($price, 2, ',', ' ');
  90. $price .= $unbreakableSpace ? '&nbsp;' : ' ' ;
  91. $price .= '€' ;
  92. return $price;
  93. }
  94. public function getMerchants()
  95. {
  96. return $this->merchantRepository->findAll();
  97. }
  98. }