src/Twig/InfotwigExtension.php line 80

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use DateInterval;
  4. use Twig\TwigFilter;
  5. use Twig\TwigFunction;
  6. use App\Entity\CentreGroupe;
  7. use App\Entity\CentrePrestation;
  8. use App\Entity\ParametreCentreGroupe;
  9. use Twig\Extension\AbstractExtension;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Contracts\Cache\ItemInterface;
  12. use Symfony\Contracts\Cache\CacheInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. class InfotwigExtension extends AbstractExtension
  17. {
  18.     private $em;
  19.     private $cache;
  20.     private $request;
  21.   
  22.     public function __construct(EntityManagerInterface $em,CacheInterface $cache,RequestStack $request)
  23.     {
  24.         $this->em $em;
  25.         $this->cache $cache;
  26.         $this->request $request->getCurrentRequest();
  27.     }
  28.     public function getFilters(): array
  29.     {
  30.         return [
  31.             // If your filter generates SAFE HTML, you should add a third
  32.             // parameter: ['is_safe' => ['html']]
  33.             // Reference: https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping
  34.             new TwigFilter('filter_name', [$this'doSomething']),
  35.         ];
  36.     }
  37.     public function getFunctions(): array
  38.     {
  39.         return [
  40.             new TwigFunction('getInfoCentreGroupe', [$this'getInfoCentreGroupe']),
  41.             new TwigFunction('getPrestationCentreGroupe', [$this'getPrestationCentreGroupe']),
  42.             new TwigFunction('getParametreCentreGroupe', [$this'getParametreCentreGroupe']),
  43.         ];
  44.     }
  45.     public function getInfoCentreGroupe()
  46.     {
  47.         if (!$this->request->getSession()->get('centre_groupe')) {
  48.            $ParametreCentreGroupe $this->em->getRepository(ParametreCentreGroupe::class)->findOneByUrl($_SERVER['SERVER_NAME']);
  49.             $this->request->getSession()->set('centre_groupe',$ParametreCentreGroupe->getCentreGroupe()->getId());
  50.         }
  51.         
  52.         $CentreGroupe $this->cache->get('Centre_groupe_'.$this->request->getSession()->get('centre_groupe') , function (ItemInterface $item)  {
  53.             $item->expiresAfter(DateInterval::createFromDateString('12 hour'));
  54.             $CentreGroupe $this->em->getrepository(CentreGroupe::class)->find($this->request->getSession()->get('centre_groupe'));
  55.             return $CentreGroupe;
  56.         });
  57.        
  58.         
  59.         return $CentreGroupe;
  60.     }
  61.     public function getParametreCentreGroupe()
  62.     {
  63.         if (!$this->request->getSession()->get('centre_groupe')) {
  64.           $ParametreCentreGroupe $this->em->getRepository(ParametreCentreGroupe::class)->findOneByUrl($_SERVER['SERVER_NAME']);
  65.             $this->request->getSession()->set('centre_groupe'$ParametreCentreGroupe->getCentreGroupe()->getId());
  66.         }
  67.         $ParametreCentreGroupe $this->cache->get('ParametreCentre_groupe_'.$this->request->getSession()->get('centre_groupe') , function (ItemInterface $item)  {
  68.             $item->expiresAfter(DateInterval::createFromDateString('12 hour'));
  69.             $ParametreCentreGroupe $this->em->getrepository(ParametreCentreGroupe::class)->findOneBy(['centre_groupe'=>$this->request->getSession()->get('centre_groupe')]);
  70.             return $ParametreCentreGroupe;
  71.         });
  72.         
  73.         return $ParametreCentreGroupe;
  74.     }
  75.     public function getPrestationCentreGroupe()
  76.     {
  77.         if (!$this->request->getSession()->get('centre_groupe')) {
  78.           $ParametreCentreGroupe $this->em->getRepository(ParametreCentreGroupe::class)->findOneByUrl($_SERVER['SERVER_NAME']);
  79.             $this->request->getSession()->set('centre_groupe'$ParametreCentreGroupe->getCentreGroupe()->getId());
  80.         }
  81.         $Prestations $this->cache->get('CentrePrestation_centre_groupe_'.$this->request->getSession()->get('centre_groupe'), function (ItemInterface $item) {
  82.             $item->expiresAfter(DateInterval::createFromDateString('12 hour'));
  83.             $Prestations $this->em->getrepository(CentrePrestation::class)->findBy(
  84.                 array('centre_groupe' => $this->request->getSession()->get('centre_groupe'), 'pst_actif' => true'pst_afficher' => true),
  85.                 array('pst_ordre' => 'asc')
  86.             );
  87.             return $Prestations;
  88.         });
  89.         
  90.         return $Prestations;
  91.     }
  92. }