API16:JControllerForm/save
From Joomla! Documentation
Description
Method to save a record.
<! removed transcluded page call, red link never existed >
Syntax
save()
Defined in
libraries/joomla/application/component/controllerform.php
Importing
jimport( 'joomla.application.component.controllerform' );
Source Body
public function save()
{
// Check for request forgeries.
JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));
// Initialise variables.
$app = JFactory::getApplication();
$model = $this->getModel();
$table = $model->getTable();
$data = JRequest::getVar('jform', array(), 'post', 'array');
$checkin = property_exists($table, 'checked_out');
$context = "$this->_option.edit.$this->_context";
$task = $this->getTask();
$recordId = (int) $app->getUserState($context.'.id');
$tmpl = JRequest::getString('tmpl');
$layout = JRequest::getString('layout', 'edit');
$append = '';
// Setup redirect info.
if ($tmpl) {
$append .= '&tmpl='.$tmpl;
}
if ($layout) {
$append .= '&layout='.$layout;
}
// Populate the row id from the session.
$key = $table->getKeyName();
$data[$key] = $recordId;
// The save2copy task needs to be handled slightly differently.
if ($task == 'save2copy')
{
// Check-in the original row.
if ($checkin && !$model->checkin($data[$key]))
{
// Check-in failed, go back to the item and display a notice.
$message = JText::sprintf('JError_Checkin_saved', $model->getError());
$this->setRedirect('index.php?option='.$this->_option.'&view='.$this->_view_item.$append, $message, 'error');
return false;
}
// Reset the ID and then treat the request as for Apply.
$data['id'] = 0;
$task = 'apply';
}
// Access check.
if (!$this->_allowSave($data)) {
$this->setRedirect(JRoute::_('index.php?option='.$this->_option.'&view='.$this->_view_items, false));
return JError::raiseWarning(403, 'JError_Save_not_permitted');
}
// Validate the posted data.
$form = &$model->getForm();
if (!$form)
{
JError::raiseError(500, $model->getError());
return false;
}
$data = $model->validate($form, $data);
// Check for validation errors.
if ($data === false)
{
// Get the validation messages.
$errors = $model->getErrors();
// Push up to three validation messages out to the user.
for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
{
if (JError::isError($errors[$i])) {
$app->enqueueMessage($errors[$i]->getMessage(), 'notice');
}
else {
$app->enqueueMessage($errors[$i], 'notice');
}
}
// Save the data in the session.
$app->setUserState($context.'.data', $data);
// Redirect back to the edit screen.
$this->setRedirect(JRoute::_('index.php?option='.$this->_option.'&view='.$this->_view_item.$append, false));
return false;
}
// Attempt to save the data.
if (!$model->save($data))
{
// Save the data in the session.
$app->setUserState($context.'.data', $data);
// Redirect back to the edit screen.
$this->setMessage(JText::sprintf('JError_Save_failed', $model->getError()), 'notice');
$this->setRedirect(JRoute::_('index.php?option='.$this->_option.'&view='.$this->_view_item.$append, false));
return false;
}
// Save succeeded, check-in the record.
if ($checkin && !$model->checkin($data[$key]))
{
// Check-in failed, go back to the record and display a notice.
$message = JText::sprintf('JError_Checkin_saved', $model->getError());
$this->setRedirect('index.php?option='.$this->_option.'&view='.$this->_view_item.$append, $message, 'error');
return false;
}
$this->setMessage(JText::_('JCONTROLLER_SAVE_SUCCESS'));
// Redirect the user and adjust session state based on the chosen task.
switch ($task)
{
case 'apply':
// Set the record data in the session.
$app->setUserState($context.'.id', $model->getState($this->_context.'.id'));
$app->setUserState($context.'.data', null);
// Redirect back to the edit screen.
$this->setRedirect(JRoute::_('index.php?option='.$this->_option.'&view='.$this->_view_item.$append, false));
break;
case 'save2new':
// Clear the record id and data from the session.
$app->setUserState($context.'.id', null);
$app->setUserState($context.'.data', null);
// Redirect back to the edit screen.
$this->setRedirect(JRoute::_('index.php?option='.$this->_option.'&view='.$this->_view_item.$append, false));
break;
default:
// Clear the record id and data from the session.
$app->setUserState($context.'.id', null);
$app->setUserState($context.'.data', null);
// Redirect to the list screen.
$this->setRedirect(JRoute::_('index.php?option='.$this->_option.'&view='.$this->_view_list, false));
break;
}
return true;
}
<! removed transcluded page call, red link never existed >
Examples
Code Examples