Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

178 lines
5.8KB

  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Factory\Reminder\ReminderFactory;
  5. use Lc\SovBundle\Form\Reminder\ReminderAdminFormType;
  6. use Lc\SovBundle\Translation\TranslatorAdmin;
  7. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\Form\FormFactoryInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  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 $formFactory;
  28. protected $reminderFactory;
  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. FormFactoryInterface $formFactory,
  39. ReminderFactory $reminderFactory
  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->formFactory = $formFactory;
  50. $this->reminderFactory = $reminderFactory;
  51. }
  52. public function getFunctions()
  53. {
  54. return array(
  55. new TwigFunction('lc_liip', [$this, 'lcLiip']),
  56. new TwigFunction('homepage_route', [$this, 'homepageRoute']),
  57. new TwigFunction('page_by_dev_alias', [$this, 'getPageByDevAlias']),
  58. new TwigFunction('translated_urls', [$this, 'getTranslatedUrls']),
  59. new TwigFunction('sov_get_form_reminder', [$this, 'getFormReminder']),
  60. );
  61. }
  62. public function getFilters()
  63. {
  64. return [
  65. new TwigFilter('lc_cache', [$this, 'lcCache']),
  66. new TwigFilter('lc_trans_admin_field', [$this, 'lcTransAdminField']),
  67. new TwigFilter('lc_trans_admin_help', [$this, 'lcTransAdminHelp']),
  68. new TwigFilter('lc_trans_admin_panel', [$this, 'lcTransAdminPanel']),
  69. new TwigFilter('lc_trans_admin_menu', [$this, 'lcTransAdminMenu']),
  70. new TwigFilter('lc_trans_admin_title', [$this, 'lcTransAdminTitle']),
  71. new TwigFilter('lc_trans_admin_action', [$this, 'lcTransAdminAction']),
  72. ];
  73. }
  74. public function lcCache($file)
  75. {
  76. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  77. if ($cacheTime) {
  78. return $file . '?c=' . $cacheTime;
  79. } else {
  80. return $file . "?c=0";
  81. }
  82. }
  83. public function lcTransAdminField($fieldName, $entityClass)
  84. {
  85. return $this->translatorAdmin->transField($fieldName, $entityClass);
  86. }
  87. public function lcTransAdminHelp($fieldName, $entityClass)
  88. {
  89. return $this->translatorAdmin->transHelp($fieldName, $entityClass);
  90. }
  91. public function lcTransAdminPanel($panelName, $entityClass)
  92. {
  93. return $this->translatorAdmin->transPanel($panelName, $entityClass);
  94. }
  95. public function lcTransAdminMenu($menuName)
  96. {
  97. return $this->translatorAdmin->transMenu($menuName);;
  98. }
  99. public function lcTransAdminAction($actionName)
  100. {
  101. return $this->translatorAdmin->transAction($actionName);;
  102. }
  103. public function lcTransAdminTitle($pageName, $entityClass = null, $params = [])
  104. {
  105. return $this->translatorAdmin->transTitle($pageName, $entityClass, $params);
  106. }
  107. public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
  108. {
  109. if (substr($path, 0, 1) === '/') {
  110. $path = substr($path, 1);
  111. }
  112. if ($path) {
  113. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  114. if (strpos($path, $fileManagerFolder) === false) {
  115. $path = $fileManagerFolder . '/' . $path;
  116. }
  117. if (file_exists($path)) {
  118. return $this->cacheManager->getBrowserPath($path, $thumb);
  119. }
  120. }
  121. return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  122. }
  123. function getTranslatedUrls()
  124. {
  125. $ret = array();
  126. $langs = $this->parameterBag->get('app.locales');
  127. $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
  128. $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
  129. if ($currentRoute) {
  130. foreach ($langs as $lg) {
  131. $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
  132. }
  133. }
  134. return $ret;
  135. }
  136. public function getFileManagerFolder()
  137. {
  138. return $this->parameterBag->get('app.path_uploads');
  139. }
  140. public function homepageRoute()
  141. {
  142. return $this->parameterBag->get('lc_sov.homepage_route');
  143. }
  144. public function getFormReminder()
  145. {
  146. $reminder = $this->reminderFactory->create();
  147. $formReminder = $this->formFactory->create(ReminderAdminFormType::class, $reminder);
  148. return $formReminder->createView();
  149. }
  150. }