/**
  * Filter the `core.request` event to decorated the action
  *
  * @param GetResponseEvent $event
  *
  * @return void
  *
  * @throws InternalErrorException
  * @throws PageNotFoundException
  */
 public function onCoreRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $cms = $this->cmsSelector->retrieve();
     if (!$cms) {
         throw new InternalErrorException('No CMS Manager available');
     }
     // true cms page
     if ($request->get('_route') == PageInterface::PAGE_ROUTE_CMS_NAME) {
         return;
     }
     if (!$this->decoratorStrategy->isRequestDecorable($request)) {
         return;
     }
     $site = $this->siteSelector->retrieve();
     if (!$site) {
         throw new InternalErrorException('No site available for the current request with uri ' . htmlspecialchars($request->getUri(), ENT_QUOTES));
     }
     if ($site->getLocale() && $site->getLocale() != $request->get('_locale')) {
         throw new PageNotFoundException(sprintf('Invalid locale - site.locale=%s - request._locale=%s', $site->getLocale(), $request->get('_locale')));
     }
     try {
         $page = $cms->getPageByRouteName($site, $request->get('_route'));
         if (!$page->getEnabled() && !$this->cmsSelector->isEditor()) {
             throw new PageNotFoundException(sprintf('The page is not enabled : id=%s', $page->getId()));
         }
         $cms->setCurrentPage($page);
     } catch (PageNotFoundException $e) {
         return;
     }
 }
 /**
  * Action for ajax route rendering a block by calling his executeAjax() method
  *
  * @param Request $request Symfony request object
  * @param integer $pageId  Page identifier
  * @param integer $blockId Block identifier
  */
 public function execute(Request $request, $pageId, $blockId)
 {
     $cmsManager = $this->cmsManagerSelector->retrieve();
     $page = $cmsManager->getPageById($pageId);
     $block = $cmsManager->getBlock($blockId);
     if (!$block instanceof BlockInterface) {
         throw new BlockNotFoundException(sprintf('Unable to find block identifier "%s" in page "%s".', $blockId, $pageId));
     }
     $response = $this->blockRenderer->render($block);
     return $response;
 }
 /**
  * @param string $name
  * @param null   $page
  * @param array  $options
  *
  * @return Response
  */
 public function renderContainer($name, $page = null, array $options = array())
 {
     $cms = $this->cmsManagerSelector->retrieve();
     $site = $this->siteSelector->retrieve();
     $targetPage = false;
     try {
         if ($page === null) {
             $targetPage = $cms->getCurrentPage();
         } elseif (!$page instanceof PageInterface && is_string($page)) {
             $targetPage = $cms->getInternalRoute($site, $page);
         } elseif ($page instanceof PageInterface) {
             $targetPage = $page;
         }
     } catch (PageNotFoundException $e) {
         // the snapshot does not exist
         $targetPage = false;
     }
     if (!$targetPage) {
         return '';
     }
     $container = $cms->findContainer($name, $targetPage);
     if (!$container) {
         return '';
     }
     return $this->renderBlock($container, $options);
 }
 /**
  * Handles a native error
  *
  * @param GetResponseForExceptionEvent $event
  *
  * @throws mixed
  */
 private function handleNativeError(GetResponseForExceptionEvent $event)
 {
     if (true === $this->debug) {
         return;
     }
     if (true === $this->status) {
         return;
     }
     $this->status = true;
     $exception = $event->getException();
     $statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
     $cmsManager = $this->cmsManagerSelector->retrieve();
     if ($event->getRequest()->get('_route') && !$this->decoratorStrategy->isRouteNameDecorable($event->getRequest()->get('_route'))) {
         return;
     }
     if (!$this->decoratorStrategy->isRouteUriDecorable($event->getRequest()->getPathInfo())) {
         return;
     }
     if (!$this->hasErrorCode($statusCode)) {
         return;
     }
     $message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine());
     $this->logException($exception, $exception, $message);
     try {
         $page = $this->getErrorCodePage($statusCode);
         $cmsManager->setCurrentPage($page);
         $response = $this->pageServiceManager->execute($page, $event->getRequest(), array(), new Response('', $statusCode));
     } catch (\Exception $e) {
         $this->logException($exception, $e);
         $event->setException($e);
         $this->handleInternalError($event);
         return;
     }
     $event->setResponse($response);
 }
 /**
  * {@inheritdoc}
  */
 public function load(BlockInterface $block)
 {
     if (is_numeric($block->getSetting('pageId', null))) {
         $cmsManager = $this->cmsManagerSelector->retrieve();
         $site = $block->getPage()->getSite();
         $block->setSetting('pageId', $cmsManager->getPage($site, $block->getSetting('pageId')));
     }
 }
 /**
  * @param null|\Sonata\PageBundle\Model\PageInterface $page
  * @param array $options
  * @return
  */
 public function breadcrumb(PageInterface $page = null, array $options = array())
 {
     if (!$page) {
         $page = $this->cmsManagerSelector->retrieve()->getCurrentPage();
     }
     $options = array_merge(array('separator' => '', 'current_class' => '', 'last_separator' => '', 'force_view_home_page' => true, 'container_attr' => array('class' => 'sonata-page-breadcrumbs')), $options);
     $breadcrumbs = array();
     if ($page) {
         $breadcrumbs = $page->getParents();
         if ($options['force_view_home_page'] && (!isset($breadcrumbs[0]) || $breadcrumbs[0]->getRouteName() != 'homepage')) {
             $homePage = $this->cmsManagerSelector->retrieve()->getPageByRouteName('homepage');
             if ($homePage) {
                 array_unshift($breadcrumbs, $homePage);
             }
         }
     }
     return $this->render('SonataPageBundle:Page:breadcrumb.html.twig', array('page' => $page, 'breadcrumbs' => $breadcrumbs, 'options' => $options));
 }
 /**
  * Filter the `core.response` event to decorate the action.
  *
  * @param FilterResponseEvent $event
  *
  * @throws InternalErrorException
  */
 public function onCoreResponse(FilterResponseEvent $event)
 {
     $cms = $this->cmsSelector->retrieve();
     $response = $event->getResponse();
     $request = $event->getRequest();
     if ($this->cmsSelector->isEditor()) {
         $response->setPrivate();
         if (!$request->cookies->has('sonata_page_is_editor')) {
             $response->headers->setCookie(new Cookie('sonata_page_is_editor', 1));
         }
     }
     $page = $cms->getCurrentPage();
     // display a validation page before redirecting, so the editor can edit the current page
     if ($page && $response->isRedirection() && $this->cmsSelector->isEditor() && !$request->get('_sonata_page_skip')) {
         $response = new Response($this->templating->render('SonataPageBundle:Page:redirect.html.twig', array('response' => $response, 'page' => $page)));
         $response->setPrivate();
         $event->setResponse($response);
         return;
     }
     if (!$this->decoratorStrategy->isDecorable($event->getRequest(), $event->getRequestType(), $response)) {
         return;
     }
     if (!$this->cmsSelector->isEditor() && $request->cookies->has('sonata_page_is_editor')) {
         $response->headers->clearCookie('sonata_page_is_editor');
     }
     if (!$page) {
         throw new InternalErrorException('No page instance available for the url, run the sonata:page:update-core-routes and sonata:page:create-snapshots commands');
     }
     // only decorate hybrid page or page with decorate = true
     if (!$page->isHybrid() || !$page->getDecorate()) {
         return;
     }
     $parameters = array('content' => $response->getContent());
     $response = $this->pageServiceManager->execute($page, $request, $parameters, $response);
     if (!$this->cmsSelector->isEditor() && $page->isCms()) {
         $response->setTtl($page->getTtl());
     }
     $event->setResponse($response);
 }
 /**
  * Handles a native error.
  *
  * @param GetResponseForExceptionEvent $event
  *
  * @throws mixed
  */
 private function handleNativeError(GetResponseForExceptionEvent $event)
 {
     if (true === $this->debug) {
         return;
     }
     if (true === $this->status) {
         return;
     }
     $this->status = true;
     $exception = $event->getException();
     $statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
     $cmsManager = $this->cmsManagerSelector->retrieve();
     if ($event->getRequest()->get('_route') && !$this->decoratorStrategy->isRouteNameDecorable($event->getRequest()->get('_route'))) {
         return;
     }
     if (!$this->decoratorStrategy->isRouteUriDecorable($event->getRequest()->getPathInfo())) {
         return;
     }
     if (!$this->hasErrorCode($statusCode)) {
         return;
     }
     $message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine());
     $this->logException($exception, $exception, $message);
     try {
         $page = $this->getErrorCodePage($statusCode);
         $cmsManager->setCurrentPage($page);
         if ($page->getSite()->getLocale() !== $event->getRequest()->getLocale()) {
             // Compare locales because Request returns the default one if null.
             // If 404, LocaleListener from HttpKernel component of Symfony is not called.
             // It uses the "_locale" attribute set by SiteSelectorInterface to set the request locale.
             // So in order to translate messages, force here the locale with the site.
             $event->getRequest()->setLocale($page->getSite()->getLocale());
         }
         $response = $this->pageServiceManager->execute($page, $event->getRequest(), array(), new Response('', $statusCode));
     } catch (\Exception $e) {
         $this->logException($exception, $e);
         $event->setException($e);
         $this->handleInternalError($event);
         return;
     }
     $event->setResponse($response);
 }
