JTableNested/orderUp
From Joomla! Documentation
< API16:JTableNested
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 left in the same level.
Syntax[edit]
orderUp($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 orderUp($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 left sibling node.
if (!$sibling = $this->_getNode($node->lft - 1, 'right')) {
// 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]