/**
  * Handle the page requests
  *
  * @param Request $request The request
  * @param string  $url     The url
  * @param bool    $preview Show in preview mode
  *
  * @throws NotFoundHttpException
  * @throws AccessDeniedException
  *
  * @return Response|array
  */
 public function slugAction(Request $request, $url = null, $preview = false)
 {
     /* @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     $locale = $request->getLocale();
     /* @var NodeTranslation $nodeTranslation */
     $nodeTranslation = $request->attributes->get('_nodeTranslation');
     // If no node translation -> 404
     if (!$nodeTranslation) {
         throw $this->createNotFoundException('No page found for slug ' . $url);
     }
     $entity = $this->getPageEntity($request, $preview, $em, $nodeTranslation);
     $node = $nodeTranslation->getNode();
     $securityEvent = new SlugSecurityEvent();
     $securityEvent->setNode($node)->setEntity($entity)->setRequest($request)->setNodeTranslation($nodeTranslation);
     $nodeMenu = $this->container->get('kunstmaan_node.node_menu');
     $nodeMenu->setLocale($locale);
     $nodeMenu->setCurrentNode($node);
     $nodeMenu->setIncludeOffline($preview);
     $eventDispatcher = $this->get('event_dispatcher');
     $eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent);
     //render page
     $renderContext = new RenderContext(array('nodetranslation' => $nodeTranslation, 'slug' => $url, 'page' => $entity, 'resource' => $entity, 'nodemenu' => $nodeMenu));
     if (method_exists($entity, 'getDefaultView')) {
         /** @noinspection PhpUndefinedMethodInspection */
         $renderContext->setView($entity->getDefaultView());
     }
     $preEvent = new SlugEvent(null, $renderContext);
     $eventDispatcher->dispatch(Events::PRE_SLUG_ACTION, $preEvent);
     $renderContext = $preEvent->getRenderContext();
     /** @noinspection PhpUndefinedMethodInspection */
     $response = $entity->service($this->container, $request, $renderContext);
     $postEvent = new SlugEvent($response, $renderContext);
     $eventDispatcher->dispatch(Events::POST_SLUG_ACTION, $postEvent);
     $response = $postEvent->getResponse();
     $renderContext = $postEvent->getRenderContext();
     if ($response instanceof Response) {
         return $response;
     }
     $view = $renderContext->getView();
     if (empty($view)) {
         throw $this->createNotFoundException('No page found for slug ' . $url);
     }
     $template = new Template(array());
     $template->setTemplate($view);
     $request->attributes->set('_template', $template);
     return $renderContext->getArrayCopy();
 }
 /**
  * Handle the page requests
  *
  * @param Request $request The request
  * @param string  $url     The url
  * @param bool    $preview Show in preview mode
  *
  * @throws NotFoundHttpException
  * @throws AccessDeniedException
  *
  * @return Response|array
  */
 public function slugAction(Request $request, $url = null, $preview = false)
 {
     /* @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     $locale = $request->getLocale();
     /* @var NodeTranslation $nodeTranslation */
     $nodeTranslation = $request->get('_nodeTranslation');
     if (!$nodeTranslation) {
         // When the SlugController is used from a different Routing or RouteLoader class, the _nodeTranslation is not set, so we need this fallback
         $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->getNodeTranslationForUrl($url, $locale);
     }
     // If no node translation -> 404
     if (!$nodeTranslation) {
         throw $this->createNotFoundException('No page found for slug ' . $url);
     }
     $entity = $this->getPageEntity($request, $preview, $em, $nodeTranslation);
     $securityEvent = new SlugSecurityEvent();
     $securityEvent->setNode($nodeTranslation->getNode())->setEntity($entity)->setRequest($request)->setNodeTranslation($nodeTranslation);
     $eventDispatcher = $this->get('event_dispatcher');
     $eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent);
     //render page
     $renderContext = new RenderContext(array('nodetranslation' => $nodeTranslation, 'slug' => $url, 'page' => $entity, 'resource' => $entity));
     if (method_exists($entity, 'getDefaultView')) {
         /** @noinspection PhpUndefinedMethodInspection */
         $renderContext->setView($entity->getDefaultView());
     }
     $preEvent = new SlugEvent(null, $renderContext);
     $eventDispatcher->dispatch(Events::PRE_SLUG_ACTION, $preEvent);
     $renderContext = $preEvent->getRenderContext();
     /** @noinspection PhpUndefinedMethodInspection */
     $response = $entity->service($this->container, $request, $renderContext);
     $postEvent = new SlugEvent($response, $renderContext);
     $eventDispatcher->dispatch(Events::POST_SLUG_ACTION, $postEvent);
     $response = $postEvent->getResponse();
     $renderContext = $postEvent->getRenderContext();
     if ($response instanceof Response) {
         return $response;
     }
     $view = $renderContext->getView();
     if (empty($view)) {
         throw $this->createNotFoundException('No page found for slug ' . $url);
     }
     $request->attributes->set('_template', $view);
     return $renderContext->getArrayCopy();
 }
 public function service(ContainerInterface $container, Request $request, RenderContext $context)
 {
     $context->exchangeArray($context->getArrayCopy() + $container->get('kunstamaan_extra.page_context')->getPageContext($this));
 }
 /**
  * Handle the page requests
  *
  * @param Request $request The request
  * @param string  $url     The url
  * @param bool    $preview Show in preview mode
  *
  * @throws NotFoundHttpException
  * @throws AccessDeniedException
  *
  * @return Response|array
  */
 public function slugAction(Request $request, $url = null, $preview = false)
 {
     /* @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     $locale = $request->getLocale();
     /* @var NodeTranslation $nodeTranslation */
     $nodeTranslation = $request->get('_nodeTranslation');
     if (!$nodeTranslation) {
         // When the SlugController is used from a different Routing or RouteLoader class, the _nodeTranslation is not set, so we need this fallback
         $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->getNodeTranslationForUrl($url, $locale);
     }
     // If no node translation -> 404
     if (!$nodeTranslation) {
         throw $this->createNotFoundException('No page found for slug ' . $url);
     }
     // check if the requested node is online, else throw a 404 exception (only when not previewing!)
     if (!$preview && !$nodeTranslation->isOnline()) {
         throw $this->createNotFoundException("The requested page is not online");
     }
     /* @var HasNodeInterface $entity */
     $entity = null;
     $node = $nodeTranslation->getNode();
     if ($preview) {
         $version = $request->get('version');
         if (!empty($version) && is_numeric($version)) {
             $nodeVersion = $em->getRepository('KunstmaanNodeBundle:NodeVersion')->find($version);
             if (!is_null($nodeVersion)) {
                 $entity = $nodeVersion->getRef($em);
             }
         }
     }
     if (is_null($entity)) {
         $entity = $nodeTranslation->getPublicNodeVersion()->getRef($em);
     }
     /* @var SecurityContextInterface $securityContext */
     $securityContext = $this->get('security.context');
     if (false === $securityContext->isGranted(PermissionMap::PERMISSION_VIEW, $node)) {
         throw new AccessDeniedException('You do not have sufficient rights to access this page.');
     }
     /* @var AclHelper $aclHelper */
     $aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
     $includeOffline = $preview;
     $nodeMenu = new NodeMenu($em, $securityContext, $aclHelper, $locale, $node, PermissionMap::PERMISSION_VIEW, $includeOffline);
     unset($securityContext);
     unset($aclHelper);
     //render page
     $renderContext = new RenderContext(array('nodetranslation' => $nodeTranslation, 'slug' => $url, 'page' => $entity, 'resource' => $entity, 'nodemenu' => $nodeMenu));
     if (method_exists($entity, 'getDefaultView')) {
         /** @noinspection PhpUndefinedMethodInspection */
         $renderContext->setView($entity->getDefaultView());
     }
     $preEvent = new SlugEvent(null, $renderContext);
     $this->container->get('event_dispatcher')->dispatch(Events::PRE_SLUG_ACTION, $preEvent);
     $renderContext = $preEvent->getRenderContext();
     /** @noinspection PhpUndefinedMethodInspection */
     $response = $entity->service($this->container, $request, $renderContext);
     $postEvent = new SlugEvent($response, $renderContext);
     $this->container->get('event_dispatcher')->dispatch(Events::POST_SLUG_ACTION, $postEvent);
     $response = $postEvent->getResponse();
     $renderContext = $postEvent->getRenderContext();
     if ($response instanceof Response) {
         return $response;
     }
     $view = $renderContext->getView();
     if (empty($view)) {
         throw $this->createNotFoundException('No page found for slug ' . $url);
     }
     $request->attributes->set('_template', $view);
     return $renderContext->getArrayCopy();
 }