FrontendTwigExtension.php 2.6KB

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