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.

156 satır
4.9KB

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