Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

72 lines
2.3KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType;
  5. use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
  6. use Lc\CaracoleBundle\Repository\Section\SectionRepository;
  7. use Lc\SovBundle\Translation\TranslatorAdmin;
  8. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\Form\FormFactoryInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. use Symfony\Component\Routing\Generator\UrlGenerator;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Twig\Extension\AbstractExtension;
  17. use Twig\TwigFunction;
  18. class TwigExtension extends AbstractExtension
  19. {
  20. protected $merchantRepository;
  21. protected $sectionRepository;
  22. protected $formFactory;
  23. protected $urlGenerator;
  24. public function __construct(
  25. MerchantRepository $merchantRepository,
  26. SectionRepository $sectionRepository,
  27. FormFactoryInterface $formFactory,
  28. UrlGeneratorInterface $urlGenerator
  29. ) {
  30. $this->merchantRepository = $merchantRepository;
  31. $this->sectionRepository = $sectionRepository;
  32. $this->formFactory = $formFactory;
  33. $this->urlGenerator = $urlGenerator;
  34. }
  35. public function getFunctions()
  36. {
  37. return array(
  38. new TwigFunction('carac_get_sections', [$this, 'getSections']),
  39. new TwigFunction('carac_get_form_switch_merchant', [$this, 'getFormSwitchMerchant']),
  40. );
  41. }
  42. public function getSections()
  43. {
  44. return $this->sectionRepository->findAll();
  45. }
  46. public function getMerchants()
  47. {
  48. return $this->merchantRepository->findAll();
  49. }
  50. public function getFormSwitchMerchant($context = 'front')
  51. {
  52. $form = $this->formFactory->create(
  53. SwitchMerchantFormType::class,
  54. null,
  55. [
  56. 'action' => $this->urlGenerator->generate('carac_switch_merchant'),
  57. 'attr' => ['class' => 'switch-merchant'],
  58. 'context' => $context
  59. ]
  60. );
  61. return $form->createView();
  62. }
  63. }