public function saveorder()
{
// CSRF prevention
$this->csrfProtection();
$type = null;
$msg = null;
$model = $this->getModel()->savestate(false);
$ids = $this->getIDsFromRequest($model, false);
$orders = $this->input->get('order', array(), 'array');
// Before saving the order, I have to check I the table really supports the ordering feature
if (!$model->hasField('ordering')) {
$msg = sprintf('%s does not support ordering.', $model->getTableName());
$type = 'error';
} else {
$ordering = $model->getFieldAlias('ordering');
// Several methods could throw exceptions, so let's wrap everything in a try-catch
try {
if ($n = count($ids)) {
for ($i = 0; $i < $n; $i++) {
$item = $model->find($ids[$i]);
$neworder = (int) $orders[$i];
if (!$item instanceof DataModel) {
continue;
}
if ($item->getId() == $ids[$i]) {
$item->{$ordering} = $neworder;
$userId = $this->container->platform->getUser()->id;
if ($model->isLocked($userId)) {
$model->checkIn($userId);
}
$model->save($item);
}
}
}
$model->reorder();
} catch (\Exception $e) {
$msg = $e->getMessage();
$type = 'error';
}
}
// Redirect
if ($customURL = $this->input->getBase64('returnurl', '')) {
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->container->componentName . '&view=' . $this->container->inflector->pluralize($this->view) . $this->getItemidURLSuffix();
$this->setRedirect($url, $msg, $type);
}