Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

86 lines
3.2KB

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