Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

212 Zeilen
7.0KB

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