/**
  * Sets a cookie for the current locale when offering a HTML view
  * @param \ride\library\event\Event $event
  * @param \ride\library\i18n\I18n $i18n
  * @return null
  */
 public function handleLocaleCookie(Event $event, I18n $i18n)
 {
     $web = $event->getArgument('web');
     $request = $web->getRequest();
     $response = $web->getResponse();
     $view = $response->getView();
     if (!$view instanceof HtmlView) {
         return;
     }
     $cookieName = $this->getCookieName();
     $locale = $i18n->getLocale()->getCode();
     $domain = $request->getHeader(Header::HEADER_HOST);
     $cookie = new Cookie($cookieName, $locale, 0, $domain, '/', $request->isSecure());
     $response->setCookie($cookie);
 }
 /**
  * Performs a clear on all the received nodes
  * @param \ride\library\event\Event $event Pre response event
  * @param \ride\web\WebApplication $web Instance of the web application
  * @return null
  */
 public function handleVarnish(Event $event)
 {
     $web = $event->getArgument('web');
     $request = $web->getRequest();
     $baseUrl = $request->getBaseUrl();
     $route = $request->getRoute();
     if ($route) {
         $locale = $route->getArgument('locale');
     } else {
         $locale = null;
     }
     foreach ($this->banNodes as $node) {
         $this->varnishService->banNode($node, $baseUrl, $locale);
     }
     $this->banNodes = array();
     $this->needsAction = false;
 }
 /**
  * Handles a ORM action to detect needed varnish bans
  * @param \ride\library\event\Event $event
  * @return null
  */
 public function handleOrmAction(Event $event)
 {
     $model = $event->getArgument('model');
     $entry = $event->getArgument('entry');
     switch ($event->getName()) {
         case GenericModel::EVENT_UPDATE_PRE:
         case GenericModel::EVENT_DELETE_PRE:
             // gather detail urls
             $this->gatherDetailUrls($model, $entry);
             $this->addBanListener();
             break;
         case GenericModel::EVENT_INSERT_POST:
         case GenericModel::EVENT_UPDATE_POST:
         case GenericModel::EVENT_DELETE_POST:
             // gather overview urls
             $this->gatherOverviewUrls($model, $entry);
             $this->addBanListener();
             break;
     }
 }
 public function prepareContentMenu(Event $event, OrmManager $ormManager)
 {
     $locale = $event->getArgument('locale');
     $menu = $event->getArgument('menu');
     $models = $ormManager->getModels();
     foreach ($models as $model) {
         $meta = $model->getMeta();
         $option = $meta->getOption('scaffold.expose');
         if (!$option) {
             continue;
         }
         $menuItem = new MenuItem();
         $title = $meta->getOption('scaffold.title');
         if ($title) {
             $menuItem->setTranslation($title);
         } else {
             $menuItem->setLabel($meta->getName());
         }
         $menuItem->setRoute('system.orm.scaffold.index', array('locale' => $locale, 'model' => $meta->getName()));
         $menu->addMenuItem($menuItem);
     }
 }
 /**
  * Sets a error view to the response if a status code above 399 is set
  * @return null
  */
 public function handleHttpError(Event $event, I18n $i18n)
 {
     $web = $event->getArgument('web');
     $request = $web->getRequest();
     $response = $web->getResponse();
     $route = $request->getRoute();
     if (!$route) {
         return;
     }
     $routeId = $route->getId();
     if ($routeId && substr($routeId, 0, 10) !== 'cms.front.') {
         // don't act on non cms pages
         return;
     }
     $statusCode = $response->getStatusCode();
     if ($statusCode != Response::STATUS_CODE_FORBIDDEN && $statusCode != Response::STATUS_CODE_NOT_FOUND || $request->isXmlHttpRequest() && $response->getView() instanceof JsonView) {
         // js request or not a forbidden or not a not found status
         return;
     }
     $locale = $i18n->getLocale()->getCode();
     // lookup site with the current base URL
     $baseUrl = $request->getBaseUrl();
     $site = null;
     $defaultSite = null;
     $sites = $this->cms->getSites();
     foreach ($sites as $site) {
         if ($site->getBaseUrl($locale) == $baseUrl) {
             break;
         }
         $defaultSite = $site;
         $site = null;
     }
     if (!$site) {
         if (!$defaultSite) {
             return;
         }
         $site = $defaultSite;
     }
     // get the error node
     $node = $site->get('error.' . $statusCode);
     if (!$node) {
         // no node setup for this error
         return;
     }
     // resolve the locale
     $locales = $site->getAvailableLocales();
     if ($locales != Node::LOCALES_ALL && !isset($locales[$locale])) {
         $locale = array_pop($locales);
         $i18n->setCurrentLocale($locale);
     }
     // dispatch the error page
     $routeContainer = $web->getRouter()->getRouteContainer();
     $route = $routeContainer->getRouteById('cms.front.' . $site->getId() . '.' . $node . '.' . $locale);
     if (!$route) {
         $route = $routeContainer->getRouteById('cms.node.frontend.locale');
         $route->setArguments(array('node' => $node, 'locale' => $locale));
     } else {
         $route->setArguments();
     }
     $loginRequest = $web->createRequest($route->getPath());
     $loginRequest->setRoute($route);
     $loginRequest->getHeaders()->setHeader(Header::HEADER_REFERER, $request->getUrl());
     if ($request->hasSession()) {
         $loginRequest->setSession($request->getSession());
     }
     $response->getMessageContainer()->removeAll();
     $response->setView(null);
     $response->setStatusCode(Response::STATUS_CODE_OK);
     $response->removeHeader(Header::HEADER_CONTENT_TYPE);
     $response->clearRedirect();
     try {
         $dispatcher = $web->getDispatcher();
         $dispatcher->dispatch($loginRequest, $response);
     } catch (UnauthorizedException $exception) {
         if ($statusCode === Response::STATUS_CODE_FORBIDDEN) {
             $response->setRedirect($request->getBaseUrl());
             return;
         } else {
             throw $exception;
         }
     }
     $response->setStatusCode($statusCode);
     if ($loginRequest->hasSession()) {
         $request->setSession($loginRequest->getSession());
     }
     if ($statusCode === Response::STATUS_CODE_FORBIDDEN) {
         $response->addMessage(new Message($i18n->getTranslator()->translate('error.unauthorized'), Message::TYPE_ERROR));
     }
 }