Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

156 lines
4.7KB

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