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 3.0KB

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