public function safeUp() { $table = new Formerly_QuestionRecord(); $this->addColumnAfter($table->getTableName(), 'errorMessage', ColumnType::LongText, 'type'); $this->addColumnAfter($table->getTableName(), 'validationPattern', 'Varchar(255)', 'type'); return true; }
public function saveQuestion(Formerly_QuestionModel $question) { if (!$question) { return false; } // get form $form = $this->getFormById($question->formId); if (!$form) { $question->addError('formId', "No form with ID '{$question->formId}' exists"); return false; } // get or create record $questionRecord = Formerly_QuestionRecord::model()->findById($question->id); if (!$questionRecord) { $questionRecord = new Formerly_QuestionRecord(); } if ($question->type == Formerly_QuestionType::CustomList) { $question->type = Formerly_QuestionType::Checkboxes; $question->handle = Formerly_QuestionType::CustomListHandle . $question->handle; } elseif ($question->type == Formerly_QuestionType::Custom) { $question->handle = Formerly_QuestionType::CustomHandle . $question->handle; } elseif ($question->type == Formerly_QuestionType::RawHTML) { $question->handle = Formerly_QuestionType::RawHTMLHandle . $question->handle; } // get or create field $field = $this->fieldForQuestion($question, $form->handle); $field->id = $questionRecord->fieldId; $field->groupId = $form->fieldGroupId; $field->instructions = $question->instructions; // set attributes on record $questionRecord->formId = $question->formId; $questionRecord->required = $question->required; $questionRecord->sortOrder = $question->sortOrder; $questionRecord->type = $question->type; $questionRecord->errorMessage = $question->errorMessage; $questionRecord->validationPattern = $question->validationPattern; $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { // save field if (craft()->fields->saveField($field)) { $question->fieldId = $field->id; $questionRecord->fieldId = $field->id; } else { $question->addErrors($field->getErrors()); } // save record $questionRecord->validate(); $question->addErrors($questionRecord->getErrors()); if (!$question->hasErrors()) { $questionRecord->save(false); if ($transaction !== null) { $transaction->commit(); } $question->id = $questionRecord->id; return true; } else { if ($transaction !== null) { $transaction->rollback(); } return false; } } catch (\Exception $ex) { if ($transaction !== null) { $transaction->rollback(); } throw $ex; } }