<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Doctrine\Persistence\ManagerRegistry;
class AppExtension extends AbstractExtension implements \Twig_Extension_GlobalsInterface
{
private $em;
private $formFactory;
private $session;
public function __construct(ManagerRegistry $registry, SessionInterface $session)
{
$this->em = $registry;
$this->session = $session;
}
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/2.x/advanced.html#automatic-escaping
new TwigFilter('filter_name', [$this, 'doSomething']),
];
}
public function getFunctions(): array
{
return [
new TwigFunction('function_name', [$this, 'doSomething']),
];
}
public function doSomething($value)
{
// ...
}
public function getGlobals()
{
$session['email'] = $this->session->get('email', null);
$session['token'] = $this->session->get('token', null);
$demoVersion = $this->em->getRepository('App:AccessDemo')->findOneBy(array('email' => $session['email'], 'token' => $session['token']));
$template = $this->em->getRepository('App:Template')->findActif()[0];
if ($demoVersion != null){
$globals = $this->em->getRepository('App:Infos')->find($demoVersion->getInfos());
$template->setColorPrime($demoVersion->getColorPrime());
$template->setColorSecond($demoVersion->getColorSecond());
$template->setColorThird($demoVersion->getColorThird());
$template->setTextColorPrime($demoVersion->getTextColorPrime());
$template->setTextColorSecond($demoVersion->getTextColorSecond());
}else{
$globals = $this->em->getRepository('App:Infos')->find(1);
$template = $this->em->getRepository('App:Template')->findActif()[0];
}
$actus = $this->em->getRepository('App:ActusConfig')->find(1);
$offres = $this->em->getRepository('App:OffresConfig')->find(1);
$offresPhotos = $this->em->getRepository('App:OffresPhotos')->findAll();
$natures = $this->em->getRepository('App:OffresNature')->findAll();
$types = $this->em->getRepository('App:OffresType')->findAll();
$localisations = $this->em->getRepository('App:Localisations')->findBy(array(), array('libelle' => 'ASC'));
$quiSommesNous = $this->em->getRepository('App:Pages')->find(1);
$mentionsLegales = $this->em->getRepository('App:Pages')->find(2);
return [
'global' => $globals,
'actuListeUrl' => $actus->getUrlListe(),
'actuConfig' => $actus,
'offreListeUrl' => $offres->getUrlListe(),
'offresConfig' => $offres,
'quiSommesNousUrl' => $quiSommesNous->getSeoUrl(),
'quiSommesNousTitre' => $quiSommesNous->getTitre(),
'mentionsLegalesUrl' => $mentionsLegales->getSeoUrl(),
'offresPhotos' => $offresPhotos,
'globalNatures' => $natures,
'globalTypes' => $types,
'globalLocalisations' => $localisations,
'template' => $template
];
}
}