Created
October 29, 2015 06:07
-
-
Save muskie9/d85e78a847c8593e5abc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyUserForm extends UserDefinedForm{ | |
private static $db = array( | |
'Template' => 'Enum("Template1","Template2","Template3")' | |
); | |
public funciton getCMSFields(){ | |
$fields = parent::getCMSFields(); | |
//implement template dropdown here | |
return $fields; | |
} | |
} | |
class MyUserForm_Controller extends UserDefinedForm_Controller{ | |
/** | |
* Process the form that is submitted through the site | |
* | |
* {@see UserForm::validate()} for validation step prior to processing | |
* | |
* @param array $data | |
* @param Form $form | |
* | |
* @return Redirection | |
*/ | |
public function process($data, $form) { | |
$submittedForm = Object::create('SubmittedForm'); | |
$submittedForm->SubmittedByID = ($id = Member::currentUserID()) ? $id : 0; | |
$submittedForm->ParentID = $this->ID; | |
// if saving is not disabled save now to generate the ID | |
if(!$this->DisableSaveSubmissions) { | |
$submittedForm->write(); | |
} | |
$attachments = array(); | |
$submittedFields = new ArrayList(); | |
foreach($this->Fields() as $field) { | |
if(!$field->showInReports()) { | |
continue; | |
} | |
$submittedField = $field->getSubmittedFormField(); | |
$submittedField->ParentID = $submittedForm->ID; | |
$submittedField->Name = $field->Name; | |
$submittedField->Title = $field->getField('Title'); | |
// save the value from the data | |
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])) { | |
$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, $foldername); | |
} 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 a file only if lower than 1MB | |
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)) { | |
foreach($recipients as $recipient) { | |
$email = new UserFormRecipientEmail($submittedFields); | |
$mergeFields = $this->getMergeFieldsMap($emailData['Fields']); | |
if($attachments) { | |
foreach($attachments as $file) { | |
if($file->ID != 0) { | |
$email->attachFile( | |
$file->Filename, | |
$file->Filename, | |
HTTP::get_mime_type($file->Filename) | |
); | |
} | |
} | |
} | |
$parsedBody = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields); | |
if (!$recipient->SendPlain && $recipient->emailTemplateExists()) { | |
$email->setTemplate($recipient->EmailTemplate); | |
} | |
$email->populateTemplate($recipient); | |
$email->populateTemplate($emailData); | |
$email->setFrom($recipient->EmailFrom); | |
$email->setBody($parsedBody); | |
$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->getEmailBodyContent()) . "\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"); | |
$referrer = (isset($data['Referrer'])) ? '?referrer=' . urlencode($data['Referrer']) : ""; | |
// set a session variable from the security ID to stop people accessing | |
// the finished method directly. | |
if(!$this->DisableAuthenicatedFinishAction) { | |
if (isset($data['SecurityID'])) { | |
Session::set('FormProcessed',$data['SecurityID']); | |
} else { | |
// if the form has had tokens disabled we still need to set FormProcessed | |
// to allow us to get through the finshed method | |
if (!$this->Form()->getSecurityToken()->isEnabled()) { | |
$randNum = rand(1, 1000); | |
$randHash = md5($randNum); | |
Session::set('FormProcessed',$randHash); | |
Session::set('FormProcessedNum',$randNum); | |
} | |
} | |
} | |
if(!$this->DisableSaveSubmissions) { | |
Session::set('userformssubmission'. $this->ID, $submittedForm->ID); | |
} | |
return $this->redirect($this->Link('finished') . $referrer . $this->config()->finished_anchor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment