81 satır
2.7KB

  1. <?php
  2. namespace Lc\ShopBundle\Twig;
  3. use App\Repository\HubRepository;
  4. use App\Services\GlobalParam;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Lc\ShopBundle\Form\Frontend\NewsletterType;
  7. use Lc\ShopBundle\Repository\ProductCategoryRepository;
  8. use Symfony\Component\Form\FormFactoryInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\Security;
  11. use Twig\Extension\AbstractExtension;
  12. use Twig\TwigFilter;
  13. use Twig\TwigFunction;
  14. class FrontendTwigExtension extends AbstractExtension
  15. {
  16. protected $em ;
  17. protected $security ;
  18. protected $globalParam ;
  19. protected $formFactory ;
  20. protected $productCategoryRepository ;
  21. protected $hubRepository ;
  22. protected $requestStack ;
  23. public function __construct(EntityManagerInterface $em, Security $security, ProductCategoryRepository $productCategoryRepository,
  24. GlobalParam $globalParam, FormFactoryInterface $formFactory, HubRepository $hubRepository, RequestStack $requestStack)
  25. {
  26. $this->em = $em ;
  27. $this->security = $security ;
  28. $this->productCategoryRepository = $productCategoryRepository ;
  29. $this->hubRepository = $hubRepository ;
  30. $this->globalParam = $globalParam ;
  31. $this->formFactory = $formFactory ;
  32. $this->requestStack = $requestStack ;
  33. }
  34. public function getFunctions()
  35. {
  36. return array(
  37. new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
  38. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  39. new TwigFunction('get_merchants', [$this, 'getMerchants']),
  40. );
  41. }
  42. public function getFilters()
  43. {
  44. return [
  45. new TwigFilter('format_price', [$this, 'formatPrice']),
  46. ];
  47. }
  48. public function getProductCategories()
  49. {
  50. $categories = $this->productCategoryRepository->findAllParents($this->globalParam->getCurrentMerchant()) ;
  51. return $categories ;
  52. }
  53. public function getFormNewsletter()
  54. {
  55. $form = $this->formFactory->create(NewsletterType::class);
  56. return $form->createView() ;
  57. }
  58. public function formatPrice($price)
  59. {
  60. $price = number_format($price, 2) ;
  61. $price = str_replace('.',',',$price) ;
  62. $price = $price .'&nbsp;€' ;
  63. return $price ;
  64. }
  65. public function getMerchants()
  66. {
  67. return $this->hubRepository->findAll() ;
  68. }
  69. }