Source for file form.php
Documentation is available at form.php
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Controller tailored to suit most form-based admin operations.
* @todo Add ability to set redirect manually to better cope with frontend usage.
* The context for storing internal data, e.g. record.
* The URL option for the component.
* The URL view item variable.
* The URL view list variable.
* The prefix to use with controller messages.
* @param array $config An optional associative array of configuration settings.
// Guess the option as com_NameOfController
// Guess the JText message prefix. Defaults to the option.
// Guess the context as the suffix, eg: OptionControllerContent.
throw
new Exception(JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
// Guess the item view as the context.
// Guess the list view as the plural of the item view.
// @TODO Probably worth moving to an inflector class based on
// http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/
// Simple pluralisation based on public domain snippet by Paul Osman
// For more complex types, just manually set the variable in your class.
array('/(x|ch|ss|sh)$/i', "$1es"),
array('/([^aeiouy]|qu)y$/i', "$1ies"),
array('/([^aeiouy]|qu)ies$/i', "$1y"),
array('/(bu)s$/i', "$1ses"),
// Check for matches using regular expressions
foreach ($plural as $pattern)
// Apply, Save & New, and Save As copy should be standard on forms.
* Method to add a new record.
* @return mixed True if the record can be added, a error object if not.
// Set the internal error and also the redirect error.
$this->setError(JText::_('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED'));
// Clear the record edit information from the session.
$app->setUserState($context .
'.data', null);
// Redirect to the edit screen.
* Method to check if you can add a new record.
* Extended classes can override this if necessary.
* @param array $data An array of input data.
protected function allowAdd($data =
array())
return ($user->authorise('core.create', $this->option) ||
count($user->getAuthorisedCategories($this->option, 'core.create')));
* Method to check if you can add a new record.
* Extended classes can override this if necessary.
* @param array $data An array of input data.
* @param string $key The name of the key for the primary key; default is id.
protected function allowEdit($data =
array(), $key =
'id')
* Method to check if you can save a new or existing record.
* Extended classes can override this if necessary.
* @param array $data An array of input data.
* @param string $key The name of the key for the primary key.
protected function allowSave($data, $key =
'id')
$recordId = isset
($data[$key]) ?
$data[$key] :
'0';
* Method to run batch operations.
* @param JModelLegacy $model The model of the component being processed.
* @return boolean True if successful, false otherwise and internal error is set.
public function batch($model)
$vars =
$this->input->post->get('batch', array(), 'array');
$cid =
$this->input->post->get('cid', array(), 'array');
// Build an array of item contexts to check
// If we're coming from com_categories, we need to use extension vs. option
if (isset
($this->extension))
$option =
$this->extension;
$contexts[$id] =
$option .
'.' .
$this->context .
'.' .
$id;
// Attempt to run the batch operation.
if ($model->batch($vars, $cid, $contexts))
* Method to cancel an edit.
* @param string $key The name of the primary key of the URL variable.
* @return boolean True if access level checks pass, false otherwise.
public function cancel($key =
null)
$table =
$model->getTable();
$key =
$table->getKeyName();
$recordId =
$app->input->getInt($key);
// Attempt to check-in the current record.
if ($model->checkin($recordId) ===
false)
// Check-in failed, go back to the record and display a notice.
// Clean the session data and redirect.
$app->setUserState($context .
'.data', null);
* 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.
public function edit($key =
null, $urlVar =
null)
$table =
$model->getTable();
$cid =
$this->input->post->get('cid', array(), 'array');
// Determine the name of the primary key for the data.
$key =
$table->getKeyName();
// To avoid data collisions the urlVar may be different from the primary key.
// Get the previous record id (if any) and the current record id.
$recordId = (int)
(count($cid) ?
$cid[0] :
$this->input->getInt($urlVar));
if (!$this->allowEdit(array($key =>
$recordId), $key))
$this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
// 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.
// Check-out succeeded, push the new record id into the session.
$app->setUserState($context .
'.data', null);
* Method to get a model object, loading it if required.
* @param string $name The model name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
* @return object The model.
public function getModel($name =
'', $prefix =
'', $config =
array('ignore_request' =>
true))
return parent::getModel($name, $prefix, $config);
* Gets the URL arguments to append to an item redirect.
* @param integer $recordId The primary key id for the item.
* @param string $urlVar The name of the URL variable for the id.
* @return string The arguments to append to the redirect URL.
$tmpl =
$this->input->get('tmpl');
$layout =
$this->input->get('layout', 'edit');
$append .=
'&tmpl=' .
$tmpl;
$append .=
'&layout=' .
$layout;
$append .=
'&' .
$urlVar .
'=' .
$recordId;
* Gets the URL arguments to append to a list redirect.
* @return string The arguments to append to the redirect URL.
$append .=
'&tmpl=' .
$tmpl;
* Function that allows child controller access to model data
* after the data has been saved.
* @param JModelLegacy $model The data model object.
* @param array $validData The validated data.
protected function postSaveHook(JModelLegacy $model, $validData =
array())
* Method to load a row from version history
* @return mixed True if the record can be added, a error object if not.
$table =
$model->getTable();
$historyId =
$app->input->get('version_id', null, 'integer');
if (!$model->loadhistory($historyId, $table))
// Determine the name of the primary key for the data.
$key =
$table->getKeyName();
$recordId =
$table->$key;
// To avoid data collisions the urlVar may be different from the primary key.
$urlVar =
empty($this->urlVar) ?
$key :
$this->urlVar;
if (!$this->allowEdit(array($key =>
$recordId), $key))
$this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
$this->setMessage(JText::sprintf('JLIB_APPLICATION_SUCCESS_LOAD_HISTORY', $model->getState('save_date'), $model->getState('version_note')));
// Invoke the postSave method to allow for the child class to access the model.
* Method to save a 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 successful, false otherwise.
public function save($key =
null, $urlVar =
null)
// Check for request forgeries.
$table =
$model->getTable();
$data =
$this->input->post->get('jform', array(), 'array');
// Determine the name of the primary key for the data.
$key =
$table->getKeyName();
// To avoid data collisions the urlVar may be different from the primary key.
$recordId =
$this->input->getInt($urlVar);
// Populate the row id from the session.
// The save2copy task needs to be handled slightly differently.
if ($task ==
'save2copy')
// Check-in the original row.
if ($checkin &&
$model->checkin($data[$key]) ===
false)
// Check-in failed. Go back to the item and display a notice.
// Reset the ID and then treat the request as for Apply.
$this->setError(JText::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'));
// Validate the posted data.
// Sometimes the form needs some posted data, such as for plugins and modules.
$form =
$model->getForm($data, false);
$app->enqueueMessage($model->getError(), 'error');
// Test whether the data is valid.
$validData =
$model->validate($form, $data);
// Check for validation errors.
if ($validData ===
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 ($errors[$i] instanceof
Exception)
$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
$app->enqueueMessage($errors[$i], 'warning');
// Save the data in the session.
$app->setUserState($context .
'.data', $data);
// Redirect back to the edit screen.
if (!isset
($validData['tags']))
$validData['tags'] =
null;
// Attempt to save the data.
if (!$model->save($validData))
// Save the data in the session.
$app->setUserState($context .
'.data', $validData);
// Redirect back to the edit screen.
// Save succeeded, so check-in the record.
if ($checkin &&
$model->checkin($validData[$key]) ===
false)
// Save the data in the session.
$app->setUserState($context .
'.data', $validData);
// Check-in failed, so go back to the record and display a notice.
($lang->hasKey($this->text_prefix .
($recordId ==
0 &&
$app->isSite() ?
'_SUBMIT' :
'') .
'_SAVE_SUCCESS')
:
'JLIB_APPLICATION') .
($recordId ==
0 &&
$app->isSite() ?
'_SUBMIT' :
'') .
'_SAVE_SUCCESS'
// Redirect the user and adjust session state based on the chosen task.
// Set the record data in the session.
$recordId =
$model->getState($this->context .
'.id');
$app->setUserState($context .
'.data', null);
$model->checkout($recordId);
// Redirect back to the edit screen.
// Clear the record id and data from the session.
$app->setUserState($context .
'.data', null);
// Redirect back to the edit screen.
// Clear the record id and data from the session.
$app->setUserState($context .
'.data', null);
// Redirect to the list screen.
// Invoke the postSave method to allow for the child class to access the model.
Documentation generated on Tue, 19 Nov 2013 15:03:43 +0100 by phpDocumentor 1.4.3