/** * 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; }
/** * 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; }
/** * Get form definition. * * @param AmForms_FormModel $form * * @return array */ private function getFormDefinition(AmForms_FormModel $form) { return array('name' => $form->name, 'fieldLayout' => craft()->schematic_fields->getFieldLayoutDefinition($form->getFieldLayout()), 'redirectEntryId' => $form->redirectEntryId, 'handle' => $form->handle, '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); }