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.

246 satır
7.8KB

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