public function onBeforeWrite() { if (!$this->owner->SubmissionID) { $parent = $this->owner->Parent(); if ($parent && $parent instanceof UserSubmissionHolder) { $submission = SubmittedForm::create(); $submission->SubmittedBy = Member::currentUserID(); $submission->ParentID = $parent->ID; $submission->write(); // Attach newly created SubmittedForm to this page $this->owner->OwnedSubmissionID = $this->owner->SubmissionID = $submission->ID; // Add missing values $submission->addMissingValues(); } } if (!$this->owner->ID) { // Setup fields based on SubmittedForm data only if new page instance $this->owner->updateDBFromSubmission(); // If $Title isn't set and the URLSegment is blank, set it to a decent default to avoid bugs with // having a blank URLSegment. if ($this->owner->hasDatabaseField('URLSegment') && !$this->owner->Title && !$this->owner->URLSegment) { if ($this->owner->SubmissionID) { $this->owner->URLSegment = 'page-submission-' . $this->owner->SubmissionID; } else { $this->owner->URLSegment = 'new-page-submission'; } } } parent::onBeforeWrite(); }
/** * Do the dirty work of processing the form submission and saving it if necessary * * This has been overridden to be able to re-edit existing form submissions */ protected function processSubmission($data, $form) { $submittedForm = SubmittedForm::create(); $reEdit = false; if (isset($data['ResumeID'])) { $resumeSubmission = DataObject::get_by_id('SubmittedForm', (int) $data['ResumeID']); // make sure it was this user that submitted it if ($resumeSubmission->isReEditable()) { $submittedForm = $resumeSubmission; $reEdit = true; } } $submittedForm->SubmittedByID = ($id = Member::currentUserID()) ? $id : 0; $submittedForm->ParentID = $this->ID; $submittedForm->Recipient = $this->EmailTo; if (!$this->DisableSaveSubmissions) { $submittedForm->write(); } // email values $values = array(); $recipientAddresses = array(); $sendCopy = false; $attachments = array(); $submittedFields = ArrayList::create(); $titleField = $this->data()->SubmissionTitleField; foreach ($this->Fields() as $field) { // don't show fields that shouldn't be shown if (!$field->showInReports()) { continue; } $submittedField = null; if ($reEdit) { // get the field from the existing submission, otherwise return it // from the form field directly $submittedField = $submittedForm->getFormField($field->Name); } // we want to do things this way to ensure that we have a submittedField - sometimes a field won't // existing on a form re-edit (eg if the form changes) if (!$submittedField) { $submittedField = $field->getSubmittedFormField(); } $submittedField->ParentID = $submittedForm->ID; $submittedField->Name = $field->Name; $submittedField->Title = $field->getField('Title'); if ($field->hasMethod('getValueFromData')) { $submittedField->Value = $field->getValueFromData($data); } else { if (isset($data[$field->Name])) { $submittedField->Value = $data[$field->Name]; } } if ($titleField == $field->Name) { $submittedForm->SubmissionTitle = $submittedField->Value; } if (!empty($data[$field->Name])) { if (in_array("EditableFileField", $field->getClassAncestry())) { if (isset($_FILES[$field->Name])) { $foldername = $field->getFormField()->getFolderName(); // create the file from post data $upload = new Upload(); $file = new File(); $file->ShowInSearch = 0; try { $upload->loadIntoFile($_FILES[$field->Name], $file); } catch (ValidationException $e) { $validationResult = $e->getResult(); $form->addErrorMessage($field->Name, $validationResult->message(), 'bad'); Controller::curr()->redirectBack(); return; } // write file to form field $submittedField->UploadedFileID = $file->ID; // Attach the file if its less than 1MB, provide a link if its over. if ($file->getAbsoluteSize() < 1024 * 1024 * 1) { $attachments[] = $file; } } } } $submittedField->extend('onPopulationFromField', $field); if (!$this->DisableSaveSubmissions) { $submittedField->write(); } $submittedFields->push($submittedField); } $emailData = array("Sender" => Member::currentUser(), "Fields" => $submittedFields); $this->extend('updateEmailData', $emailData, $attachments); // email users on submit. if ($recipients = $this->FilteredEmailRecipients($data, $form)) { $email = new UserDefinedForm_SubmittedFormEmail($submittedFields); if ($attachments) { foreach ($attachments as $file) { if ($file->ID != 0) { $email->attachFile($file->Filename, $file->Filename, HTTP::get_mime_type($file->Filename)); } } } foreach ($recipients as $recipient) { $email->populateTemplate($recipient); $email->populateTemplate($emailData); $email->setFrom($recipient->EmailFrom); $email->setBody($recipient->EmailBody); $email->setTo($recipient->EmailAddress); $email->setSubject($recipient->EmailSubject); if ($recipient->EmailReplyTo) { $email->setReplyTo($recipient->EmailReplyTo); } // check to see if they are a dynamic reply to. eg based on a email field a user selected if ($recipient->SendEmailFromField()) { $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name); if ($submittedFormField && is_string($submittedFormField->Value)) { $email->setReplyTo($submittedFormField->Value); } } // check to see if they are a dynamic reciever eg based on a dropdown field a user selected if ($recipient->SendEmailToField()) { $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name); if ($submittedFormField && is_string($submittedFormField->Value)) { $email->setTo($submittedFormField->Value); } } // check to see if there is a dynamic subject if ($recipient->SendEmailSubjectField()) { $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailSubjectField()->Name); if ($submittedFormField && trim($submittedFormField->Value)) { $email->setSubject($submittedFormField->Value); } } $this->extend('updateEmail', $email, $recipient, $emailData); if ($recipient->SendPlain) { $body = strip_tags($recipient->EmailBody) . "\n"; if (isset($emailData['Fields']) && !$recipient->HideFormData) { foreach ($emailData['Fields'] as $Field) { $body .= $Field->Title . ': ' . $Field->Value . " \n"; } } $email->setBody($body); $email->sendPlain(); } else { $email->send(); } } } $submittedForm->extend('updateAfterProcess'); Session::clear("FormInfo.{$form->FormName()}.errors"); Session::clear("FormInfo.{$form->FormName()}.data"); return $submittedForm; }