Ejemplo n.º 1
0
    /**
     * Move
     *
     * @param ORM_MPTT|integer $target target node id or ORM_MPTT object.
     * @param bool $left_column use the left column or right column from target
     * @param integer $left_offset left value for the new node position.
     * @param integer $level_offset level
     * @param bool allow this movement to be allowed on the root node
     */
    protected function move($target, $left_column, $left_offset, $level_offset, $allow_root_target)
    {
        if (!$this->loaded()) {
            return FALSE;
        }
        // Make sure we have the most upto date version of this AFTER we lock
        $this->lock()->reload();
        if (!$target instanceof $this) {
            $target = ORM_MPTT::factory($this->_object_name, $target);
            if (!$target->loaded()) {
                $this->unlock();
                return FALSE;
            }
        } else {
            $target->reload();
        }
        // Stop $this being moved into a descendant or disallow if target is root
        if ($target->is_descendant($this) or $allow_root_target === FALSE and $target->is_root()) {
            $this->unlock();
            return FALSE;
        }
        $left_offset = ($left_column === TRUE ? $target->{$this->left_column} : $target->{$this->right_column}) + $left_offset;
        $level_offset = $target->{$this->level_column} - $this->{$this->level_column} + $level_offset;
        $size = $this->get_size();
        $this->create_space($left_offset, $size);
        // if node is moved to a position in the tree "above" its current placement
        // then its lft/rgt may have been altered by create_space
        $this->reload();
        $offset = $left_offset - $this->{$this->left_column};
        // Update the values.
        $this->_db->query(Database::UPDATE, 'UPDATE ' . $this->_table_name . ' SET `' . $this->left_column . '` = `' . $this->left_column . '` + ' . $offset . ', `' . $this->right_column . '` = `' . $this->right_column . '` + ' . $offset . '
		, `' . $this->level_column . '` = `' . $this->level_column . '` + ' . $level_offset . '
		, `' . $this->scope_column . '` = ' . $target->{$this->scope_column} . '
		WHERE `' . $this->left_column . '` >= ' . $this->{$this->left_column} . ' AND `' . $this->right_column . '` <= ' . $this->{$this->right_column} . ' AND `' . $this->scope_column . '` = ' . $this->{$this->scope_column}, FALSE);
        $this->delete_space($this->{$this->left_column}, $size);
        if ($this->path_calculation_enabled) {
            $this->update_path();
            parent::save();
        }
        $this->unlock();
        return $this;
    }
Ejemplo n.º 2
0
 /**
  * Move to Next Sibling.
  *
  * Moves the current node to the next sibling of the target node.
  *
  * @param ORM_MPTT|integer $target target node id or ORM_MPTT object.
  * @return ORM_MPTT
  */
 public function move_to_next_sibling($target)
 {
     // Move should only work on nodes that are already in the tree.. if its not already it the tree it needs to be inserted!
     if (!$this->loaded) {
         return FALSE;
     }
     $this->lock();
     $this->reload();
     // Make sure we have the most upto date version of this AFTER we lock
     if (!$target instanceof $this) {
         $target = ORM_MPTT::factory($this->table_name, $target);
     }
     // Stop $this being moved into a descendant
     if ($target->is_descendant($this)) {
         $this->unlock();
         return FALSE;
     }
     $new_left = $target->{$this->right_column} + 1;
     $level_offset = $target->{$this->level_column} - $this->{$this->level_column};
     $this->move($new_left, $level_offset, $target->{$this->scope_column});
     $this->unlock();
     return $this;
 }