<?php
namespace App\Twig;
use App\Entity\ActualityCategory;
use App\Entity\DefinitionPage;
use App\Entity\Manuel;
use App\Entity\ManuelPage;
use App\Entity\Page;
use App\Entity\PublicationPage;
use App\Entity\Theme;
use App\Entity\ThemePublication;
use App\Entity\ThesePage;
use App\Repository\DefinitionPageRepository;
use App\Repository\ManuelPageRepository;
use App\Repository\PageRepository;
use App\Repository\PublicationPageRepository;
use App\Repository\ThemePublicationRepository;
use App\Repository\ThemeRepository;
use App\Repository\ThesePageRepository;
use Cofondateur\SocleTechniqueBundle\Twig\AppExtension as BaseAppExtension;
use Twig\TwigFunction;
use IntlDateFormatter;
class AppExtension extends BaseAppExtension
{
public function getFilters(): array
{
$result = parent::getFilters();
return $result;
}
public function getFunctions(): array
{
$result = parent::getFunctions();
$result[] = new TwigFunction('getThemes', array($this, 'getThemes'));
$result[] = new TwigFunction('getThemePublication', array($this, 'getThemePublication'));
$result[] = new TwigFunction('getPagesPublication', array($this, 'getPagesPublication'));
$result[] = new TwigFunction('getManuels', array($this, 'getManuels'));
$result[] = new TwigFunction('getCategoriesActu', array($this, 'getCategoriesActu'));
$result[] = new TwigFunction('formatDateMoisAnnee', array($this, 'formatDateMoisAnnee'));
$result[] = new TwigFunction('getFirstPageOfTheme', array($this, 'getFirstPageOfTheme'));
$result[] = new TwigFunction('getPublicationPage', array($this, 'getPublicationPage'));
$result[] = new TwigFunction('getThesePage', array($this, 'getThesePage'));
$result[] = new TwigFunction('getManuelPage', array($this, 'getManuelPage'));
$result[] = new TwigFunction('getDefinitionPage', array($this, 'getDefinitionPage'));
return $result;
}
public function getThemes()
{
/** @var ThemeRepository $repo */
$repo = $this->em->getRepository(Theme::class);
return $repo->findBy([], ['position' => 'ASC']);
}
public function getThemePublication()
{
/** @var ThemePublicationRepository $repo */
$repo = $this->em->getRepository(ThemePublication::class);
return $repo->findUnique();
}
public function getPublicationPage()
{
/** @var PublicationPageRepository $repo */
$repo = $this->em->getRepository(PublicationPage::class);
return $repo->findUnique();
}
public function getThesePage()
{
/** @var ThesePageRepository $repo */
$repo = $this->em->getRepository(ThesePage::class);
return $repo->findUnique();
}
public function getManuelPage()
{
/** @var ManuelPageRepository $repo */
$repo = $this->em->getRepository(ManuelPage::class);
return $repo->findUnique();
}
public function getDefinitionPage()
{
/** @var DefinitionPageRepository $repo */
$repo = $this->em->getRepository(DefinitionPage::class);
return $repo->findUnique();
}
public function getManuels()
{
/** @var ThemeRepository $repo */
$repo = $this->em->getRepository(Manuel::class);
return $repo->findBy(['display' => true], ['id' => 'DESC']);
}
public function getPagesPublication()
{
/** @var PageRepository $repo */
$repo = $this->em->getRepository(Page::class);
return $repo->findBy(['activate' => true, 'themePublication' => true], ['position' => 'ASC']);
}
public function getCategoriesActu()
{
/** @var ThemeRepository $repo */
$repo = $this->em->getRepository(ActualityCategory::class);
return $repo->findAll();
}
public function formatDateMoisAnnee(\DateTimeInterface $date)
{
$formatter = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE, null, null, 'MMMM yyyy');
return ucfirst($formatter->format($date));
}
// function to get the page with the lowest id of a theme in parameter
public function getFirstPageOfTheme(Theme $theme)
{
$repo = $this->em->getRepository(Page::class);
return $repo->findOneBy(['theme' => $theme, 'activate' => true], ['id' => 'ASC']);
}
}