/** * Generate a node with a unique name. * * @param NodeType $nodeType * @param Translation $translation * @param Node|null $parent * @param Tag|null $tag * @param boolean $pushToTop * * @return RZ\Roadiz\Core\Entities\NodesSources */ public function generate(NodeType $nodeType, Translation $translation, Node $parent = null, Tag $tag = null, $pushToTop = false) { $name = $nodeType->getDisplayName() . " " . uniqid(); $node = new Node($nodeType); $node->setParent($parent); $node->setNodeName($name); if (null !== $tag) { $node->addTag($tag); } $this->entityManager->persist($node); if ($pushToTop) { $node->setPosition(0.5); } $sourceClass = "GeneratedNodeSources\\" . $nodeType->getSourceEntityClassName(); $source = new $sourceClass($node, $translation); $source->setTitle($name); $this->entityManager->persist($source); $this->entityManager->flush(); return $source; }
/** * @param array $data * @param Node $parentNode * @param Translation $translation * * @return RZ\Roadiz\Core\Entities\Node */ protected function createChildNode($data, Node $parentNode = null, Translation $translation = null) { $type = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\NodeType', (int) $data['nodeTypeId']); if (null === $type) { throw new \Exception("Cannot create a node without a valid node-type", 1); } if (null !== $parentNode && $data['parentId'] != $parentNode->getId()) { throw new \Exception("Requested parent node does not match form values", 1); } $node = new Node($type); $node->setParent($parentNode); $node->setNodeName($data['nodeName']); $this->getService('em')->persist($node); $sourceClass = "GeneratedNodeSources\\" . $type->getSourceEntityClassName(); $source = new $sourceClass($node, $translation); $source->setTitle($data['nodeName']); $this->getService('em')->persist($source); $this->getService('em')->flush(); return $node; }
/** * @param array $parameters * @param Node $node */ protected function updatePosition($parameters, Node $node) { /* * First, we set the new parent */ $parent = null; if (!empty($parameters['newParent']) && $parameters['newParent'] > 0) { $parent = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Node', (int) $parameters['newParent']); if ($parent !== null) { $node->setParent($parent); } } else { // if no parent or null we place node at root level $node->setParent(null); } /* * Then compute new position */ if (!empty($parameters['nextNodeId']) && $parameters['nextNodeId'] > 0) { $nextNode = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Node', (int) $parameters['nextNodeId']); if ($nextNode !== null) { $node->setPosition($nextNode->getPosition() - 0.5); } } elseif (!empty($parameters['prevNodeId']) && $parameters['prevNodeId'] > 0) { $prevNode = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Node', (int) $parameters['prevNodeId']); if ($prevNode !== null) { $node->setPosition($prevNode->getPosition() + 0.5); } } elseif (!empty($parameters['firstPosition']) && (bool) $parameters['firstPosition'] === true) { $node->setPosition(-0.5); } elseif (!empty($parameters['lastPosition']) && (bool) $parameters['lastPosition'] === true) { $node->setPosition(9999999); } // Apply position update before cleaning $this->getService('em')->flush(); if ($parent !== null) { $parent->getHandler()->cleanChildrenPositions(); } else { NodeHandler::cleanRootNodesPositions(); } /* * Dispatch event */ $event = new FilterNodeEvent($node); $this->getService('dispatcher')->dispatch(NodeEvents::NODE_UPDATED, $event); }
private function duplicateRec(Node $node, $level) { $childrenArray = []; $sourceArray = []; $childs = new ArrayCollection($node->getChildren()->toArray()); $node->getChildren()->clear(); foreach ($childs as $child) { $childrenArray[] = $this->duplicateRec($child, $level + 1); } $nodeSources = new ArrayCollection($node->getNodeSources()->toArray()); $node->getNodeSources()->clear(); foreach ($nodeSources as $nodeSource) { $nodeSource->setNode(null); $tran = Kernel::getService('em')->merge($nodeSource->getTranslation()); $nodeSource->setTranslation($tran); Kernel::getService('em')->persist($nodeSource); $nsdocs = $nodeSource->getDocumentsByFields(); foreach ($nsdocs as $nsdoc) { $nsdoc->setNodeSource($nodeSource); $doc = Kernel::getService('em')->merge($nsdoc->getDocument()); $nsdoc->setDocument($doc); $f = Kernel::getService('em')->merge($nsdoc->getField()); $nsdoc->setField($f); Kernel::getService('em')->persist($nsdoc); } Kernel::getService('em')->flush(); $sourceArray[] = $nodeSource; } $nodetype = Kernel::getService('em')->merge($node->getNodeType()); $node->setNodeType($nodetype); $node->setParent(null); Kernel::getService('em')->persist($node); foreach ($childrenArray as $child) { $child->setParent($node); } foreach ($sourceArray as $source) { $source->setNode($node); } Kernel::getService('em')->flush(); return $node; }