mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 15:05:42 +00:00
FEATURE: added ability to run script with 1 form at a time. FEATURE: added ability to dry run the task. BUGFIX: fixed assignment instead of comparsion with MultipleOption fields such as checkbox fields. BUGFIX: fixed failback name on _obsolete table in findDatabaseName()
This commit is contained in:
parent
8fd9de7d1d
commit
a7bb5064a4
@ -2,12 +2,17 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Migration Task for older versions of userforms to the newer version of userforms.
|
* Migration Task for older versions of userforms to the newer version of userforms.
|
||||||
* Handles the datamodel changes
|
* This will handle the datamodel changes of the form page as well as the form fields.
|
||||||
|
* Nothing is done with Submissions as the datamodel for that has not changed
|
||||||
*
|
*
|
||||||
* This has been designed to port 0.1 userforms to 0.2 userforms as this had major
|
* This has been designed to port 0.1 userforms to 0.2 userforms as this had major
|
||||||
* api changes
|
* api changes
|
||||||
*
|
*
|
||||||
* @todo Finish this off.
|
* You can import 1 form at a time by entering the formID in the URL
|
||||||
|
* - /dev/tasks/UserFormsMigrationTask/?formID=12
|
||||||
|
*
|
||||||
|
* You can also run this without writing anything to the database - by doing a dryrun
|
||||||
|
* - /dev/tasks/UserFormsMigrationTask/?dryRun=1
|
||||||
*
|
*
|
||||||
* @package userforms
|
* @package userforms
|
||||||
*/
|
*/
|
||||||
@ -31,22 +36,44 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
// load the forms
|
// load the forms
|
||||||
$forms = DataObject::get("UserDefinedForm");
|
$forms = DataObject::get("UserDefinedForm");
|
||||||
|
|
||||||
if(!$forms) return;
|
// set debugging / useful test
|
||||||
echo "Proceeding to update ". $forms->Count() . " Forms to ensure proper structure<br />";
|
$this->dryRun = (isset($_GET['dryRun'])) ? true : false;
|
||||||
|
|
||||||
|
if($this->dryRun) {
|
||||||
|
echo "Will be running this test as a dry run. No data will be added or removed.<br />";
|
||||||
|
}
|
||||||
|
|
||||||
|
// if they want to import just 1 form - eg for testing
|
||||||
|
if(isset($_GET['formID'])) {
|
||||||
|
$id = Convert::raw2sql($_GET['formID']);
|
||||||
|
$forms = DataObject::get("UserDefinedForm", "`UserDefinedForm`.ID = '$id'");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!$forms) {
|
||||||
|
echo "No UserForms Found on Database";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Proceeding to update ". $forms->Count() . " Forms<br />";
|
||||||
|
|
||||||
foreach($forms as $form) {
|
foreach($forms as $form) {
|
||||||
echo " -- Updating $form->URLSegment <br />";
|
echo " -- Updating $form->URLSegment <br />";
|
||||||
// easy step first port over email data from the structure
|
// easy step first port over email data from the structure
|
||||||
if($form->EmailOnSubmit && $form->EmailTo) {
|
if($form->EmailOnSubmit && $form->EmailTo) {
|
||||||
$emailTo = new UserDefinedForm_EmailRecipient();
|
// EmailTo can be a comma separated list so we need to explode that
|
||||||
$emailTo->EmailAddress = $form->EmailTo;
|
$emails = explode(",", $form->EmailTo);
|
||||||
$emailTo->EmailSubject = _t('UserFormsMigrationTask.DEFAULTSUBMISSIONTITLE',"Submission Data");
|
if($emails) {
|
||||||
$emailTo->EmailFrom = Email::getAdminEmail();
|
foreach($emails as $email) {
|
||||||
$emailTo->EmailBody = $form->EmailMessageToSubmitter;
|
$emailTo = new UserDefinedForm_EmailRecipient();
|
||||||
$emailTo->FormID = $form->ID;
|
$emailTo->EmailAddress = trim($email);
|
||||||
$emailTo->write();
|
$emailTo->EmailSubject = ($form) ? $form->Title : _t('UserFormsMigrationTask.DEFAULTSUBMISSIONTITLE',"Submission Data");
|
||||||
|
$emailTo->EmailFrom = Email::getAdminEmail();
|
||||||
|
$emailTo->FormID = $form->ID;
|
||||||
|
echo " -- -- Created new Email Recipient $email<br />";
|
||||||
|
if(!$this->dryRun) $emailTo->write();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// now fix all the fields
|
// now fix all the fields
|
||||||
if($form->Fields()) {
|
if($form->Fields()) {
|
||||||
foreach($form->Fields() as $field) {
|
foreach($form->Fields() as $field) {
|
||||||
@ -54,15 +81,13 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
case 'EditableDropdown':
|
case 'EditableDropdown':
|
||||||
case 'EditableRadioField':
|
case 'EditableRadioField':
|
||||||
case 'EditableCheckboxGroupField':
|
case 'EditableCheckboxGroupField':
|
||||||
|
|
||||||
$optionClass = "EditableDropdownOption";
|
$optionClass = "EditableDropdownOption";
|
||||||
if($field->ClassName = "EditableRadioField") {
|
if($field->ClassName == "EditableRadioField") {
|
||||||
$optionClass = "EditableRadioOption";
|
$optionClass = "EditableRadioOption";
|
||||||
}
|
}
|
||||||
else if($field->ClassName = "EditableCheckboxGroupField") {
|
else if($field->ClassName == "EditableCheckboxGroupField") {
|
||||||
$optionClass = "EditableCheckboxOption";
|
$optionClass = "EditableCheckboxOption";
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = DB::query("SELECT * FROM $optionClass WHERE ParentID = '$field->ID'");
|
$query = DB::query("SELECT * FROM $optionClass WHERE ParentID = '$field->ID'");
|
||||||
$result = $query->first();
|
$result = $query->first();
|
||||||
if($result) {
|
if($result) {
|
||||||
@ -72,9 +97,8 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'EditableTextField':
|
|
||||||
|
|
||||||
// find what table to use
|
case 'EditableTextField':
|
||||||
$database = $this->findDatabaseTableName('EditableTextField');
|
$database = $this->findDatabaseTableName('EditableTextField');
|
||||||
|
|
||||||
// get the data from the table
|
// get the data from the table
|
||||||
@ -90,6 +114,7 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'EditableLiteralField':
|
case 'EditableLiteralField':
|
||||||
if($field->Content) {
|
if($field->Content) {
|
||||||
// find what table to use
|
// find what table to use
|
||||||
@ -105,6 +130,7 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'EditableMemberListField':
|
case 'EditableMemberListField':
|
||||||
if($field->GroupID) {
|
if($field->GroupID) {
|
||||||
// find what table to use
|
// find what table to use
|
||||||
@ -120,7 +146,8 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'EditableCheckbox':
|
|
||||||
|
case 'EditableCheckbox':
|
||||||
if($field->Checked) {
|
if($field->Checked) {
|
||||||
// find what table to use
|
// find what table to use
|
||||||
$database = $this->findDatabaseTableName('EditableCheckbox');
|
$database = $this->findDatabaseTableName('EditableCheckbox');
|
||||||
@ -135,11 +162,28 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'EditableEmailField':
|
||||||
|
$database = $this->findDatabaseTableName('EditableEmailField');
|
||||||
|
$result = DB::query("SELECT * FROM $database WHERE ID = $field->ID")->first();
|
||||||
|
if($result && isset($result['SendCopy']) && $result['SendCopy'] == true) {
|
||||||
|
// we do not store send copy on email field anymore. This has been wrapped into
|
||||||
|
// the email recipients
|
||||||
|
$emailTo = new UserDefinedForm_EmailRecipient();
|
||||||
|
$emailTo->EmailSubject = ($form) ? $form->Title : _t('UserFormsMigrationTask.DEFAULTSUBMISSIONTITLE',"Submission Data");
|
||||||
|
$emailTo->EmailFrom = Email::getAdminEmail();
|
||||||
|
$emailTo->FormID = $form->ID;
|
||||||
|
$emailTo->SendEmailToFieldID = $field->ID;
|
||||||
|
$emailTo->EmailBody = $form->EmailMessageToSubmitter;
|
||||||
|
if(!$this->dryRun) $emailTo->write();
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
$field->write();
|
if(!$this->dryRun) $field->write();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
echo "<h3>Migration Complete</h3>";
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Find if this table is obsolete or used
|
* Find if this table is obsolete or used
|
||||||
@ -148,7 +192,9 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
function findDatabaseTableName($tableName) {
|
function findDatabaseTableName($tableName) {
|
||||||
$table = DB::query("SHOW TABLES LIKE '$tableName'")->value();
|
$table = DB::query("SHOW TABLES LIKE '$tableName'")->value();
|
||||||
if(!$table) {
|
if(!$table) {
|
||||||
$table = DB::query("SHOW TABLES LIKE '_obsolete_EditableTextField'")->value();
|
$table = DB::query("SHOW TABLES LIKE '_obsolete_$tableName'")->value();
|
||||||
|
|
||||||
|
if(!$table) echo '<strong>!! Could Not Find '.$tableName;
|
||||||
}
|
}
|
||||||
return $table;
|
return $table;
|
||||||
}
|
}
|
||||||
@ -157,11 +203,12 @@ class UserFormsMigrationTask extends MigrationTask {
|
|||||||
* form field it is coming from
|
* form field it is coming from
|
||||||
*/
|
*/
|
||||||
function createOption($option, $class) {
|
function createOption($option, $class) {
|
||||||
|
|
||||||
$editableOption = new EditableOption();
|
$editableOption = new EditableOption();
|
||||||
$editableOption->ParentID = $option['ParentID'];
|
$editableOption->ParentID = $option['ParentID'];
|
||||||
$editableOption->populateFromPostData($option);
|
if(!$this->dryRun) $editableOption->populateFromPostData($option);
|
||||||
// log
|
// log
|
||||||
echo " -- -- Created new option '$editableOption->Title'<br />";
|
echo " -- -- Created new option $editableOption->Title<br />";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
x
Reference in New Issue
Block a user