/** * @throws Exception */ public function beforeUpdate() { if ($this->node !== null && !$this->node->getIsNewRecord()) { $this->node->refresh(); } switch ($this->operation) { case self::OPERATION_MAKE_ROOT: if ($this->treeAttribute === false) { throw new Exception('Can not move a node as the root when "treeAttribute" is false.'); } if ($this->owner->isRoot()) { throw new Exception('Can not move the root node as the root.'); } break; case self::OPERATION_INSERT_BEFORE: case self::OPERATION_INSERT_AFTER: if ($this->node->isRoot()) { throw new Exception('Can not move a node when the target node is root.'); } case self::OPERATION_PREPEND_TO: case self::OPERATION_APPEND_TO: if ($this->node->getIsNewRecord()) { throw new Exception('Can not move a node when the target node is new record.'); } if ($this->owner->equals($this->node)) { throw new Exception('Can not move a node when the target node is same.'); } if ($this->node->isChildOf($this->owner)) { throw new Exception('Can not move a node when the target node is child.'); } } }
/** * @param bool $forInsertNear * @throws Exception */ protected function checkNode($forInsertNear = false) { if ($forInsertNear && $this->node->isRoot()) { throw new Exception('Can not move a node before/after root.'); } if ($this->node->getIsNewRecord()) { throw new Exception('Can not move a node when the target node is new record.'); } if ($this->owner->equals($this->node)) { throw new Exception('Can not move a node when the target node is same.'); } if ($this->checkLoop && $this->node->isChildOf($this->owner)) { throw new Exception('Can not move a node when the target node is child.'); } }
/** * @param int $to * @param int $depth * @throws Exception */ protected function insertNode($to, $depth = 0) { if ($this->node->getIsNewRecord()) { throw new Exception('Can not create a node when the target node is new record.'); } if ($depth === 0 && $this->node->isRoot()) { throw new Exception('Can not insert a node before/after root.'); } $this->owner->setAttribute($this->leftAttribute, $to); $this->owner->setAttribute($this->rightAttribute, $to + 1); $this->owner->setAttribute($this->depthAttribute, $this->node->getAttribute($this->depthAttribute) + $depth); if ($this->treeAttribute !== null) { $this->owner->setAttribute($this->treeAttribute, $this->node->getAttribute($this->treeAttribute)); } $this->shift($to, null, 2); }
/** * @throws Exception */ public function beforeUpdate() { if ($this->node !== null && !$this->node->getIsNewRecord()) { $this->node->refresh(); } switch ($this->operation) { case self::OPERATION_MAKE_ROOT: if ($this->treeAttribute === false) { throw new Exception('Can not move a node as the root when "treeAttribute" is false.'); } if ($this->owner->isRoot()) { throw new Exception('Can not move the root node as the root.'); } if (!$this->treeAttributeById) { if ($this->owner->getOldAttribute($this->treeAttribute) === $this->owner->getAttribute($this->treeAttribute)) { throw new Exception('Can not move a node as the root when its tree attribute "' . $this->treeAttribute . '" is not changed.'); } if ($this->owner->find()->andWhere([$this->treeAttribute => $this->owner->getAttribute($this->treeAttribute), $this->leftAttribute => 1])->one()) { throw new Exception('Can not move a node as the root when another root with this "' . $this->treeAttribute . '" already exists.'); } } break; case self::OPERATION_INSERT_BEFORE: case self::OPERATION_INSERT_AFTER: if ($this->node->isRoot()) { throw new Exception('Can not move a node when the target node is root.'); } case self::OPERATION_PREPEND_TO: case self::OPERATION_APPEND_TO: if ($this->node->getIsNewRecord()) { throw new Exception('Can not move a node when the target node is new record.'); } if ($this->owner->equals($this->node)) { throw new Exception('Can not move a node when the target node is same.'); } if ($this->node->isChildOf($this->owner)) { throw new Exception('Can not move a node when the target node is child.'); } } }