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.

257 lines
8.1KB

  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. return $this->fileComponent->getAssetUrl($path);
  118. }
  119. public function rot13(string $string): string
  120. {
  121. return str_rot13($string);
  122. }
  123. public function isInstanceOf(EntityInterface $entity, string $interfaceName)
  124. {
  125. $reflection = new \ReflectionClass($entity);
  126. $interfaceNameArray = $reflection->getInterfaceNames();
  127. foreach ($interfaceNameArray as $interfaceNameEntity) {
  128. if (strpos($interfaceNameEntity, $interfaceName) !== false) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. public function sovCache($file)
  135. {
  136. $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
  137. if ($cacheTime) {
  138. return $file . '?c=' . $cacheTime;
  139. } else {
  140. return $file . "?c=0";
  141. }
  142. }
  143. public function formatPrice($price, $unbreakableSpace = true)
  144. {
  145. $price = number_format($price, 2, ',', ' ');
  146. $price .= $unbreakableSpace ? '&nbsp;' : ' ';
  147. $price .= '€';
  148. return $price;
  149. }
  150. public function ucFirst($string)
  151. {
  152. return ucfirst($string);
  153. }
  154. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  155. {
  156. return $this->fileComponent->liip($path, $thumb, $default);
  157. }
  158. public function getFileManagerFolder()
  159. {
  160. return $this->parameterBag->get('app.path_uploads');
  161. }
  162. public function getHomepageRoute()
  163. {
  164. return $this->parameterBag->get('lc_sov.homepage_route');
  165. }
  166. public function getParameter($name)
  167. {
  168. return $this->parameterBag->get($name);
  169. }
  170. public function getFormNewsletter()
  171. {
  172. $form = $this->formFactory->create(NewsletterType::class);
  173. return $form->createView();
  174. }
  175. public function limitText(string $text, int $limit)
  176. {
  177. return $this->stringComponent->limitText($text, $limit);
  178. }
  179. public function getDayByNumber($day): string
  180. {
  181. return $this->dateComponent->getDayByNumber($day);
  182. }
  183. public function slugify($string)
  184. {
  185. return $this->stringComponent->slugify($string);
  186. }
  187. public function getUserCurrent()
  188. {
  189. return $this->security->getUser();
  190. }
  191. public function generateEaUrl(
  192. string $controller = null,
  193. string $action = null,
  194. int $entityId = null,
  195. array $extraParam = array()
  196. ): string {
  197. $adminUrlGenerator = $this->adminUrlGenerator;
  198. if ($controller) {
  199. $adminUrlGenerator->setController($controller);
  200. }
  201. if ($action) {
  202. $adminUrlGenerator->setAction($action);
  203. }
  204. $adminUrlGenerator->setEntityId($entityId);
  205. if ($extraParam) {
  206. foreach ($extraParam as $key => $value) {
  207. $adminUrlGenerator->set($key, $value);
  208. }
  209. }
  210. return $adminUrlGenerator->generateUrl();
  211. }
  212. public function md5(string $text): string
  213. {
  214. return md5($text);
  215. }
  216. public function getLogoAdmin(): string
  217. {
  218. $image = $this->getParameter('app.admin.logo_sidebar');
  219. if(!$image) {
  220. $image = $this->getParameter('app.admin.logo');
  221. }
  222. return '<img class="logo-admin" src="assets/img/' . $image . '" >';
  223. }
  224. }