<?php
/**
 * Created by PhpStorm.
 * User: smessaoudi
 * Date: 26/12/16
 * Time: 17:05
 */

namespace Ayaline\Bundle\EzMiniSitesCollectivitesBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\DependencyInjection\ContainerInterface;

class CollectiviteLayoutListener {

    /**
     * CollectiviteLayoutListener constructor.
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $currentSiteaccess = $event->getRequest()->get('siteaccess');

        $twig = $this->container->get('twig');
        $globals = $twig->getGlobals();

        //  update default pagelayout value
        $defaultLayout = $globals['default_layout'];
        $defaultLayout = $this->container->hasParameter('ezpublish_legacy.' . $currentSiteaccess->name . '.view_default_layout') ? $this->container->getParameter('ezpublish_legacy.' . $currentSiteaccess->name . '.view_default_layout') : $defaultLayout;
        $twig->addGlobal('default_layout', $defaultLayout);

    }
} 