/**
* Method to edit an existing record.
*
* @param string $key The name of the primary key of the URL variable.
* @param string $urlVar The name of the URL variable if different from the primary key
* (sometimes required to avoid router collisions).
*
* @return boolean True if access level check and checkout passes, false otherwise.
*
* @since 1.6
*/
public function edit($key = null, $urlVar = null)
{
// Do not cache the response to this, its a redirect, and mod_expires and google chrome browser bugs cache it forever!
Factory::getApplication()->allowCache(false);
$model = $this->getModel();
$table = $model->getTable();
$cid = (array) $this->input->post->get('cid', array(), 'int');
$context = "{$this->option}.edit.{$this->context}";
// Determine the name of the primary key for the data.
if (empty($key)) {
$key = $table->getKeyName();
}
// To avoid data collisions the urlVar may be different from the primary key.
if (empty($urlVar)) {
$urlVar = $key;
}
// Get the previous record id (if any) and the current record id.
$recordId = (int) (\count($cid) ? $cid[0] : $this->input->getInt($urlVar));
$checkin = $table->hasField('checked_out');
// Access check.
if (!$this->allowEdit(array($key => $recordId), $key)) {
$this->setMessage(Text::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'), 'error');
$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
return false;
}
// Attempt to check-out the new record for editing and redirect.
if ($checkin && !$model->checkout($recordId)) {
// Check-out failed, display a notice but allow the user to see the record.
$this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError()), 'error');
$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false));
return false;
} else {
// Check-out succeeded, push the new record id into the session.
$this->holdEditId($context, $recordId);
Factory::getApplication()->setUserState($context . '.data', null);
$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false));
return true;
}
}