You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TwigExtension.php 2.9KB

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