src/Twig/CoreExtension.php line 181

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Services\Core\Core;
  4. use App\Services\Core\Tools;
  5. use App\Services\Core\Users;
  6. use App\Services\Cvs;
  7. use App\Services\MarkdownParserService;
  8. use App\Repository\Articles\ArticlesRepository;
  9. use App\Repository\Cvs\LandingJobsRepository;
  10. use App\Repository\Cvs\JobsFiltersRepository;
  11. use App\Repository\Cvs\EnterprisesFiltersRepository;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFilter;
  14. use Twig\TwigFunction;
  15. class CoreExtension extends AbstractExtension
  16. {
  17. public function __construct(Core $coreService,
  18. Tools $toolService,
  19. Users $usersService,
  20. Cvs $cvsService,
  21. MarkdownParserService $markdownParser,
  22. ArticlesRepository $articlesRepository,
  23. LandingJobsRepository $landingJobsRepository,
  24. JobsFiltersRepository $jobsFiltersRepository,
  25. EnterprisesFiltersRepository $enterprisesFiltersRepository
  26. ) {
  27. $this->core = $coreService;
  28. $this->tool = $toolService;
  29. $this->users = $usersService;
  30. $this->cvs = $cvsService;
  31. $this->markdownParser = $markdownParser;
  32. $this->articlesRepository = $articlesRepository;
  33. $this->landingJobsRepository = $landingJobsRepository;
  34. $this->jobsFiltersRepository = $jobsFiltersRepository;
  35. $this->enterprisesFiltersRepository = $enterprisesFiltersRepository;
  36. }
  37. public function getFunctions(): array
  38. {
  39. return [
  40. new TwigFunction('asset', [$this, 'asset']),
  41. new TwigFunction('file_exists', [$this, 'file_exists']),
  42. new TwigFunction('cleanSubstr', [$this, 'cleanSubstr']),
  43. new TwigFunction('getCoreTool', [$this, 'getCoreTool']),
  44. new TwigFunction('ctsToEur', [$this, 'ctsToEur']),
  45. new TwigFunction('transformTextToDivs', [$this, 'transformTextToDivs'], ['is_safe' => ['html']]),
  46. new TwigFunction('getEnv', [$this, 'getEnv']),
  47. new TwigFunction('removehttps', [$this, 'removehttps']),
  48. new TwigFunction('getFooterArticles', [$this, 'getFooterArticles']),
  49. new TwigFunction('getFooterFeaturedLandingJobs', [$this, 'getFooterFeaturedLandingJobs']),
  50. new TwigFunction('getFooterJobsCities', [$this, 'getFooterJobsCities']),
  51. new TwigFunction('getFooterEnterprisesCities', [$this, 'getFooterEnterprisesCities']),
  52. ];
  53. }
  54. public function getFilters()
  55. {
  56. return [
  57. new TwigFilter('json_decode', [$this, 'GetJsonDecode']),
  58. new TwigFilter('parseMarkdown', [$this, 'parseMarkdown']),
  59. new TwigFilter('parseMarkdownTag', [$this, 'parseMarkdownTag']),
  60. new TwigFilter('strip_query', [$this, 'stripQuery']),
  61. ];
  62. }
  63. public function getEnv($tag)
  64. {
  65. return $_ENV[$tag];
  66. }
  67. public function parseMarkdown(string $content): string
  68. {
  69. return $this->markdownParser->parse($content);
  70. }
  71. public function GetJsonDecode($chain)
  72. {
  73. return json_decode($chain,true);
  74. }
  75. public function parseMarkdownTag(string $content, string $interditKey): string
  76. {
  77. return $this->markdownParser->parseTag($content,$interditKey);
  78. }
  79. public function getCoreTool($toolID)
  80. {
  81. return $this->tool->getTool($toolID);
  82. }
  83. public function cleanSubstr($chain,$number)
  84. {
  85. return $this->core->cleanSubstr($chain,$number);
  86. }
  87. public function asset($chain)
  88. {
  89. return $chain;
  90. }
  91. public function file_exists($filename)
  92. {
  93. return file_exists($filename);
  94. }
  95. public function ctsToEur($montant)
  96. {
  97. return $this->core->ctsToEur($montant);
  98. }
  99. public function transformTextToDivs($text)
  100. {
  101. $labels = explode(';', $text);
  102. $output = '';
  103. $index = 1;
  104. foreach ($labels as $label) {
  105. $label = trim($label);
  106. $output .= '<div class="variable-item" draggable="true" ondragstart="drag(event)" id="label' . $index . '">' . htmlspecialchars($label) . '</div> ';
  107. $index++;
  108. }
  109. return $output;
  110. }
  111. public function removehttps($domain)
  112. {
  113. $domain = str_replace(['https://', 'http://'], '', $domain);
  114. return $domain;
  115. }
  116. public function stripQuery(?string $url): string
  117. {
  118. if (empty($url)) {
  119. return '';
  120. }
  121. return preg_replace('/[?#].*$/', '', $url);
  122. }
  123. /**
  124. * Retourne les articles featured (mis en avant) pour le footer.
  125. * Utilise la méthode getLast() existante d'ArticlesRepository qui filtre déjà
  126. * sur visibility=true, language=locale, featured=1, publishedAt NOT NULL.
  127. *
  128. * @return \App\Entity\Articles\Articles[]
  129. */
  130. public function getFooterArticles(string $locale, int $limit = 5): array
  131. {
  132. return $this->articlesRepository->getLast($locale, $limit);
  133. }
  134. /**
  135. * Retourne les landing jobs marquées "featured" pour le footer.
  136. *
  137. * @return \App\Entity\Cvs\LandingJobs[]
  138. */
  139. public function getFooterFeaturedLandingJobs(string $locale, int $limit = 5): array
  140. {
  141. return $this->landingJobsRepository->findFeaturedByLocale($locale, $limit);
  142. }
  143. /**
  144. * Retourne les filtres "city" actifs pour les jobs (pour le footer).
  145. *
  146. * @return \App\Entity\Cvs\JobsFilters[]
  147. */
  148. public function getFooterJobsCities(string $locale, int $limit = 6): array
  149. {
  150. return $this->jobsFiltersRepository->findActiveByLocaleAndType($locale, 'city', $limit);
  151. }
  152. /**
  153. * Retourne les filtres "city" actifs pour les entreprises (pour le footer).
  154. *
  155. * @return \App\Entity\Cvs\EnterprisesFilters[]
  156. */
  157. public function getFooterEnterprisesCities(string $locale, int $limit = 6): array
  158. {
  159. return $this->enterprisesFiltersRepository->findActiveByLocaleAndType($locale, 'city', $limit);
  160. }
  161. }