Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

176 Zeilen
5.6KB

  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. new TwigFunction('sov_get_parameter', [$this, 'getParameter']),
  54. );
  55. }
  56. public function getFilters()
  57. {
  58. return [
  59. new TwigFilter('lc_cache', [$this, 'lcCache']),
  60. new TwigFilter('lc_trans_admin_field', [$this, 'lcTransAdminField']),
  61. new TwigFilter('lc_trans_admin_help', [$this, 'lcTransAdminHelp']),
  62. new TwigFilter('lc_trans_admin_panel', [$this, 'lcTransAdminPanel']),
  63. new TwigFilter('lc_trans_admin_menu', [$this, 'lcTransAdminMenu']),
  64. new TwigFilter('lc_trans_admin_title', [$this, 'lcTransAdminTitle']),
  65. new TwigFilter('lc_trans_admin_action', [$this, 'lcTransAdminAction']),
  66. ];
  67. }
  68. public function lcCache($file)
  69. {
  70. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  71. if ($cacheTime) {
  72. return $file . '?c=' . $cacheTime;
  73. } else {
  74. return $file . "?c=0";
  75. }
  76. }
  77. public function lcTransAdminField($fieldName, $entityClass)
  78. {
  79. return $this->translatorAdmin->transField($fieldName, $entityClass);
  80. }
  81. public function lcTransAdminHelp($fieldName, $entityClass)
  82. {
  83. return $this->translatorAdmin->transHelp($fieldName, $entityClass);
  84. }
  85. public function lcTransAdminPanel($panelName, $entityClass)
  86. {
  87. return $this->translatorAdmin->transPanel($panelName, $entityClass);
  88. }
  89. public function lcTransAdminMenu($menuName)
  90. {
  91. return $this->translatorAdmin->transMenu($menuName);;
  92. }
  93. public function lcTransAdminAction($actionName)
  94. {
  95. return $this->translatorAdmin->transAction($actionName);;
  96. }
  97. public function lcTransAdminTitle($pageName, $entityClass = null, $params = [])
  98. {
  99. return $this->translatorAdmin->transTitle($pageName, $entityClass, $params);
  100. }
  101. public function lcLiip($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. function getTranslatedUrls()
  118. {
  119. $ret = array();
  120. $langs = $this->parameterBag->get('app.locales');
  121. $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
  122. $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
  123. if ($currentRoute) {
  124. foreach ($langs as $lg) {
  125. $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
  126. }
  127. }
  128. return $ret;
  129. }
  130. public function getFileManagerFolder()
  131. {
  132. return $this->parameterBag->get('app.path_uploads');
  133. }
  134. public function homepageRoute()
  135. {
  136. return $this->parameterBag->get('lc_sov.homepage_route');
  137. }
  138. public function getParameter($name)
  139. {
  140. return $this->parameterBag->get($name) ;
  141. }
  142. public function getReminders()
  143. {
  144. $reminders = $this->em->getRepository(ReminderInterface::class)->findAll() ;
  145. return $reminders;
  146. }
  147. }