/** * Removes a node * @param \ride\library\cms\node\Node $node Node to delete * @param boolean $recursive Flag to see if child nodes should be deleted * @return */ public function removeNode(Node $node, $recursive = true) { $siteId = $node->getRootNodeId(); $revision = $node->getRevision(); $parent = $node->getParent(); $path = $node->getPath(); $orderIndex = $node->getOrderIndex(); $baseOrderIndex = $orderIndex - 1; $changedNodes = array(); // remove children or move the children the the parent's path $numChildren = 0; $children = $this->getNodesByPath($siteId, $revision, $path); foreach ($children as $child) { if ($recursive) { $this->removeNode($child, true); continue; } $childParent = $child->getParent(); if ($childParent === $path) { $child->setOrderIndex($baseOrderIndex + $child->getOrderIndex()); $numChildren++; } $child->setParent(str_replace($path, $parent, $childParent)); $changedNodes[] = $child; } if (!$recursive) { // fix order index for nodes after the removed node $siblings = $this->getChildren($node->getRootNodeId(), $node->getRevision(), $parent, 0); foreach ($siblings as $sibling) { $siblingOrderIndex = $sibling->getOrderIndex(); if ($siblingOrderIndex <= $orderIndex) { continue; } $sibling->setOrderIndex($siblingOrderIndex + $numChildren - 1); $changedNodes[] = $sibling; } } // save and remove the necessairy nodes foreach ($changedNodes as $changedNode) { $this->setNode($changedNode); } $this->deleteNode($node); unset($this->nodes[$siteId][$revision][$node->getId()]); }
/** * Clones a node * @param \ride\library\cms\node\Node $node Node to clone * @param boolean $recursive Set to true to also clone the children of the * node * @param boolean $reorder Set to false to just clone the order index * instead of adding the cloned node after the source node * @param boolean $keepOriginalName Set to true to keep the name untouched, * else a suffix like (clone) or (clone 2, 3 ...) will be added to the name * of the clone * @param boolean $cloneRoutesAndId Set to true to clone the routes of the nodes. * This will only work when copying a root node, else a validation error * will occur * @param boolean $newParent Provide a new parent for the clone, needed for * recursive cloning * @param boolean $autoPublish Set to false to skip auto publishing * @return null */ public function cloneNode(Node $node, $recursive = true, $reorder = null, $keepOriginalName = false, $cloneRoutesAndId = null, $newParent = null, $autoPublish = true) { $id = $node->getId(); $rootNodeId = $node->getRootNodeId(); $isRootNode = $id === $rootNodeId; if ($reorder === null) { if ($isRootNode) { $reorder = false; } else { $reorder = true; } } if ($cloneRoutesAndId === null) { if ($isRootNode) { $cloneRoutesAndId = true; } else { $cloneRoutesAndId = false; } } if ($newParent === null) { $this->widgetTable = array(); } $nodeType = $this->nodeTypeManager->getNodeType($node->getType()); $clone = $nodeType->createNode(); $clone->setRevision($node->getRevision()); if (!$isRootNode && $cloneRoutesAndId) { $clone->setId($node->getId()); } if ($newParent) { $clone->setParent($newParent); } else { $clone->setParent($node->getParent()); } if ($clone->getParent()) { $clone->setParentNode($this->io->getNode($clone->getRootNodeId(), $node->getRevision(), $clone->getParentNodeId())); } if ($reorder) { $clone->setOrderIndex($node->getOrderIndex() + 1); } else { $clone->setOrderIndex($node->getOrderIndex()); } $this->cloneNodeProperties($node, $clone, $keepOriginalName, $cloneRoutesAndId); if ($reorder) { // reorder the siblings to insert the clone $cloneOrderIndex = $clone->getOrderIndex(); $siblings = $this->io->getChildren($node->getRootNodeId(), $node->getRevision(), $node->getParent(), 0); foreach ($siblings as $sibling) { $siblingOrderIndex = $sibling->getOrderIndex(); if ($siblingOrderIndex < $cloneOrderIndex) { continue; } $sibling->setOrderIndex($siblingOrderIndex + 1); $this->setNode($sibling, 'Reordered ' . $sibling->getName() . ' after clone of ' . $node->getName(), false); } } $this->setNode($clone, 'Cloned ' . $node->getName(), false); if ($recursive) { // clone the children $path = $clone->getPath(); $children = $this->io->getChildren($node->getRootNodeId(), $node->getRevision(), $node->getPath(), 0); foreach ($children as $child) { $this->cloneNode($child, true, false, true, $cloneRoutesAndId, $path, false); } } if ($newParent === null) { unset($this->widgetTable); } // save the root for newly created widgets $this->setNode($clone->getRootNode(), 'Updated widgets for clone of ' . $node->getName(), false); // perform auto publishing if enabled if (!$isRootNode && $autoPublish && $node->getRootNode()->isAutoPublish()) { $this->publishNode($node); } return $clone; }
/** * Perform the actual publishing of a single node * @param \ride\library\cms\node\SiteNode $site * @param \ride\library\cms\node\Node $node * @param string $revision * @param \ride\library\system\file\File $publishDirectory * @param boolean $isRecursive Flag to see if this publishing is part of a * recursive publish action * @return null */ protected function publishNode(SiteNode $site, Node $node, $revision, File $publishDirectory, $isRecursive) { // initialize needed variables $siteId = $site->getId(); $nodeId = $node->getId(); $changedNodes = array(); try { $publishSite = $this->getSite($siteId, $revision); } catch (NodeNotFoundException $exception) { $publishSite = null; } // process and merge the necessairy nodes try { $oldNode = $this->getNode($siteId, $revision, $nodeId); // check for expired routes $oldRoutes = $oldNode->getRoutes(); $newRoutes = $node->getRoutes(); if ($oldRoutes && $oldRoutes !== $newRoutes) { foreach ($oldRoutes as $locale => $route) { if (isset($newRoutes[$locale]) && $route === $newRoutes[$locale]) { continue; } $this->expiredRouteModel->addExpiredRoute($siteId, $nodeId, $locale, $route, $site->getBaseUrl($locale)); } } // check for order conflicts $nodeOrderIndex = $node->getOrderIndex(); $nodeParent = $node->getParent(); if (!$isRecursive && ($nodeOrderIndex != $oldNode->getOrderIndex() || $nodeParent != $oldNode->getParent())) { $orderIndex = 0; $parentNodes = $this->getChildren($siteId, $revision, $nodeParent, 0); foreach ($parentNodes as $parentNodeId => $parentNode) { $orderIndex++; $parentOrderIndex = $parentNode->getOrderIndex(); $isBefore = $parentOrderIndex < $nodeOrderIndex; if ($isBefore && $parentOrderIndex == $orderIndex) { continue; } elseif ($nodeOrderIndex == $parentOrderIndex && $nodeId != $parentNodeId) { $orderIndex++; $parentNode->setOrderIndex($orderIndex); $changedNodes[] = $parentNode; } elseif ($nodeId == $parentNodeId) { $orderIndex--; continue; } else { $parentNode->setOrderIndex($orderIndex); $changedNodes[] = $parentNode; } } } } catch (NodeNotFoundException $exception) { // new node in the revision } // check for new widgets if ($publishSite) { $isPublishSiteChanged = false; $usedWidgets = $node->getUsedWidgets(); $availableWidgetsSite = $site->getAvailableWidgets(); $availableWidgetsPublishSite = $publishSite->getAvailableWidgets(); foreach ($usedWidgets as $widgetId) { if (!$widgetId || isset($availableWidgetsPublishSite[$widgetId]) || !isset($availableWidgetsSite[$widgetId])) { continue; } $publishSite->set(Node::PROPERTY_WIDGET . '.' . $widgetId, $availableWidgetsSite[$widgetId], true); $isPublishSiteChanged = true; } if ($isPublishSiteChanged) { $changedNodes[] = $publishSite; } } // write the changed nodes foreach ($changedNodes as $changedNode) { $this->writeNode($changedNode); } // write the node file to the publish directory $nodeFile = $this->getNodeFile($node); $publishFile = $publishDirectory->getChild($nodeFile->getName()); if ($nodeFile->exists()) { // node has been created or updated $nodeFile->copy($publishFile); return null; } elseif ($publishFile->exists()) { // node has been deleted $publishFile->delete(); return $node; } }
/** * Gets an array of a node with all it's properties * @param \ride\library\cms\node\Node $node * @return array */ protected function getArrayFromNode(Node $node) { $array = array(); $array[self::PROPERTY_TYPE] = new NodeProperty(self::PROPERTY_TYPE, $node->getType()); $array[self::PROPERTY_ID] = new NodeProperty(self::PROPERTY_ID, $node->getId()); $array[self::PROPERTY_PARENT] = new NodeProperty(self::PROPERTY_PARENT, $node->getParent()); $array[self::PROPERTY_ORDER] = new NodeProperty(self::PROPERTY_ORDER, $node->getOrderIndex()); $array += $node->getProperties(); return $array; }