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.
|
||||
* 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
|
||||
* 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
|
||||
*/
|
||||
@ -31,22 +36,44 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
// load the forms
|
||||
$forms = DataObject::get("UserDefinedForm");
|
||||
|
||||
if(!$forms) return;
|
||||
echo "Proceeding to update ". $forms->Count() . " Forms to ensure proper structure<br />";
|
||||
// set debugging / useful test
|
||||
$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) {
|
||||
echo " -- Updating $form->URLSegment <br />";
|
||||
// easy step first port over email data from the structure
|
||||
if($form->EmailOnSubmit && $form->EmailTo) {
|
||||
// EmailTo can be a comma separated list so we need to explode that
|
||||
$emails = explode(",", $form->EmailTo);
|
||||
if($emails) {
|
||||
foreach($emails as $email) {
|
||||
$emailTo = new UserDefinedForm_EmailRecipient();
|
||||
$emailTo->EmailAddress = $form->EmailTo;
|
||||
$emailTo->EmailSubject = _t('UserFormsMigrationTask.DEFAULTSUBMISSIONTITLE',"Submission Data");
|
||||
$emailTo->EmailAddress = trim($email);
|
||||
$emailTo->EmailSubject = ($form) ? $form->Title : _t('UserFormsMigrationTask.DEFAULTSUBMISSIONTITLE',"Submission Data");
|
||||
$emailTo->EmailFrom = Email::getAdminEmail();
|
||||
$emailTo->EmailBody = $form->EmailMessageToSubmitter;
|
||||
$emailTo->FormID = $form->ID;
|
||||
$emailTo->write();
|
||||
echo " -- -- Created new Email Recipient $email<br />";
|
||||
if(!$this->dryRun) $emailTo->write();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now fix all the fields
|
||||
if($form->Fields()) {
|
||||
foreach($form->Fields() as $field) {
|
||||
@ -54,15 +81,13 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
case 'EditableDropdown':
|
||||
case 'EditableRadioField':
|
||||
case 'EditableCheckboxGroupField':
|
||||
|
||||
$optionClass = "EditableDropdownOption";
|
||||
if($field->ClassName = "EditableRadioField") {
|
||||
if($field->ClassName == "EditableRadioField") {
|
||||
$optionClass = "EditableRadioOption";
|
||||
}
|
||||
else if($field->ClassName = "EditableCheckboxGroupField") {
|
||||
else if($field->ClassName == "EditableCheckboxGroupField") {
|
||||
$optionClass = "EditableCheckboxOption";
|
||||
}
|
||||
|
||||
$query = DB::query("SELECT * FROM $optionClass WHERE ParentID = '$field->ID'");
|
||||
$result = $query->first();
|
||||
if($result) {
|
||||
@ -72,9 +97,8 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
}
|
||||
|
||||
break;
|
||||
case 'EditableTextField':
|
||||
|
||||
// find what table to use
|
||||
case 'EditableTextField':
|
||||
$database = $this->findDatabaseTableName('EditableTextField');
|
||||
|
||||
// get the data from the table
|
||||
@ -90,6 +114,7 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'EditableLiteralField':
|
||||
if($field->Content) {
|
||||
// find what table to use
|
||||
@ -105,6 +130,7 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'EditableMemberListField':
|
||||
if($field->GroupID) {
|
||||
// find what table to use
|
||||
@ -120,6 +146,7 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'EditableCheckbox':
|
||||
if($field->Checked) {
|
||||
// find what table to use
|
||||
@ -135,11 +162,28 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
$field->write();
|
||||
break;
|
||||
}
|
||||
if(!$this->dryRun) $field->write();
|
||||
}
|
||||
}
|
||||
}
|
||||
echo "<h3>Migration Complete</h3>";
|
||||
}
|
||||
/**
|
||||
* Find if this table is obsolete or used
|
||||
@ -148,7 +192,9 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
function findDatabaseTableName($tableName) {
|
||||
$table = DB::query("SHOW TABLES LIKE '$tableName'")->value();
|
||||
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;
|
||||
}
|
||||
@ -157,11 +203,12 @@ class UserFormsMigrationTask extends MigrationTask {
|
||||
* form field it is coming from
|
||||
*/
|
||||
function createOption($option, $class) {
|
||||
|
||||
$editableOption = new EditableOption();
|
||||
$editableOption->ParentID = $option['ParentID'];
|
||||
$editableOption->populateFromPostData($option);
|
||||
if(!$this->dryRun) $editableOption->populateFromPostData($option);
|
||||
// log
|
||||
echo " -- -- Created new option '$editableOption->Title'<br />";
|
||||
echo " -- -- Created new option $editableOption->Title<br />";
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user