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.

TwigExtension.php 7.7KB

3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. ];
  97. }
  98. public function die()
  99. {
  100. die();
  101. }
  102. public function getFilters()
  103. {
  104. return [
  105. new TwigFilter('uc_first', [$this, 'ucFirst']),
  106. new TwigFilter('format_price', [$this, 'formatPrice']),
  107. new TwigFilter('sov_cache', [$this, 'sovCache']),
  108. new TwigFilter('slugify', [$this, 'slugify']),
  109. new TwigFilter('md5', [$this, 'md5']),
  110. new TwigFilter('rot13', [$this, 'rot13']),
  111. ];
  112. }
  113. public function rot13(string $string): string
  114. {
  115. return str_rot13($string);
  116. }
  117. public function isInstanceOf(EntityInterface $entity, string $interfaceName)
  118. {
  119. $reflection = new \ReflectionClass($entity);
  120. $interfaceNameArray = $reflection->getInterfaceNames();
  121. foreach($interfaceNameArray as $interfaceNameEntity) {
  122. if(strpos($interfaceNameEntity, $interfaceName) !== false) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. public function sovCache($file)
  129. {
  130. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  131. if ($cacheTime) {
  132. return $file . '?c=' . $cacheTime;
  133. } else {
  134. return $file . "?c=0";
  135. }
  136. }
  137. public function formatPrice($price, $unbreakableSpace = true)
  138. {
  139. $price = number_format($price, 2, ',', ' ');
  140. $price .= $unbreakableSpace ? '&nbsp;' : ' ';
  141. $price .= '€';
  142. return $price;
  143. }
  144. public function ucFirst($string)
  145. {
  146. return ucfirst($string);
  147. }
  148. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  149. {
  150. return $this->fileComponent->liip($path, $thumb, $default);
  151. }
  152. public function getFileManagerFolder()
  153. {
  154. return $this->parameterBag->get('app.path_uploads');
  155. }
  156. public function getHomepageRoute()
  157. {
  158. return $this->parameterBag->get('lc_sov.homepage_route');
  159. }
  160. public function getParameter($name)
  161. {
  162. return $this->parameterBag->get($name);
  163. }
  164. public function getFormNewsletter()
  165. {
  166. $form = $this->formFactory->create(NewsletterType::class);
  167. return $form->createView();
  168. }
  169. public function limitText(string $text, int $limit)
  170. {
  171. return $this->stringComponent->limitText($text, $limit);
  172. }
  173. public function getDayByNumber($day): string
  174. {
  175. return $this->dateComponent->getDayByNumber($day);
  176. }
  177. public function slugify($string)
  178. {
  179. return $this->stringComponent->slugify($string);
  180. }
  181. public function getUserCurrent()
  182. {
  183. return $this->security->getUser();
  184. }
  185. public function generateEaUrl(string $controller =null, string $action=null, int $entityId=null, array $extraParam = array()): string
  186. {
  187. $adminUrlGenerator = $this->adminUrlGenerator;
  188. if ($controller) {
  189. $adminUrlGenerator->setController($controller);
  190. }
  191. if ($action) {
  192. $adminUrlGenerator->setAction($action);
  193. }
  194. $adminUrlGenerator->setEntityId($entityId);
  195. if($extraParam){
  196. foreach ($extraParam as $key=>$value) {
  197. $adminUrlGenerator->set($key, $value);
  198. }
  199. }
  200. return $adminUrlGenerator->generateUrl();
  201. }
  202. public function md5(string $text): string
  203. {
  204. return md5($text);
  205. }
  206. }