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.3KB

4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Routing\RouterInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Twig\Extension\AbstractExtension;
  17. use Twig\TwigFilter;
  18. use Twig\TwigFunction;
  19. class FrontendTwigExtension extends AbstractExtension
  20. {
  21. protected $em;
  22. protected $security;
  23. protected $merchantUtils;
  24. protected $formFactory;
  25. protected $requestStack;
  26. protected $productCategoryRepository;
  27. protected $merchantRepository;
  28. protected $productFamilyRepository;
  29. protected $liipCacheHelper;
  30. protected $parameterBag;
  31. protected $kernel;
  32. protected $router ;
  33. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils,
  34. FormFactoryInterface $formFactory, RequestStack $requestStack, ParameterBagInterface $parameterBag,
  35. KernelInterface $kernel, RouterInterface $router)
  36. {
  37. $this->em = $em;
  38. $this->security = $security;
  39. $this->merchantUtils = $merchantUtils;
  40. $this->formFactory = $formFactory;
  41. $this->requestStack = $requestStack;
  42. $this->productCategoryRepository = $this->em->getRepository($this->em->getClassMetadata(ProductCategoryInterface::class)->getName());
  43. $this->merchantRepository = $this->em->getRepository($this->em->getClassMetadata(MerchantInterface::class)->getName());
  44. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetadata(ProductFamilyInterface::class)->getName());
  45. $this->parameterBag = $parameterBag;
  46. $this->kernel = $kernel;
  47. $this->router = $router ;
  48. }
  49. public function getFunctions()
  50. {
  51. return array(
  52. new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
  53. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  54. new TwigFunction('get_merchants', [$this, 'getMerchants']),
  55. new TwigFunction('get_file_manager_folder', [$this, 'getFileManagerFolder']),
  56. new TwigFunction('lc_format_price', [$this, 'formatPrice']),
  57. );
  58. }
  59. public function getFilters()
  60. {
  61. return [
  62. new TwigFilter('format_price', [$this, 'formatPrice']),
  63. new TwigFilter('lc_cache', [$this, 'lcCache']),
  64. ];
  65. }
  66. public function lcCache($file)
  67. {
  68. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  69. if ($cacheTime) {
  70. return $file . '?c=' . $cacheTime;
  71. } else {
  72. return $file . "?c=0";
  73. }
  74. }
  75. public function getProductCategories()
  76. {
  77. $categories = $this->productCategoryRepository->findAllParents(false);
  78. return $categories;
  79. }
  80. public function getFormNewsletter()
  81. {
  82. $form = $this->formFactory->create(NewsletterType::class);
  83. return $form->createView();
  84. }
  85. public function formatPrice($price, $unbreakableSpace = true)
  86. {
  87. $price = number_format($price, 2, ',', ' ');
  88. $price .= $unbreakableSpace ? '&nbsp;' : ' ' ;
  89. $price .= '€' ;
  90. return $price;
  91. }
  92. public function getMerchants()
  93. {
  94. return $this->merchantRepository->findAll();
  95. }
  96. }