Form Controller

This method is used to edit an existing record.

  • string $key - The name of the primary key of the URL variable.
  • 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.
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!
\JFactory::getApplication()->allowCache(false);

$model = $this->getModel();
$table = $model->getTable();
$cid = $this->input->post->get('cid', array(), 'array');
$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 = property_exists($table, $table->getColumnAlias('checked_out'));

// Access check.
if (!$this->allowEdit(array($key => $recordId), $key))
{
$this->setError(\JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
$this->setMessage($this->getError(), 'error');

$this->setRedirect(
\JRoute::_(
'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->setError(\JText::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError()));
$this->setMessage($this->getError(), 'error');

$this->setRedirect(
\JRoute::_(
'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);
\JFactory::getApplication()->setUserState($context . '.data', null);

$this->setRedirect(
\JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item
. $this->getRedirectToItemAppend($recordId, $urlVar), false
)
);

return true;
}
}