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.

TwigExtension.php 7.0KB

3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Lc\SovBundle\Twig;
  3. use App\Controller\Ticket\TicketAdminController;
  4. use App\Repository\ReminderRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  7. use Lc\SovBundle\Component\DateComponent;
  8. use Lc\SovBundle\Component\FileComponent;
  9. use Lc\SovBundle\Component\MetaComponent;
  10. use Lc\SovBundle\Component\StringComponent;
  11. use Lc\SovBundle\Form\Newsletter\NewsletterType;
  12. use Lc\SovBundle\Model\File\FileInterface;
  13. use Lc\SovBundle\Repository\Reminder\ReminderStore;
  14. use Lc\SovBundle\Translation\TranslatorAdmin;
  15. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. use Symfony\Component\Form\FormFactoryInterface;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. use Symfony\Component\Security\Core\Security;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. use Twig\Extension\AbstractExtension;
  24. use Twig\TwigFilter;
  25. use Twig\TwigFunction;
  26. class TwigExtension extends AbstractExtension
  27. {
  28. protected EntityManagerInterface $em;
  29. protected KernelInterface $kernel;
  30. protected ParameterBagInterface $parameterBag;
  31. protected CacheManager $cacheManager;
  32. protected RequestStack $requestStack;
  33. protected UrlGeneratorInterface $router;
  34. protected TranslatorInterface $translator;
  35. protected TranslatorAdmin $translatorAdmin;
  36. protected ReminderStore $reminderStore;
  37. protected Security $security;
  38. protected FormFactoryInterface $formFactory;
  39. protected StringComponent $stringComponent;
  40. protected MetaComponent $metaComponent;
  41. protected DateComponent $dateComponent;
  42. protected FileComponent $fileComponent;
  43. protected AdminUrlGenerator $adminUrlGenerator;
  44. public function __construct(
  45. KernelInterface $kernel,
  46. ParameterBagInterface $parameterBag,
  47. CacheManager $cacheManager,
  48. EntityManagerInterface $entityManager,
  49. RequestStack $requestStack,
  50. UrlGeneratorInterface $router,
  51. TranslatorInterface $translator,
  52. TranslatorAdmin $translatorAdmin,
  53. ReminderStore $reminderStore,
  54. Security $security,
  55. FormFactoryInterface $formFactory,
  56. StringComponent $stringComponent,
  57. MetaComponent $metaComponent,
  58. DateComponent $dateComponent,
  59. FileComponent $fileComponent,
  60. AdminUrlGenerator $adminUrlGenerator
  61. ) {
  62. $this->kernel = $kernel;
  63. $this->parameterBag = $parameterBag;
  64. $this->cacheManager = $cacheManager;
  65. $this->em = $entityManager;
  66. $this->requestStack = $requestStack;
  67. $this->router = $router;
  68. $this->translator = $translator;
  69. $this->translatorAdmin = $translatorAdmin;
  70. $this->reminderStore = $reminderStore;
  71. $this->security = $security;
  72. $this->formFactory = $formFactory;
  73. $this->stringComponent = $stringComponent;
  74. $this->metaComponent = $metaComponent;
  75. $this->dateComponent = $dateComponent;
  76. $this->fileComponent = $fileComponent;
  77. $this->adminUrlGenerator = $adminUrlGenerator;
  78. }
  79. public function getFunctions()
  80. {
  81. return [
  82. new TwigFunction('sov_liip', [$this, 'liip']),
  83. new TwigFunction('liip', [$this, 'liip']),
  84. new TwigFunction('sov_get_by_devalias', [$this, 'getByDevAlias']),
  85. new TwigFunction('sov_parameter', [$this, 'getParameter']),
  86. new TwigFunction('sov_homepage_route', [$this, 'getHomepageRoute']),
  87. new TwigFunction('lc_format_price', [$this, 'formatPrice']),
  88. new TwigFunction('die', [$this, 'die']),
  89. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  90. new TwigFunction('sov_limit_text', [$this, 'limitText']),
  91. new TwigFunction('day_by_number', [$this, 'getDayByNumber']),
  92. new TwigFunction('user_current', [$this, 'getUserCurrent']),
  93. new TwigFunction('ea_url_short', [$this, 'generateEaUrl']),
  94. ];
  95. }
  96. public function die()
  97. {
  98. die();
  99. }
  100. public function getFilters()
  101. {
  102. return [
  103. new TwigFilter('uc_first', [$this, 'ucFirst']),
  104. new TwigFilter('format_price', [$this, 'formatPrice']),
  105. new TwigFilter('sov_cache', [$this, 'sovCache']),
  106. new TwigFilter('slugify', [$this, 'slugify']),
  107. new TwigFilter('md5', [$this, 'md5']),
  108. ];
  109. }
  110. public function sovCache($file)
  111. {
  112. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  113. if ($cacheTime) {
  114. return $file . '?c=' . $cacheTime;
  115. } else {
  116. return $file . "?c=0";
  117. }
  118. }
  119. public function formatPrice($price, $unbreakableSpace = true)
  120. {
  121. $price = number_format($price, 2, ',', ' ');
  122. $price .= $unbreakableSpace ? '&nbsp;' : ' ';
  123. $price .= '€';
  124. return $price;
  125. }
  126. public function ucFirst($string)
  127. {
  128. return ucfirst($string);
  129. }
  130. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  131. {
  132. return $this->fileComponent->liip($path, $thumb, $default);
  133. }
  134. public function getFileManagerFolder()
  135. {
  136. return $this->parameterBag->get('app.path_uploads');
  137. }
  138. public function getHomepageRoute()
  139. {
  140. return $this->parameterBag->get('lc_sov.homepage_route');
  141. }
  142. public function getParameter($name)
  143. {
  144. return $this->parameterBag->get($name);
  145. }
  146. public function getFormNewsletter()
  147. {
  148. $form = $this->formFactory->create(NewsletterType::class);
  149. return $form->createView();
  150. }
  151. public function limitText(string $text, int $limit)
  152. {
  153. return $this->stringComponent->limitText($text, $limit);
  154. }
  155. public function getDayByNumber($day): string
  156. {
  157. return $this->dateComponent->getDayByNumber($day);
  158. }
  159. public function slugify($string)
  160. {
  161. return $this->stringComponent->slugify($string);
  162. }
  163. public function getUserCurrent()
  164. {
  165. return $this->security->getUser();
  166. }
  167. public function generateEaUrl(string $controller =null, string $action=null, int $entityId=null, array $extraParam = array()): string
  168. {
  169. $adminUrlGenerator = $this->adminUrlGenerator;
  170. if ($controller) {
  171. $adminUrlGenerator->setController($controller);
  172. }
  173. if ($action) {
  174. $adminUrlGenerator->setAction($action);
  175. }
  176. $adminUrlGenerator->setEntityId($entityId);
  177. if($extraParam){
  178. foreach ($extraParam as $key=>$value) {
  179. $adminUrlGenerator->set($key, $value);
  180. }
  181. }
  182. return $adminUrlGenerator->generateUrl();
  183. }
  184. public function md5(string $text): string
  185. {
  186. return md5($text);
  187. }
  188. }