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.

110 satır
4.4KB

  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. }