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.

109 lines
4.3KB

  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. );
  59. }
  60. public function getFilters()
  61. {
  62. return [
  63. new TwigFilter('format_price', [$this, 'formatPrice']),
  64. new TwigFilter('lc_cache', [$this, 'lcCache']),
  65. ];
  66. }
  67. public function lcCache($file)
  68. {
  69. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  70. if ($cacheTime) {
  71. return $file . '?c=' . $cacheTime;
  72. } else {
  73. return $file . "?c=0";
  74. }
  75. }
  76. public function getProductCategories()
  77. {
  78. $categories = $this->productCategoryRepository->findAllParents(false);
  79. return $categories;
  80. }
  81. public function getFormNewsletter()
  82. {
  83. $form = $this->formFactory->create(NewsletterType::class);
  84. return $form->createView();
  85. }
  86. public function formatPrice($price, $unbreakableSpace = true)
  87. {
  88. $price = number_format($price, 2, ',', ' ');
  89. $price .= $unbreakableSpace ? '&nbsp;' : ' ' ;
  90. $price .= '€' ;
  91. return $price;
  92. }
  93. public function getMerchants()
  94. {
  95. return $this->merchantRepository->findAll();
  96. }
  97. }