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ů.

170 lines
5.4KB

  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use App\Repository\ReminderRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\SovBundle\Model\Reminder\ReminderInterface;
  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\Contracts\Translation\TranslatorInterface;
  13. use Twig\Extension\AbstractExtension;
  14. use Twig\TwigFilter;
  15. use Twig\TwigFunction;
  16. class TwigExtension extends AbstractExtension
  17. {
  18. protected $em;
  19. protected $kernel;
  20. protected $parameterBag;
  21. protected $cacheManager;
  22. protected $requestStack;
  23. protected $router;
  24. protected $translator;
  25. protected $translatorAdmin;
  26. public function __construct(
  27. KernelInterface $kernel,
  28. ParameterBagInterface $parameterBag,
  29. CacheManager $cacheManager,
  30. EntityManagerInterface $entityManager,
  31. RequestStack $requestStack,
  32. UrlGeneratorInterface $router,
  33. TranslatorInterface $translator,
  34. TranslatorAdmin $translatorAdmin
  35. ) {
  36. $this->kernel = $kernel;
  37. $this->parameterBag = $parameterBag;
  38. $this->cacheManager = $cacheManager;
  39. $this->em = $entityManager;
  40. $this->requestStack = $requestStack;
  41. $this->router = $router;
  42. $this->translator = $translator;
  43. $this->translatorAdmin = $translatorAdmin;
  44. }
  45. public function getFunctions()
  46. {
  47. return array(
  48. new TwigFunction('lc_liip', [$this, 'lcLiip']),
  49. new TwigFunction('homepage_route', [$this, 'homepageRoute']),
  50. new TwigFunction('page_by_dev_alias', [$this, 'getPageByDevAlias']),
  51. new TwigFunction('translated_urls', [$this, 'getTranslatedUrls']),
  52. new TwigFunction('sov_get_reminders', [$this, 'getReminders']),
  53. );
  54. }
  55. public function getFilters()
  56. {
  57. return [
  58. new TwigFilter('lc_cache', [$this, 'lcCache']),
  59. new TwigFilter('lc_trans_admin_field', [$this, 'lcTransAdminField']),
  60. new TwigFilter('lc_trans_admin_help', [$this, 'lcTransAdminHelp']),
  61. new TwigFilter('lc_trans_admin_panel', [$this, 'lcTransAdminPanel']),
  62. new TwigFilter('lc_trans_admin_menu', [$this, 'lcTransAdminMenu']),
  63. new TwigFilter('lc_trans_admin_title', [$this, 'lcTransAdminTitle']),
  64. new TwigFilter('lc_trans_admin_action', [$this, 'lcTransAdminAction']),
  65. ];
  66. }
  67. public function lcCache($file)
  68. {
  69. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  70. if ($cacheTime) {
  71. return $file . '?c=' . $cacheTime;
  72. } else {
  73. return $file . "?c=0";
  74. }
  75. }
  76. public function lcTransAdminField($fieldName, $entityClass)
  77. {
  78. return $this->translatorAdmin->transField($fieldName, $entityClass);
  79. }
  80. public function lcTransAdminHelp($fieldName, $entityClass)
  81. {
  82. return $this->translatorAdmin->transHelp($fieldName, $entityClass);
  83. }
  84. public function lcTransAdminPanel($panelName, $entityClass)
  85. {
  86. return $this->translatorAdmin->transPanel($panelName, $entityClass);
  87. }
  88. public function lcTransAdminMenu($menuName)
  89. {
  90. return $this->translatorAdmin->transMenu($menuName);;
  91. }
  92. public function lcTransAdminAction($actionName)
  93. {
  94. return $this->translatorAdmin->transAction($actionName);;
  95. }
  96. public function lcTransAdminTitle($pageName, $entityClass = null, $params = [])
  97. {
  98. return $this->translatorAdmin->transTitle($pageName, $entityClass, $params);
  99. }
  100. public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
  101. {
  102. if (substr($path, 0, 1) === '/') {
  103. $path = substr($path, 1);
  104. }
  105. if ($path) {
  106. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  107. if (strpos($path, $fileManagerFolder) === false) {
  108. $path = $fileManagerFolder . '/' . $path;
  109. }
  110. if (file_exists($path)) {
  111. return $this->cacheManager->getBrowserPath($path, $thumb);
  112. }
  113. }
  114. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  115. }
  116. function getTranslatedUrls()
  117. {
  118. $ret = array();
  119. $langs = $this->parameterBag->get('app.locales');
  120. $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
  121. $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
  122. if ($currentRoute) {
  123. foreach ($langs as $lg) {
  124. $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
  125. }
  126. }
  127. return $ret;
  128. }
  129. public function getFileManagerFolder()
  130. {
  131. return $this->parameterBag->get('app.path_uploads');
  132. }
  133. public function homepageRoute()
  134. {
  135. return $this->parameterBag->get('lc_sov.homepage_route');
  136. }
  137. public function getReminders()
  138. {
  139. $reminders = $this->em->getRepository(ReminderInterface::class)->findAll() ;
  140. return $reminders;
  141. }
  142. }