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.
";
}
// 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
";
foreach($forms as $form) {
echo " -- Updating $form->URLSegment
";
// 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 = trim($email);
$emailTo->EmailSubject = ($form) ? $form->Title : _t('UserFormsMigrationTask.DEFAULTSUBMISSIONTITLE',"Submission Data");
$emailTo->EmailFrom = Email::getAdminEmail();
$emailTo->FormID = $form->ID;
echo " -- -- Created new Email Recipient $email
";
if(!$this->dryRun) $emailTo->write();
}
}
}
// now fix all the fields
if($form->Fields()) {
foreach($form->Fields() as $field) {
switch($field->ClassName) {
case 'EditableDropdown':
case 'EditableRadioField':
case 'EditableCheckboxGroupField':
$optionClass = "EditableDropdownOption";
if($field->ClassName == "EditableRadioField") {
$optionClass = "EditableRadioOption";
}
else if($field->ClassName == "EditableCheckboxGroupField") {
$optionClass = "EditableCheckboxOption";
}
$query = DB::query("SELECT * FROM $optionClass WHERE ParentID = '$field->ID'");
$result = $query->first();
if($result) {
do {
$this->createOption($result, $optionClass);
} while($result = $query->next());
}
break;
case 'EditableTextField':
$database = $this->findDatabaseTableName('EditableTextField');
// get the data from the table
$result = DB::query("SELECT * FROM $database WHERE ID = $field->ID")->first();
if($result) {
$field->setFieldSettings(array(
'Size' => $result['Size'],
'MinLength' => $result['MinLength'],
'MaxLength' => $result['MaxLength'],
'Rows' => $result['Rows']
));
}
break;
case 'EditableLiteralField':
if($field->Content) {
// find what table to use
$database = $this->findDatabaseTableName('EditableLiteralField');
// get the data from the table
$result = DB::query("SELECT * FROM $database WHERE ID = $field->ID")->first();
if($result) {
$field->setFieldSettings(array(
'Content' => $result['Content']
));
}
}
break;
case 'EditableMemberListField':
if($field->GroupID) {
// find what table to use
$database = $this->findDatabaseTableName('EditableMemberListField');
// get the data from the table
$result = DB::query("SELECT * FROM $database WHERE ID = $field->ID")->first();
if($result) {
$field->setFieldSettings(array(
'GroupID' => $result['GroupID']
));
}
}
break;
case 'EditableCheckbox':
if($field->Checked) {
// find what table to use
$database = $this->findDatabaseTableName('EditableCheckbox');
// get the data from the table
$result = DB::query("SELECT * FROM $database WHERE ID = $field->ID")->first();
if($result) {
$field->setFieldSettings(array(
'Default' => $result['Checked']
));
}
}
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;
}
if(!$this->dryRun) $field->write();
}
}
}
echo "