/**
  * Get Form By Handle
  *
  */
 public function getFormByHandle($handle)
 {
     $formRecord = FormBuilder2_FormRecord::model()->findByAttributes(array('handle' => $handle));
     if (!$formRecord) {
         return false;
     }
     return FormBuilder2_FormModel::populateModel($formRecord);
 }
 /**
  * Backup All Forms
  */
 public function backupAllForms()
 {
     $forms = FormBuilder2_FormRecord::model()->ordered()->findAll();
     $table = 'craft_formbuilder2_forms';
     if ($forms) {
         $this->_currentVersion = 'v' . craft()->getVersion() . '.' . craft()->getBuild();
         $siteName = IOHelper::cleanFilename(StringHelper::asciiString(craft()->getSiteName()));
         $fileName = ($siteName ? $siteName . '_' : '') . gmdate('ymd_His') . '_' . $this->_currentVersion . '.sql';
         $this->_filePath = craft()->path->getDbBackupPath() . StringHelper::toLowerCase($fileName);
         $this->_processHeader();
         $results = $this->_processResult($table);
         $this->_processConstraints();
         $this->_processFooter();
         $filepath = $this->_filePath;
         $this->_processFile($fileName, $filepath);
     } else {
         return false;
     }
 }
 /**
  * Save New Form
  *
  */
 public function saveForm(FormBuilder2_FormModel $form)
 {
     if ($form->id) {
         $formRecord = FormBuilder2_FormRecord::model()->findById($form->id);
         if (!$formRecord) {
             throw new Exception(Craft::t('No form exists with the ID “{id}”', array('id' => $form->id)));
         }
         $oldForm = FormBuilder2_FormModel::populateModel($formRecord);
         $isNewForm = false;
     } else {
         $formRecord = new FormBuilder2_FormRecord();
         $isNewForm = true;
     }
     $formRecord->name = $form->name;
     $formRecord->handle = $form->handle;
     $formRecord->fieldLayoutId = $form->fieldLayoutId;
     $formRecord->formSettings = JsonHelper::encode($form->formSettings);
     $formRecord->spamProtectionSettings = JsonHelper::encode($form->spamProtectionSettings);
     $formRecord->messageSettings = JsonHelper::encode($form->messageSettings);
     $formRecord->notificationSettings = JsonHelper::encode($form->notificationSettings);
     $attributes = $form->getAttributes();
     $formSettings = $attributes['formSettings'];
     $spamProtectionSettings = $attributes['spamProtectionSettings'];
     $messageSettings = $attributes['messageSettings'];
     $notificationSettings = $attributes['notificationSettings'];
     // Cant use Ajax with file uplaods (for now)
     if ($formSettings['hasFileUploads'] != '' && $formSettings['ajaxSubmit'] != '') {
         $form->addError('cannotUseFileUploadAndAjax', Craft::t('Cannot use file uploads with ajax at the moment. Please unselect one.'));
     }
     if ($formSettings['formRedirect']['customRedirect'] && $formSettings['formRedirect']['customRedirectUrl'] == '') {
         $form->addError('customRedirectUrl', Craft::t('Please enter Redirect URL.'));
     }
     if ($spamProtectionSettings['spamTimeMethod'] != '' && $spamProtectionSettings['spamTimeMethodTime'] == '') {
         $form->addError('spamTimeMethodTime', Craft::t('Please enter time.'));
     }
     if ($spamProtectionSettings['spamHoneypotMethod'] != '' && $spamProtectionSettings['spamHoneypotMethodMessage'] == '') {
         $form->addError('spamHoneypotMethodMessage', Craft::t('Please enter message for screen readers.'));
     }
     if ($notificationSettings['notifySubmission'] == '1' && $notificationSettings['emailSettings']['notifyEmail'] == '') {
         $form->addError('notifyEmail', Craft::t('Please enter notification email.'));
     }
     if ($notificationSettings['notifySubmission'] == '1' && $notificationSettings['emailSettings']['emailSubject'] == '') {
         $form->addError('emailSubject', Craft::t('Please enter notification email subject.'));
     }
     $formRecord->validate();
     $form->addErrors($formRecord->getErrors());
     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;
     }
 }