Beispiel #9
0
    /**
     * @param Request $request
     *
     * @return Response
     */
    public function cacheAction(Request $request)
    {
        $cms = $this->cmsSelector->retrieve();
        try {
            $page = $cms->getPageById($request->get('page_id'));
        } catch (PageNotFoundException $e) {
            $page = false;
        }
        $block = $cms->getBlock($request->get('block_id'));
        if (!$page || !$block) {
            return new Response('', 404);
        }
        $options = array();
        $blockContext = $this->contextManager->get($block, $options);
        $response = $this->blockRenderer->render($blockContext);
        $response->setPrivate();
        //  always set to private
        if ($this->sync) {
            return $response;
        }
        $response->setContent(sprintf('
    (function () {
        var block = document.getElementById("block-cms-%s");

        var div = document.createElement("div");
        div.innerHTML = %s;

        for (var node in div.childNodes) {
            if (div.childNodes[node] && div.childNodes[node].nodeType == 1) {
                block.parentNode.replaceChild(div.childNodes[node], block);
            }
        }
    })();
', $block->getId(), json_encode($response->getContent())));
        $response->headers->set('Content-Type', 'application/javascript');
        return $response;
    }
 /**
  * Retrieves a page object from a page alias.
  *
  * @param string $alias
  *
  * @return \Sonata\PageBundle\Model\PageInterface|null
  *
  * @throws PageNotFoundException
  */
 protected function getPageByPageAlias($alias)
 {
     $site = $this->siteSelector->retrieve();
     $page = $this->cmsSelector->retrieve()->getPageByPageAlias($site, $alias);
     return $page;
 }
 /**
  * Return the current Page.
  *
  * @return PageInterface
  */
 protected function getCurrentPage()
 {
     $cms = $this->cmsSelector->retrieve();
     return $cms->getCurrentPage();
 }