|
- <?php
-
- namespace Lc\ShopBundle\Twig;
-
- use App\Repository\HubRepository;
- use App\Services\GlobalParam;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Form\Frontend\NewsletterType;
- use Lc\ShopBundle\Repository\ProductCategoryRepository;
- use Symfony\Component\Form\FormFactoryInterface;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\Security\Core\Security;
- use Twig\Extension\AbstractExtension;
- use Twig\TwigFilter;
- use Twig\TwigFunction;
-
- class FrontendTwigExtension extends AbstractExtension
- {
- protected $em ;
- protected $security ;
- protected $globalParam ;
- protected $formFactory ;
- protected $productCategoryRepository ;
- protected $hubRepository ;
- protected $requestStack ;
-
- public function __construct(EntityManagerInterface $em, Security $security, ProductCategoryRepository $productCategoryRepository,
- GlobalParam $globalParam, FormFactoryInterface $formFactory, HubRepository $hubRepository, RequestStack $requestStack)
- {
- $this->em = $em ;
- $this->security = $security ;
- $this->productCategoryRepository = $productCategoryRepository ;
- $this->hubRepository = $hubRepository ;
- $this->globalParam = $globalParam ;
- $this->formFactory = $formFactory ;
- $this->requestStack = $requestStack ;
- }
-
- public function getFunctions()
- {
- return array(
- new TwigFunction('get_product_categories', [$this, 'getProductCategories']),
- new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
- new TwigFunction('get_merchants', [$this, 'getMerchants']),
- );
- }
-
- public function getFilters()
- {
- return [
- new TwigFilter('format_price', [$this, 'formatPrice']),
- ];
- }
-
- public function getProductCategories()
- {
- $categories = $this->productCategoryRepository->findAllParents($this->globalParam->getCurrentMerchant()) ;
- return $categories ;
- }
-
- public function getFormNewsletter()
- {
- $form = $this->formFactory->create(NewsletterType::class);
- return $form->createView() ;
- }
-
- public function formatPrice($price)
- {
- $price = number_format($price, 2) ;
- $price = str_replace('.',',',$price) ;
- $price = $price .' €' ;
- return $price ;
- }
-
- public function getMerchants()
- {
- return $this->hubRepository->findAll() ;
- }
-
- }
|