No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

TwigExtension.php 8.2KB

hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\Doctrine\EntityInterface;
  12. use Lc\SovBundle\Form\Newsletter\NewsletterType;
  13. use Lc\SovBundle\Model\File\FileInterface;
  14. use Lc\SovBundle\Repository\Reminder\ReminderStore;
  15. use Lc\SovBundle\Translation\TranslatorAdmin;
  16. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  17. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  18. use Symfony\Component\Form\FormFactoryInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\HttpKernel\KernelInterface;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Component\Security\Core\Security;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. use Twig\Extension\AbstractExtension;
  25. use Twig\TwigFilter;
  26. use Twig\TwigFunction;
  27. class TwigExtension extends AbstractExtension
  28. {
  29. protected EntityManagerInterface $em;
  30. protected KernelInterface $kernel;
  31. protected ParameterBagInterface $parameterBag;
  32. protected CacheManager $cacheManager;
  33. protected RequestStack $requestStack;
  34. protected UrlGeneratorInterface $router;
  35. protected TranslatorInterface $translator;
  36. protected TranslatorAdmin $translatorAdmin;
  37. protected ReminderStore $reminderStore;
  38. protected Security $security;
  39. protected FormFactoryInterface $formFactory;
  40. protected StringComponent $stringComponent;
  41. protected MetaComponent $metaComponent;
  42. protected DateComponent $dateComponent;
  43. protected FileComponent $fileComponent;
  44. protected AdminUrlGenerator $adminUrlGenerator;
  45. public function __construct(
  46. KernelInterface $kernel,
  47. ParameterBagInterface $parameterBag,
  48. CacheManager $cacheManager,
  49. EntityManagerInterface $entityManager,
  50. RequestStack $requestStack,
  51. UrlGeneratorInterface $router,
  52. TranslatorInterface $translator,
  53. TranslatorAdmin $translatorAdmin,
  54. ReminderStore $reminderStore,
  55. Security $security,
  56. FormFactoryInterface $formFactory,
  57. StringComponent $stringComponent,
  58. MetaComponent $metaComponent,
  59. DateComponent $dateComponent,
  60. FileComponent $fileComponent,
  61. AdminUrlGenerator $adminUrlGenerator
  62. ) {
  63. $this->kernel = $kernel;
  64. $this->parameterBag = $parameterBag;
  65. $this->cacheManager = $cacheManager;
  66. $this->em = $entityManager;
  67. $this->requestStack = $requestStack;
  68. $this->router = $router;
  69. $this->translator = $translator;
  70. $this->translatorAdmin = $translatorAdmin;
  71. $this->reminderStore = $reminderStore;
  72. $this->security = $security;
  73. $this->formFactory = $formFactory;
  74. $this->stringComponent = $stringComponent;
  75. $this->metaComponent = $metaComponent;
  76. $this->dateComponent = $dateComponent;
  77. $this->fileComponent = $fileComponent;
  78. $this->adminUrlGenerator = $adminUrlGenerator;
  79. }
  80. public function getFunctions()
  81. {
  82. return [
  83. new TwigFunction('sov_liip', [$this, 'liip']),
  84. new TwigFunction('liip', [$this, 'liip']),
  85. new TwigFunction('sov_get_by_devalias', [$this, 'getByDevAlias']),
  86. new TwigFunction('sov_parameter', [$this, 'getParameter']),
  87. new TwigFunction('sov_homepage_route', [$this, 'getHomepageRoute']),
  88. new TwigFunction('lc_format_price', [$this, 'formatPrice']),
  89. new TwigFunction('die', [$this, 'die']),
  90. new TwigFunction('get_form_newsletter', [$this, 'getFormNewsletter']),
  91. new TwigFunction('sov_limit_text', [$this, 'limitText']),
  92. new TwigFunction('day_by_number', [$this, 'getDayByNumber']),
  93. new TwigFunction('user_current', [$this, 'getUserCurrent']),
  94. new TwigFunction('ea_url_short', [$this, 'generateEaUrl']),
  95. new TwigFunction('is_instance_of', [$this, 'isInstanceOf']),
  96. new TwigFunction('logo_admin', [$this, 'getLogoAdmin']),
  97. new TwigFunction('asset_url', [$this, 'getAssetUrl']),
  98. ];
  99. }
  100. public function die()
  101. {
  102. die();
  103. }
  104. public function getFilters()
  105. {
  106. return [
  107. new TwigFilter('uc_first', [$this, 'ucFirst']),
  108. new TwigFilter('format_price', [$this, 'formatPrice']),
  109. new TwigFilter('sov_cache', [$this, 'sovCache']),
  110. new TwigFilter('slugify', [$this, 'slugify']),
  111. new TwigFilter('md5', [$this, 'md5']),
  112. new TwigFilter('rot13', [$this, 'rot13']),
  113. ];
  114. }
  115. public function getAssetUrl($path)
  116. {
  117. $context = $this->router->getContext();
  118. $host = $context->getScheme().'://'.$context->getHost().'/';
  119. return $host.$path;
  120. }
  121. public function rot13(string $string): string
  122. {
  123. return str_rot13($string);
  124. }
  125. public function isInstanceOf(EntityInterface $entity, string $interfaceName)
  126. {
  127. $reflection = new \ReflectionClass($entity);
  128. $interfaceNameArray = $reflection->getInterfaceNames();
  129. foreach ($interfaceNameArray as $interfaceNameEntity) {
  130. if (strpos($interfaceNameEntity, $interfaceName) !== false) {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. public function sovCache($file)
  137. {
  138. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  139. if ($cacheTime) {
  140. return $file . '?c=' . $cacheTime;
  141. } else {
  142. return $file . "?c=0";
  143. }
  144. }
  145. public function formatPrice($price, $unbreakableSpace = true)
  146. {
  147. $price = number_format($price, 2, ',', ' ');
  148. $price .= $unbreakableSpace ? '&nbsp;' : ' ';
  149. $price .= '€';
  150. return $price;
  151. }
  152. public function ucFirst($string)
  153. {
  154. return ucfirst($string);
  155. }
  156. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  157. {
  158. return $this->fileComponent->liip($path, $thumb, $default);
  159. }
  160. public function getFileManagerFolder()
  161. {
  162. return $this->parameterBag->get('app.path_uploads');
  163. }
  164. public function getHomepageRoute()
  165. {
  166. return $this->parameterBag->get('lc_sov.homepage_route');
  167. }
  168. public function getParameter($name)
  169. {
  170. return $this->parameterBag->get($name);
  171. }
  172. public function getFormNewsletter()
  173. {
  174. $form = $this->formFactory->create(NewsletterType::class);
  175. return $form->createView();
  176. }
  177. public function limitText(string $text, int $limit)
  178. {
  179. return $this->stringComponent->limitText($text, $limit);
  180. }
  181. public function getDayByNumber($day): string
  182. {
  183. return $this->dateComponent->getDayByNumber($day);
  184. }
  185. public function slugify($string)
  186. {
  187. return $this->stringComponent->slugify($string);
  188. }
  189. public function getUserCurrent()
  190. {
  191. return $this->security->getUser();
  192. }
  193. public function generateEaUrl(
  194. string $controller = null,
  195. string $action = null,
  196. int $entityId = null,
  197. array $extraParam = array()
  198. ): string {
  199. $adminUrlGenerator = $this->adminUrlGenerator;
  200. if ($controller) {
  201. $adminUrlGenerator->setController($controller);
  202. }
  203. if ($action) {
  204. $adminUrlGenerator->setAction($action);
  205. }
  206. $adminUrlGenerator->setEntityId($entityId);
  207. if ($extraParam) {
  208. foreach ($extraParam as $key => $value) {
  209. $adminUrlGenerator->set($key, $value);
  210. }
  211. }
  212. return $adminUrlGenerator->generateUrl();
  213. }
  214. public function md5(string $text): string
  215. {
  216. return md5($text);
  217. }
  218. public function getLogoAdmin(): string
  219. {
  220. $image = $this->getParameter('app.admin.logo_sidebar');
  221. if(!$image) {
  222. $image = $this->getParameter('app.admin.logo');
  223. }
  224. return '<img class="logo-admin" src="assets/img/' . $image . '" >';
  225. }
  226. }