diff --git a/code/UserDefinedForm.php b/code/UserDefinedForm.php index a9bb26e..1e5633b 100755 --- a/code/UserDefinedForm.php +++ b/code/UserDefinedForm.php @@ -42,6 +42,10 @@ class UserDefinedForm extends Page { 'OnCompleteMessage' => '

Thanks, we\'ve received your submission.

' ); + static $extensions = array( + "Versioned('Stage', 'Live')" + ); + /** * @var Array */ @@ -92,28 +96,73 @@ class UserDefinedForm extends Page { 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 + * Publishing Versioning support. * - * @param bool Is the Form readonly - * @return FieldSet + * When publishing copy the editable form fields to the live database + * Not going to version emails and submissions as they are likely to + * persist over multiple versions + * + * @return void */ - 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) - ); + public function doPublish() { + if($this->Fields()) { + foreach($this->Fields() as $field) { + $field->publish('Stage', 'Live'); + } + } + + parent::doPublish(); + } + + /** + * Unpublishing Versioning support + * + * When unpublishing the page it has to remove all the fields from + * the live database table + * + * @return void + */ + public function doUnpublish() { + if($this->Fields()) { + foreach($this->Fields() as $field) { + $field->deleteFromStage('Live'); + } + } + + parent::doUnpublish(); + } + + /** + * Roll back a form to a previous version + * + * @param String|int Version to roll back to + */ + public function doRollbackTo($version) { + if($this->Fields()) { + foreach($this->Fields() as $field) { + $field->publish($version, "Stage", true); + $field->writeWithoutVersion(); + } + } + + parent::doRollbackTo($version); + } + + /** + * Revert the draft site to the current live site + * + * @return void + */ + public function doRevertToLive() { + if($this->Fields()) { + foreach($this->Fields() as $field) { + $field->writeToStage('Live', 'Stage'); + } + } + + parent::doRevertToLive(); } /** @@ -131,6 +180,19 @@ class UserDefinedForm extends Page { } return $page; } + + /** + * 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) + ); + } } /** diff --git a/code/editor/EditableFormField.php b/code/editor/EditableFormField.php index 849930a..81265c6 100755 --- a/code/editor/EditableFormField.php +++ b/code/editor/EditableFormField.php @@ -30,7 +30,10 @@ class EditableFormField extends DataObject { static $has_one = array( "Parent" => "SiteTree", ); - + + static $extensions = array( + "Versioned('Stage', 'Live')" + ); /** * @var FieldEditor The current editor */ diff --git a/tests/UserDefinedFormEditorTest.php b/tests/UserDefinedFormEditorTest.php new file mode 100644 index 0000000..69f0374 --- /dev/null +++ b/tests/UserDefinedFormEditorTest.php @@ -0,0 +1,52 @@ +form = new UserDefinedForm(); + $this->form->write(); + } + + function testPublishing() { + $id = $this->form->ID; + $this->form->Fields()->add(new EditableFormField()); + $this->form->doPublish(); + $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "`UserDefinedForm`.ID = $id"); + $this->assertEquals($live->Fields()->Count(), 1); + } + + function testUnpublishing() { + $id = $this->form->ID; + $this->form->Fields()->removeAll(); + $this->form->Fields()->add(new EditableFormField()); + $this->form->doUnPublish(); + $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "`UserDefinedForm`.ID = $id"); + $stage = Versioned::get_one_by_stage("UserDefinedForm", "Stage", "`UserDefinedForm`.ID = $id"); + $this->assertEquals($live, false); + $this->assertEquals($stage->Fields()->Count(), 1); + } + + function testDuplicatingPage() { + $this->form->Fields()->add(new EditableFormField()); + $form_copy = $this->form->duplicate(); + + $this->assertEquals($this->form->Fields()->Count(), $form_copy->Fields()->Count()); + } + + function tearDown() { + $this->form->delete(); + + parent::tearDown(); + } +} \ No newline at end of file diff --git a/tests/UserDefinedFormTest.php b/tests/UserDefinedFormFieldTest.php similarity index 90% rename from tests/UserDefinedFormTest.php rename to tests/UserDefinedFormFieldTest.php index f26f794..7cb7c35 100644 --- a/tests/UserDefinedFormTest.php +++ b/tests/UserDefinedFormFieldTest.php @@ -8,7 +8,7 @@ * @package userforms */ -class UserDefinedFormTest extends SapphireTest { +class UserDefinedFormFieldTest extends SapphireTest { /** * Basic Test creating all the editable form fields @@ -25,5 +25,4 @@ class UserDefinedFormTest extends SapphireTest { $object->delete(); } } -} -?> \ No newline at end of file +} \ No newline at end of file