<?php
namespace App\Twig;
use DateInterval;
use Twig\TwigFilter;
use Twig\TwigFunction;
use App\Entity\CentreGroupe;
use App\Entity\CentrePrestation;
use App\Entity\ParametreCentreGroupe;
use Twig\Extension\AbstractExtension;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\RedirectResponse;
class InfotwigExtension extends AbstractExtension
{
private $em;
private $cache;
private $request;
public function __construct(EntityManagerInterface $em,CacheInterface $cache,RequestStack $request)
{
$this->em = $em;
$this->cache = $cache;
$this->request = $request->getCurrentRequest();
}
public function getFilters(): array
{
return [
// If your filter generates SAFE HTML, you should add a third
// parameter: ['is_safe' => ['html']]
// Reference: https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping
new TwigFilter('filter_name', [$this, 'doSomething']),
];
}
public function getFunctions(): array
{
return [
new TwigFunction('getInfoCentreGroupe', [$this, 'getInfoCentreGroupe']),
new TwigFunction('getPrestationCentreGroupe', [$this, 'getPrestationCentreGroupe']),
new TwigFunction('getParametreCentreGroupe', [$this, 'getParametreCentreGroupe']),
];
}
public function getInfoCentreGroupe()
{
if (!$this->request->getSession()->get('centre_groupe')) {
$ParametreCentreGroupe = $this->em->getRepository(ParametreCentreGroupe::class)->findOneByUrl($_SERVER['SERVER_NAME']);
$this->request->getSession()->set('centre_groupe',$ParametreCentreGroupe->getCentreGroupe()->getId());
}
$CentreGroupe = $this->cache->get('Centre_groupe_'.$this->request->getSession()->get('centre_groupe') , function (ItemInterface $item) {
$item->expiresAfter(DateInterval::createFromDateString('12 hour'));
$CentreGroupe = $this->em->getrepository(CentreGroupe::class)->find($this->request->getSession()->get('centre_groupe'));
return $CentreGroupe;
});
return $CentreGroupe;
}
public function getParametreCentreGroupe()
{
if (!$this->request->getSession()->get('centre_groupe')) {
$ParametreCentreGroupe = $this->em->getRepository(ParametreCentreGroupe::class)->findOneByUrl($_SERVER['SERVER_NAME']);
$this->request->getSession()->set('centre_groupe', $ParametreCentreGroupe->getCentreGroupe()->getId());
}
$ParametreCentreGroupe = $this->cache->get('ParametreCentre_groupe_'.$this->request->getSession()->get('centre_groupe') , function (ItemInterface $item) {
$item->expiresAfter(DateInterval::createFromDateString('12 hour'));
$ParametreCentreGroupe = $this->em->getrepository(ParametreCentreGroupe::class)->findOneBy(['centre_groupe'=>$this->request->getSession()->get('centre_groupe')]);
return $ParametreCentreGroupe;
});
return $ParametreCentreGroupe;
}
public function getPrestationCentreGroupe()
{
if (!$this->request->getSession()->get('centre_groupe')) {
$ParametreCentreGroupe = $this->em->getRepository(ParametreCentreGroupe::class)->findOneByUrl($_SERVER['SERVER_NAME']);
$this->request->getSession()->set('centre_groupe', $ParametreCentreGroupe->getCentreGroupe()->getId());
}
$Prestations = $this->cache->get('CentrePrestation_centre_groupe_'.$this->request->getSession()->get('centre_groupe'), function (ItemInterface $item) {
$item->expiresAfter(DateInterval::createFromDateString('12 hour'));
$Prestations = $this->em->getrepository(CentrePrestation::class)->findBy(
array('centre_groupe' => $this->request->getSession()->get('centre_groupe'), 'pst_actif' => true, 'pst_afficher' => true),
array('pst_ordre' => 'asc')
);
return $Prestations;
});
return $Prestations;
}
}