/**
* Saves the manually set order of records.
*
* @param array $pks An array of primary key ids.
* @param integer $order +1 or -1
*
* @return boolean Boolean true on success, false on failure
*
* @since 1.6
*/
public function saveorder($pks = array(), $order = null)
{
// Initialize re-usable member properties
$this->initBatch();
$conditions = array();
if (empty($pks)) {
Factory::getApplication()->enqueueMessage(Text::_($this->text_prefix . '_ERROR_NO_ITEMS_SELECTED'), 'error');
return false;
}
$orderingField = $this->table->getColumnAlias('ordering');
// Update ordering values
foreach ($pks as $i => $pk) {
$this->table->load((int) $pk);
// We don't want to modify tags on reorder, not removing the tagsHelper removes all associated tags
if ($this->table instanceof TaggableTableInterface) {
$this->table->clearTagsHelper();
}
// Access checks.
if (!$this->canEditState($this->table)) {
// Prune items that you can't change.
unset($pks[$i]);
Log::add(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), Log::WARNING, 'jerror');
} elseif ($this->table->{$orderingField} != $order[$i]) {
$this->table->{$orderingField} = $order[$i];
if (!$this->table->store()) {
$this->setError($this->table->getError());
return false;
}
// Remember to reorder within position and client_id
$condition = $this->getReorderConditions($this->table);
$found = false;
foreach ($conditions as $cond) {
if ($cond[1] == $condition) {
$found = true;
break;
}
}
if (!$found) {
$key = $this->table->getKeyName();
$conditions[] = array($this->table->{$key}, $condition);
}
}
}
// Execute reorder for each category.
foreach ($conditions as $cond) {
$this->table->load($cond[0]);
$this->table->reorder($cond[1]);
}
// Clear the component's cache
$this->cleanCache();
return true;
}