Skip to content
Merged
39 changes: 21 additions & 18 deletions libraries/joomla/table/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1287,14 +1287,15 @@ 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'))
$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 @@ -1347,7 +1348,8 @@ 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'))
$orderingField = $this->getColumnAlias('ordering');
if (!property_exists($this, $orderingField))
{
throw new UnexpectedValueException(sprintf('%s does not support ordering.', get_class($this)));
}
Expand All @@ -1356,10 +1358,10 @@ public function reorder($where = '')

// 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) . ', ' . $this->_db->quoteName($orderingField))
->from($this->_tbl)
->where('ordering >= 0')
->order('ordering');
->where($this->_db->quoteName($orderingField) . ' >= 0')
->order($this->_db->quoteName($orderingField));

// Setup the extra where and ordering clause data.
if ($where)
Expand All @@ -1374,15 +1376,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($this->_db->quoteName($orderingField) . ' = ' . ($i + 1));
$this->appendPrimaryKeys($query, $row);
$this->_db->setQuery($query);
$this->_db->execute();
Expand All @@ -1409,7 +1411,8 @@ 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'))
$orderingField = $this->getColumnAlias('ordering');
if (!property_exists($this, $orderingField))
{
throw new UnexpectedValueException(sprintf('%s does not support ordering.', get_class($this)));
}
Expand All @@ -1425,20 +1428,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) . ', ' . $this->_db->quoteName($orderingField))
->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($this->_db->quoteName($orderingField) . ' < ' . (int) $this->$orderingField)
->order($this->_db->quoteName($orderingField) . ' 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($this->_db->quoteName($orderingField) . ' > ' . (int) $this->$orderingField)
->order($this->_db->quoteName($orderingField) . ' ASC');
}

// Add the custom WHERE clause if set.
Expand All @@ -1457,15 +1460,15 @@ 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($this->_db->quoteName($orderingField) . ' = ' . (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($this->_db->quoteName($orderingField) . ' = ' . (int) $this->$orderingField);
$this->appendPrimaryKeys($query, $row);
$this->_db->setQuery($query);
$this->_db->execute();
Expand All @@ -1478,7 +1481,7 @@ public function move($delta, $where = '')
// Update the ordering field for this instance.
$query->clear()
->update($this->_tbl)
->set('ordering = ' . (int) $this->ordering);
->set($this->_db->quoteName($orderingField) . ' = ' . (int) $this->$orderingField);
$this->appendPrimaryKeys($query);
$this->_db->setQuery($query);
$this->_db->execute();
Expand Down
7 changes: 5 additions & 2 deletions libraries/legacy/model/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,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 @@ -1318,9 +1320,10 @@ 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