/**
  * Return all submissions in this category
  *
  * @return DataList || empty ArrayList
  */
 public function getSubmissions()
 {
     if ($consultations = $this->getConsultations() && $consultations->count() > 0) {
         return SubmittedForm::get()->filter(array('ParentID' => $list));
     }
     return new ArrayList();
 }
	/**
	 * Process the form that is submitted through the site
	 * 
	 * @param Array Data
	 * @param Form Form 
	 * @return Redirection
	 */
	function process($data, $form) {
		// submitted form object
		$submittedForm = new SubmittedForm();
		$submittedForm->SubmittedBy = Member::currentUser();
		$submittedForm->ParentID = $this->ID;
		$submittedForm->Recipient = $this->EmailTo;
		$submittedForm->write();
		
		// email values
		$values = array();
		$recipientAddresses = array();
		$sendCopy = false;
        $attachments = array();

		$submittedFields = new DataObjectSet();
		
		foreach($this->Fields() as $field) {
			// don't show fields that shouldn't be shown
			if(!$field->showInReports()) continue;
			
			$submittedField = new SubmittedFormField();
			$submittedField->ParentID = $submittedForm->ID;
			$submittedField->Name = $field->Name;
			$submittedField->Title = $field->Title;
					
			if($field->hasMethod('getValueFromData')) {
				$submittedField->Value = $field->getValueFromData($data);
			}
			else {
				if(isset($data[$field->Name])) $submittedField->Value = $data[$field->Name];
			}
			
			$submittedField->write();
			$submittedFields->push($submittedField);

			if(!empty($data[$field->Name])){
				/**
				 * @todo this should be on the EditableFile class. Just need to sort out
				 * 		attachments array
				 */
				if($field->ClassName == "EditableFileField"){	
					if(isset($_FILES[$field->Name])) {
						
						// create the file from post data
						$upload = new Upload();
						$file = new File();
						$upload->loadIntoFile($_FILES[$field->Name], $file);

						// 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;
						}

						// Always provide the link if present.
						if($file->ID) {
							$submittedField->Value = "<a href=\"". $file->getFilename() ."\" title=\"". $file->getFilename() . "\">". $file->Title . "</a>";
						} else {
							$submittedField->Value = "";
						}
					}									
				}
			}
			
			// make sure we save
			$submittedField->write();
		}	
		$emailData = array(
			"Sender" => Member::currentUser(),
			"Fields" => $submittedFields,
		);

		// email users on submit. All have their own custom options. 
		if($this->EmailRecipients()) {
			$email = new UserDefinedForm_SubmittedFormEmail($submittedFields);                     
			$email->populateTemplate($emailData);
			if($attachments){
				foreach($attachments as $file){
					// bug with double decorated fields, valid ones should have an ID.
					if($file->ID != 0) {
						$email->attachFile($file->Filename,$file->Filename, $file->getFileType());
					}
				}
			}

			foreach($this->EmailRecipients() as $recipient) {
				$email->populateTemplate($emailData);
				$email->setFrom($recipient->EmailFrom);
				$email->setBody($recipient->EmailBody);
				$email->setSubject($recipient->EmailSubject);
				$email->setTo($recipient->EmailAddress);
				
				// check to see if they are a dynamic sender. eg based on a email field
				// a user selected
				if($recipient->SendEmailFromField()) {
					$name = Convert::raw2sql($recipient->SendEmailFromField()->Name);
					
					if(defined('Database::USE_ANSI_SQL')) {
						$submittedFormField = DataObject::get_one("SubmittedFormField", "\"Name\" = '$name' AND \"ParentID\" = '$submittedForm->ID'");
					} else {
						$submittedFormField = DataObject::get_one("SubmittedFormField", "Name = '$name' AND ParentID = '$submittedForm->ID'");
					}
					
					if($submittedFormField) {
						$email->setFrom($submittedFormField->Value);	
					}
				}
				// check to see if they are a dynamic reciever eg based on a dropdown field
				// a user selected
				if($recipient->SendEmailToField()) {
					$name = Convert::raw2sql($recipient->SendEmailToField()->Name);
					
					if(defined('Database::USE_ANSI_SQL')) {
						$submittedFormField = DataObject::get_one("SubmittedFormField", "\"Name\" = '$name' AND \"ParentID\" = '$submittedForm->ID'");
					} else {
						$submittedFormField = DataObject::get_one("SubmittedFormField", "Name = '$name' AND ParentID = '$submittedForm->ID'");
					}
					
					if($submittedFormField) {
						$email->setTo($submittedFormField->Value);	
					}
				}
				
				if($recipient->SendPlain) {
					$body = strip_tags($recipient->EmailBody) . "\n ";
					if(isset($emailData['Fields'])) {
						foreach($emailData['Fields'] as $Field) {
							$body .= $Field->Title .' - '. $Field->Value .'\n';
						}
					}
					$email->setBody($body);
					$email->sendPlain();
				}
				else {
					$email->send();	
				}
			}
		}
		
		// Redirect to the finished method on this controller with the referrer data
		Director::redirect($this->Link() . 'finished?referrer=' . urlencode($data['Referrer']));
	}
 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();
 }
 /**
  * This action handles rendering the "finished" message, which is 
  * customizable by editing the ReceivedFormSubmission template.
  *
  * @return ViewableData
  */
 public function finished()
 {
     $submission = Session::get('userformssubmission' . $this->ID);
     if ($submission) {
         $submission = SubmittedForm::get()->byId($submission);
     }
     $referrer = isset($_GET['referrer']) ? urldecode($_GET['referrer']) : null;
     if (!$this->DisableAuthenicatedFinishAction) {
         $formProcessed = Session::get('FormProcessed');
         if (!isset($formProcessed)) {
             return $this->redirect($this->Link() . $referrer);
         } else {
             $securityID = Session::get('SecurityID');
             // make sure the session matches the SecurityID and is not left over from another form
             if ($formProcessed != $securityID) {
                 // they may have disabled tokens on the form
                 $securityID = md5(Session::get('FormProcessedNum'));
                 if ($formProcessed != $securityID) {
                     return $this->redirect($this->Link() . $referrer);
                 }
             }
         }
         Session::clear('FormProcessed');
     }
     return $this->customise(array('Content' => $this->customise(array('Submission' => $submission, 'Link' => $referrer))->renderWith('ReceivedFormSubmission'), 'Form' => ''));
 }
 /**
  * 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;
 }
 /**
  * Get all submission for all consultation
  */
 public static function getAllSubmissions()
 {
     $consultations = self::get()->column('ID');
     return SubmittedForm::get()->filter('ParentID', $consultations);
 }
 public function doSearch($data)
 {
     $userSubmissionHolder = $this->controller->data();
     $userSubmissionHolder->SearchData = new ArrayData(array('Keywords' => 'asfaf'));
     // Get list of page IDs of approved submissions
     $submissionIDs = $userSubmissionHolder->PublishedSubmittedFormIDs();
     // If the 'Keywords' field exists, then utilize sent Keywords data.
     $keywords = '';
     if (isset($data['Keywords']) && $data['Keywords'] && $this->Fields()->dataFieldByName('Keywords')) {
         $keywords = $data['Keywords'];
     }
     // Sets up where statements to be seperated by disjunctive (OR) or conjunctive (AND) keywords.
     $wheres = array();
     foreach ($this->Fields() as $field) {
         if ($field->EditableFormField) {
             $name = $field->getName();
             if (isset($data[$name]) && ($value = $data[$name])) {
                 $nameEscaped = Convert::raw2sql($name);
                 if ($field instanceof DropdownField) {
                     // eg. (Name = 'EditableTextField_34' AND Value = 'VIC')
                     $valueEscaped = is_string($value) ? "'" . Convert::raw2sql($value) . "'" : (int) $value;
                     $wheres[$name] = array("SubmittedFormField.Name = '{$nameEscaped}'", "SubmittedFormField.Value = {$valueEscaped}");
                 } else {
                     // eg. (Name = 'EditableTextField_33' AND Value LIKE '%hello%')
                     $valueEscaped = is_string($value) ? "LIKE '%" . Convert::raw2sql($value) . "%'" : '= ' . (int) $value;
                     $wheres[$name] = array("SubmittedFormField.Name = '{$nameEscaped}'", "SubmittedFormField.Value {$valueEscaped}");
                 }
             }
         }
     }
     // Do a keyword search on each of the fields that have it enabled.
     //
     // eg: (((Name = 'EditableTextField_33' AND Value LIKE '%hello%') OR (Name = 'EditableTextField_42' AND Value LIKE '%hello%')))
     //
     if ($keywords) {
         $whereKeywords = array();
         $keywordsEscaped = Convert::raw2sql($keywords);
         foreach ($userSubmissionHolder->Fields() as $editableFormField) {
             if ($editableFormField->UseInKeywordSearch) {
                 $nameEscaped = Convert::raw2sql($editableFormField->Name);
                 $whereKeywords[$editableFormField->Name . '_keywords'] = array("SubmittedFormField.Name = '{$nameEscaped}'", "SubmittedFormField.Value LIKE '%{$keywordsEscaped}%'");
             }
         }
         if ($whereKeywords) {
             $whereKeywordsSQL = '';
             foreach ($whereKeywords as $whereGroup) {
                 $whereKeywordsSQL .= $whereKeywordsSQL ? ' OR ' : '';
                 $whereKeywordsSQL .= '(' . implode(' AND ', $whereGroup) . ')';
             }
             $wheres['_keywords'] = array($whereKeywordsSQL);
         }
     }
     // Only search form field values that belong to a SubmittedForm object that belongs to
     // a UserSubmissionPage (or page extended with UserSubmissionExtended)
     $list = SubmittedForm::get()->filter('ID', $submissionIDs)->innerJoin('SubmittedFormField', 'SubmittedForm.ID = SubmittedFormField.ParentID')->alterDataQuery(function ($query) {
         // This is so you can match against multiple submitted form fields, and do something like "having 3 matches", where 3 is the number of user filters.
         $query->groupby('SubmittedFormField.ParentID');
     });
     // For explicit searches on fields, ie selecting a dropdown value or typing on a text field
     // that searches on a specific field.
     //
     // eg. (Name = 'EditableTextField_34' AND Value = 'VIC') AND (Name = 'EditableTextField_34' AND Value LIKE '%school%')
     //
     if ($wheres) {
         $whereSQL = '';
         foreach ($wheres as $whereGroup) {
             $whereSQL .= $whereSQL ? ' OR ' : '';
             $whereSQL .= '(' . implode(' AND ', $whereGroup) . ')';
         }
         $list = $list->where($whereSQL)->alterDataQuery(function ($query) use($wheres) {
             // This is so you can match against multiple submitted form fields, and do something like "having 3 matches", where 3 is the number of user filters.
             $query->having('COUNT(*) >= ' . count($wheres));
         });
     }
     $resultRecords = array();
     foreach ($list as $submission) {
         if ($page = $submission->UserSubmissionPage()) {
             $resultRecords[$page->ClassName . '_' . $page->ID] = $page;
         }
     }
     $userSubmissionHolder->AllListing = new ArrayList($resultRecords);
     return array();
 }
 /**
  * Process the form that is submitted through the site
  * 
  * @param Array Data
  * @param Form Form 
  * @return Redirection
  */
 function process($data, $form)
 {
     // submitted form object
     $submittedForm = new SubmittedForm();
     $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 = new DataObjectSet();
     foreach ($this->Fields() as $field) {
         // don't show fields that shouldn't be shown
         if (!$field->showInReports()) {
             continue;
         }
         $submittedField = $field->getSubmittedFormField();
         $submittedField->ParentID = $submittedForm->ID;
         $submittedField->Name = $field->Name;
         $submittedField->Title = $field->Title;
         if ($field->hasMethod('getValueFromData')) {
             $submittedField->Value = $field->getValueFromData($data);
         } else {
             if (isset($data[$field->Name])) {
                 $submittedField->Value = $data[$field->Name];
             }
         }
         if (!empty($data[$field->Name])) {
             if (in_array("EditableFileField", $field->getClassAncestry())) {
                 if (isset($_FILES[$field->Name])) {
                     // create the file from post data
                     $upload = new Upload();
                     $file = new File();
                     $upload->loadIntoFile($_FILES[$field->Name], $file);
                     // 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;
                     }
                 }
             }
         }
         if (!$this->DisableSaveSubmissions) {
             $submittedField->write();
         }
         $submittedFields->push($submittedField);
     }
     $emailData = array("Sender" => Member::currentUser(), "Fields" => $submittedFields);
     // email users on submit. All have their own custom options.
     if ($this->EmailRecipients()) {
         $email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
         $email->populateTemplate($emailData);
         if ($attachments) {
             foreach ($attachments as $file) {
                 // bug with double decorated fields, valid ones should have an ID.
                 if ($file->ID != 0) {
                     $email->attachFile($file->Filename, $file->Filename, $file->getFileType());
                 }
             }
         }
         foreach ($this->EmailRecipients() as $recipient) {
             $email->populateTemplate($recipient);
             $email->populateTemplate($emailData);
             $email->setFrom($recipient->EmailFrom);
             $email->setBody($recipient->EmailBody);
             $email->setSubject($recipient->EmailSubject);
             $email->setTo($recipient->EmailAddress);
             // check to see if they are a dynamic sender. eg based on a email field a user selected
             if ($recipient->SendEmailFromField()) {
                 $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name);
                 if ($submittedFormField) {
                     $email->setFrom($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) {
                     $email->setTo($submittedFormField->Value);
                 }
             }
             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();
             }
         }
     }
     return Director::redirect($this->Link() . 'finished?referrer=' . urlencode($data['Referrer']));
 }
 function process($data, $form)
 {
     $submittedForm = new SubmittedForm();
     $submittedForm->SubmittedBy = Member::currentUser();
     $submittedForm->ParentID = $this->ID;
     $submittedForm->Recipient = $this->EmailTo;
     $submittedForm->write();
     $values = array();
     $recipientAddresses = array();
     $sendCopy = false;
     $attachments = array();
     $submittedFields = new DataObjectSet();
     foreach ($this->Fields() as $field) {
         $submittedField = new SubmittedFormField();
         $submittedField->ParentID = $submittedForm->ID;
         $submittedField->Name = $field->Name;
         $submittedField->Title = $field->Title;
         if ($field->hasMethod('getValueFromData')) {
             $submittedField->Value = $field->getValueFromData($data);
         } else {
             if (isset($data[$field->Name])) {
                 $submittedField->Value = $data[$field->Name];
             }
         }
         $submittedField->write();
         $submittedFields->push($submittedField);
         if (!empty($data[$field->Name])) {
             // execute the appropriate functionality based on the form field.
             switch ($field->ClassName) {
                 case "EditableEmailField":
                     if ($field->SendCopy) {
                         $recipientAddresses[] = $data[$field->Name];
                         $sendCopy = true;
                         $values[$field->Title] = '<a style="white-space: nowrap" href="mailto:' . $data[$field->Name] . '">' . $data[$field->Name] . '</a>';
                     }
                     break;
                 case "EditableFileField":
                     // Returns a file type which we attach to the email.
                     $submittedfile = $field->createSubmittedField($data[$field->Name], $submittedForm);
                     $file = $submittedfile->UploadedFile();
                     $filename = $file->getFilename();
                     // Attach the file if its less than 1MB, provide a link if its over.
                     if ($file->getAbsoluteSize() < 1024 * 1024 * 1) {
                         $attachments[] = $file;
                     }
                     // Always provide the link if present.
                     if ($file->ID) {
                         $submittedField->Value = $values[$field->Title] = "<a href=\"" . $filename . "\" title=\"" . Director::absoluteBaseURL() . $filename . "\">Uploaded to: " . Director::absoluteBaseURL() . $filename . "</a>";
                     } else {
                         $submittedField->Value = $values[$field->Title] = "";
                     }
                     break;
             }
         } elseif ($field->hasMethod('getValueFromData')) {
             $values[$field->Title] = Convert::linkIfMatch($field->getValueFromData($data));
         } else {
             if (isset($data[$field->Name])) {
                 $values[$field->Title] = Convert::linkIfMatch($data[$field->Name]);
             }
         }
     }
     if ($this->EmailOnSubmit || $sendCopy) {
         $emailData = array("Recipient" => $this->EmailTo, "Sender" => Member::currentUser(), "Fields" => $submittedFields);
         $email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
         $email->populateTemplate($emailData);
         $email->setTo($this->EmailTo);
         $email->setSubject($this->Title);
         // add attachments to email (<1MB)
         if ($attachments) {
             foreach ($attachments as $file) {
                 $email->attachFile($filename, $filename);
             }
         }
         $email->send();
         // send to each of email fields
         foreach ($recipientAddresses as $addr) {
             $email->setTo($addr);
             $email->send();
         }
     }
     $custom = $this->customise(array("Content" => $this->customise(array('Link' => $data['Referrer']))->renderWith('ReceivedFormSubmission'), "Form" => " "));
     return $custom->renderWith('Page');
 }