/** * 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); }
/** * Saves New Form. * */ public function actionSaveForm() { $this->requirePostRequest(); $form = new FormBuilder2_FormModel(); $form->id = craft()->request->getPost('formId'); $form->name = craft()->request->getPost('name'); $form->handle = craft()->request->getPost('handle'); $form->fieldLayoutId = craft()->request->getPost('fieldLayoutId'); $form->formSettings = craft()->request->getPost('formSettings'); $form->spamProtectionSettings = craft()->request->getPost('spamProtectionSettings'); $form->messageSettings = craft()->request->getPost('messageSettings'); $form->notificationSettings = craft()->request->getPost('notificationSettings'); $fieldLayout = craft()->fields->assembleLayoutFromPost(); $fieldLayout->type = ElementType::Asset; $form->setFieldLayout($fieldLayout); if (craft()->formBuilder2_form->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)); }
/** * 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; } }