mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
ENHANCEMENT: added inital versioning / staging support to userforms. Supports different forms on stage / live. ENHANCEMENT: added unit tests to cover versioning / staging
This commit is contained in:
parent
61edb74f36
commit
379bcc7921
@ -42,6 +42,10 @@ class UserDefinedForm extends Page {
|
|||||||
'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>'
|
'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
static $extensions = array(
|
||||||
|
"Versioned('Stage', 'Live')"
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Array
|
* @var Array
|
||||||
*/
|
*/
|
||||||
@ -92,28 +96,73 @@ class UserDefinedForm extends Page {
|
|||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called on before delete remove all the fields from the database
|
* Publishing Versioning support.
|
||||||
*/
|
|
||||||
public function delete() {
|
|
||||||
foreach($this->Fields() as $field) {
|
|
||||||
$field->delete();
|
|
||||||
}
|
|
||||||
parent::delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Custom Form Actions for the form
|
|
||||||
*
|
*
|
||||||
* @param bool Is the Form readonly
|
* When publishing copy the editable form fields to the live database
|
||||||
* @return FieldSet
|
* Not going to version emails and submissions as they are likely to
|
||||||
|
* persist over multiple versions
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function customFormActions($isReadonly = false) {
|
public function doPublish() {
|
||||||
return new FieldSet(
|
if($this->Fields()) {
|
||||||
new TextField("SubmitButtonText", _t('UserDefinedForm.TEXTONSUBMIT', 'Text on submit button:'), $this->SubmitButtonText),
|
foreach($this->Fields() as $field) {
|
||||||
new CheckboxField("ShowClearButton", _t('UserDefinedForm.SHOWCLEARFORM', 'Show Clear Form Button'), $this->ShowClearButton)
|
$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;
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,7 +30,10 @@ class EditableFormField extends DataObject {
|
|||||||
static $has_one = array(
|
static $has_one = array(
|
||||||
"Parent" => "SiteTree",
|
"Parent" => "SiteTree",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
static $extensions = array(
|
||||||
|
"Versioned('Stage', 'Live')"
|
||||||
|
);
|
||||||
/**
|
/**
|
||||||
* @var FieldEditor The current editor
|
* @var FieldEditor The current editor
|
||||||
*/
|
*/
|
||||||
|
52
tests/UserDefinedFormEditorTest.php
Normal file
52
tests/UserDefinedFormEditorTest.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests covering the form editor / builder and
|
||||||
|
* some of the user interface
|
||||||
|
*
|
||||||
|
* @package userforms
|
||||||
|
*/
|
||||||
|
|
||||||
|
class UserDefinedFormEditorTest extends FunctionalTest {
|
||||||
|
|
||||||
|
protected $form;
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,7 @@
|
|||||||
* @package userforms
|
* @package userforms
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class UserDefinedFormTest extends SapphireTest {
|
class UserDefinedFormFieldTest extends SapphireTest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic Test creating all the editable form fields
|
* Basic Test creating all the editable form fields
|
||||||
@ -25,5 +25,4 @@ class UserDefinedFormTest extends SapphireTest {
|
|||||||
$object->delete();
|
$object->delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
|
Loading…
Reference in New Issue
Block a user