182 lines
5.3 KiB
PHP
182 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\ContentManagerBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Trinity\Bundle\ContentManagerBundle\Event\SecurityEvent;
|
|
use Trinity\Bundle\ContentManagerBundle\Model\Page;
|
|
use Trinity\Bundle\ContentManagerBundle\TrinityContentManagerEvents;
|
|
use Trinity\Bundle\UserBundle\User\SessionUser;
|
|
use Trinity\Bundle\ContentManagerBundle\Model\PageableInterface;
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
use Trinity\Bundle\ContentManagerBundle\Model\Nav;
|
|
|
|
class PageController extends Controller
|
|
{
|
|
public function catchAllAction()
|
|
{
|
|
if ($response = $this->handleNodeSecurity()) {
|
|
return $response;
|
|
}
|
|
|
|
$this->initPager();
|
|
|
|
return $this->defaultRender();
|
|
}
|
|
|
|
protected function generateCmsUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
|
|
{
|
|
$route = sprintf('%d_%s', $this->getNavigationSelector()->retrieve()->getId(), $route);
|
|
|
|
return $this->container->get('router')->generate($route, $parameters, $referenceType);
|
|
}
|
|
|
|
protected function generateCmsUrlByNav(Nav $nav, $route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
|
|
{
|
|
$route = sprintf('%d_%s', $nav->getId(), $route);
|
|
|
|
return $this->container->get('router')->generate($route, $parameters, $referenceType);
|
|
}
|
|
|
|
protected function handleNodeSecurity()
|
|
{
|
|
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
|
|
$dispatcher = $this->container->get('event_dispatcher');
|
|
|
|
$security_context = $this->container->get('security.context');
|
|
$request = $this->container->get('request');
|
|
|
|
$event = new SecurityEvent($request, $security_context);
|
|
$dispatcher->dispatch(TrinityContentManagerEvents::SECURITY_CHECK, $event);
|
|
|
|
if ($event->getResponse()) {
|
|
return $event->getResponse();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected function initPager()
|
|
{
|
|
if ($this->getPage() instanceof PageableInterface) {
|
|
$this->getPage()->initPager();
|
|
|
|
if ($current_page = intval($this->getRequest()->get('page'))) {
|
|
if ($current_page > $this->getPage()->getPager()->getNbPages()) {
|
|
$current_page = 1;
|
|
}
|
|
|
|
$this->getPage()->setPage($current_page);
|
|
} else {
|
|
$this->getPage()->setPage(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function getNavigationSelector()
|
|
{
|
|
return $this->container->get('navigation.selector');
|
|
}
|
|
|
|
protected function getPageManager()
|
|
{
|
|
return $this->container->get('page.manager.selector')->retrieve();
|
|
}
|
|
|
|
protected function getDecoratorStrategy()
|
|
{
|
|
return $this->container->get('page.decorator_strategy');
|
|
}
|
|
|
|
protected function getNode()
|
|
{
|
|
return $this->getPageManager()->getCurrentNode();
|
|
}
|
|
|
|
protected function getCurrentNode()
|
|
{
|
|
return $this->getNode();
|
|
}
|
|
|
|
/**
|
|
* @return mixed|Page
|
|
*/
|
|
protected function getPage()
|
|
{
|
|
return $this->getPageManager()->getPage();
|
|
}
|
|
|
|
public function render($view, array $parameters = array(), Response $response = null)
|
|
{
|
|
$parameters = array_merge(
|
|
$this->getDefaultRenderParameters(),
|
|
$parameters
|
|
);
|
|
|
|
$response = parent::render($view, $parameters, $response);
|
|
|
|
$this->configureEsiResponse($response, $parameters);
|
|
|
|
return $response;
|
|
}
|
|
|
|
protected function getDefaultRenderParameters()
|
|
{
|
|
return array(
|
|
'page' => $this->getPage(),
|
|
'current_node' => $this->getNode(),
|
|
'manager' => $this->getPageManager(),
|
|
'session_user' => $this->getSessionUser(),
|
|
'nav' => $this->getNavigationSelector()->retrieve(),
|
|
);
|
|
}
|
|
|
|
protected function defaultRender(array $parameters = array(), Response $response = null)
|
|
{
|
|
$formats = array(
|
|
'html' => 'text/html',
|
|
'xml' => 'text/xml',
|
|
'json' => 'text/json',
|
|
);
|
|
|
|
$response = $this->render($this->getPage()->getTemplate(), $parameters, $response);
|
|
$response->headers->set('Content-type', $formats[$this->getNode()->getFormat()]);
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @return SessionUser
|
|
*/
|
|
protected function getSessionUser()
|
|
{
|
|
return $this->container->get('trinity.session_user');
|
|
}
|
|
|
|
public function sitemapAction()
|
|
{
|
|
$sitemap_nodes = $this->getNavigationSelector()->retrieve()->getSitemapNodes();
|
|
|
|
return $this->defaultRender(array('nodes' => $sitemap_nodes));
|
|
}
|
|
|
|
protected function configureEsiResponse(Response $response, array $parameters)
|
|
{
|
|
$response->setSharedMaxAge(
|
|
$this->getPage()->getConfiguration()->getSharedMaxAge()
|
|
);
|
|
|
|
$methods = array(
|
|
's_maxage' => 'setSharedMaxAge',
|
|
);
|
|
|
|
foreach ($methods as $param => $method) {
|
|
if (isset($parameters[$param])) {
|
|
call_user_func(array($response, $method), $parameters[$param]);
|
|
}
|
|
}
|
|
}
|
|
}
|