예제 #1
0
 /**
  * Populates an element model based on a query result.
  *
  * @param array $row
  *
  * @return AmForms_FormModel
  */
 public function populateElementModel($row)
 {
     return AmForms_FormModel::populateModel($row);
 }
예제 #2
0
 /**
  * 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;
 }