Skip to content
Merged
6 changes: 4 additions & 2 deletions libraries/joomla/table/nested.php
Original file line number Diff line number Diff line change
Expand Up @@ -1296,9 +1296,11 @@ public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = '')
->where('parent_id = %d');

// If the table has an ordering field, use that for ordering.
if (property_exists($this, 'ordering'))
$orderingField = $this->getColumnAlias('ordering');

if (property_exists($this, $orderingField))
{
$query->order('parent_id, ordering, lft');
$query->order('parent_id, ' . $this->_db->quoteName($orderingField) . ', lft');
}
else
{
Expand Down
56 changes: 33 additions & 23 deletions libraries/joomla/table/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ public function bind($src, $ignore = array())
}
}

// If the source value is not an array or object return false.
// Check if the source value is an array or object
if (!is_object($src) && !is_array($src))
{
throw new InvalidArgumentException(sprintf('%s::bind(*%s*)', get_class($this), gettype($src)));
Expand Down Expand Up @@ -1288,15 +1288,17 @@ public function isCheckedOut($with = 0, $against = null)
*/
public function getNextOrder($where = '')
{
// If there is no ordering field set an error and return false.
if (!property_exists($this, 'ordering'))
// Check if there is an ordering field set
$orderingField = $this->getColumnAlias('ordering');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely this is not doing what the doc block above it says?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you change the doc block?


if (!property_exists($this, $orderingField))
{
throw new UnexpectedValueException(sprintf('%s does not support ordering.', get_class($this)));
}

// Get the largest ordering value for a given where clause.
$query = $this->_db->getQuery(true)
->select('MAX(ordering)')
->select('MAX(' . $this->_db->quoteName($orderingField) . ')')
->from($this->_tbl);

if ($where)
Expand Down Expand Up @@ -1348,18 +1350,22 @@ public function getPrimaryKey(array $keys = array())
*/
public function reorder($where = '')
{
// If there is no ordering field set an error and return false.
if (!property_exists($this, 'ordering'))
// Check if there is an ordering field set
$orderingField = $this->getColumnAlias('ordering');

if (!property_exists($this, $orderingField))
{
throw new UnexpectedValueException(sprintf('%s does not support ordering.', get_class($this)));
}

$quotedOrderingField = $this->_db->quoteName($orderingField);

// Get the primary keys and ordering values for the selection.
$query = $this->_db->getQuery(true)
->select(implode(',', $this->_tbl_keys) . ', ordering')
->select(implode(',', $this->_tbl_keys) . ', ' . $quotedOrderingField)
->from($this->_tbl)
->where('ordering >= 0')
->order('ordering');
->where($quotedOrderingField . ' >= 0')
->order($quotedOrderingField);

// Setup the extra where and ordering clause data.
if ($where)
Expand All @@ -1374,15 +1380,15 @@ public function reorder($where = '')
foreach ($rows as $i => $row)
{
// Make sure the ordering is a positive integer.
if ($row->ordering >= 0)
if ($row->$orderingField >= 0)
{
// Only update rows that are necessary.
if ($row->ordering != $i + 1)
if ($row->$orderingField != $i + 1)
{
// Update the row ordering field.
$query->clear()
->update($this->_tbl)
->set('ordering = ' . ($i + 1));
->set($quotedOrderingField . ' = ' . ($i + 1));
$this->appendPrimaryKeys($query, $row);
$this->_db->setQuery($query);
$this->_db->execute();
Expand All @@ -1408,12 +1414,16 @@ public function reorder($where = '')
*/
public function move($delta, $where = '')
{
// If there is no ordering field set an error and return false.
if (!property_exists($this, 'ordering'))
// Check if there is an ordering field set
$orderingField = $this->getColumnAlias('ordering');

if (!property_exists($this, $orderingField))
{
throw new UnexpectedValueException(sprintf('%s does not support ordering.', get_class($this)));
}

$quotedOrderingField = $this->_db->quoteName($orderingField);

// If the change is none, do nothing.
if (empty($delta))
{
Expand All @@ -1424,20 +1434,20 @@ public function move($delta, $where = '')
$query = $this->_db->getQuery(true);

// Select the primary key and ordering values from the table.
$query->select(implode(',', $this->_tbl_keys) . ', ordering')
$query->select(implode(',', $this->_tbl_keys) . ', ' . $quotedOrderingField)
->from($this->_tbl);

// If the movement delta is negative move the row up.
if ($delta < 0)
{
$query->where('ordering < ' . (int) $this->ordering)
->order('ordering DESC');
$query->where($quotedOrderingField . ' < ' . (int) $this->$orderingField)
->order($quotedOrderingField . ' DESC');
}
// If the movement delta is positive move the row down.
elseif ($delta > 0)
{
$query->where('ordering > ' . (int) $this->ordering)
->order('ordering ASC');
$query->where($quotedOrderingField . ' > ' . (int) $this->$orderingField)
->order($quotedOrderingField . ' ASC');
}

// Add the custom WHERE clause if set.
Expand All @@ -1456,28 +1466,28 @@ public function move($delta, $where = '')
// Update the ordering field for this instance to the row's ordering value.
$query->clear()
->update($this->_tbl)
->set('ordering = ' . (int) $row->ordering);
->set($quotedOrderingField . ' = ' . (int) $row->$orderingField);
$this->appendPrimaryKeys($query);
$this->_db->setQuery($query);
$this->_db->execute();

// Update the ordering field for the row to this instance's ordering value.
$query->clear()
->update($this->_tbl)
->set('ordering = ' . (int) $this->ordering);
->set($quotedOrderingField . ' = ' . (int) $this->$orderingField);
$this->appendPrimaryKeys($query, $row);
$this->_db->setQuery($query);
$this->_db->execute();

// Update the instance value.
$this->ordering = $row->ordering;
$this->$orderingField = $row->$orderingField;
}
else
{
// Update the ordering field for this instance.
$query->clear()
->update($this->_tbl)
->set('ordering = ' . (int) $this->ordering);
->set($quotedOrderingField . ' = ' . (int) $this->$orderingField);
$this->appendPrimaryKeys($query);
$this->_db->setQuery($query);
$this->_db->execute();
Expand Down
6 changes: 4 additions & 2 deletions libraries/legacy/model/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,8 @@ public function saveorder($pks = array(), $order = null)
return JError::raiseWarning(500, JText::_($this->text_prefix . '_ERROR_NO_ITEMS_SELECTED'));
}

$orderingField = $table->getColumnAlias('ordering');

// Update ordering values
foreach ($pks as $i => $pk)
{
Expand All @@ -1320,9 +1322,9 @@ public function saveorder($pks = array(), $order = null)
unset($pks[$i]);
JLog::add(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), JLog::WARNING, 'jerror');
}
elseif ($table->ordering != $order[$i])
elseif ($table->$orderingField != $order[$i])
{
$table->ordering = $order[$i];
$table->$orderingField = $order[$i];

if ($type)
{
Expand Down