src/Twig/CoreExtension.php line 31

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('GetJsonDecode', [$this'GetJsonDecode']),
  41.             new  TwigFunction('asset', [$this'asset']),
  42.             new  TwigFunction('file_exists', [$this'file_exists']),
  43.             new  TwigFunction('getFirstLetters', [$this'getFirstLetters']),
  44.             new  TwigFunction('cleanSubstr', [$this'cleanSubstr']),
  45.             new  TwigFunction('timeago', [$this'timeago']),
  46.             new  TwigFunction('cleanMail', [$this'cleanMail']),
  47.             new  TwigFunction('cleanRemoveText', [$this'cleanRemoveText']),
  48.             new  TwigFunction('getCoreTool', [$this'getCoreTool']),
  49.             new  TwigFunction('ctsToEur', [$this'ctsToEur']),
  50.             new  TwigFunction('getContractPartner', [$this'getContractPartner']),
  51.             new  TwigFunction('transformTextToDivs', [$this'transformTextToDivs'], ['is_safe' => ['html']]),
  52.             new  TwigFunction('getEnv', [$this'getEnv']),
  53.             new  TwigFunction('removehttps', [$this'removehttps']),
  54.             new  TwigFunction('getFooterArticles', [$this'getFooterArticles']),
  55.             new  TwigFunction('getFooterFeaturedLandingJobs', [$this'getFooterFeaturedLandingJobs']),
  56.             new  TwigFunction('getFooterJobsCities', [$this'getFooterJobsCities']),
  57.             new  TwigFunction('getFooterEnterprisesCities', [$this'getFooterEnterprisesCities']),
  58.         ];
  59.     }
  60.     public function getFilters()
  61.     {
  62.         return [
  63.             new TwigFilter('json_decode', [$this'GetJsonDecode']),
  64.             new TwigFilter('clean_n', [$this'clean_n']),
  65.             new TwigFilter('jour_francais', [$this'traduireJour']),
  66.             new TwigFilter('jour_court_francais', [$this'traduireJourCourt']),
  67.             new TwigFilter('parseMarkdown', [$this'parseMarkdown']),
  68.             new TwigFilter('parseMarkdownElearning', [$this'parseMarkdownElearning']),
  69.             new TwigFilter('parseMarkdownTag', [$this'parseMarkdownTag']),
  70.             new TwigFilter('strip_query', [$this'stripQuery']),
  71.         ];
  72.     }
  73.     public function getEnv($tag)
  74.     {
  75.         return $_ENV[$tag];
  76.     }
  77.     public function parseMarkdown(string $content): string
  78.     {
  79.         return $this->markdownParser->parse($content);
  80.     }
  81.     public function parseMarkdownElearning(string $content): string
  82.     {
  83.         return $this->markdownParser->parseElearning($content);
  84.     }
  85.     public function parseMarkdownTag(string $contentstring $interditKey): string
  86.     {
  87.         return $this->markdownParser->parseTag($content,$interditKey);
  88.     }
  89.     public function getCoreTool($toolID)
  90.     {
  91.         return  $this->tool->getTool($toolID);
  92.     }
  93.     public function cleanRemoveText($text,$html)
  94.     {
  95.         return $this->core->cleanRemoveText($text,$html);
  96.     }
  97.     public function cleanMail($html)
  98.     {
  99.         return $this->core->cleanMail($html);
  100.     }
  101.     public function timeago($datetime)
  102.     {
  103.         return $this->core->timeago($datetime);
  104.     }
  105.     public function cleanSubstr($chain,$number)
  106.     {
  107.         return $this->core->cleanSubstr($chain,$number);
  108.     }
  109.     public function getFirstLetters($chain)
  110.     {
  111.         return $this->core->getFirstLetters($chain);
  112.     }
  113.     public function clean_n($chain)
  114.     {
  115.         return $this->core->clean_n($chain);
  116.     }
  117.     /**
  118.      * Décoder un JSON en Array.
  119.      */
  120.     public function GetJsonDecode($chain)
  121.     {
  122.         return json_decode($chain,true);
  123.     }
  124.     public function asset($chain)
  125.     {
  126.         return $chain;
  127.     }
  128.     public function file_exists($filename)
  129.     {
  130.         return file_exists($filename);
  131.     }
  132.     public function ctsToEur($montant)
  133.     {
  134.         return $this->core->ctsToEur($montant);
  135.     }
  136.     public function getContractPartner($userID)
  137.     {
  138.         return $this->users->getContractPartner($userID);
  139.     }
  140.     public function traduireJour($date)
  141.     {
  142.         $jours = [
  143.             'Monday'    => 'Lundi',
  144.             'Tuesday'   => 'Mardi',
  145.             'Wednesday' => 'Mercredi',
  146.             'Thursday'  => 'Jeudi',
  147.             'Friday'    => 'Vendredi',
  148.             'Saturday'  => 'Samedi',
  149.             'Sunday'    => 'Dimanche',
  150.         ];
  151.         // Assurez-vous que $date est un objet \DateTime
  152.         if (!$date instanceof \DateTime) {
  153.             $date = new \DateTime($date);
  154.         }
  155.         $jourAnglais $date->format('l');
  156.         return $jours[$jourAnglais] ?? 'Jour inconnu';
  157.     }
  158.     public function traduireJourCourt($date)
  159.     {
  160.         $joursCourts = [
  161.             'Monday'    => 'Lun',
  162.             'Tuesday'   => 'Mar',
  163.             'Wednesday' => 'Mer',
  164.             'Thursday'  => 'Jeu',
  165.             'Friday'    => 'Ven',
  166.             'Saturday'  => 'Sam',
  167.             'Sunday'    => 'Dim',
  168.         ];
  169.         // Assurez-vous que $date est un objet \DateTime
  170.         if (!$date instanceof \DateTime) {
  171.             $date = new \DateTime($date);
  172.         }
  173.         $jourAnglais $date->format('l');
  174.         return $joursCourts[$jourAnglais] ?? 'Inconnu';
  175.     }
  176.     public function transformTextToDivs($text)
  177.     {
  178.         $labels explode(';'$text);
  179.         $output '';
  180.         $index 1;
  181.         foreach ($labels as $label) {
  182.             $label trim($label);
  183.             $output .= '<div class="variable-item" draggable="true" ondragstart="drag(event)" id="label' $index '">' htmlspecialchars($label) . '</div> ';
  184.             $index++;
  185.         }
  186.         return $output;
  187.     }
  188.     public function removehttps($domain)
  189.     {
  190.         $domain str_replace(['https://''http://'], ''$domain);
  191.         return $domain;
  192.     }
  193.     public function stripQuery(?string $url): string
  194.     {
  195.         if (empty($url)) {
  196.             return '';
  197.         }
  198.         return preg_replace('/[?#].*$/'''$url);
  199.     }
  200.     /**
  201.      * Retourne les articles featured (mis en avant) pour le footer.
  202.      * Utilise la méthode getLast() existante d'ArticlesRepository qui filtre déjà
  203.      * sur visibility=true, language=locale, featured=1, publishedAt NOT NULL.
  204.      *
  205.      * @return \App\Entity\Articles\Articles[]
  206.      */
  207.     public function getFooterArticles(string $localeint $limit 5): array
  208.     {
  209.         return $this->articlesRepository->getLast($locale$limit);
  210.     }
  211.     /**
  212.      * Retourne les landing jobs marquées "featured" pour le footer.
  213.      *
  214.      * @return \App\Entity\Cvs\LandingJobs[]
  215.      */
  216.     public function getFooterFeaturedLandingJobs(string $localeint $limit 5): array
  217.     {
  218.         return $this->landingJobsRepository->findFeaturedByLocale($locale$limit);
  219.     }
  220.     /**
  221.      * Retourne les filtres "city" actifs pour les jobs (pour le footer).
  222.      *
  223.      * @return \App\Entity\Cvs\JobsFilters[]
  224.      */
  225.     public function getFooterJobsCities(string $localeint $limit 6): array
  226.     {
  227.         return $this->jobsFiltersRepository->findActiveByLocaleAndType($locale'city'$limit);
  228.     }
  229.     /**
  230.      * Retourne les filtres "city" actifs pour les entreprises (pour le footer).
  231.      *
  232.      * @return \App\Entity\Cvs\EnterprisesFilters[]
  233.      */
  234.     public function getFooterEnterprisesCities(string $localeint $limit 6): array
  235.     {
  236.         return $this->enterprisesFiltersRepository->findActiveByLocaleAndType($locale'city'$limit);
  237.     }
  238. }