Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

189 lines
6.0KB

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