API16

JTableNested/orderDown

From Joomla! Documentation

< API16:JTableNested
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Method to move a node one position to the right in the same level.



Syntax[edit]

orderDown($pk)
Parameter Name Default Value Description
$pk Primary key of the node to move.

Returns[edit]

boolean True on success.

Defined in[edit]

libraries/joomla/database/tablenested.php

Importing[edit]

jimport( 'joomla.database.tablenested' );

Source Body[edit]

public function orderDown($pk)
{
        // Initialise variables.
        $k = $this->_tbl_key;
        $pk = (is_null($pk)) ? $this->$k : $pk;

        // Lock the table for writing.
        if (!$this->_lock()) {
                // Error message set in lock method.
                return false;
        }

        // Get the node by primary key.
        if (!$node = $this->_getNode($pk)) {
                // Error message set in getNode method.
                $this->_unlock();
                return false;
        }

        // Get the right sibling node.
        if (!$sibling = $this->_getNode($node->rgt + 1, 'left')) {
                // Error message set in getNode method.
                $this->_unlock();
                return false;
        }

        // Get the primary keys of child nodes.
        $this->_db->setQuery(
                'SELECT `'.$this->_tbl_key.'`' .
                ' FROM `'.$this->_tbl.'`' .
                ' WHERE `lft` BETWEEN '.(int) $node->lft.' AND '.(int) $node->rgt
        );
        $children = $this->_db->loadResultArray();

        // Check for a database error.
        if ($this->_db->getErrorNum()) {
                $this->setError($this->_db->getErrorMsg());
                $this->_unlock();
                return false;
        }

        // Shift left and right values for the node and it's children.
        $this->_db->setQuery(
                'UPDATE `'.$this->_tbl.'`' .
                ' SET `lft` = `lft` + '.(int) $sibling->width.', `rgt` = `rgt` + '.(int) $sibling->width.'' .
                ' WHERE `lft` BETWEEN '.(int) $node->lft.' AND '.(int) $node->rgt
        );
        $this->_db->query();

        // Check for a database error.
        if ($this->_db->getErrorNum()) {
                $this->setError($this->_db->getErrorMsg());
                $this->_unlock();
                return false;
        }

        // Shift left and right values for the sibling and it's children.
        $this->_db->setQuery(
                'UPDATE `'.$this->_tbl.'`' .
                ' SET `lft` = `lft` - '.(int) $node->width.', `rgt` = `rgt` - '.(int) $node->width .
                ' WHERE `lft` BETWEEN '.(int) $sibling->lft.' AND '.(int) $sibling->rgt .
                ' AND `'.$this->_tbl_key.'` NOT IN ('.implode(',', $children).')'
        );
        $this->_db->query();

        // Check for a database error.
        if ($this->_db->getErrorNum()) {
                $this->setError($this->_db->getErrorMsg());
                $this->_unlock();
                return false;
        }

        // Unlock the table for writing.
        $this->_unlock();

        return true;
}



Examples[edit]

Code Examples[edit]