/** * Save a submission. * * @param AmForms_SubmissionModel $submission * * @throws Exception * @return bool */ public function saveSubmission(AmForms_SubmissionModel $submission) { $isNewSubmission = !$submission->id; // If we don't need to save it, return a success for other events if ($isNewSubmission && !$submission->form->submissionEnabled) { return true; } // Get the submission record if ($submission->id) { $submissionRecord = AmForms_SubmissionRecord::model()->findById($submission->id); if (!$submissionRecord) { throw new Exception(Craft::t('No submission exists with the ID “{id}”.', array('id' => $submission->id))); } } else { $submissionRecord = new AmForms_SubmissionRecord(); } // Submission attributes $submissionRecord->setAttributes($submission->getAttributes(), false); // Validate the attributes $submissionRecord->validate(); $submission->addErrors($submissionRecord->getErrors()); if (!$submission->hasErrors()) { // Fire an 'onBeforeSaveSubmission' event $event = new Event($this, array('submission' => $submission, 'isNewSubmission' => $isNewSubmission)); $this->onBeforeSaveSubmission($event); // Is the event giving us the go-ahead? if ($event->performAction) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { // Submission title based on form's title format $submission->getContent()->title = craft()->templates->renderObjectTemplate($submission->form->titleFormat, $submission); // Set field context and content $oldFieldContext = craft()->content->fieldContext; $oldContentTable = craft()->content->contentTable; craft()->content->fieldContext = $submission->getFieldContext(); craft()->content->contentTable = $submission->getContentTable(); // Save the element! if (craft()->elements->saveElement($submission)) { // Reset field context and content craft()->content->fieldContext = $oldFieldContext; craft()->content->contentTable = $oldContentTable; // Now that we have an element ID, save it on the other stuff if ($isNewSubmission) { $submissionRecord->id = $submission->id; } // Save the submission! $submissionRecord->save(false); // Skip validation now if ($transaction !== null) { $transaction->commit(); } // Fire an 'onSaveSubmission' event $this->onSaveSubmission(new Event($this, array('submission' => $submission, 'isNewSubmission' => $isNewSubmission))); return true; } // Reset field context and content craft()->content->fieldContext = $oldFieldContext; craft()->content->contentTable = $oldContentTable; } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } } } return false; }
/** * Do redirect with {placeholders} support. * * @param AmForms_SubmissionModel $submission * @param boolean $submitted */ private function _doRedirect(AmForms_SubmissionModel $submission, $submitted) { $vars = array_merge(array('siteUrl' => craft()->getSiteUrl(), 'submitted' => $submitted), $submission->getContent()->getAttributes(), $submission->getAttributes()); $url = null; $redirectUrl = $submission->getForm()->getRedirectUrl(); if (empty($redirectUrl)) { $url = craft()->request->getPath() . '?submitted=' . ($submitted ? $submission->getForm()->handle : 0); } $this->redirectToPostedUrl($vars, $url); }