/** * Populate form. * * @param AmForms_FormModel $form * @param array $formDefinition * @param string $formHandle */ private function populateFormModel(AmForms_FormModel $form, array $formDefinition, $formHandle) { $form->setAttributes(array('handle' => $formHandle, 'name' => $formDefinition['name'], 'titleFormat' => $form->titleFormat, 'submitAction' => $form->submitAction, 'submitButton' => $form->submitButton, 'afterSubmitText' => $form->afterSubmitText, 'submissionEnabled' => $form->submissionEnabled, 'sendCopy' => $form->sendCopy, 'sendCopyTo' => $form->sendCopyTo, 'notificationEnabled' => $form->notificationEnabled, 'notificationFilesEnabled' => $form->notificationFilesEnabled, 'notificationRecipients' => $form->notificationRecipients, 'notificationSubject' => $form->notificationSubject, 'notificationSenderName' => $form->notificationSenderName, 'notificationReplyToEmail' => $form->notificationReplyToEmail, 'formTemplate' => $form->formTemplate, 'tabTemplate' => $form->tabTemplate, 'fieldTemplate' => $form->fieldTemplate, 'notificationTemplate' => $form->notificationTemplate)); craft()->content->fieldContext = 'amforms'; craft()->content->contentTable = 'amforms_content'; $fieldLayout = craft()->schematic_fields->getFieldLayout($formDefinition['fieldLayout']); craft()->content->fieldContext = 'global'; craft()->content->contentTable = 'content'; $form->setFieldLayout($fieldLayout); }
/** * Save a form. */ public function actionSaveForm() { $this->requirePostRequest(); // Get form if available $formId = craft()->request->getPost('formId'); if ($formId) { $form = craft()->amForms_forms->getFormById($formId); if (!$form) { throw new Exception(Craft::t('No form exists with the ID “{id}”.', array('id' => $formId))); } } else { $form = new AmForms_FormModel(); } // Field layout $fieldLayout = craft()->fields->assembleLayoutFromPost(); $fieldLayout->type = AmFormsModel::ElementTypeForm; $form->setFieldLayout($fieldLayout); // Get redirectEntryId $redirectEntryId = craft()->request->getPost('redirectEntryId'); // Form attributes $form->redirectEntryId = $redirectEntryId && is_array($redirectEntryId) && count($redirectEntryId) ? $redirectEntryId[0] : null; $form->name = craft()->request->getPost('name'); $form->handle = craft()->request->getPost('handle'); $form->titleFormat = craft()->request->getPost('titleFormat'); $form->submitAction = craft()->request->getPost('submitAction'); $form->submitButton = craft()->request->getPost('submitButton'); $form->afterSubmitText = craft()->request->getPost('afterSubmitText'); $form->submissionEnabled = craft()->request->getPost('submissionEnabled'); $form->sendCopy = craft()->request->getPost('sendCopy'); $form->sendCopyTo = craft()->request->getPost('sendCopyTo'); $form->notificationEnabled = craft()->request->getPost('notificationEnabled'); $form->notificationFilesEnabled = craft()->request->getPost('notificationFilesEnabled'); $form->notificationRecipients = craft()->request->getPost('notificationRecipients'); $form->notificationSubject = craft()->request->getPost('notificationSubject'); $form->notificationSenderName = craft()->request->getPost('notificationSenderName'); $form->notificationSenderEmail = craft()->request->getPost('notificationSenderEmail'); $form->notificationReplyToEmail = craft()->request->getPost('notificationReplyToEmail'); $form->formTemplate = craft()->request->getPost('formTemplate', $form->formTemplate); $form->tabTemplate = craft()->request->getPost('tabTemplate', $form->tabTemplate); $form->fieldTemplate = craft()->request->getPost('fieldTemplate', $form->fieldTemplate); $form->notificationTemplate = craft()->request->getPost('notificationTemplate', $form->notificationTemplate); // Save form if (craft()->amForms_forms->saveForm($form)) { craft()->userSession->setNotice(Craft::t('Form saved.')); $this->redirectToPostedUrl($form); } else { craft()->userSession->setError(Craft::t('Couldn’t save form.')); // Send the form back to the template craft()->urlManager->setRouteVariables(array('form' => $form)); } }
/** * Get export fields for a form. * * @param AmForms_FormModel $form * * @return array */ public function getExportFields(AmForms_FormModel $form) { // Standard fields $exportFields = array('id' => array('id' => 'id', 'handle' => 'id', 'name' => Craft::t('id'), 'checked' => 0, 'type' => 'PlainText'), 'title' => array('id' => 'title', 'handle' => 'title', 'name' => Craft::t('Title'), 'checked' => 1, 'type' => 'PlainText'), 'dateCreated' => array('id' => 'dateCreated', 'handle' => 'dateCreated', 'name' => Craft::t('Date created'), 'checked' => 0, 'type' => 'Date'), 'dateUpdated' => array('id' => 'dateUpdated', 'handle' => 'dateUpdated', 'name' => Craft::t('Date updated'), 'checked' => 0, 'type' => 'Date'), 'submittedFrom' => array('id' => 'submittedFrom', 'handle' => 'submittedFrom', 'name' => Craft::t('Submitted from'), 'checked' => 0, 'type' => 'PlainText')); // Get fieldlayout fields foreach ($form->getFieldLayout()->getTabs() as $tab) { // Tab fields $fields = $tab->getFields(); foreach ($fields as $layoutField) { // Get actual field $field = $layoutField->getField(); // Add to fields $exportFields[$field->handle] = $field; } } return $exportFields; }
/** * Populates an element model based on a query result. * * @param array $row * * @return AmForms_FormModel */ public function populateElementModel($row) { return AmForms_FormModel::populateModel($row); }
/** * Save a form. * * @param AmForms_FormModel $form * * @throws Exception * @return bool */ public function saveForm(AmForms_FormModel $form) { $isNewForm = !$form->id; // Get the Form record if ($form->id) { $formRecord = AmForms_FormRecord::model()->findById($form->id); if (!$formRecord) { throw new Exception(Craft::t('No form exists with the ID “{id}”.', array('id' => $form->id))); } $oldForm = AmForms_FormModel::populateModel($formRecord); } else { $formRecord = new AmForms_FormRecord(); } // Form attributes $formRecord->setAttributes($form->getAttributes(), false); // Validate the attributes $formRecord->validate(); $form->addErrors($formRecord->getErrors()); // Is submissions or notifications enabled? if (!$form->submissionEnabled && !$form->notificationEnabled) { $form->addError('submissionEnabled', Craft::t('Submissions or notifications must be enabled, otherwise you will lose the submission.')); $form->addError('notificationEnabled', Craft::t('Notifications or submissions must be enabled, otherwise you will lose the submission.')); } if (!$form->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { // Set field context otherwise the layout could fail craft()->content->fieldContext = AmFormsModel::FieldContext; craft()->content->contentTable = AmFormsModel::FieldContent; // Do we need to delete an old field layout? if (!$isNewForm) { $oldLayout = $oldForm->getFieldLayout(); if ($oldLayout) { craft()->fields->deleteLayoutById($oldLayout->id); } } // Do we have a new field layout? if (count($form->getFieldLayout()->getFields()) > 0) { $fieldLayout = $form->getFieldLayout(); // Save the field layout craft()->fields->saveLayout($fieldLayout); // Assign layout to our form $formRecord->fieldLayoutId = $fieldLayout->id; } else { // No field layout given $formRecord->fieldLayoutId = null; } // Save the element! if (craft()->elements->saveElement($form)) { // Now that we have an element ID, save it on the other stuff if ($isNewForm) { $formRecord->id = $form->id; } // Save the form! $formRecord->save(false); // Skip validation now if ($transaction !== null) { $transaction->commit(); } return true; } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } } return false; }