protected function getLastChildPosition(NodeInterface $node, NodeInterface $newParent) { $method = array($this->_nodeClassName, 'where'); $pos = call_user_func($method, $this->parentCol(), '=', $newParent->__get($this->pkCol()))->max($this->sortCol()); if (is_numeric($pos) && $pos) { return (int) $pos + 1; } return 1; }
public static function flatify(NodeInterface $node, &$flatArray = NULL) { if ($flatArray === NULL) { $flatArray = array(); } $flatArray[] = $node; foreach ($node->childNodes() as $child) { static::flatify($child, $flatArray); } return $flatArray; }
/** * @brief Removes a child node * * @return NodeInterface */ public function removeChildNode(NodeInterface $childNode) { $deleteIndex = -1; $idx = 0; foreach ($this->_childNodes as $child) { if ($child->getIdentifier() == $childNode->getIdentifier()) { $deleteIndex = $idx; break; } $idx++; } if ($deleteIndex != -1) { unset($this->_childNodes[$deleteIndex]); $this->_childNodes = array_values($this->_childNodes); } return $this; }
public function toJsTree(NodeInterface $node, $titleProperty, $currentIdentifier = '', &$string = NULL) { if ($string === NULL) { $string = '<ul>'; } $liClasses = ['jstree-open']; if ($node->isRootNode()) { $liClasses[] = 'root-node'; } $spanClasses = []; if ($currentIdentifier == $node->getIdentifier()) { $spanClasses[] = 'active'; } $liClass = implode(' ', $liClasses); $spanClass = implode(' ', $spanClasses); $title = $node->{$titleProperty}; $id = $node->getIdentifier(); $string .= "\n <li id=\"node-{$id}\" class=\"{$liClass}\"><span class=\"{$spanClass}\">{$title}</span>"; if (count($node->childNodes())) { $string .= "\n <ul>"; foreach ($node->childNodes() as $child) { $this->toJsTree($child, $titleProperty, $currentIdentifier, $string); } $string .= "\n </ul>"; } $string .= "</li>"; return $string . "\n</ul>"; }
public function delete(Node $node, $deleteChildNodes = TRUE) { if (!$deleteChildNodes) { throw new DomainException('Non-recursive delete is not supported right now'); } $subtree = $this->get($node->__get($this->pkCol()), $node->__get($this->rootCol())); $byDepth = array(); foreach (Helper::flatify($subtree) as $deleteNode) { if (!isset($byDepth[$deleteNode->getDepth()])) { $byDepth[$deleteNode->getDepth()] = array(); } $byDepth[$deleteNode->getDepth()][] = $deleteNode; } krsort($byDepth, SORT_NUMERIC); foreach ($byDepth as $depth => $depthNodes) { foreach ($depthNodes as $deleteNode) { $deleteNode->delete(); } } return $this; }
/** * @brief Adds a childNode to this node * * @return NodeInterface */ public function addChildNode(NodeInterface $childNode) { $this->childNodes[] = $childNode; $childNode->setParentNode($this); return $this; }