Пример #1
0
 /**
  * Gets a id for a new node
  * @param \ride\library\cms\node\Node $node Node in need of a id
  * @return string
  */
 protected function getNewNodeId(Node $node)
 {
     $revision = $node->getRevision();
     $nodeName = $node->getName();
     if (!$nodeName) {
         $nodeName = 'node';
     }
     $baseId = StringHelper::safeString($nodeName);
     $baseId = str_replace(array('.', '-', '_', ' '), '', $baseId);
     $id = $baseId;
     $index = 1;
     try {
         $siteId = $node->getRootNodeId();
         $this->getNodes($siteId, $revision);
         while (isset($this->nodes[$siteId][$revision][$id])) {
             $id = $baseId . $index;
             $index++;
         }
     } catch (CmsException $exception) {
         // new site node
         $this->getSites();
         while (isset($this->sites[$id])) {
             $id = $baseId . $index;
             $index++;
         }
     }
     return $id;
 }
Пример #2
0
 /**
  * Publish a node or site
  * @param \ride\library\cms\node\Node $node Node to publish
  * @param string $revision Name of the published revision, falls back to the
  * default revision
  * @param boolean $recursive Flag to see if the node's children should be
  * published as well
  * @return null
  */
 public function publishNode(Node $node, $revision = null, $recursive = true)
 {
     if ($revision === null) {
         $revision = $this->getDefaultRevision();
     }
     if ($this->eventManager) {
         $eventArguments = array('action' => 'publish', 'description' => 'Publish node ' . $node->getName(), 'nodes' => array($node), 'revision' => $revision, 'recursive' => $recursive);
         $this->eventManager->triggerEvent(self::EVENT_PRE_ACTION, $eventArguments);
     }
     $deletedNodes = $this->io->publish($node, $revision, $recursive);
     if ($this->eventManager) {
         $eventArguments['deletedNodes'] = $deletedNodes;
         $this->eventManager->triggerEvent(self::EVENT_POST_ACTION, $eventArguments);
     }
 }
 /**
  * Dispatches the node
  * @param \ride\library\mvc\Request $request
  * @param \ride\library\mvc\Response $response
  * @param \ride\library\security\SecurityManager $securityManager
  * @param \ride\library\cache\pool\CachePool $cache
  * @return array Array with the region name as key and a view array as
  * value. The view array has the widget id as key and the dispatched
  * widget view as value
  */
 public function dispatch(Request $request, Response $response, SecurityManager $securityManager, CachePool $cache = null)
 {
     $this->locale = $this->view->getLocale();
     // initialize context
     $context = array('title' => array('site' => $this->node->getRootNode()->getName($this->locale, 'title'), 'node' => $this->node->getName($this->locale, 'title')), 'breadcrumbs' => $this->breadcrumbs, 'styles' => array(), 'scripts' => array());
     // prepare and process incoming route arguments
     $route = $request->getRoute();
     $this->routeArguments = $route->getArguments();
     unset($this->routeArguments['node']);
     unset($this->routeArguments['locale']);
     if (isset($this->routeArguments['site'])) {
         // preview has site and revision
         unset($this->routeArguments['site']);
         unset($this->routeArguments['revision']);
     }
     $nodeRoute = $this->node->getRoute($this->locale);
     $nodeRouteTokens = explode('/', ltrim($nodeRoute, '/'));
     foreach ($nodeRouteTokens as $tokenIndex => $tokenValue) {
         if (isset($this->routeArguments[$tokenIndex]) && $this->routeArguments[$tokenIndex] === $tokenValue) {
             unset($this->routeArguments[$tokenIndex]);
         }
     }
     $route->setPredefinedArguments(array());
     $route->setArguments();
     $routeArgumentsMatched = false;
     // check for cache
     $cacheKey = null;
     $cacheItem = null;
     $cachedViews = array();
     $dispatchedViews = array();
     $nodeCacheTtl = false;
     if ($cache) {
         $method = $request->getMethod();
         $isCacheable = $method == 'GET' || $method == 'HEAD' ? true : false;
         $isNoCache = $request->isNoCache();
         if ($isCacheable) {
             $parameters = $this->routeArguments ? '-' . implode('-', $this->routeArguments) : '';
             $parameters .= $request->getQueryParametersAsString();
             $containsUserContent = false;
             $nodeCacheTtl = 0;
             $cacheKey = 'node.view.' . $this->node->getId() . '.' . $this->node->getRevision() . '.' . $this->locale . '.' . substr(md5($parameters), 0, 10);
             if ($securityManager->getUser()) {
                 $cacheKey .= '.authenticated';
             }
             $cacheItem = $cache->get($cacheKey);
             if ($cacheItem->isValid()) {
                 $cachedViews = $cacheItem->getValue();
             } else {
                 $cachedViews = array();
             }
         }
     } else {
         $isCacheable = false;
     }
     foreach ($this->widgets as $this->region => $sections) {
         $dispatchedViews[$this->region] = array();
         foreach ($sections as $this->section => $blocks) {
             $dispatchedViews[$this->region][$this->section] = array();
             foreach ($blocks as $this->block => $widgets) {
                 $dispatchedViews[$this->region][$this->section][$this->block] = array();
                 foreach ($widgets as $widgetId => $widget) {
                     if ($this->log) {
                         $this->log->logDebug('Rendering widget ' . $widget->getName() . '#' . $widgetId . ' for region ' . $this->region, null, ApplicationListener::LOG_SOURCE);
                     }
                     $widgetProperties = $this->node->getWidgetProperties($widgetId);
                     if (!$widgetProperties->isPublished()) {
                         if ($this->log) {
                             $this->log->logDebug('Widget ' . $widget->getName() . '#' . $widgetId . ' is not published', null, ApplicationListener::LOG_SOURCE);
                         }
                         continue;
                     } elseif (!$widgetProperties->isAllowed($securityManager)) {
                         if ($this->log) {
                             $this->log->logDebug('Widget ' . $widget->getName() . '#' . $widgetId . ' is not allowed', null, ApplicationListener::LOG_SOURCE);
                         }
                         continue;
                     }
                     if ($isCacheable) {
                         $widgetCacheKey = $this->region . '.' . $this->section . '.' . $this->block . '.' . $widgetId . '.';
                     }
                     $isWidgetCache = $widgetProperties->isCacheEnabled() || $widgetProperties->isAutoCache() && $widget->isAutoCache();
                     if ($isCacheable && !$isNoCache && $isWidgetCache) {
                         if (isset($cachedViews[$this->region][$this->section][$this->block][$widgetId])) {
                             if ($this->log) {
                                 $this->log->logDebug('Retrieved widget ' . $widget->getName() . '#' . $widgetId . ' from cache', null, ApplicationListener::LOG_SOURCE);
                             }
                             $cacheView = $cachedViews[$this->region][$this->section][$this->block][$widgetId];
                             if ($cacheView->areRoutesMatched()) {
                                 $widgetMatchedRouteArguments = true;
                             }
                             $cacheContext = $cacheView->getContext();
                             if ($cacheContext) {
                                 foreach ($cacheContext as $key => $value) {
                                     if ($value !== null) {
                                         $context[$key] = $value;
                                     } elseif (isset($context[$key])) {
                                         unset($context[$key]);
                                     }
                                 }
                             }
                             if ($cacheView->isContent()) {
                                 $dispatchedViews = null;
                                 $this->view->setContentView($view, $widgetId, $this->block, $this->section, $this->region);
                                 break 4;
                             } elseif ($cacheView->isRegion()) {
                                 $dispatchedViews[$this->region] = array($this->section => array($this->block => array($widgetId => $cacheView->getView())));
                                 break 3;
                             } elseif ($cacheView->isSection()) {
                                 $dispatchedViews[$this->region][$this->section] = array($this->block => array($widgetId => $cacheView->getView()));
                                 break 2;
                             } elseif ($cacheView->isBlock()) {
                                 $dispatchedViews[$this->region][$this->section][$this->block] = array($widgetId => $cacheView->getView());
                                 break;
                             } else {
                                 $dispatchedViews[$this->region][$this->section][$this->block][$widgetId] = $cacheView->getView();
                                 continue;
                             }
                         }
                     }
                     $widget->setProperties($widgetProperties);
                     $widget->setContext($context);
                     $widgetMatchedRouteArguments = $this->dispatchWidget($request, $response, $widgetId, $widget);
                     if ($widgetMatchedRouteArguments) {
                         $routeArgumentsMatched = true;
                     }
                     $statusCode = $response->getStatusCode();
                     if ($statusCode != Response::STATUS_CODE_OK && $statusCode != Response::STATUS_CODE_BAD_REQUEST && $statusCode != Response::STATUS_CODE_UNPROCESSABLE_ENTITY) {
                         return;
                     }
                     $view = $response->getView();
                     $response->setView(null);
                     $isContent = $widget->isContent();
                     $isRegion = $widget->isRegion();
                     $isSection = $widget->isSection();
                     $isBlock = $widget->isBlock();
                     if ($isCacheable && !$containsUserContent && $widget->containsUserContent()) {
                         $containsUserContent = true;
                     }
                     $oldContext = $context;
                     $context = $widget->getContext();
                     if ($isCacheable && $isWidgetCache) {
                         $widgetContext = $this->getContextDifference($context, $oldContext);
                         if (!$widgetContext) {
                             // calculate node cache time based on the least widget cache time
                             $cacheTtl = $widgetProperties->getCacheTtl();
                             if ($nodeCacheTtl !== false && $cacheTtl) {
                                 if ($nodeCacheTtl == 0) {
                                     $nodeCacheTtl = $cacheTtl;
                                 } else {
                                     $nodeCacheTtl = min($nodeCacheTtl, $cacheTtl);
                                 }
                             }
                             $widgetCachedView = new WidgetCacheData($widgetContext, $isContent, $isRegion, $isSection, $isBlock, $widgetMatchedRouteArguments);
                             $cachedViews[$this->region][$this->section][$this->block][$widgetId] = $widgetCachedView;
                         }
                     }
                     if ($isContent) {
                         $dispatchedViews = null;
                         $this->view->setContentView($view, $widgetId, $this->block, $this->section, $this->region);
                         break 4;
                     } elseif ($isRegion) {
                         $dispatchedViews[$this->region] = array($this->section => array($this->block => array($widgetId => $view)));
                         break 3;
                     } elseif ($isSection) {
                         $dispatchedViews[$this->region][$this->section] = array($this->block => array($widgetId => $view));
                         break 2;
                     } elseif ($isBlock) {
                         $dispatchedViews[$this->region][$this->section][$this->block] = array($widgetId => $view);
                         break;
                     }
                     $dispatchedViews[$this->region][$this->section][$this->block][$widgetId] = $view;
                 }
                 if (!$dispatchedViews[$this->region][$this->section][$this->block]) {
                     unset($dispatchedViews[$this->region][$this->section][$this->block]);
                 }
             }
             if (!$dispatchedViews[$this->region][$this->section]) {
                 unset($dispatchedViews[$this->region][$this->section]);
             }
         }
         if (!$dispatchedViews[$this->region]) {
             unset($dispatchedViews[$this->region]);
         }
     }
     if ($this->routeArguments && !$routeArgumentsMatched) {
         // sub route provided but never matched
         $response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
         $response->setView(null);
         $dispatchedViews = null;
     }
     // if ($this->eventManager && $isCacheable && $nodeCacheTtl !== false) {
     // if ($user && $containsUserContent) {
     // $isCacheable = false;
     // }
     // if ($isCacheable) {
     // $this->cache = $cache;
     // $this->cacheTtl = $nodeCacheTtl;
     // $this->eventManager->addEventListener(WebApplication::EVENT_POST_RESPONSE, array($this, 'cacheResponse'));
     // }
     // }
     $this->view->setContext($context);
     if (is_array($dispatchedViews)) {
         $this->view->setDispatchedViews($dispatchedViews);
         $this->view->setRegions($this->regions);
         if ($nodeCacheTtl !== false && $cachedViews) {
             $cacheItem->setValue($cachedViews);
             $cacheItem->setTtl($nodeCacheTtl);
             $this->view->setCachedViews($cache, $cacheItem);
         }
     }
     $response->setView($this->view);
 }
 /**
  * Gets the breadcrumbs to the current locale
  * @param \ride\web\WebApplication $web
  * @param \ride\library\cms\node\Node $node
  * @param string $locale
  * @return array Array with the URL to the node as key and the name of the
  * node as value
  */
 protected function getBreadcrumbs(WebApplication $web, Node $node, $locale)
 {
     $breadcrumbs = array();
     do {
         $url = $web->getUrl('cms.node.default', array('site' => $node->getRootNodeId(), 'revision' => $node->getRevision(), 'locale' => $locale, 'node' => $node->getId()));
         $breadcrumbs[$url] = $node->getName($locale);
         $node = $node->getParentNode();
     } while ($node);
     return array_reverse($breadcrumbs, true);
 }