2007-09-16 03:52:35 +02:00
|
|
|
<?php
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bulk of the form system
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for all forms.
|
|
|
|
* The form class is an extensible base for all forms on a sapphire application. It can be used
|
|
|
|
* either by extending it, and creating processor methods on the subclass, or by creating instances
|
|
|
|
* of form whose actions are handled by the parent controller.
|
2007-09-16 03:52:35 +02:00
|
|
|
*
|
|
|
|
* In either case, if you want to get a form to do anything, it must be inextricably tied to a
|
2007-07-19 12:40:28 +02:00
|
|
|
* controller. The constructor is passed a controller and a method on that controller. This method
|
|
|
|
* should return the form object, and it shouldn't require any arguments. Parameters, if necessary,
|
|
|
|
* can be passed using the URL or get variables. These restrictions are in place so that we can
|
|
|
|
* recreate the form object upon form submission, without the use of a session, which would be too
|
|
|
|
* resource-intensive.
|
|
|
|
*
|
|
|
|
* @example forms/Form.php See how you can create a form on your controller.
|
|
|
|
*/
|
|
|
|
class Form extends ViewableData {
|
|
|
|
protected $fields, $actions, $controller, $name, $validator;
|
|
|
|
protected $formMethod = "post";
|
|
|
|
|
|
|
|
public static $backup_post_data = false;
|
|
|
|
|
|
|
|
protected static $current_action;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Keeps track of whether this form has a default action or not.
|
|
|
|
* Set to false by $this->disableDefaultAction();
|
|
|
|
*/
|
|
|
|
protected $hasDefaultAction = true;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Accessed by Form.ss; modified by formHtmlContent.
|
|
|
|
* A performance enhancement over the generate-the-form-tag-and-then-remove-it code that was there previously
|
|
|
|
*/
|
|
|
|
public $IncludeFormTag = true;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new form, with the given fields an action buttons.
|
|
|
|
* @param controller The parent controller, necessary to create the appropriate form action tag.
|
2007-09-16 03:52:35 +02:00
|
|
|
* @param name The method on the controller that will return this form object.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param fields All of the fields in the form - a {@link FieldSet} of {@link FormField} objects.
|
|
|
|
* @param actions All of the action buttons in the form - a {@link FieldSet} of {@link FormAction} objects
|
|
|
|
*/
|
2007-09-16 03:52:35 +02:00
|
|
|
function __construct($controller, $name, FieldSet $fields, FieldSet $actions, $validator = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$field->setForm($this);
|
|
|
|
}
|
|
|
|
foreach($actions as $action) $actions->setForm($this);
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->fields = $fields;
|
|
|
|
$this->actions = $actions;
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->name = $name;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Form validation
|
|
|
|
if($validator) {
|
|
|
|
$this->validator = $validator;
|
|
|
|
$this->validator->setForm($this);
|
|
|
|
}
|
|
|
|
|
2007-09-16 03:52:35 +02:00
|
|
|
// Form error controls
|
2007-07-19 12:40:28 +02:00
|
|
|
$errorInfo = Session::get("FormInfo.{$this->FormName()}");
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($errorInfo['errors']) && is_array($errorInfo['errors'])){
|
|
|
|
foreach($errorInfo['errors'] as $error){
|
|
|
|
$field = $this->fields->dataFieldByName($error['fieldName']);
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
if(!$field){
|
2007-07-19 12:40:28 +02:00
|
|
|
$errorInfo['message'] = $error['message'];
|
|
|
|
$errorInfo['type'] = $error['messageType'];
|
|
|
|
} else {
|
|
|
|
$field->setError($error['message'],$error['messageType']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load data in from previous submission upon error
|
2007-09-16 03:52:35 +02:00
|
|
|
if(isset($errorInfo['data']))
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->loadDataFrom($errorInfo['data']);
|
|
|
|
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($errorInfo['message']) && isset($errorInfo['type'])) {
|
|
|
|
$this->setMessage($errorInfo['message'],$errorInfo['type']);
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Convert this form into a readonly form
|
|
|
|
*/
|
|
|
|
function makeReadonly() {
|
|
|
|
$this->transform(new ReadonlyTransformation());
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Add an error message to a field on this form. It will be saved into the session
|
|
|
|
* and used the next time this form is displayed.
|
|
|
|
*/
|
|
|
|
function addErrorMessage($fieldName, $message, $messageType) {
|
|
|
|
Session::addToArray("FormInfo.{$this->FormName()}.errors", array(
|
|
|
|
'fieldName' => $fieldName,
|
|
|
|
'message' => $message,
|
|
|
|
'messageType' => $messageType,
|
|
|
|
));
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function transform(FormTransformation $trans) {
|
|
|
|
$newFields = new FieldSet();
|
|
|
|
foreach($this->fields as $field) {
|
|
|
|
$newFields->push($field->transform($trans));
|
|
|
|
}
|
|
|
|
$this->fields = $newFields;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$newActions = new FieldSet();
|
|
|
|
foreach($this->actions as $action) {
|
|
|
|
$newActions->push($action->transform($trans));
|
|
|
|
}
|
|
|
|
$this->actions = $newActions;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We have to remove validation, if the fields are not editable ;-)
|
|
|
|
if($this->validator)
|
|
|
|
$this->validator->removeValidation();
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Get the {@link Validator} attached to this form.
|
|
|
|
* @return Validator
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getValidator() {
|
|
|
|
return $this->validator;
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Set the {@link Validator} on this form.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function setValidator( Validator $validator ) {
|
|
|
|
if($validator) {
|
|
|
|
$this->validator = $validator;
|
|
|
|
$this->validator->setForm($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-23 02:10:19 +01:00
|
|
|
function unsetValidator(){
|
|
|
|
$this->validator = null;
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Remove the {@link Validator} from this from.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function unsetValidator(){
|
|
|
|
$this->validator = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert this form to another format.
|
|
|
|
*/
|
|
|
|
function transformTo(FormTransformation $format) {
|
|
|
|
$newFields = new FieldSet();
|
|
|
|
foreach($this->fields as $field) {
|
|
|
|
$newFields->push($field->transformTo($format));
|
|
|
|
}
|
|
|
|
$this->fields = $newFields;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We have to remove validation, if the fields are not editable ;-)
|
|
|
|
if($this->validator)
|
|
|
|
$this->validator->removeValidation();
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the form's fields - used by the templates
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return FieldSet The form fields
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function Fields() {
|
2007-10-28 22:44:38 +01:00
|
|
|
if($this->securityTokenEnabled()) {
|
2007-10-26 04:23:55 +02:00
|
|
|
if(Session::get('SecurityID')) {
|
|
|
|
$securityID = Session::get('SecurityID');
|
|
|
|
} else {
|
|
|
|
$securityID = rand();
|
|
|
|
Session::set('SecurityID', $securityID);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fieldsClone = clone $this->fields;
|
|
|
|
$fieldsClone->push(new HiddenField('SecurityID', 'SecurityID', $securityID));
|
|
|
|
|
|
|
|
return $fieldsClone;
|
|
|
|
} else {
|
|
|
|
return $this->fields;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-21 01:22:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a named field from this form's fields.
|
|
|
|
* It will traverse into composite fields for you, to find the field you want.
|
|
|
|
* It will only return a data field.
|
|
|
|
* @return FormField
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function dataFieldByName($name) {
|
|
|
|
return $this->fields->dataFieldByName($name);
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the form's action buttons - used by the templates
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return FieldSet The action list
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function Actions() {
|
|
|
|
return $this->actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unset all form actions
|
|
|
|
*/
|
|
|
|
function unsetAllActions(){
|
|
|
|
$this->actions = new FieldSet();
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Unset the form's action button by its name
|
|
|
|
*/
|
|
|
|
function unsetActionByName($name) {
|
|
|
|
$action = $this->actions->fieldByName($name);
|
|
|
|
|
|
|
|
$action->unsetthis();
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Unset the form's dataField by its name
|
|
|
|
*/
|
|
|
|
function unsetDataFieldByName($fieldName){
|
|
|
|
//Debug::show($this->Fields()->dataFields());
|
|
|
|
foreach($this->Fields()->dataFields() as $child) {
|
|
|
|
//Debug::show($child->Name());
|
|
|
|
if(is_object($child) && ($child->Name() == $fieldName || $child->Title() == $fieldName)) {
|
|
|
|
$child=null;
|
|
|
|
/*array_splice($this->Fields()->dataFields(), $i, 1);
|
|
|
|
break;*/
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a field from the given tab.
|
|
|
|
*/
|
|
|
|
public function unsetFieldFromTab($tabName, $fieldName) {
|
|
|
|
// Find the tab
|
|
|
|
$tab = $this->Fields()->findOrMakeTab($tabName);
|
|
|
|
$tab->removeByName($fieldName);
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the attributes of the form tag - used by the templates
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return string The attribute string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function FormAttributes() {
|
|
|
|
// Forms shouldn't be cached, cos their error messages won't be shown
|
|
|
|
HTTP::set_cache_age(0);
|
|
|
|
|
|
|
|
if($this->validator) $this->validator->includeJavascriptValidation();
|
|
|
|
if($this->target) $target = " target=\"".$this->target."\"";
|
|
|
|
else $target = "";
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
return "id=\"" . $this->FormName() . "\" action=\"" . $this->FormAction()
|
2007-07-19 12:40:28 +02:00
|
|
|
. "\" method=\"" . $this->FormMethod() . "\" enctype=\"" . $this->FormEncType() . "\"$target";
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $target;
|
|
|
|
/**
|
|
|
|
* Set the target of this form to any value - useful for opening the form contents in a new window or refreshing another frame
|
|
|
|
* @param target The value of the target
|
|
|
|
*/
|
|
|
|
function setTarget($target) {
|
|
|
|
$this->target = $target;
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Returns the encoding type of the form.
|
|
|
|
* This will be either multipart/form-data - if there are field fields - or application/x-www-form-urlencoded
|
|
|
|
* @return string The encoding mime type
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function FormEncType() {
|
|
|
|
if(is_array($this->fields->dataFields())){
|
|
|
|
foreach($this->fields->dataFields() as $field) {
|
|
|
|
if(is_a($field, "FileField")) return "multipart/form-data";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "application/x-www-form-urlencoded";
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Returns the form method.
|
|
|
|
* @return string 'get' or 'post'
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function FormMethod() {
|
|
|
|
return $this->formMethod;
|
|
|
|
}
|
2007-08-21 01:22:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the form method - get or post
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function setFormMethod($method) {
|
|
|
|
$this->formMethod = strtolower($method);
|
|
|
|
if($this->formMethod == 'get') $this->fields->push(new HiddenField('executeForm', '', $this->name));
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Return the form's action attribute.
|
|
|
|
* This is build by adding an executeForm get variable to the parent controller's Link() value
|
|
|
|
* @return string The
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function FormAction() {
|
|
|
|
// "get" form needs ?executeForm added as a hidden field
|
|
|
|
if($this->formMethod == 'post') {
|
|
|
|
if($this->controller->hasMethod("FormObjectLink")) {
|
|
|
|
return $this->controller->FormObjectLink($this->name);
|
|
|
|
} else {
|
|
|
|
return $this->controller->Link() . "?executeForm=" . $this->name;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $this->controller->Link();
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-16 03:52:35 +02:00
|
|
|
* Returns the name of the form
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function FormName() {
|
|
|
|
return $this->class . '_' . str_replace('.','',$this->name);
|
|
|
|
}
|
|
|
|
|
2007-10-22 21:56:26 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function Name(){
|
2007-08-23 07:47:54 +02:00
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the field referenced by $_GET[fieldName].
|
|
|
|
* Used for embedding entire extra helper forms inside complex field types (such as ComplexTableField)
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return FormField The field referenced by $_GET[fieldName]
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function ReferencedField() {
|
|
|
|
return $this->dataFieldByName($_GET['fieldName']);
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The next functions store and modify the forms
|
2007-09-16 03:52:35 +02:00
|
|
|
* message attributes. messages are stored in session under
|
2007-07-19 12:40:28 +02:00
|
|
|
* $_SESSION[formname][message];
|
|
|
|
*/
|
|
|
|
protected $message, $messageType;
|
|
|
|
function Message() {
|
2007-09-16 03:52:35 +02:00
|
|
|
$this->getMessageFromSession();
|
2007-07-19 12:40:28 +02:00
|
|
|
$message = $this->message;
|
|
|
|
$this->clearMessage();
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
function MessageType() {
|
|
|
|
$this->getMessageFromSession();
|
|
|
|
return $this->messageType;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected function getMessageFromSession() {
|
|
|
|
if($this->message || $this->messageType) {
|
2007-09-16 03:52:35 +02:00
|
|
|
return $this->message;
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
|
|
|
$this->message = Session::get("FormInfo.{$this->FormName()}.formError.message");
|
|
|
|
$this->messageType = Session::get("FormInfo.{$this->FormName()}.formError.type");
|
|
|
|
|
|
|
|
Session::clear("FormInfo.{$this->FormName()}");
|
2007-09-16 03:52:35 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set a status message for the form.
|
|
|
|
* @param message the text of the message
|
|
|
|
* @param type Should be set to good, bad, or warning.
|
|
|
|
*/
|
|
|
|
function setMessage($message, $type) {
|
|
|
|
$this->message = $message;
|
|
|
|
$this->messageType = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a message to the session, for display next time this form is shown.
|
|
|
|
* @param message the text of the message
|
|
|
|
* @param type Should be set to good, bad, or warning.
|
|
|
|
*/
|
|
|
|
function sessionMessage($message, $type) {
|
|
|
|
Session::set("FormInfo.{$this->FormName()}.formError.message", $message);
|
|
|
|
Session::set("'FormInfo.{$this->FormName()}.formError.type", $type);
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static function messageForForm( $formName, $message, $type ) {
|
|
|
|
Session::set("FormInfo.{$formName}.formError.message", $message);
|
|
|
|
Session::set("FormInfo.{$formName}.formError.type", $type);
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function clearMessage() {
|
|
|
|
$this->message = null;
|
|
|
|
Session::clear("FormInfo.{$this->FormName()}.errors");
|
|
|
|
Session::clear("FormInfo.{$this->FormName()}.formError");
|
|
|
|
}
|
|
|
|
function resetValidation() {
|
|
|
|
Session::clear("FormInfo.{$this->FormName()}.errors");
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $record;
|
2007-08-21 01:22:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the DataObject that has given this form its data.
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getRecord() {
|
|
|
|
return $this->record;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Processing that occurs before a form is executed.
|
|
|
|
* This includes form validation, if it fails, we redirect back
|
|
|
|
* to the form with appropriate error messages
|
|
|
|
*/
|
|
|
|
function beforeProcessing(){
|
|
|
|
if($this->validator){
|
2007-09-16 03:52:35 +02:00
|
|
|
$errors = $this->validator->validate();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($errors){
|
|
|
|
if(Director::is_ajax()) {
|
|
|
|
// Send validation errors back as JSON with a flag at the start
|
|
|
|
//echo "VALIDATIONERROR:" . Convert::array2json($errors);
|
2007-10-25 04:47:45 +02:00
|
|
|
FormResponse::status_message(_t('Form.VALIDATIONFAILED', 'Validation failed'), 'bad');
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($errors as $error) {
|
|
|
|
FormResponse::add(sprintf(
|
|
|
|
"validationError('%s', '%s', '%s');\n",
|
|
|
|
Convert::raw2js($error['fieldName']),
|
|
|
|
Convert::raw2js($error['message']),
|
|
|
|
Convert::raw2js($error['messageType'])
|
|
|
|
));
|
|
|
|
}
|
|
|
|
echo FormResponse::respond();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$data = $this->getData();
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// People will get worried if you leave credit card information in session..
|
|
|
|
if(isset($data['CreditCardNumber'])) unset($data['CreditCardNumber']);
|
|
|
|
if(isset($data['DateExpiry'])) unset($data['Expiry']);
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Load errors into session and post back
|
|
|
|
Session::set("FormInfo.{$this->FormName()}", array(
|
|
|
|
'errors' => $errors,
|
|
|
|
'data' => $data,
|
|
|
|
));
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
Director::redirectBack();
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Load data from the given object.
|
|
|
|
* It will call $object->MyField to get the value of MyField.
|
|
|
|
* If you passed an array, it will call $object[MyField]
|
|
|
|
* @param object Either an object or an array to get the data from.
|
|
|
|
* @param forceChanges Load blank values into the form.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function loadDataFrom($object, $loadBlanks = false) {
|
|
|
|
if(is_object($object)) {
|
|
|
|
$o = true;
|
|
|
|
$this->record = $object;
|
|
|
|
} else if(is_array($object)) {
|
|
|
|
$o = false;
|
|
|
|
} else {
|
|
|
|
user_error("Form::loadDataFrom() not passed an array or an object", E_USER_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$dataFields = $this->fields->dataFields();
|
|
|
|
if($dataFields) foreach($dataFields as $field) {
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($name = $field->Name()) {
|
|
|
|
if($o) {
|
2007-11-19 02:26:49 +01:00
|
|
|
// this was failing with the field named 'Name'
|
2007-11-19 02:27:35 +01:00
|
|
|
$val = $object->__get($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$val = isset($object[$name]) ? $object[$name] : null;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// First check looks for (fieldname)_unchanged, an indicator that we shouldn't overwrite the field value
|
|
|
|
if($o || !isset($object[$name . '_unchanged'])) {
|
|
|
|
// Second check was the original check: save the value if we have one
|
2007-09-16 03:52:35 +02:00
|
|
|
if(isset($val) || $loadBlanks) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$field->setValue($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load data from the given object.
|
|
|
|
* It will call $object->MyField to get the value of MyField.
|
|
|
|
* If you passed an array, it will call $object[MyField]
|
|
|
|
*/
|
|
|
|
function loadNonBlankDataFrom($object) {
|
|
|
|
$this->record = $object;
|
|
|
|
if(is_object($object)) $o = true;
|
|
|
|
else if(is_array($object)) $o = false;
|
|
|
|
else {
|
|
|
|
user_error("Form::loadDataFrom() not passed an array or an object", E_USER_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$dataFields = $this->fields->dataFields();
|
|
|
|
if($dataFields) foreach($dataFields as $field) {
|
|
|
|
$name = $field->Name();
|
2007-08-10 04:20:47 +02:00
|
|
|
$val = $o ? $object->$name : (isset($object[$name]) ? $object[$name] : null);
|
2007-07-19 12:40:28 +02:00
|
|
|
if($name && $val) $field->setValue($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Save the contents of this form into the given data object.
|
|
|
|
* It will make use of setCastedField() to do this.
|
|
|
|
*/
|
2007-09-16 03:52:35 +02:00
|
|
|
function saveInto(DataObject $dataObject) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$dataFields = $this->fields->dataFields();
|
|
|
|
$lastField = null;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($dataFields) foreach($dataFields as $field) {
|
|
|
|
$saveMethod = "save{$field->Name()}";
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($field->Name() == "ClassName"){
|
2007-09-16 03:52:35 +02:00
|
|
|
$lastField = $field;
|
2007-07-19 12:40:28 +02:00
|
|
|
}else if( $dataObject->hasMethod( $saveMethod ) ){
|
2007-11-19 02:12:35 +01:00
|
|
|
$dataObject->$saveMethod( $field->dataValue());
|
2007-07-19 12:40:28 +02:00
|
|
|
} else if($field->Name() != "ID"){
|
2007-09-16 03:52:35 +02:00
|
|
|
$field->saveInto($dataObject);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if($lastField) $lastField->saveInto($dataObject);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the data from this form
|
|
|
|
*/
|
|
|
|
function getData() {
|
|
|
|
$dataFields = $this->fields->dataFields();
|
|
|
|
if($dataFields){
|
|
|
|
foreach($dataFields as $field) {
|
|
|
|
if($field->Name()) {
|
|
|
|
$data[$field->Name()] = $field->dataValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-11-19 03:02:25 +01:00
|
|
|
function resetData($fieldName, $fieldValue){
|
|
|
|
$dataFields = $this->fields->dataFields();
|
|
|
|
if($dataFields){
|
|
|
|
foreach($dataFields as $field) {
|
|
|
|
if($field->Name()==$fieldName) {
|
|
|
|
$field = $field->setValue($fieldValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Call the given method on the given field.
|
|
|
|
* This is used by Ajax-savvy form fields. By putting '&action=callfieldmethod' to the end
|
|
|
|
* of the form action, they can access server-side data.
|
|
|
|
* @param fieldName The name of the field. Can be overridden by $_REQUEST[fieldName]
|
|
|
|
* @param methodName The name of the field. Can be overridden by $_REQUEST[methodName]
|
|
|
|
*/
|
|
|
|
|
|
|
|
function callfieldmethod($data) {
|
|
|
|
$fieldName = $data['fieldName'];
|
|
|
|
$methodName = $data['methodName'];
|
|
|
|
$fields = $this->fields->dataFields();
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// special treatment needed for TableField-class and TreeDropdownField
|
|
|
|
if(strpos($fieldName, '[')) {
|
|
|
|
preg_match_all('/([^\[]*)/',$fieldName, $fieldNameMatches);
|
|
|
|
preg_match_all('/\[([^\]]*)\]/',$fieldName, $subFieldMatches);
|
|
|
|
$tableFieldName = $fieldNameMatches[1][0];
|
|
|
|
$subFieldName = $subFieldMatches[1][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($tableFieldName) && isset($subFieldName) && is_a($fields[$tableFieldName], 'TableField')) {
|
|
|
|
$field = $fields[$tableFieldName]->getField($subFieldName, $fieldName);
|
2007-09-16 03:52:35 +02:00
|
|
|
return $field->$methodName();
|
2007-09-14 19:10:37 +02:00
|
|
|
} else if(isset($fields[$fieldName])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $fields[$fieldName]->$methodName();
|
|
|
|
} else {
|
|
|
|
user_error("Form::callfieldmethod() Field '$fieldName' not found", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return a rendered version of this form.
|
|
|
|
* This is returned when you access a form as $FormObject rather than <% control FormObject %>
|
|
|
|
*/
|
|
|
|
function forTemplate() {
|
|
|
|
$form = $this->renderWith("Form");
|
|
|
|
return $form;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns an HTML rendition of this form, without the <form> tag itself.
|
|
|
|
* Attaches 3 extra hidden files, _form_action, _form_name, _form_method, and _form_enctype. These are
|
|
|
|
* the attributes of the form. These fields can be used to send the form to Ajax.
|
|
|
|
*/
|
|
|
|
function formHtmlContent() {
|
|
|
|
$this->IncludeFormTag = false;
|
|
|
|
$content = $this->forTemplate();
|
|
|
|
$this->IncludeFormTag = true;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$content .= "<input type=\"hidden\" name=\"_form_action\" id=\"" . $this->FormName . "_form_action\" value=\"" . $this->FormAction() . "\" />\n";
|
|
|
|
$content .= "<input type=\"hidden\" name=\"_form_name\" value=\"" . $this->FormName() . "\" />\n";
|
|
|
|
$content .= "<input type=\"hidden\" name=\"_form_method\" value=\"" . $this->FormMethod() . "\" />\n";
|
|
|
|
$content .= "<input type=\"hidden\" name=\"_form_enctype\" value=\"" . $this->FormEncType() . "\" />\n";
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $content;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function debug() {
|
|
|
|
$result = "<h3>$this->class</h3><ul>";
|
|
|
|
foreach($this->fields as $field) {
|
|
|
|
$result .= "<li>$field" . $field->debug() . "</li>";
|
|
|
|
}
|
|
|
|
$result .= "</ul>";
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if( $this->validator )
|
|
|
|
$result .= "<h3>Validator</h3>" . $this->validator->debug();
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Render this form using the given template, and return the result as a string
|
|
|
|
* You can pass either an SSViewer or a template name
|
|
|
|
*/
|
|
|
|
function renderWithoutActionButton($template) {
|
|
|
|
$custom = $this->customise(array(
|
|
|
|
"Actions" => "",
|
|
|
|
));
|
|
|
|
|
|
|
|
if(is_string($template)) $template = new SSViewer($template);
|
|
|
|
return $template->process($custom);
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $buttonClickedFunc;
|
|
|
|
/**
|
|
|
|
* Sets the button that was clicked. This should only be called by the Controller.
|
|
|
|
* @param funcName The name of the action method that will be called.
|
|
|
|
*/
|
|
|
|
function setButtonClicked($funcName) {
|
|
|
|
$this->buttonClickedFunc = $funcName;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function buttonClicked() {
|
|
|
|
foreach($this->actions as $action) {
|
|
|
|
if($this->buttonClickedFunc == $action->actionName()) return $action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Return the default button that should be clicked when another one isn't available
|
|
|
|
*/
|
|
|
|
function defaultAction() {
|
2007-09-16 03:52:35 +02:00
|
|
|
if($this->hasDefaultAction && $this->actions)
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->actions->First();
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Disable the default button.
|
|
|
|
* Ordinarily, when a form is processed and no action_XXX button is available, then the first button in the actions list
|
|
|
|
* will be pressed. However, if this is "delete", for example, this isn't such a good idea.
|
|
|
|
*/
|
|
|
|
function disableDefaultAction() {
|
|
|
|
$this->hasDefaultAction = false;
|
|
|
|
}
|
2007-10-26 04:23:55 +02:00
|
|
|
|
|
|
|
private $security = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the requirement of a SecurityID in the Form. This security protects
|
|
|
|
* against CSRF attacks, but you should disable this if you don't want to tie
|
|
|
|
* a form to a session - eg a search form.
|
|
|
|
*/
|
2007-10-28 22:44:38 +01:00
|
|
|
function disableSecurityToken() {
|
2007-10-26 04:23:55 +02:00
|
|
|
$this->security = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if security is enabled - that is if the SecurityID
|
|
|
|
* should be included and checked on this form.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2007-10-28 22:44:38 +01:00
|
|
|
function securityTokenEnabled() {
|
2007-10-26 04:23:55 +02:00
|
|
|
return $this->security;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the name of a field, if that's the only field that the current controller is interested in.
|
|
|
|
* It checks for a call to the callfieldmethod action.
|
|
|
|
* This is useful for optimising your forms
|
|
|
|
*/
|
|
|
|
static function single_field_required() {
|
|
|
|
if(self::current_action() == 'callfieldmethod') return $_REQUEST['fieldName'];
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the current form action being called, if available.
|
|
|
|
* This is useful for optimising your forms
|
|
|
|
*/
|
|
|
|
static function current_action() {
|
|
|
|
return self::$current_action;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the current form action. Should only be called by Controller.
|
|
|
|
*/
|
|
|
|
static function set_current_action($action) {
|
|
|
|
self::$current_action = $action;
|
|
|
|
}
|
2007-08-17 05:09:46 +02:00
|
|
|
|
2007-10-26 04:23:55 +02:00
|
|
|
|
2007-08-17 05:09:46 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TESTING HELPERS
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Test a submission of this form.
|
|
|
|
* @return HTTPResponse the response object that the handling controller produces. You can interrogate this in your unit test.
|
|
|
|
*/
|
2007-08-17 05:09:46 +02:00
|
|
|
function testSubmission($action, $data) {
|
|
|
|
$data['action_' . $action] = true;
|
2007-08-17 07:44:46 +02:00
|
|
|
$data['executeForm'] = $this->name;
|
2007-08-21 00:39:44 +02:00
|
|
|
|
|
|
|
return Director::test($this->FormAction(), $data, Controller::curr()->getSession());
|
2007-08-17 05:09:46 +02:00
|
|
|
|
2007-08-21 00:39:44 +02:00
|
|
|
//$response = $this->controller->run($data);
|
|
|
|
//return $response;
|
2007-08-17 07:44:46 +02:00
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Test an ajax submission of this form.
|
|
|
|
* @return HTTPResponse the response object that the handling controller produces. You can interrogate this in your unit test.
|
|
|
|
*/
|
2007-08-17 07:44:46 +02:00
|
|
|
function testAjaxSubmission($action, $data) {
|
|
|
|
$data['ajax'] = 1;
|
|
|
|
return $this->testSubmission($action, $data);
|
2007-08-17 05:09:46 +02:00
|
|
|
}
|
2007-11-23 02:10:19 +01:00
|
|
|
function dropDatalessField(){
|
|
|
|
foreach($this->Fields() as $field){
|
|
|
|
if(get_class($field)!='SelectionGroup'&&get_class($field)!='TableListField'&&!is_subclass_of($field, 'TableListField')){
|
|
|
|
if((get_class($field)=='DatalessField' ||is_subclass_of($field, 'DatalessField'))&&get_class($field)!='HeaderField'&&get_class($field)!='LabelField'){
|
|
|
|
$this->Fields()->removeByName($field->Name());
|
|
|
|
}elseif($field->isComposite()){
|
|
|
|
$field->dropDatalessField();
|
|
|
|
}else{
|
|
|
|
if(get_class($field) != "HeaderField" &&get_class($field) != "LabelField"&& $field->Value() === NULL){
|
|
|
|
$this->Fields()->removeByName($field->Name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|