src/EventSubscriber/TwigEventSubscriber.php line 133

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Croqvacances\CookieConsentBundle\Storage\CookieConsentSessionStorage;
  4. use App\Repository\FrontOptionRepository;
  5. use App\Repository\ArticleRepository;
  6. use App\Repository\SeoRepository;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Twig\Environment;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use App\Form\MainFormType;
  13. use App\Repository\AgeRepository;
  14. use App\Repository\CountryRepository;
  15. use App\Repository\SaisonRepository;
  16. use App\Repository\SejourRepository;
  17. use App\Repository\ThemeRepository;
  18. use App\Repository\NotificationRepository;
  19. use App\Repository\MediaRepository;
  20. use App\Repository\MediaCategorieRepository;
  21. use App\Entity\Notification;
  22. use App\Repository\VilleDesservieRepository;
  23. use App\Croqvacances\CookieConsentBundle\Repository\Cook2Repository;
  24. use App\Repository\SejourSessionRepository;
  25. class TwigEventSubscriber implements EventSubscriberInterface
  26. {
  27. private $twig;
  28. private $frontOptionRepository;
  29. private $articleRepository;
  30. private $saisonRepository;
  31. private $seoRepository;
  32. private $sejourRepository;
  33. private $sejourSessionRepository;
  34. private $countryRepository;
  35. private $container;
  36. private $ageRepository;
  37. private $themeRepository;
  38. private $villeDesservieRepository;
  39. private $formFactory;
  40. private $cook2Repo;
  41. private $notificationRepository;
  42. private $mediaRepository;
  43. private $mediaCategoryRepository;
  44. public function __construct(
  45. Environment $twig,
  46. FrontOptionRepository $frontOptionRepository,
  47. ArticleRepository $articleRepository,
  48. AgeRepository $ageRepository,
  49. ThemeRepository $themeRepository,
  50. SeoRepository $seoRepository,
  51. SejourRepository $sejourRepository,
  52. SejourSessionRepository $sejourSessionRepository,
  53. SaisonRepository $saisonRepository,
  54. CountryRepository $countryRepository,
  55. VilleDesservieRepository $villeDesservieRepository,
  56. RequestStack $container,
  57. Cook2Repository $cook2Repository,
  58. NotificationRepository $notificationRepository,
  59. MediaRepository $mediaRepository,
  60. MediaCategorieRepository $mediaCategoryRepository
  61. )
  62. {
  63. $this->twig = $twig;
  64. $this->cook2Repo = $cook2Repository;
  65. $this->frontOptionRepository = $frontOptionRepository;
  66. $this->articleRepository = $articleRepository;
  67. $this->container = $container;
  68. $this->sejourSessionRepository = $sejourSessionRepository;
  69. $this->saisonRepository = $saisonRepository;
  70. $this->seoRepository = $seoRepository;
  71. $this->sejourRepository = $sejourRepository;
  72. $this->ageRepository = $ageRepository;
  73. $this->villeDesservieRepository = $villeDesservieRepository;
  74. $this->themeRepository = $themeRepository;
  75. $this->countryRepository = $countryRepository;
  76. $this->notificationRepository = $notificationRepository;
  77. $this->mediaRepository = $mediaRepository;
  78. $this->mediaCategoryRepository = $mediaCategoryRepository;
  79. }
  80. public function onKernelController(ControllerEvent $event)
  81. {
  82. $path = $this->container->getCurrentRequest()->getPathInfo();
  83. $cookies = $this->container->getCurrentRequest()->cookies;
  84. $cookiRulesFinal = [];
  85. if($cookies->has(CookieConsentSessionStorage::COOKIE_KEY_NAME)){
  86. $cookieId = $cookies->get(CookieConsentSessionStorage::COOKIE_KEY_NAME);
  87. $cookiesRules = $this->cook2Repo->findBy(['cookieConsentKey' => $cookieId]);
  88. foreach($cookiesRules as $cr){
  89. $cookiRulesFinal[$cr->getCookieName()] = $cr->getCookieValue();
  90. }
  91. }
  92. $selected = [];
  93. $isSearch =strpos($path,"colonie-de-vacances");
  94. if($isSearch!== false){
  95. $selected = explode("/",$path);
  96. }
  97. array_shift($selected);
  98. $seoDataA = $this->seoRepository->createQueryBuilder('s')
  99. ->andWhere('s.path = :path')
  100. ->setParameter('path', $path)
  101. ->orderBy('s.id','DESC')
  102. ->getQuery()
  103. ->getResult();
  104. if(count($seoDataA)>0){
  105. $seoData = $seoDataA[0];
  106. }else{
  107. $seoData = null;
  108. }
  109. $sejourData = $this->sejourRepository->createQueryBuilder('s')
  110. ->andWhere('s.url = :path')
  111. ->setParameter('path', $path)
  112. ->getQuery()
  113. ->getOneOrNullResult();
  114. $articleData = $this->articleRepository->createQueryBuilder('s')
  115. ->andWhere('s.path = :path')
  116. ->setParameter('path', $path)
  117. ->getQuery()
  118. ->getOneOrNullResult();
  119. $bannerOnline = $this->frontOptionRepository->findOneBy([
  120. 'name' => 'bannerOnline'
  121. ]);
  122. $banner = ($bannerOnline->getContent() == "1") ? $this->frontOptionRepository->findOneBy(['name' => 'banner']): null;
  123. $allSaisons = $this->saisonRepository->findBy(['isActive'=> true]);
  124. $allAges = $this->ageRepository->findBy(['isActive'=> true],['age'=>'ASC']);
  125. $allDestinations = $this->countryRepository->findBy(['isActive' => true],['position'=>'ASC','title'=>'ASC']);
  126. $futuresCount = count($this->sejourSessionRepository->findFutures());
  127. $allCities= $this->villeDesservieRepository->findBy(['isActive' => true],['title'=>'ASC']);
  128. $mediaCategorie = $this->mediaCategoryRepository->findOneBy(['name'=>'imageFond']);
  129. $mediaHeadersAll = $this->mediaRepository->findBy(['mediaCategorie' => $mediaCategorie]);
  130. $mediaHeaders = array();
  131. foreach($mediaHeadersAll as $media){
  132. if($media->getFilePath() !== null)
  133. $mediaHeaders[] = $media;
  134. }
  135. $allThemes = $this->themeRepository->findBy(
  136. [
  137. 'isActivite' => false,
  138. 'isActive' => true
  139. ],['title'=>'ASC']
  140. );
  141. $allActivities = $this->themeRepository->findBy(
  142. [
  143. 'isActivite' => true,
  144. 'isActive' => true
  145. ],['title'=>'ASC']
  146. );
  147. $saisonId = $this->frontOptionRepository->findOneBy(['name'=>'saisonActuelle']);
  148. $saison = $this->saisonRepository->findOneBySlug($saisonId->getContent());
  149. $animateurs = $this->frontOptionRepository->findOneBy([
  150. 'name' => 'animateurs'
  151. ])->getContent();
  152. $destinations = $this->frontOptionRepository->findOneBy([
  153. 'name' => 'destinations'
  154. ])->getContent();
  155. $themes = $this->frontOptionRepository->findOneBy([
  156. 'name' => 'themes'
  157. ])->getContent();
  158. $annees = $this->frontOptionRepository->findOneBy([
  159. 'name' => 'années'
  160. ])->getContent();
  161. $enfants = $this->frontOptionRepository->findOneBy([
  162. 'name' => 'enfants'
  163. ])->getContent();
  164. $colonies = $this->frontOptionRepository->findOneBy([
  165. 'name' => 'colonies'
  166. ])->getContent();
  167. $lastArticle = $this->articleRepository->findOneBy(['isUne' => true],['creationDate' => 'DESC']);
  168. $lastNotification = $this->notificationRepository->findBy(['statut' => Notification::STATUT_OPEN],['date'=>'DESC'],10);
  169. //$this->twig->addGlobal('footer_seo', $this->articleRepository->findOneBy(['title'=>'footer']));
  170. $this->twig->addGlobal('last_article', $lastArticle);
  171. $this->twig->addGlobal('seo_params', $seoData);
  172. $this->twig->addGlobal('actualSaison',$saison);
  173. $this->twig->addGlobal('sejour_params', $sejourData);
  174. $this->twig->addGlobal('article_params', $articleData);
  175. $this->twig->addGlobal('banner',$banner);
  176. $this->twig->addGlobal('futuresCount',$futuresCount);
  177. $this->twig->addGlobal('allSaisons', $allSaisons);
  178. $this->twig->addGlobal('allAges', $allAges);
  179. $this->twig->addGlobal('allThemes', $allThemes);
  180. $this->twig->addGlobal('allCities', $allCities);
  181. $this->twig->addGlobal('allDestinations', $allDestinations);
  182. $this->twig->addGlobal('allActivities', $allActivities);
  183. $this->twig->addGlobal('selected', $selected);
  184. $this->twig->addGlobal('animateurs', $animateurs);
  185. $this->twig->addGlobal('colonies', $colonies);
  186. $this->twig->addGlobal('destinations', $destinations);
  187. $this->twig->addGlobal('themes', $themes);
  188. $this->twig->addGlobal('annees', $annees);
  189. $this->twig->addGlobal('enfants', $enfants);
  190. $this->twig->addGlobal('cookiesRules', $cookiRulesFinal);
  191. $this->twig->addGlobal('notifications',$lastNotification);
  192. $this->twig->addGlobal('mediaHeaders',$mediaHeaders);
  193. }
  194. public static function getSubscribedEvents()
  195. {
  196. return [
  197. 'kernel.controller' => 'onKernelController',
  198. ];
  199. }
  200. }