src/Twig/CoreExtension.php line 27

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 Twig\Extension\AbstractExtension;
  9. use Twig\TwigFilter;
  10. use Twig\TwigFunction;
  11. class CoreExtension extends AbstractExtension
  12. {
  13.     public function __construct(Core $coreService,
  14.                                 Tools $toolService,
  15.                                 Users $usersService,
  16.                                 Cvs $cvsService,
  17.                                 MarkdownParserService $markdownParser
  18.     ) {
  19.         $this->core $coreService;
  20.         $this->tool $toolService;
  21.         $this->users $usersService;
  22.         $this->cvs $cvsService;
  23.         $this->markdownParser $markdownParser;
  24.     }
  25.     public function getFunctions(): array
  26.     {
  27.         return [
  28.             new  TwigFunction('GetJsonDecode', [$this'GetJsonDecode']),
  29.             new  TwigFunction('asset', [$this'asset']),
  30.             new  TwigFunction('file_exists', [$this'file_exists']),
  31.             new  TwigFunction('getFirstLetters', [$this'getFirstLetters']),
  32.             new  TwigFunction('cleanSubstr', [$this'cleanSubstr']),
  33.             new  TwigFunction('timeago', [$this'timeago']),
  34.             new  TwigFunction('cleanMail', [$this'cleanMail']),
  35.             new  TwigFunction('cleanRemoveText', [$this'cleanRemoveText']),
  36.             new  TwigFunction('getCoreTool', [$this'getCoreTool']),
  37.             new  TwigFunction('ctsToEur', [$this'ctsToEur']),
  38.             new  TwigFunction('getContractPartner', [$this'getContractPartner']),
  39.             new  TwigFunction('transformTextToDivs', [$this'transformTextToDivs'], ['is_safe' => ['html']]),
  40.             new  TwigFunction('getEnv', [$this'getEnv']),
  41.             new  TwigFunction('removehttps', [$this'removehttps'])
  42.         ];
  43.     }
  44.     public function getFilters()
  45.     {
  46.         return [
  47.             new TwigFilter('json_decode', [$this'GetJsonDecode']),
  48.             new TwigFilter('clean_n', [$this'clean_n']),
  49.             new TwigFilter('jour_francais', [$this'traduireJour']),
  50.             new TwigFilter('jour_court_francais', [$this'traduireJourCourt']),
  51.             new TwigFilter('parseMarkdown', [$this'parseMarkdown']),
  52.             new TwigFilter('parseMarkdownElearning', [$this'parseMarkdownElearning']),
  53.             new TwigFilter('parseMarkdownTag', [$this'parseMarkdownTag']),
  54.         ];
  55.     }
  56.     public function getEnv($tag)
  57.     {
  58.         return $_ENV[$tag];
  59.     }
  60.     public function parseMarkdown(string $content): string
  61.     {
  62.         return $this->markdownParser->parse($content);
  63.     }
  64.     public function parseMarkdownElearning(string $content): string
  65.     {
  66.         return $this->markdownParser->parseElearning($content);
  67.     }
  68.     public function parseMarkdownTag(string $contentstring $interditKey): string
  69.     {
  70.         return $this->markdownParser->parseTag($content,$interditKey);
  71.     }
  72.     public function getCoreTool($toolID)
  73.     {
  74.         return  $this->tool->getTool($toolID);
  75.     }
  76.     public function cleanRemoveText($text,$html)
  77.     {
  78.         return $this->core->cleanRemoveText($text,$html);
  79.     }
  80.     public function cleanMail($html)
  81.     {
  82.         return $this->core->cleanMail($html);
  83.     }
  84.     public function timeago($datetime)
  85.     {
  86.         return $this->core->timeago($datetime);
  87.     }
  88.     public function cleanSubstr($chain,$number)
  89.     {
  90.         return $this->core->cleanSubstr($chain,$number);
  91.     }
  92.     public function getFirstLetters($chain)
  93.     {
  94.         return $this->core->getFirstLetters($chain);
  95.     }
  96.     public function clean_n($chain)
  97.     {
  98.         return $this->core->clean_n($chain);
  99.     }
  100.     /**
  101.      * Décoder un JSON en Array.
  102.      */
  103.     public function GetJsonDecode($chain)
  104.     {
  105.         return json_decode($chain,true);
  106.     }
  107.     public function asset($chain)
  108.     {
  109.         return $chain;
  110.     }
  111.     public function file_exists($filename)
  112.     {
  113.         return file_exists($filename);
  114.     }
  115.     public function ctsToEur($montant)
  116.     {
  117.         return $this->core->ctsToEur($montant);
  118.     }
  119.     public function getContractPartner($userID)
  120.     {
  121.         return $this->users->getContractPartner($userID);
  122.     }
  123.     public function traduireJour($date)
  124.     {
  125.         $jours = [
  126.             'Monday'    => 'Lundi',
  127.             'Tuesday'   => 'Mardi',
  128.             'Wednesday' => 'Mercredi',
  129.             'Thursday'  => 'Jeudi',
  130.             'Friday'    => 'Vendredi',
  131.             'Saturday'  => 'Samedi',
  132.             'Sunday'    => 'Dimanche',
  133.         ];
  134.         // Assurez-vous que $date est un objet \DateTime
  135.         if (!$date instanceof \DateTime) {
  136.             $date = new \DateTime($date);
  137.         }
  138.         $jourAnglais $date->format('l');
  139.         return $jours[$jourAnglais] ?? 'Jour inconnu';
  140.     }
  141.     public function traduireJourCourt($date)
  142.     {
  143.         $joursCourts = [
  144.             'Monday'    => 'Lun',
  145.             'Tuesday'   => 'Mar',
  146.             'Wednesday' => 'Mer',
  147.             'Thursday'  => 'Jeu',
  148.             'Friday'    => 'Ven',
  149.             'Saturday'  => 'Sam',
  150.             'Sunday'    => 'Dim',
  151.         ];
  152.         // Assurez-vous que $date est un objet \DateTime
  153.         if (!$date instanceof \DateTime) {
  154.             $date = new \DateTime($date);
  155.         }
  156.         $jourAnglais $date->format('l');
  157.         return $joursCourts[$jourAnglais] ?? 'Inconnu';
  158.     }
  159.     public function transformTextToDivs($text)
  160.     {
  161.         $labels explode(';'$text);
  162.         $output '';
  163.         $index 1;
  164.         foreach ($labels as $label) {
  165.             $label trim($label);
  166.             $output .= '<div class="variable-item" draggable="true" ondragstart="drag(event)" id="label' $index '">' htmlspecialchars($label) . '</div> ';
  167.             $index++;
  168.         }
  169.         return $output;
  170.     }
  171.     public function removehttps($domain)
  172.     {
  173.         $domain str_replace(['https://''http://'], ''$domain);
  174.         return $domain;
  175.     }
  176. }