API16:JControllerForm/edit
From Joomla! Documentation
Description
Method to edit an existing record.
<! removed transcluded page call, red link never existed >
Syntax
edit()
Defined in
libraries/joomla/application/component/controllerform.php
Importing
jimport( 'joomla.application.component.controllerform' );
Source Body
public function edit()
{
// Initialise variables.
$app = JFactory::getApplication();
$model = $this->getModel();
$table = $model->getTable();
$cid = JRequest::getVar('cid', array(), 'post', 'array');
$context = "$this->_option.edit.$this->_context";
$tmpl = JRequest::getString('tmpl');
$layout = JRequest::getString('layout', 'edit');
$append = '';
// Setup redirect info.
if ($tmpl) {
$append .= '&tmpl='.$tmpl;
}
if ($layout) {
$append .= '&layout='.$layout;
}
// Get the previous record id (if any) and the current record id.
$previousId = (int) $app->getUserState($context.'.id');
$recordId = (int) (count($cid) ? $cid[0] : JRequest::getInt('id'));
$checkin = property_exists($table, 'checked_out');
// Access check.
$key = $table->getKeyName();
if (!$this->_allowEdit(array($key => $recordId), $key)) {
$this->setRedirect(JRoute::_('index.php?option='.$this->_option.'&view='.$this->_view_items, false));
return JError::raiseWarning(403, 'JERROR_CORE_EDIT_NOT_PERMITTED');
}
// If record ids do not match, checkin previous record.
if ($checkin && ($previousId > 0) && ($recordId != $previousId))
{
if (!$model->checkin($previousId))
{
// Check-in failed, go back to the record and display a notice.
$message = JText::sprintf('JError_Checkin_failed', $model->getError());
$this->setRedirect('index.php?option='.$this->_option.'&view='.$this->_view_item.$append, $message, 'error');
return false;
}
}
// Attempt to check-out the new record for editing and redirect.
if ($checkin && !$model->checkout($recordId))
{
// Check-out failed, go back to the list and display a notice.
$message = JText::sprintf('JError_Checkout_failed', $model->getError());
$this->setRedirect('index.php?option='.$this->_option.'&view='.$this->_view_item.$append.'&id='.$recordId, $message, 'error');
return false;
}
else
{
// Check-out succeeded, push the new record id into the session.
$app->setUserState($context.'.id', $recordId);
$app->setUserState($context.'.data', null);
$this->setRedirect('index.php?option='.$this->_option.'&view='.$this->_view_item.$append);
return true;
}
}
<! removed transcluded page call, red link never existed >
Examples
Code Examples