"Varchar", "OnCompleteMessage" => "HTMLText", "ShowClearButton" => "Boolean" ); /** * @var Array Default values of variables when this page is created */ static $defaults = array( 'Content' => '$UserDefinedForm', 'OnCompleteMessage' => '
Thanks, we\'ve received your submission.
' ); /** * @var Array */ static $has_many = array( "Fields" => "EditableFormField", "Submissions" => "SubmittedForm", "EmailRecipients" => "UserDefinedForm_EmailRecipient" ); /** * Setup the CMS Fields for the User Defined Form * * @return FieldSet */ public function getCMSFields() { $fields = parent::getCMSFields(); // field editor $fields->addFieldToTab("Root.Content."._t('UserDefinedForm.FORM', 'Form'), new FieldEditor("Fields", 'Fields', "", $this )); // view the submissions $fields->addFieldToTab("Root.Content."._t('UserDefinedForm.SUBMISSIONS','Submissions'), new SubmittedFormReportField( "Reports", _t('UserDefinedForm.RECEIVED', 'Received Submissions'), "", $this ) ); // who do we email on submission $emailRecipients = new HasManyComplexTableField($this, 'EmailRecipients', 'UserDefinedForm_EmailRecipient', array( 'EmailAddress' => 'Email', 'EmailSubject' => 'Subject', 'EmailFrom' => 'From' ), 'getCMSFields_forPopup', "FormID = '$this->ID'" ); $emailRecipients->setAddTitle(_t('UserDefinedForm.AEMAILRECIPIENT', 'A Email Recipient')); $fields->addFieldToTab("Root.Content."._t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $emailRecipients); // text to show on complete $onCompleteFieldSet = new FieldSet( new HtmlEditorField( "OnCompleteMessage", _t('UserDefinedForm.ONCOMPLETELABEL', 'Show on completion'),3,"",_t('UserDefinedForm.ONCOMPLETEMESSAGE', $this->OnCompleteMessage), $this ), new TextField("EmailOnSubmitSubject", _t('UserDefinedForm.ONSUBMITSUBJECT', 'Email Subject')), new HtmlEditorField( "EmailMessageToSubmitter", _t('UserDefinedForm.EMAILMESSAGETOSUBMITTER', 'Email message to submitter'),3,"",_t('UserDefinedForm.EMAILMESSAGETOSUBMITTER', $this->EmailMessageToSubmitter), $this ) ); $fields->addFieldsToTab("Root.Content."._t('UserDefinedForm.ONCOMPLETE','On complete'), $onCompleteFieldSet); return $fields; } /** * Called on before delete remove all the fields from the database */ public function delete() { foreach($this->Fields() as $field) { $field->delete(); } parent::delete(); } /** * Custom Form Actions for the form * * @param bool Is the Form readonly * @return FieldSet */ public function customFormActions($isReadonly = false) { return new FieldSet( new TextField("SubmitButtonText", _t('UserDefinedForm.TEXTONSUBMIT', 'Text on submit button:'), $this->SubmitButtonText), new CheckboxField("ShowClearButton", _t('UserDefinedForm.SHOWCLEARFORM', 'Show Clear Form Button'), $this->ShowClearButton) ); } /** * Duplicate this UserDefinedForm page, and its form fields. * Submissions, on the other hand, won't be duplicated. * * @return Page */ public function duplicate() { $page = parent::duplicate(); foreach($this->Fields() as $field) { $newField = $field->duplicate(); $newField->ParentID = $page->ID; $newField->write(); } return $page; } } /** * Controller for the {@link UserDefinedForm} page type. * * @package userform * @subpackage pagetypes */ class UserDefinedForm_Controller extends Page_Controller { /** * Load all the custom jquery needed to run the custom * validation */ public function init() { // block prototype validation Validator::set_javascript_validation_handler('none'); // load the jquery Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery/plugins/validate/jquery.validate.min.js'); parent::init(); } /** * Using $UserDefinedForm in the Content area of the page shows * where the form should be rendered into. If it does not exist * then default back to $Form * * @return Array */ public function index() { if($this->Content && $form = $this->Form()) { $hasLocation = stristr($this->Content, '$UserDefinedForm'); if($hasLocation) { $content = str_ireplace('$UserDefinedForm', $form->forTemplate(), $this->Content); return array( 'Content' => $content, 'Form' => "" ); } } return array( 'Content' => $this->Content, 'Form' => $this->Form ); } /** * User Defined Form. Feature of the user defined form is if you want the * form to appear in a custom location on the page you can use $UserDefinedForm * in the content area to describe where you want the form * * @return Form */ public function Form() { $fields = new FieldSet(); $fieldValidation = array(); $fieldValidationRules = array(); $CustomDisplayRules = ""; $defaults = ""; $this->SubmitButtonText = ($this->SubmitButtonText) ? $this->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit'); if($this->Fields()) { foreach($this->Fields() as $field) { $fieldToAdd = $field->getFormField(); if(!$fieldToAdd) break; $fieldValidationOptions = array(); // Set the Error Messages $errorMessage = sprintf(_t('Form.FIELDISREQUIRED').'.', strip_tags("'". ($field->Title ? $field->Title : $field->Name) . "'")); $errorMessage = ($field->CustomErrorMessage) ? $field->CustomErrorMessage : $errorMessage; $fieldToAdd->setCustomValidationMessage($errorMessage); // Is this field required if($field->Required) { $fieldValidation[$field->Name] = $errorMessage; $fieldValidationOptions['required'] = true; } // Add field to the form $fields->push($fieldToAdd); // Ask our form field for some more information on hour it should be validated $fieldValidationOptions = array_merge($fieldValidationOptions, $field->getValidation()); // Check if we have need to update the global validation if($fieldValidationOptions) { $fieldValidationRules[$field->Name] = $fieldValidationOptions; } // Is this Field Show by Default if(!$field->ShowOnLoad) { $defaults .= "$(\"#" . $field->Name . "\").hide();\n"; } // Check for field dependencies / default if($field->Dependencies()) { foreach($field->Dependencies() as $dependency) { if(is_array($dependency) && isset($dependency['ConditionField']) && $dependency['ConditionField'] != "") { // get the field which is effected $formName = Convert::raw2sql($dependency['ConditionField']); $formFieldWatch = DataObject::get_one("EditableFormField", "Name = '$formName'"); if(!$formFieldWatch) break; $fieldToWatch = "$(\"#Form_Form_".$dependency['ConditionField']."\")"; // show or hide? $view = (isset($dependency['Display']) && $dependency['Display'] == "Show") ? "show" : "hide"; $opposite = ($view == "show") ? "hide" : "show"; $Action = ($formFieldWatch->ClassName == "EditableTextField") ? "keyup" : "change"; switch($dependency['ConditionOption']) { case 'IsNotBlank': $matches = '!= ""'; break; case 'IsBlank': $matches = '== ""'; break; case 'HasValue': $matches = '== "'. $dependency['Value'] .'"'; break; default: $matches = '!= "'. $dependency['Value'] .'"'; break; } // put it all together $CustomDisplayRules .= $fieldToWatch.".$Action(function() { if($(this).val() ". $matches ." ) { $(\"#". $field->Name ."\").".$view."(); } else { $(\"#". $field->Name ."\").".$opposite."(); } });"; } } } } } $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; // Keep track of the referer $fields->push( new HiddenField( "Referrer", "", $referer ) ); // Build actions $actions = new FieldSet( new FormAction("process", $this->SubmitButtonText) ); // Do we want to add a clear form. if($this->ShowClearButton) { $actions->push(new ResetFormAction("clearForm")); } // return the form $form = new Form( $this, "Form", $fields, $actions, new RequiredFields(array_keys($fieldValidation))); $form->loadDataFrom($this->failover); $FormName = $form->FormName(); // Set the Form Name $rules = $this->array2json($fieldValidationRules); $messages = $this->array2json($fieldValidation); // set the custom script for this form Requirements::customScript(<<