No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

172 líneas
5.5KB

  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use App\Repository\ReminderRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\SovBundle\Component\MetaComponent;
  6. use Lc\SovBundle\Component\StringComponent;
  7. use Lc\SovBundle\Form\Newsletter\NewsletterType;
  8. use Lc\SovBundle\Model\File\FileInterface;
  9. use Lc\SovBundle\Repository\Reminder\ReminderStore;
  10. use Lc\SovBundle\Translation\TranslatorAdmin;
  11. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpKernel\KernelInterface;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Security\Core\Security;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use Twig\Extension\AbstractExtension;
  20. use Twig\TwigFilter;
  21. use Twig\TwigFunction;
  22. class TwigExtension extends AbstractExtension
  23. {
  24. protected EntityManagerInterface $em;
  25. protected KernelInterface $kernel;
  26. protected ParameterBagInterface $parameterBag;
  27. protected CacheManager $cacheManager;
  28. protected RequestStack $requestStack;
  29. protected UrlGeneratorInterface $router;
  30. protected TranslatorInterface $translator;
  31. protected TranslatorAdmin $translatorAdmin;
  32. protected ReminderStore $reminderStore;
  33. protected Security $security;
  34. protected FormFactoryInterface $formFactory;
  35. protected StringComponent $stringComponent;
  36. protected MetaComponent $metaComponent;
  37. public function __construct(
  38. KernelInterface $kernel,
  39. ParameterBagInterface $parameterBag,
  40. CacheManager $cacheManager,
  41. EntityManagerInterface $entityManager,
  42. RequestStack $requestStack,
  43. UrlGeneratorInterface $router,
  44. TranslatorInterface $translator,
  45. TranslatorAdmin $translatorAdmin,
  46. ReminderStore $reminderStore,
  47. Security $security,
  48. FormFactoryInterface $formFactory,
  49. StringComponent $stringComponent,
  50. MetaComponent $metaComponent
  51. ) {
  52. $this->kernel = $kernel;
  53. $this->parameterBag = $parameterBag;
  54. $this->cacheManager = $cacheManager;
  55. $this->em = $entityManager;
  56. $this->requestStack = $requestStack;
  57. $this->router = $router;
  58. $this->translator = $translator;
  59. $this->translatorAdmin = $translatorAdmin;
  60. $this->reminderStore = $reminderStore;
  61. $this->security = $security;
  62. $this->formFactory = $formFactory;
  63. $this->stringComponent = $stringComponent;
  64. $this->metaComponent = $metaComponent;
  65. }
  66. public function getFunctions()
  67. {
  68. return [
  69. new TwigFunction('sov_liip', [$this, 'liip']),
  70. new TwigFunction('liip', [$this, 'liip']),
  71. new TwigFunction('sov_get_by_devalias', [$this, 'getByDevAlias']),
  72. new TwigFunction('sov_parameter', [$this, 'getParameter']),
  73. new TwigFunction('sov_homepage_route', [$this, 'getHomepageRoute']),
  74. new TwigFunction('lc_format_price', [$this, 'formatPrice']),
  75. new TwigFunction('die', [$this, 'die']),
  76. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  77. new TwigFunction('sov_limit_text', [$this, 'limitText']),
  78. ];
  79. }
  80. public function die()
  81. {
  82. die();
  83. }
  84. public function getFilters()
  85. {
  86. return [
  87. new TwigFilter('uc_first', [$this, 'ucFirst']),
  88. new TwigFilter('format_price', [$this, 'formatPrice']),
  89. new TwigFilter('sov_cache', [$this, 'sovCache']),
  90. ];
  91. }
  92. public function sovCache($file)
  93. {
  94. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  95. if ($cacheTime) {
  96. return $file . '?c=' . $cacheTime;
  97. } else {
  98. return $file . "?c=0";
  99. }
  100. }
  101. public function formatPrice($price, $unbreakableSpace = true)
  102. {
  103. $price = number_format($price, 2, ',', ' ');
  104. $price .= $unbreakableSpace ? '&nbsp;' : ' ';
  105. $price .= '€';
  106. return $price;
  107. }
  108. public function ucFirst($string)
  109. {
  110. return ucfirst($string);
  111. }
  112. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  113. {
  114. if (substr($path, 0, 1) === '/') {
  115. $path = substr($path, 1);
  116. }
  117. if ($path) {
  118. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  119. if (strpos($path, $fileManagerFolder) === false) {
  120. $path = $fileManagerFolder . '/' . $path;
  121. }
  122. if (file_exists($path)) {
  123. return $this->cacheManager->getBrowserPath($path, $thumb);
  124. }
  125. }
  126. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  127. }
  128. public function getFileManagerFolder()
  129. {
  130. return $this->parameterBag->get('app.path_uploads');
  131. }
  132. public function getHomepageRoute()
  133. {
  134. return $this->parameterBag->get('lc_sov.homepage_route');
  135. }
  136. public function getParameter($name)
  137. {
  138. return $this->parameterBag->get($name);
  139. }
  140. public function getFormNewsletter()
  141. {
  142. $form = $this->formFactory->create(NewsletterType::class);
  143. return $form->createView();
  144. }
  145. public function limitText(string $text, int $limit)
  146. {
  147. return $this->stringComponent->limitText($text, $limit);
  148. }
  149. }