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.

144 lines
4.2KB

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