Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

230 rindas
7.5KB

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