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.

210 lines
6.9KB

  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  5. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpKernel\KernelInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Twig\Extension\AbstractExtension;
  12. use Twig\TwigFilter;
  13. use Twig\TwigFunction;
  14. class TwigExtension extends AbstractExtension
  15. {
  16. protected $em;
  17. protected $kernel;
  18. protected $parameterBag;
  19. protected $cacheManager;
  20. protected $requestStack;
  21. protected $router;
  22. protected $translator;
  23. public function __construct(
  24. KernelInterface $kernel,
  25. ParameterBagInterface $parameterBag,
  26. CacheManager $cacheManager,
  27. EntityManagerInterface $entityManager,
  28. RequestStack $requestStack,
  29. UrlGeneratorInterface $router,
  30. TranslatorInterface $translator
  31. ) {
  32. $this->kernel = $kernel;
  33. $this->parameterBag = $parameterBag;
  34. $this->cacheManager = $cacheManager;
  35. $this->em = $entityManager;
  36. $this->requestStack = $requestStack;
  37. $this->router = $router;
  38. $this->translator = $translator;
  39. }
  40. public function getFunctions()
  41. {
  42. return array(
  43. new TwigFunction('lc_liip', [$this, 'lcLiip']),
  44. new TwigFunction('homepage_route', [$this, 'homepageRoute']),
  45. new TwigFunction('page_by_dev_alias', [$this, 'getPageByDevAlias']),
  46. new TwigFunction('translated_urls', [$this, 'getTranslatedUrls'])
  47. );
  48. }
  49. public function getFilters()
  50. {
  51. return [
  52. new TwigFilter('lc_cache', [$this, 'lcCache']),
  53. new TwigFilter('lc_trans_admin_field', [$this, 'lcTransAdminField']),
  54. new TwigFilter('lc_trans_admin_panel', [$this, 'lcTransAdminPanel']),
  55. new TwigFilter('lc_trans_admin_menu', [$this, 'lcTransAdminMenu']),
  56. new TwigFilter('lc_trans_admin_title', [$this, 'lcTransAdminTitle']),
  57. ];
  58. }
  59. public function lcCache($file)
  60. {
  61. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  62. if ($cacheTime) {
  63. return $file . '?c=' . $cacheTime;
  64. } else {
  65. return $file . "?c=0";
  66. }
  67. }
  68. public function lcTransAdminField($fieldName, $entityClass, $isHelp = false)
  69. {
  70. if ($isHelp) {
  71. $fieldName = $fieldName . '_help';
  72. }
  73. $entityName = $this->getEntityName($entityClass) ;
  74. $entityLabel = 'entity.' . $entityName . '.fields.' . $fieldName;
  75. $translatedLabel = $this->translator->trans($entityLabel, [], 'admin');
  76. if ($translatedLabel == $entityLabel) {
  77. $defaultLabel = 'entity.default.fields.' . $fieldName;
  78. $translatedLabel = $this->translator->trans($defaultLabel, [], 'admin');
  79. if ($translatedLabel == $defaultLabel) {
  80. if ($isHelp) {
  81. $translatedLabel = '';
  82. } else {
  83. $translatedLabel = $entityLabel;
  84. }
  85. }
  86. }
  87. return $translatedLabel;
  88. }
  89. public function lcTransAdminPanel($panelName, $entityClass)
  90. {
  91. $entityName = $this->getEntityName($entityClass) ;
  92. $panelLabel = 'entity.' . $entityName . '.panels.' . $panelName;
  93. $translatedLabel = $this->translator->trans($panelLabel, [], 'admin');
  94. if ($translatedLabel == $panelLabel) {
  95. $defaultLabel = 'entity.default.panels.' . $panelName;
  96. $translatedLabel = $this->translator->trans($defaultLabel, [], 'admin');
  97. if ($translatedLabel == $defaultLabel) {
  98. $translatedLabel = $panelLabel;
  99. }
  100. }
  101. return $translatedLabel;
  102. }
  103. public function lcTransAdminMenu($menuName)
  104. {
  105. $menuLabel = 'menu.'.$menuName;
  106. $translatedLabel = $this->translator->trans($menuLabel, [], 'admin');
  107. return $translatedLabel ;
  108. }
  109. public function lcTransAdminTitle($pageName, $entityClass)
  110. {
  111. $entityName = $this->getEntityName($entityClass) ;
  112. $translatedLabel = '' ;
  113. if(Crud::PAGE_INDEX == $pageName) {
  114. $titleLabel = 'entity.'.$entityName.'.label_plurial' ;
  115. $translatedLabel = $this->translator->trans($titleLabel, [], 'admin');
  116. }
  117. elseif(Crud::PAGE_EDIT == $pageName) {
  118. $entityLabel = 'entity.'.$entityName.'.label' ;
  119. $translatedEntityLabel = $this->translator->trans($entityLabel, [],'admin');
  120. $translatedLabel = $this->translator->trans('form.title.edit', ['%name%' => $translatedEntityLabel], 'admin');
  121. }
  122. elseif(Crud::PAGE_NEW == $pageName) {
  123. $entityLabel = 'entity.'.$entityName.'.label' ;
  124. $translatedEntityLabel = $this->translator->trans($entityLabel, [], 'admin');
  125. $translatedLabel = $this->translator->trans('form.title.new', ['%name%' => $translatedEntityLabel], 'admin');
  126. }
  127. return $translatedLabel ;
  128. }
  129. public function getEntityName($entityClass)
  130. {
  131. $entityNameExplode = explode('\\', $entityClass);
  132. $entityName = strtolower($entityNameExplode[count($entityNameExplode) - 1]);
  133. return $entityName ;
  134. }
  135. public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
  136. {
  137. if (substr($path, 0, 1) === '/') {
  138. $path = substr($path, 1);
  139. }
  140. if ($path) {
  141. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  142. if (strpos($path, $fileManagerFolder) === false) {
  143. $path = $fileManagerFolder . '/' . $path;
  144. }
  145. if (file_exists($path)) {
  146. return $this->cacheManager->getBrowserPath($path, $thumb);
  147. }
  148. }
  149. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  150. }
  151. function getTranslatedUrls()
  152. {
  153. $ret = array();
  154. $langs = $this->parameterBag->get('app.locales');
  155. $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
  156. $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
  157. if ($currentRoute) {
  158. foreach ($langs as $lg) {
  159. $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
  160. }
  161. }
  162. return $ret;
  163. }
  164. public function getFileManagerFolder()
  165. {
  166. return $this->parameterBag->get('app.path_uploads');
  167. }
  168. public function homepageRoute()
  169. {
  170. return $this->parameterBag->get('lc_sov.homepage_route');
  171. }
  172. }