silverstripe-userforms/code/model/UserDefinedForm.php

834 lines
24 KiB
PHP
Raw Normal View History

<?php
/**
* @package userforms
*/
class UserDefinedForm extends Page {
2015-09-11 00:20:06 +02:00
/**
2012-09-11 11:30:40 +02:00
* @var string
*/
private static $description = 'Adds a customizable form.';
2012-09-11 11:30:40 +02:00
/**
* @var string Required Identifier
*/
private static $required_identifier = null;
/**
2015-07-24 04:37:48 +02:00
* @var string
*/
private static $email_template_directory = 'userforms/templates/email/';
/**
* Should this module automatically upgrade on dev/build?
*
* @config
2015-07-24 04:37:48 +02:00
* @var bool
*/
2015-07-24 04:37:48 +02:00
private static $upgrade_on_build = true;
/**
2015-07-24 04:37:48 +02:00
* Built in extensions required by this page
* @config
* @var array
*/
2015-07-24 04:37:48 +02:00
private static $extensions = array(
'UserFormFieldEditorExtension'
);
2015-09-11 00:20:06 +02:00
/**
2012-09-11 11:30:40 +02:00
* @var array Fields on the user defined form page.
*/
private static $db = array(
"SubmitButtonText" => "Varchar",
"ClearButtonText" => "Varchar",
"OnCompleteMessage" => "HTMLText",
"ShowClearButton" => "Boolean",
'DisableSaveSubmissions' => 'Boolean',
'EnableLiveValidation' => 'Boolean',
'HideFieldLabels' => 'Boolean',
'DisplayErrorMessagesAtTop' => 'Boolean',
'DisableAuthenicatedFinishAction' => 'Boolean',
'DisableCsrfSecurityToken' => 'Boolean'
);
2015-09-11 00:20:06 +02:00
/**
2012-09-11 11:30:40 +02:00
* @var array Default values of variables when this page is created
2015-09-11 00:20:06 +02:00
*/
private static $defaults = array(
'Content' => '$UserDefinedForm',
'DisableSaveSubmissions' => 0,
'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>'
);
/**
* @var array
*/
private static $has_many = array(
"Submissions" => "SubmittedForm",
"EmailRecipients" => "UserDefinedForm_EmailRecipient"
);
/**
* @var array
* @config
*/
private static $casting = array(
'ErrorContainerID' => 'Text'
);
/**
* Error container selector which matches the element for grouped messages
*
* @var string
* @config
*/
private static $error_container_id = 'error-container';
/**
* Temporary storage of field ids when the form is duplicated.
* Example layout: array('EditableCheckbox3' => 'EditableCheckbox14')
* @var array
*/
protected $fieldsFromTo = array();
/**
* @return FieldList
*/
public function getCMSFields() {
Requirements::css(USERFORMS_DIR . '/css/UserForm_cms.css');
2015-09-11 00:20:06 +02:00
$self = $this;
2015-09-11 00:20:06 +02:00
$this->beforeUpdateCMSFields(function($fields) use ($self) {
2015-09-11 00:20:06 +02:00
// define tabs
$fields->findOrMakeTab('Root.FormOptions', _t('UserDefinedForm.CONFIGURATION', 'Configuration'));
2015-07-13 05:36:20 +02:00
$fields->findOrMakeTab('Root.Recipients', _t('UserDefinedForm.RECIPIENTS', 'Recipients'));
$fields->findOrMakeTab('Root.Submissions', _t('UserDefinedForm.SUBMISSIONS', 'Submissions'));
2015-09-11 00:20:06 +02:00
// text to show on complete
$onCompleteFieldSet = new CompositeField(
$label = new LabelField('OnCompleteMessageLabel',_t('UserDefinedForm.ONCOMPLETELABEL', 'Show on completion')),
$editor = new HtmlEditorField( 'OnCompleteMessage', '', _t('UserDefinedForm.ONCOMPLETEMESSAGE', $self->OnCompleteMessage))
);
2015-09-11 00:20:06 +02:00
$onCompleteFieldSet->addExtraClass('field');
2015-09-11 00:20:06 +02:00
$editor->setRows(3);
$label->addExtraClass('left');
2015-09-11 00:20:06 +02:00
2015-07-13 05:36:20 +02:00
// Define config for email recipients
$emailRecipientsConfig = GridFieldConfig_RecordEditor::create(10);
$emailRecipientsConfig->getComponentByType('GridFieldAddNewButton')
->setButtonName(
_t('UserDefinedForm.ADDEMAILRECIPIENT', 'Add Email Recipient')
);
2015-09-11 00:20:06 +02:00
// who do we email on submission
2015-07-13 05:36:20 +02:00
$emailRecipients = new GridField(
'EmailRecipients',
_t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'),
$self->EmailRecipients(),
$emailRecipientsConfig
);
$emailRecipients
->getConfig()
->getComponentByType('GridFieldDetailForm')
2015-07-24 04:37:48 +02:00
->setItemRequestClass('UserFormRecipientItemRequest');
2015-09-11 00:20:06 +02:00
$fields->addFieldsToTab('Root.FormOptions', $onCompleteFieldSet);
2015-07-13 05:36:20 +02:00
$fields->addFieldToTab('Root.Recipients', $emailRecipients);
$fields->addFieldsToTab('Root.FormOptions', $self->getFormOptions());
2015-09-11 00:20:06 +02:00
// view the submissions
$submissions = new GridField(
2015-09-11 00:20:06 +02:00
'Submissions',
_t('UserDefinedForm.SUBMISSIONS', 'Submissions'),
$self->Submissions()->sort('Created', 'DESC')
);
2015-09-11 00:20:06 +02:00
// make sure a numeric not a empty string is checked against this int column for SQL server
$parentID = (!empty($self->ID)) ? $self->ID : 0;
2015-09-11 00:20:06 +02:00
// get a list of all field names and values used for print and export CSV views of the GridField below.
$columnSQL = <<<SQL
SELECT "Name", "Title"
FROM "SubmittedFormField"
LEFT JOIN "SubmittedForm" ON "SubmittedForm"."ID" = "SubmittedFormField"."ParentID"
WHERE "SubmittedForm"."ParentID" = '$parentID'
ORDER BY "Title" ASC
SQL;
// Sanitise periods in title
$columns = array();
foreach(DB::query($columnSQL)->map() as $name => $title) {
$columns[$name] = trim(strtr($title, '.', ' '));
}
2015-09-11 00:20:06 +02:00
$config = new GridFieldConfig();
$config->addComponent(new GridFieldToolbarHeader());
$config->addComponent($sort = new GridFieldSortableHeader());
$config->addComponent($filter = new UserFormsGridFieldFilterHeader());
$config->addComponent(new GridFieldDataColumns());
$config->addComponent(new GridFieldEditButton());
$config->addComponent(new GridFieldDeleteAction());
$config->addComponent(new GridFieldPageCount('toolbar-header-right'));
$config->addComponent($pagination = new GridFieldPaginator(25));
$config->addComponent(new GridFieldDetailForm());
2015-08-21 03:41:00 +02:00
$config->addComponent(new GridFieldButtonRow('after'));
$config->addComponent($export = new GridFieldExportButton('buttons-after-left'));
$config->addComponent($print = new GridFieldPrintButton('buttons-after-left'));
2015-09-11 00:20:06 +02:00
/**
* Support for {@link https://github.com/colymba/GridFieldBulkEditingTools}
*/
if(class_exists('GridFieldBulkManager')) {
$config->addComponent(new GridFieldBulkManager());
}
2015-09-11 00:20:06 +02:00
$sort->setThrowExceptionOnBadDataType(false);
$filter->setThrowExceptionOnBadDataType(false);
$pagination->setThrowExceptionOnBadDataType(false);
2015-09-11 00:20:06 +02:00
// attach every column to the print view form
$columns['Created'] = 'Created';
$filter->setColumns($columns);
2015-09-11 00:20:06 +02:00
// print configuration
2015-09-11 00:20:06 +02:00
$print->setPrintHasHeader(true);
$print->setPrintColumns($columns);
2015-09-11 00:20:06 +02:00
// export configuration
$export->setCsvHasHeader(true);
$export->setExportColumns($columns);
2015-09-11 00:20:06 +02:00
$submissions->setConfig($config);
$fields->addFieldToTab('Root.Submissions', $submissions);
$fields->addFieldToTab('Root.FormOptions', new CheckboxField('DisableSaveSubmissions', _t('UserDefinedForm.SAVESUBMISSIONS', 'Disable Saving Submissions to Server')));
2015-09-11 00:20:06 +02:00
});
2015-09-11 00:20:06 +02:00
$fields = parent::getCMSFields();
2015-09-11 00:20:06 +02:00
return $fields;
}
/**
* Allow overriding the EmailRecipients on a {@link DataExtension}
* so you can customise who receives an email.
* Converts the RelationList to an ArrayList so that manipulation
* of the original source data isn't possible.
*
* @return ArrayList
*/
public function FilteredEmailRecipients($data = null, $form = null) {
2015-07-13 05:36:20 +02:00
$recipients = new ArrayList($this->EmailRecipients()->toArray());
// Filter by rules
$recipients = $recipients->filterByCallback(function($recipient) use ($data, $form) {
return $recipient->canSend($data, $form);
});
$this->extend('updateFilteredEmailRecipients', $recipients, $data, $form);
return $recipients;
}
/**
2015-09-11 00:20:06 +02:00
* Custom options for the form. You can extend the built in options by
* using {@link updateFormOptions()}
*
* @return FieldList
*/
public function getFormOptions() {
$submit = ($this->SubmitButtonText) ? $this->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit');
$clear = ($this->ClearButtonText) ? $this->ClearButtonText : _t('UserDefinedForm.CLEARBUTTON', 'Clear');
2015-09-11 00:20:06 +02:00
$options = new FieldList(
new TextField("SubmitButtonText", _t('UserDefinedForm.TEXTONSUBMIT', 'Text on submit button:'), $submit),
new TextField("ClearButtonText", _t('UserDefinedForm.TEXTONCLEAR', 'Text on clear button:'), $clear),
new CheckboxField("ShowClearButton", _t('UserDefinedForm.SHOWCLEARFORM', 'Show Clear Form Button'), $this->ShowClearButton),
new CheckboxField("EnableLiveValidation", _t('UserDefinedForm.ENABLELIVEVALIDATION', 'Enable live validation')),
new CheckboxField("HideFieldLabels", _t('UserDefinedForm.HIDEFIELDLABELS', 'Hide field labels')),
new CheckboxField("DisplayErrorMessagesAtTop", _t('UserDefinedForm.DISPLAYERRORMESSAGESATTOP', 'Display error messages above the form?')),
new CheckboxField('DisableCsrfSecurityToken', _t('UserDefinedForm.DISABLECSRFSECURITYTOKEN', 'Disable CSRF Token')),
2015-08-31 04:49:16 +02:00
new CheckboxField('DisableAuthenicatedFinishAction', _t('UserDefinedForm.DISABLEAUTHENICATEDFINISHACTION', 'Disable Authentication on finish action'))
);
2015-09-11 00:20:06 +02:00
$this->extend('updateFormOptions', $options);
2015-09-11 00:20:06 +02:00
return $options;
}
/**
* Get the HTML id of the error container displayed above the form.
*
* @return string
*/
public function getErrorContainerID() {
return $this->config()->error_container_id;
}
2015-07-24 04:37:48 +02:00
public function requireDefaultRecords() {
parent::requireDefaultRecords();
if(!$this->config()->upgrade_on_build) {
return;
}
// Perform migrations
Injector::inst()
->create('UserFormsUpgradeService')
->setQuiet(true)
->run();
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
DB::alteration_message('Migrated userforms', 'changed');
}
/**
* Validate formfields
*/
public function getCMSValidator() {
return new UserFormValidator();
}
}
/**
* Controller for the {@link UserDefinedForm} page type.
*
* @package userforms
*/
class UserDefinedForm_Controller extends Page_Controller {
2015-09-11 00:20:06 +02:00
private static $finished_anchor = '#uff';
2013-07-15 10:49:49 +02:00
private static $allowed_actions = array(
'index',
'ping',
'Form',
'finished'
);
public function init() {
parent::init();
2015-09-11 00:20:06 +02:00
// load the jquery
$lang = i18n::get_lang_from_locale(i18n::get_locale());
2015-08-06 04:19:05 +02:00
Requirements::css(USERFORMS_DIR . '/css/UserForm.css');
Requirements::javascript(FRAMEWORK_DIR .'/thirdparty/jquery/jquery.js');
Requirements::javascript(USERFORMS_DIR . '/thirdparty/jquery-validate/jquery.validate.min.js');
Requirements::add_i18n_javascript(USERFORMS_DIR . '/javascript/lang');
2015-08-06 06:06:56 +02:00
Requirements::javascript(USERFORMS_DIR . '/javascript/UserForm.js');
2015-08-21 01:41:42 +02:00
Requirements::javascript(
USERFORMS_DIR . "/thirdparty/jquery-validate/localization/messages_{$lang}.min.js"
);
Requirements::javascript(
USERFORMS_DIR . "/thirdparty/jquery-validate/localization/methods_{$lang}.min.js"
);
if($this->HideFieldLabels) {
Requirements::javascript(USERFORMS_DIR . '/thirdparty/Placeholders.js/Placeholders.min.js');
}
}
2015-09-11 00:20:06 +02:00
/**
* Using $UserDefinedForm in the Content area of the page shows
* where the form should be rendered into. If it does not exist
* then default back to $Form.
*
* @return array
*/
public function index() {
if($this->Content && $form = $this->Form()) {
$hasLocation = stristr($this->Content, '$UserDefinedForm');
if($hasLocation) {
$content = preg_replace('/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', $form->forTemplate(), $this->Content);
return array(
'Content' => DBField::create_field('HTMLText', $content),
'Form' => ""
);
}
}
return array(
'Content' => DBField::create_field('HTMLText', $this->Content),
'Form' => $this->Form()
);
}
/**
* Keep the session alive for the user.
*
* @return int
*/
public function ping() {
return 1;
}
/**
* Get the form for the page. Form can be modified by calling {@link updateForm()}
* on a UserDefinedForm extension.
*
2015-07-24 04:37:48 +02:00
* @return Forms
*/
public function Form() {
2015-07-24 04:37:48 +02:00
$form = UserForm::create($this);
$this->generateConditionalJavascript();
2015-07-24 04:37:48 +02:00
return $form;
}
/**
* Generate the javascript for the conditional field show / hiding logic.
*
* @return void
*/
public function generateConditionalJavascript() {
$default = "";
$rules = "";
$watch = array();
$watchLoad = array();
if($this->Fields()) {
foreach($this->Fields() as $field) {
$holderSelector = $field->getSelectorHolder();
2015-07-24 04:37:48 +02:00
// Is this Field Show by Default
2015-07-24 04:37:48 +02:00
if(!$field->ShowOnLoad) {
2015-08-17 00:43:51 +02:00
$default .= "{$holderSelector}.hide().trigger('userform.field.hide');\n";
}
// Check for field dependencies / default
2015-07-24 04:37:48 +02:00
foreach($field->DisplayRules() as $rule) {
// Get the field which is effected
$formFieldWatch = EditableFormField::get()->byId($rule->ConditionFieldID);
2015-09-18 01:07:58 +02:00
// Skip deleted fields
if(!$formFieldWatch) {
continue;
}
2015-07-24 04:37:48 +02:00
$fieldToWatch = $formFieldWatch->getSelectorField($rule);
$fieldToWatchOnLoad = $formFieldWatch->getSelectorField($rule, true);
2015-07-24 04:37:48 +02:00
// show or hide?
$view = ($rule->Display == 'Hide') ? 'hide' : 'show';
$opposite = ($view == "show") ? "hide" : "show";
// what action do we need to keep track of. Something nicer here maybe?
// @todo encapulsation
$action = "change";
if($formFieldWatch instanceof EditableTextField) {
2015-07-24 04:37:48 +02:00
$action = "keyup";
}
// is this field a special option field
$checkboxField = false;
$radioField = false;
if(in_array($formFieldWatch->ClassName, array('EditableCheckboxGroupField', 'EditableCheckbox'))) {
$action = "click";
$checkboxField = true;
} else if ($formFieldWatch->ClassName == "EditableRadioField") {
$radioField = true;
}
// and what should we evaluate
switch($rule->ConditionOption) {
case 'IsNotBlank':
$expression = ($checkboxField || $radioField) ? '$(this).is(":checked")' :'$(this).val() != ""';
2015-07-24 04:37:48 +02:00
break;
case 'IsBlank':
$expression = ($checkboxField || $radioField) ? '!($(this).is(":checked"))' : '$(this).val() == ""';
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
break;
case 'HasValue':
if ($checkboxField) {
$expression = '$(this).prop("checked")';
} else if ($radioField) {
// We cannot simply get the value of the radio group, we need to find the checked option first.
$expression = '$(this).parents(".field, .control-group").find("input:checked").val()=="'. $rule->FieldValue .'"';
} else {
$expression = '$(this).val() == "'. $rule->FieldValue .'"';
}
2015-07-24 04:37:48 +02:00
break;
case 'ValueLessThan':
$expression = '$(this).val() < parseFloat("'. $rule->FieldValue .'")';
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
break;
case 'ValueLessThanEqual':
$expression = '$(this).val() <= parseFloat("'. $rule->FieldValue .'")';
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
break;
case 'ValueGreaterThan':
$expression = '$(this).val() > parseFloat("'. $rule->FieldValue .'")';
break;
case 'ValueGreaterThanEqual':
$expression = '$(this).val() >= parseFloat("'. $rule->FieldValue .'")';
break;
default: // ==HasNotValue
if ($checkboxField) {
$expression = '!$(this).prop("checked")';
} else if ($radioField) {
// We cannot simply get the value of the radio group, we need to find the checked option first.
$expression = '$(this).parents(".field, .control-group").find("input:checked").val()!="'. $rule->FieldValue .'"';
} else {
$expression = '$(this).val() != "'. $rule->FieldValue .'"';
}
2015-07-24 04:37:48 +02:00
break;
}
2015-07-24 04:37:48 +02:00
if(!isset($watch[$fieldToWatch])) {
$watch[$fieldToWatch] = array();
}
2015-08-11 22:57:19 +02:00
$watch[$fieldToWatch][] = array(
2015-07-24 04:37:48 +02:00
'expression' => $expression,
'holder_selector' => $holderSelector,
2015-07-24 04:37:48 +02:00
'view' => $view,
'opposite' => $opposite,
'action' => $action
);
2015-07-24 04:37:48 +02:00
$watchLoad[$fieldToWatchOnLoad] = true;
}
}
}
2015-09-11 00:20:06 +02:00
if($watch) {
foreach($watch as $key => $values) {
$logic = array();
$actions = array();
foreach($values as $rule) {
2015-08-17 00:43:51 +02:00
// Assign action
$actions[$rule['action']] = $rule['action'];
2015-08-17 00:43:51 +02:00
// Assign behaviour
$expression = $rule['expression'];
$holder = $rule['holder_selector'];
$view = $rule['view']; // hide or show
$opposite = $rule['opposite'];
// Generated javascript for triggering visibility
$logic[] = <<<"EOS"
if({$expression}) {
{$holder}
.{$view}()
.trigger('userform.field.{$view}');
} else {
{$holder}
.{$opposite}()
.trigger('userform.field.{$opposite}');
}
EOS;
}
$logic = implode("\n", $logic);
$rules .= $key.".each(function() {\n
$(this).data('userformConditions', function() {\n
$logic\n
}); \n
});\n";
foreach($actions as $action) {
$rules .= $key.".$action(function() {
$(this).data('userformConditions').call(this);\n
});\n";
}
}
}
if($watchLoad) {
foreach($watchLoad as $key => $value) {
$rules .= $key.".each(function() {
$(this).data('userformConditions').call(this);\n
});\n";
}
}
// Only add customScript if $default or $rules is defined
if($default || $rules) {
Requirements::customScript(<<<JS
(function($) {
$(document).ready(function() {
$default
$rules
})
})(jQuery);
JS
, 'UserFormsConditional');
}
}
/**
* 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();
}
2015-07-24 04:37:48 +02:00
$attachments = array();
$submittedFields = new ArrayList();
2015-07-24 04:37:48 +02:00
foreach($this->Fields() as $field) {
if(!$field->showInReports()) {
continue;
}
2015-07-24 04:37:48 +02:00
$submittedField = $field->getSubmittedFormField();
$submittedField->ParentID = $submittedForm->ID;
$submittedField->Name = $field->Name;
$submittedField->Title = $field->getField('Title');
2015-07-24 04:37:48 +02:00
// save the value from the data
2009-04-27 08:17:02 +02:00
if($field->hasMethod('getValueFromData')) {
$submittedField->Value = $field->getValueFromData($data);
} else {
if(isset($data[$field->Name])) {
$submittedField->Value = $data[$field->Name];
}
}
2015-09-10 14:08:09 +02:00
if(!empty($data[$field->Name])) {
if(in_array("EditableFileField", $field->getClassAncestry())) {
if(isset($_FILES[$field->Name])) {
$foldername = $field->getFormField()->getFolderName();
2015-09-11 00:20:06 +02:00
// 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;
2015-09-11 00:20:06 +02:00
// attach a file only if lower than 1MB
2015-09-10 14:08:09 +02:00
if($file->getAbsoluteSize() < 1024*1024*1) {
$attachments[] = $file;
}
}
}
}
2015-07-24 04:37:48 +02:00
$submittedField->extend('onPopulationFromField', $field);
2015-09-11 00:20:06 +02:00
if(!$this->DisableSaveSubmissions) {
$submittedField->write();
}
2015-07-24 04:37:48 +02:00
$submittedFields->push($submittedField);
}
2015-07-24 04:37:48 +02:00
$emailData = array(
"Sender" => Member::currentUser(),
2009-07-27 01:23:45 +02:00
"Fields" => $submittedFields
);
2015-07-24 04:37:48 +02:00
2014-02-03 00:10:44 +01:00
$this->extend('updateEmailData', $emailData, $attachments);
2015-07-24 04:37:48 +02:00
// email users on submit.
if($recipients = $this->FilteredEmailRecipients($data, $form)) {
2015-07-24 04:37:48 +02:00
$email = new UserFormRecipientEmail($submittedFields);
$mergeFields = $this->getMergeFieldsMap($emailData['Fields']);
2014-02-03 00:10:44 +01:00
if($attachments) {
foreach($attachments as $file) {
if($file->ID != 0) {
$email->attachFile(
2015-09-11 00:20:06 +02:00
$file->Filename,
$file->Filename,
HTTP::get_mime_type($file->Filename)
);
}
}
}
foreach($recipients as $recipient) {
$parsedBody = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields);
if (!$recipient->SendPlain && $recipient->emailTemplateExists()) {
$email->setTemplate($recipient->EmailTemplate);
}
2009-07-27 01:23:45 +02:00
$email->populateTemplate($recipient);
$email->populateTemplate($emailData);
$email->setFrom($recipient->EmailFrom);
$email->setBody($parsedBody);
$email->setTo($recipient->EmailAddress);
$email->setSubject($recipient->EmailSubject);
2015-09-11 00:20:06 +02:00
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);
2015-09-11 00:20:06 +02:00
if($submittedFormField && is_string($submittedFormField->Value)) {
$email->setTo($submittedFormField->Value);
}
}
2015-09-11 00:20:06 +02:00
// 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();
}
}
}
2015-07-24 04:37:48 +02:00
$submittedForm->extend('updateAfterProcess');
2014-07-11 23:43:52 +02:00
Session::clear("FormInfo.{$form->FormName()}.errors");
Session::clear("FormInfo.{$form->FormName()}.data");
2015-09-11 00:20:06 +02:00
$referrer = (isset($data['Referrer'])) ? '?referrer=' . urlencode($data['Referrer']) : "";
2015-09-11 00:20:06 +02:00
// 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);
}
}
}
2015-07-24 04:37:48 +02:00
2014-07-11 23:43:52 +02:00
if(!$this->DisableSaveSubmissions) {
Session::set('userformssubmission'. $this->ID, $submittedForm->ID);
}
return $this->redirect($this->Link('finished') . $referrer . $this->config()->finished_anchor);
}
/**
* Allows the use of field values in email body.
*
* @param ArrayList fields
* @return ArrayData
*/
private function getMergeFieldsMap($fields = array()) {
$data = new ArrayData(array());
foreach ($fields as $field) {
$data->setField($field->Name, DBField::create_field('Text', $field->Value));
}
return $data;
}
/**
2015-09-11 00:20:06 +02:00
* This action handles rendering the "finished" message, which is
* customizable by editing the ReceivedFormSubmission template.
*
* @return ViewableData
*/
public function finished() {
2014-07-11 23:43:52 +02:00
$submission = Session::get('userformssubmission'. $this->ID);
if($submission) {
$submission = SubmittedForm::get()->byId($submission);
}
$referrer = isset($_GET['referrer']) ? urldecode($_GET['referrer']) : null;
2015-09-11 00:20:06 +02:00
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');
}
2009-07-05 09:39:28 +02:00
return $this->customise(array(
2014-07-11 23:43:52 +02:00
'Content' => $this->customise(array(
'Submission' => $submission,
'Link' => $referrer
))->renderWith('ReceivedFormSubmission'),
'Form' => '',
));
}
}