public function actionSaveForm()
 {
     $this->requirePostRequest();
     $form = new FormBuilder_FormModel();
     $form->id = craft()->request->getPost('formId');
     $form->name = craft()->request->getPost('name');
     $form->handle = craft()->request->getPost('handle');
     $form->subject = craft()->request->getPost('subject');
     $form->ajaxSubmit = craft()->request->getPost('ajaxSubmit');
     $form->successPageRedirect = craft()->request->getPost('successPageRedirect');
     $form->redirectUrl = craft()->request->getPost('redirectUrl');
     $form->useReCaptcha = craft()->request->getPost('useReCaptcha');
     $form->hasFileUploads = craft()->request->getPost('hasFileUploads');
     $form->uploadSource = craft()->request->getPost('uploadSource');
     $form->successMessage = craft()->request->getPost('successMessage');
     $form->errorMessage = craft()->request->getPost('errorMessage');
     $form->notifyFormAdmin = craft()->request->getPost('notifyFormAdmin');
     $form->toEmail = craft()->request->getPost('toEmail');
     $form->notificationTemplatePath = craft()->request->getPost('notificationTemplatePath');
     $form->notifyRegistrant = craft()->request->getPost('notifyRegistrant');
     $form->notificationTemplatePathRegistrant = craft()->request->getPost('notificationTemplatePathRegistrant');
     $form->notificationFieldHandleName = craft()->request->getPost('notificationFieldHandleName');
     $fieldLayout = craft()->fields->assembleLayoutFromPost();
     $fieldLayout->type = ElementType::Asset;
     $form->setFieldLayout($fieldLayout);
     if (craft()->formBuilder_forms->saveForm($form)) {
         craft()->userSession->setNotice(Craft::t('Form saved.'));
         $this->redirectToPostedUrl($form);
     } else {
         craft()->userSession->setError(Craft::t('Couldn’t save form.'));
     }
     craft()->urlManager->setRouteVariables(array('form' => $form));
 }
 public function getFormByHandle($handle)
 {
     $formRecord = FormBuilder_FormRecord::model()->findByAttributes(array('handle' => $handle));
     if (!$formRecord) {
         return false;
     }
     return FormBuilder_FormModel::populateModel($formRecord);
 }
 public function saveForm(FormBuilder_FormModel $form)
 {
     if ($form->id) {
         $formRecord = FormBuilder_FormRecord::model()->findById($form->id);
         if (!$formRecord) {
             throw new Exception(Craft::t('No form exists with the ID “{id}”', array('id' => $form->id)));
         }
         $oldForm = FormBuilder_FormModel::populateModel($formRecord);
         $isNewForm = false;
     } else {
         $formRecord = new FormBuilder_FormRecord();
         $isNewForm = true;
     }
     $formRecord->name = $form->name;
     $formRecord->handle = $form->handle;
     $formRecord->subject = $form->subject;
     $formRecord->ajaxSubmit = $form->ajaxSubmit;
     $formRecord->successPageRedirect = $form->successPageRedirect;
     $formRecord->redirectUrl = $form->redirectUrl;
     $formRecord->useReCaptcha = $form->useReCaptcha;
     $formRecord->hasFileUploads = $form->hasFileUploads;
     $formRecord->uploadSource = $form->uploadSource;
     $formRecord->successMessage = $form->successMessage;
     $formRecord->errorMessage = $form->errorMessage;
     $formRecord->notifyFormAdmin = $form->notifyFormAdmin;
     $formRecord->toEmail = $form->toEmail;
     $formRecord->notificationTemplatePath = $form->notificationTemplatePath;
     $formRecord->notifyRegistrant = $form->notifyRegistrant;
     $formRecord->notificationTemplatePathRegistrant = $form->notificationTemplatePathRegistrant;
     $formRecord->notificationFieldHandleName = $form->notificationFieldHandleName;
     $formRecord->validate();
     $form->addErrors($formRecord->getErrors());
     $emailField = craft()->fields->getFieldByHandle(craft()->request->getPost('notificationFieldHandleName'));
     if ($form->notifyRegistrant) {
         if ($emailField) {
             if ($emailField->type != 'FormBuilder_Email') {
                 $form->addError('notificationFieldHandleName', 'Must be an email fieldtype.');
                 return false;
             }
         } else {
             $form->addError('notificationFieldHandleName', 'Please select an email field.');
             return false;
         }
     }
     if (!$form->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             if (!$isNewForm && $oldForm->fieldLayoutId) {
                 craft()->fields->deleteLayoutById($oldForm->fieldLayoutId);
             }
             $fieldLayout = $form->getFieldLayout();
             craft()->fields->saveLayout($fieldLayout);
             $form->fieldLayoutId = $fieldLayout->id;
             $formRecord->fieldLayoutId = $fieldLayout->id;
             $formRecord->save();
             if (!$form->id) {
                 $form->id = $formRecord->id;
             }
             $this->_formsById[$form->id] = $form;
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }