From 745b614710965e7e70ac5cd0d13b631c8fb4904a Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Wed, 7 Jul 2010 09:17:37 +0000 Subject: [PATCH] BUGFIX: fixed sortable events not updated when loading new fields. ENHANCEMENT: Added RightTitle support to EditableFormFields. Removed parsePrepopulateValue() as usage is not clear and is broken --- code/UserDefinedForm.php | 21 +++++- code/editor/EditableFormField.php | 106 +++++++++++---------------- javascript/UserForm.js | 115 +++++++++++++++++------------- templates/EditableFormField.ss | 3 +- templates/Includes/CustomRule.ss | 2 +- 5 files changed, 127 insertions(+), 120 deletions(-) diff --git a/code/UserDefinedForm.php b/code/UserDefinedForm.php index a258045..7c2cf10 100755 --- a/code/UserDefinedForm.php +++ b/code/UserDefinedForm.php @@ -1,8 +1,11 @@ DBField::create('HTMLText', $this->Content), 'Form' => $this->Form @@ -291,15 +295,20 @@ class UserDefinedForm_Controller extends Page_Controller { * 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 * + * @todo Abstract the Conditional Logic from the Form. This should be tied + * to the EditableFormField class so that fields (eg checkboxes) + * can define their own logic + * * @return Form */ public function Form() { $fields = new FieldSet(); $fieldValidation = array(); $fieldValidationRules = array(); - $CustomDisplayRules = ""; + $customDisplayRules = ""; $defaults = ""; $this->SubmitButtonText = ($this->SubmitButtonText) ? $this->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit'); + if($this->Fields()) { foreach($this->Fields() as $field) { @@ -312,8 +321,14 @@ class UserDefinedForm_Controller extends Page_Controller { // 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); + // Set the right title on this field + if($right = $field->getSetting('RightTitle')) { + $fieldToAdd->setRightTitle($right); + } + // Is this field required if($field->Required) { $fieldValidation[$field->Name] = $errorMessage; @@ -424,7 +439,7 @@ class UserDefinedForm_Controller extends Page_Controller { break; } // put it all together - $CustomDisplayRules .= $fieldToWatch.".$action(function() { + $customDisplayRules .= $fieldToWatch.".$action(function() { if(". $expression ." ) { $(\"#". $fieldId ."\").".$view."(); } @@ -477,7 +492,7 @@ class UserDefinedForm_Controller extends Page_Controller { rules: $rules }); - $CustomDisplayRules + $customDisplayRules }); })(jQuery); JS diff --git a/code/editor/EditableFormField.php b/code/editor/EditableFormField.php index fb935e2..8187e80 100755 --- a/code/editor/EditableFormField.php +++ b/code/editor/EditableFormField.php @@ -16,17 +16,12 @@ class EditableFormField extends DataObject { "Default" => "Varchar", "Sort" => "Int", "Required" => "Boolean", - "CanDelete" => "Boolean", "CustomErrorMessage" => "Varchar(255)", "CustomRules" => "Text", "CustomSettings" => "Text", "CustomParameter" => "Varchar(200)" ); - static $defaults = array( - "CanDelete" => "1" - ); - static $has_one = array( "Parent" => "UserDefinedForm", ); @@ -46,7 +41,7 @@ class EditableFormField extends DataObject { } /** - * Is this multipleoption field readonly to the user + * Is this field readonly to the user * * @return bool */ @@ -185,6 +180,7 @@ class EditableFormField extends DataObject { public function Dependencies() { return ($this->CustomRules) ? unserialize($this->CustomRules) : array(); } + /** * Return the custom validation fields for the field * @@ -208,7 +204,7 @@ class EditableFormField extends DataObject { $outputFields->push($new); } $output->push(new ArrayData(array( - 'FieldName' => $this->FieldName(), + 'FieldName' => $this->getFieldName(), 'Display' => $data['Display'], 'Fields' => $outputFields, 'ConditionField' => $data['ConditionField'], @@ -230,19 +226,26 @@ class EditableFormField extends DataObject { /** * Return the base name for this form field in the - * form builder + * form builder. Optionally returns the name with the given field + * + * @param String Field Name * * @return String */ - public function FieldName() { - return "Fields[".$this->ID."]"; + public function getFieldName($field = false) { + return ($field) ? "Fields[".$this->ID."][".$field."]" : "Fields[".$this->ID."]"; } /** - * @todo Fix this - shouldn't name be returning name?!? + * Generate a name for the Setting field + * + * @param String name of the setting + * @return String */ - public function BaseName() { - return $this->Name; + public function getSettingFieldName($field) { + $name = $this->getFieldName('CustomSettings'); + + return $name . '[' . $field .']'; } /** @@ -256,16 +259,16 @@ class EditableFormField extends DataObject { * @access public */ public function populateFromPostData($data) { - $this->Title = (isset($data['Title'])) ? $data['Title']: ""; - $this->Default = (isset($data['Default'])) ? $data['Default'] : ""; - $this->Sort = (isset($data['Sort'])) ? $data['Sort'] : null; - $this->Required = !empty($data['Required']) ? 1 : 0; - $this->CanDelete = (isset($data['CanDelete']) && !$data['CanDelete']) ? 0 : 1; - $this->Name = $this->class.$this->ID; - $this->CustomErrorMessage = (isset($data['CustomErrorMessage'])) ? $data['CustomErrorMessage'] : ""; - $this->CustomRules = ""; - $this->CustomSettings = ""; - $this->ShowOnLoad = (isset($data['ShowOnLoad']) && $data['ShowOnLoad'] == "Show") ? 1 : 0; + $this->Title = (isset($data['Title'])) ? $data['Title']: ""; + $this->Default = (isset($data['Default'])) ? $data['Default'] : ""; + $this->Sort = (isset($data['Sort'])) ? $data['Sort'] : null; + $this->Required = !empty($data['Required']) ? 1 : 0; + $this->Name = $this->class.$this->ID; + $this->ShowOnLoad = (isset($data['ShowOnLoad']) && $data['ShowOnLoad'] == "Show") ? 1 : 0; + $this->CustomRules = ""; + $this->CustomErrorMessage = (isset($data['CustomErrorMessage'])) ? $data['CustomErrorMessage'] : ""; + $this->CustomSettings = ""; + // custom settings if(isset($data['CustomSettings'])) { $this->setFieldSettings($data['CustomSettings']); @@ -301,7 +304,13 @@ class EditableFormField extends DataObject { * @return FieldSet */ public function getFieldConfiguration() { - return new FieldSet(); + return new FieldSet( + new TextField( + $this->getSettingFieldName('RightTitle'), + _t('EditableFormField.RIGHTTITLE', 'Right Title'), + $this->getSetting('RightTitle') + ) + ); } /** @@ -312,8 +321,8 @@ class EditableFormField extends DataObject { */ public function getFieldValidationOptions() { $fields = new FieldSet( - new CheckboxField("Fields[$this->ID][Required]", _t('EditableFormField.REQUIRED', 'Is this field Required?'), $this->Required), - new TextField("Fields[$this->ID][CustomErrorMessage]", _t('EditableFormField.CUSTOMERROR','Custom Error Message'), $this->CustomErrorMessage) + new CheckboxField($this->getFieldName('Required'), _t('EditableFormField.REQUIRED', 'Is this field Required?'), $this->Required), + new TextField($this->getFieldName('CustomErrorMessage'), _t('EditableFormField.CUSTOMERROR','Custom Error Message'), $this->CustomErrorMessage) ); if(!$this->canEdit()) { @@ -321,6 +330,7 @@ class EditableFormField extends DataObject { $fields->performReadonlyTransformation(); } } + return $fields; } @@ -342,46 +352,16 @@ class EditableFormField extends DataObject { public function getSubmittedFormField() { return new SubmittedFormField(); } - + + + /** + * Show this form field (and its related value) in the reports and in emails. + * + * @return bool + */ function showInReports() { return true; } - - function prepopulate($value) { - $this->prepopulateFromMap($this->parsePrepopulateValue($value)); - } - - protected function parsePrepopulateValue($value) { - $paramList = explode(',', $value); - $paramMap = array(); - - foreach($paramList as $param) { - - if(preg_match( '/([^=]+)=(.+)/', $param, $match)) { - if(isset($paramMap[$match[1]]) && is_array($paramMap[$match[1]])) { - $paramMap[$match[1]][] = $match[2]; - } else if(isset( $paramMap[$match[1]])) { - $paramMap[$match[1]] = array($paramMap[$match[1]]); - $paramMap[$match[1]][] = $match[2]; - } else { - $paramMap[$match[1]] = $match[2]; - } - } - } - return $paramMap; - } - - protected function prepopulateFromMap($paramMap) { - foreach($paramMap as $field => $fieldValue) { - if(!is_array($fieldValue)) { - $this->$field = $fieldValue; - } - } - } - - function Type() { - return $this->class; - } /** * Return the validation information related to this field. This is diff --git a/javascript/UserForm.js b/javascript/UserForm.js index 2787da7..2bd6612 100644 --- a/javascript/UserForm.js +++ b/javascript/UserForm.js @@ -23,6 +23,11 @@ /*-------------------- FIELD EDITOR ----------------------- */ + /** + * Update the Field sortable + */ + update_sortable(); + /** * Create a new instance of a field in the current form * area. the type information should all be on this object @@ -42,7 +47,7 @@ var securityID = ($("#SecurityID").length > 0) ? '&SecurityID='+$("#SecurityID").attr("value") : ''; var type = $(this).siblings("select").val(); - //send ajax request to the page + // send ajax request to the page $.ajax({ type: "GET", url: action, @@ -65,7 +70,7 @@ } }); - $("#Fields_fields").sortable("refresh"); + update_sortable(); return false; }); @@ -83,7 +88,8 @@ $(domElement).text(value); } }); - }) + }); + /** * Show the more options popdown. Or hide it if we * currently have it open @@ -147,7 +153,7 @@ var action = $("#Form_EditForm").attr("action") + '/field/Fields/addoptionfield'; var parent = $(this).attr("rel"); - //send ajax request to the page + // send ajax request to the page $.ajax({ type: "GET", url: action, @@ -164,6 +170,9 @@ statusMessage(ss.i18n._t('UserForms.ERRORCREATINGFIELD', 'Error Creating Field')); } }); + + update_sortable(); + return false; }); @@ -181,52 +190,6 @@ return false; }); - - /** - * Sort Fields in the Field List - */ - $("#Fields_fields").sortable({ - handle : '.fieldHandler', - cursor: 'pointer', - items: 'li.EditableFormField', - placeholder: 'removed-form-field', - opacity: 0.6, - revert: true, - change : function (event, ui) { - $("#Fields_fields").sortable('refreshPositions'); - }, - update : function (event, ui) { - // get all the fields - var sort = 1; - $("li.EditableFormField").each(function() { - $(this).find(".sortHidden").val(sort++); - }); - } - }); - - /** - * Sort Options in a Field List - Such as options in a - * dropdown field. - */ - $(".editableOptions").sortable({ - handle : '.handle', - cursor: 'pointer', - items: 'li', - placeholder: 'removed-form-field', - opacity: 0.6, - revert: true, - change : function (event, ui) { - $(this).sortable('refreshPositions'); - }, - update : function (event, ui) { - // get all the fields - var sort = 1; - $(".editableOptions li").each(function() { - $(this).find(".sortOptionHidden").val(sort++); - }); - } - }); - /** * Custom Rules Interface */ @@ -282,10 +245,60 @@ // append to the list currentRules.append(newRule); - $(".editableOptions").sortable("refresh"); + update_sortable(); return false; }); + + /** + * Store the sortable as a function which we can call + */ + function update_sortable() { + /** + * Sort Fields in the Field List + */ + $("#Fields_fields").sortable({ + handle : '.fieldHandler', + cursor: 'pointer', + items: 'li.EditableFormField', + placeholder: 'removed-form-field', + opacity: 0.6, + revert: true, + change : function (event, ui) { + $("#Fields_fields").sortable('refreshPositions'); + }, + update : function (event, ui) { + // get all the fields + var sort = 1; + $("li.EditableFormField").each(function() { + $(this).find(".sortHidden").val(sort++); + }); + } + }); + + /** + * Sort Options in a Field List - Such as options in a + * dropdown field. + */ + $(".editableOptions").sortable({ + handle : '.handle', + cursor: 'pointer', + items: 'li', + placeholder: 'removed-form-field', + opacity: 0.6, + revert: true, + change : function (event, ui) { + $(this).sortable('refreshPositions'); + }, + update : function (event, ui) { + // get all the fields + var sort = 1; + $(".editableOptions li").each(function() { + $(this).find(".sortOptionHidden").val(sort++); + }); + } + }); + } }); }) (jQuery); \ No newline at end of file diff --git a/templates/EditableFormField.ss b/templates/EditableFormField.ss index 2602f6d..d70cdf7 100755 --- a/templates/EditableFormField.ss +++ b/templates/EditableFormField.ss @@ -1,5 +1,5 @@ -
  • +
  • <% if isReadonly %> <% _t('LOCKED', 'These fields cannot be modified') %> @@ -127,7 +127,6 @@ <% end_if %> -
  • \ No newline at end of file diff --git a/templates/Includes/CustomRule.ss b/templates/Includes/CustomRule.ss index 4a960b2..8b56614 100644 --- a/templates/Includes/CustomRule.ss +++ b/templates/Includes/CustomRule.ss @@ -7,7 +7,7 @@