Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

169 rindas
5.2KB

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