diff --git a/code/extensions/UserFormFieldEditorExtension.php b/code/extensions/UserFormFieldEditorExtension.php index 08efb2a..db0bac9 100644 --- a/code/extensions/UserFormFieldEditorExtension.php +++ b/code/extensions/UserFormFieldEditorExtension.php @@ -34,6 +34,8 @@ class UserFormFieldEditorExtension extends DataExtension { public function getFieldEditorGrid() { $fields = $this->owner->Fields(); + $this->createInitialFormStep(); + $editableColumns = new GridFieldEditableColumns(); $editableColumns->setDisplayFields(array( 'ClassName' => function($record, $column, $grid) { @@ -66,6 +68,30 @@ class UserFormFieldEditorExtension extends DataExtension { return $fieldEditor; } + /** + * A UserForm must have at least one step. + * If no steps exist, create an initial step, and put all fields inside it. + * + * @return void + */ + public function createInitialFormStep() { + // If there's already an initial step, do nothing. + if ($this->owner->Fields()->filter('ClassName', 'EditableFormStep')->Count()) { + return; + } + + $step = EditableFormStep::create(); + + $step->ParentID = $this->owner->ID; + $step->write(); + + // Assign each field to the initial step. + foreach ($this->owner->Fields()->exclude('ID', $step->ID) as $field) { + $field->StepID = $step->ID; + $field->write(); + } + } + /** * @return array */ diff --git a/code/forms/UserForm.php b/code/forms/UserForm.php index f3f4b89..1807c6f 100644 --- a/code/forms/UserForm.php +++ b/code/forms/UserForm.php @@ -35,17 +35,41 @@ class UserForm extends Form { $this->extend('updateForm'); } + /** + * Get the form steps. + * + * @return ArrayList + */ + public function getFormSteps() { + $steps = new ArrayList(); + + foreach ($this->controller->Fields()->filter('ClassName', 'EditableFormStep') as $step) { + $steps->push(array( + 'Title' => $step->Title, + 'Fields' => $this->getFormFields($step) + )); + } + + return $steps; + } + /** * Get the form fields for the form on this page. Can modify this FieldSet * by using {@link updateFormFields()} on an {@link Extension} subclass which * is applied to this controller. * + * @param EditableFormStep $parent + * * @return FieldList */ - public function getFormFields() { + public function getFormFields($parent = null) { + if(!$parent) { + $parent = $this->controller; + } + $fields = new FieldList(); - foreach($this->controller->Fields() as $editableField) { + foreach($parent->Fields() as $editableField) { // get the raw form field from the editable version $field = $editableField->getFormField(); diff --git a/code/model/editableformfields/EditableFormStep.php b/code/model/editableformfields/EditableFormStep.php new file mode 100644 index 0000000..c0b666f --- /dev/null +++ b/code/model/editableformfields/EditableFormStep.php @@ -0,0 +1,57 @@ + 'EditableFormField' + ); + + /** + * @return FieldList + */ + public function getCMSFields() { + $fields = parent::getCMSFields(); + + $fields->removeByName('MergeField'); + $fields->removeByName('StepID'); + $fields->removeByName('Default'); + $fields->removeByName('Validation'); + $fields->removeByName('CustomRules'); + + return $fields; + } + + /** + * @return FormField + */ + public function getFormField() { + return false; + } + + /** + * @return boolean + */ + public function showInReports() { + return false; + } +} diff --git a/templates/UserForm.ss b/templates/UserForm.ss new file mode 100644 index 0000000..44a12fe --- /dev/null +++ b/templates/UserForm.ss @@ -0,0 +1,36 @@ +<% if $IncludeFormTag %> +
+<% end_if %> + +<% if $Message %> +

$Message

+<% else %> + +<% end_if %> + +
+ <% if $Legend %>$Legend<% end_if %> + + <% loop $FormSteps %> +
+

$Title

+ <% loop $Fields %> + $FieldHolder + <% end_loop %> +
+ <% end_loop %> + +
+
+ +<% if $Actions %> +
+ <% loop $Actions %> + $Field + <% end_loop %> +
+<% end_if %> + +<% if $IncludeFormTag %> +
+<% end_if %>