mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
FEATURE: added ability to send emails to multiple people on form submission with custom email subjects and body text. MINOR: fixed comments in userforms as it has been moved out of cms package
This commit is contained in:
parent
597c579373
commit
9e7e211927
@ -1,44 +1,102 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Page type that lets users build a contact form.
|
* User Defined Form Page type that lets users build a form in the CMS
|
||||||
* @package cms
|
* using the FieldEditor Field.
|
||||||
* @subpackage pagetypes
|
*
|
||||||
|
* @package userforms
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class UserDefinedForm extends Page {
|
class UserDefinedForm extends Page {
|
||||||
|
|
||||||
static $add_action = "a contact form";
|
/**
|
||||||
|
* Add Action in the CMS
|
||||||
|
*
|
||||||
|
* @var String
|
||||||
|
*/
|
||||||
|
static $add_action = "A User Form";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Icon for the User Defined Form in the CMS. Without the extension
|
||||||
|
* or the -file
|
||||||
|
*
|
||||||
|
* @var String
|
||||||
|
*/
|
||||||
static $icon = "cms/images/treeicons/task";
|
static $icon = "cms/images/treeicons/task";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What level permission is needed to edit / add
|
||||||
|
* this page type
|
||||||
|
*
|
||||||
|
* @var String
|
||||||
|
*/
|
||||||
static $need_permission = 'ADMIN';
|
static $need_permission = 'ADMIN';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fields on the user defined form page.
|
||||||
|
*
|
||||||
|
* @var Array
|
||||||
|
*/
|
||||||
static $db = array(
|
static $db = array(
|
||||||
"EmailTo" => "Varchar(255)",
|
|
||||||
"EmailOnSubmit" => "Boolean",
|
"EmailOnSubmit" => "Boolean",
|
||||||
|
"EmailOnSubmitSubject" => "Varchar(200)",
|
||||||
"SubmitButtonText" => "Varchar",
|
"SubmitButtonText" => "Varchar",
|
||||||
"OnCompleteMessage" => "HTMLText",
|
"OnCompleteMessage" => "HTMLText",
|
||||||
"EmailMessageToSubmitter" => "HTMLText",
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default values of variables when this page is created
|
||||||
|
* in the CMS
|
||||||
|
*
|
||||||
|
* @var Array
|
||||||
|
*/
|
||||||
static $defaults = array(
|
static $defaults = array(
|
||||||
"OnCompleteMessage" => "<p>Thanks, we've received your submission.</p>",
|
'Content' => '$UserDefinedForm',
|
||||||
|
'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Array
|
||||||
|
*/
|
||||||
static $has_many = array(
|
static $has_many = array(
|
||||||
"Fields" => "EditableFormField",
|
"Fields" => "EditableFormField",
|
||||||
"Submissions" => "SubmittedForm"
|
"Submissions" => "SubmittedForm",
|
||||||
|
"EmailRecipients" => "UserDefinedForm_EmailRecipient"
|
||||||
);
|
);
|
||||||
|
|
||||||
protected $fields;
|
protected $fields;
|
||||||
|
|
||||||
function getCMSFields($cms) {
|
/**
|
||||||
$fields = parent::getCMSFields($cms);
|
* Setup the CMS Fields for the User Defined Form
|
||||||
|
*
|
||||||
|
* @return FieldSet
|
||||||
|
*/
|
||||||
|
function getCMSFields() {
|
||||||
|
$fields = parent::getCMSFields();
|
||||||
|
|
||||||
|
// field editor
|
||||||
$fields->addFieldToTab("Root."._t('UserDefinedForm.FORM', 'Form'), new FieldEditor("Fields", 'Fields', "", $this ));
|
$fields->addFieldToTab("Root."._t('UserDefinedForm.FORM', 'Form'), new FieldEditor("Fields", 'Fields', "", $this ));
|
||||||
|
|
||||||
|
// view the submissions
|
||||||
$fields->addFieldToTab("Root."._t('UserDefinedForm.SUBMISSIONS','Submissions'), new SubmittedFormReportField( "Reports", _t('UserDefinedForm.RECEIVED', 'Received Submissions'), "", $this ) );
|
$fields->addFieldToTab("Root."._t('UserDefinedForm.SUBMISSIONS','Submissions'), new SubmittedFormReportField( "Reports", _t('UserDefinedForm.RECEIVED', 'Received Submissions'), "", $this ) );
|
||||||
|
|
||||||
|
// who do we email on submission
|
||||||
|
$emailRecipients = new HasManyComplexTableField($this,
|
||||||
|
'EmailRecipients',
|
||||||
|
'UserDefinedForm_EmailRecipient',
|
||||||
|
array(
|
||||||
|
'EmailAddress' => 'Email',
|
||||||
|
'EmailSubject' => 'Subject',
|
||||||
|
'EmailFrom' => 'From'
|
||||||
|
),
|
||||||
|
'getCMSFields_forPopup'
|
||||||
|
);
|
||||||
|
$emailRecipients->setAddTitle(_t('UserDefinedForm.AEMAILRECIPIENT', 'A Email Recipient'));
|
||||||
|
$fields->addFieldToTab("Root."._t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $emailRecipients);
|
||||||
|
|
||||||
|
// text to show on complete
|
||||||
$onCompleteFieldSet = new FieldSet(
|
$onCompleteFieldSet = new FieldSet(
|
||||||
new HtmlEditorField( "OnCompleteMessage", _t('UserDefinedForm.ONCOMPLETELABEL', 'Show on completion'),3,"",_t('UserDefinedForm.ONCOMPLETEMESSAGE', $this->OnCompleteMessage), $this ),
|
new HtmlEditorField( "OnCompleteMessage", _t('UserDefinedForm.ONCOMPLETELABEL', 'Show on completion'),3,"",_t('UserDefinedForm.ONCOMPLETEMESSAGE', $this->OnCompleteMessage), $this ),
|
||||||
|
new TextField("EmailOnSubmitSubject", _t('UserDefinedForm.ONSUBMITSUBJECT', 'Email Subject')),
|
||||||
new HtmlEditorField( "EmailMessageToSubmitter", _t('UserDefinedForm.EMAILMESSAGETOSUBMITTER', 'Email message to submitter'),3,"",_t('UserDefinedForm.EMAILMESSAGETOSUBMITTER', $this->EmailMessageToSubmitter), $this )
|
new HtmlEditorField( "EmailMessageToSubmitter", _t('UserDefinedForm.EMAILMESSAGETOSUBMITTER', 'Email message to submitter'),3,"",_t('UserDefinedForm.EMAILMESSAGETOSUBMITTER', $this->EmailMessageToSubmitter), $this )
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -158,9 +216,11 @@ class UserDefinedForm extends Page {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller for the {@link UserDefinedForm} page type.
|
* Controller for the {@link UserDefinedForm} page type.
|
||||||
* @package cms
|
*
|
||||||
|
* @package userform
|
||||||
* @subpackage pagetypes
|
* @subpackage pagetypes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class UserDefinedForm_Controller extends Page_Controller {
|
class UserDefinedForm_Controller extends Page_Controller {
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
@ -176,6 +236,8 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
*
|
*
|
||||||
* In order to run this export function, the user must be
|
* In order to run this export function, the user must be
|
||||||
* able to edit the page, so we check canEdit()
|
* able to edit the page, so we check canEdit()
|
||||||
|
*
|
||||||
|
* @return HTTPResponse / bool
|
||||||
*/
|
*/
|
||||||
function export() {
|
function export() {
|
||||||
if(!$this->canEdit()) return false;
|
if(!$this->canEdit()) return false;
|
||||||
@ -252,7 +314,7 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
user_error("No submissions to export.", E_USER_ERROR);
|
user_error("No submissions to export.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTP::sendFileToBrowser($csvData, $fileName);
|
HTTPRequest::send_file($csvData, $fileName)->output();
|
||||||
} else {
|
} else {
|
||||||
user_error("'$SQL_ID' is a valid type, but we can't find a UserDefinedForm in the database that matches the ID.", E_USER_ERROR);
|
user_error("'$SQL_ID' is a valid type, but we can't find a UserDefinedForm in the database that matches the ID.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
@ -261,6 +323,13 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User Defined Form. Feature of the user defined form is if you want the
|
||||||
|
* form to appear in a custom location on the page you can use $UserDefinedForm
|
||||||
|
* in the content area to describe where you want the form
|
||||||
|
*
|
||||||
|
* @return Form
|
||||||
|
*/
|
||||||
function Form() {
|
function Form() {
|
||||||
// Build fields
|
// Build fields
|
||||||
$fields = new FieldSet();
|
$fields = new FieldSet();
|
||||||
@ -298,47 +367,56 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
return new SubmittedFormReportField_FilterForm( $this, 'ReportFilterForm' );
|
return new SubmittedFormReportField_FilterForm( $this, 'ReportFilterForm' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process the form that is submitted through the site
|
||||||
|
*
|
||||||
|
* @param Array Data
|
||||||
|
* @param Form Form
|
||||||
|
* @return Redirection
|
||||||
|
*/
|
||||||
function process($data, $form) {
|
function process($data, $form) {
|
||||||
|
|
||||||
|
// submitted form object
|
||||||
$submittedForm = new SubmittedForm();
|
$submittedForm = new SubmittedForm();
|
||||||
$submittedForm->SubmittedBy = Member::currentUser();
|
$submittedForm->SubmittedBy = Member::currentUser();
|
||||||
$submittedForm->ParentID = $this->ID;
|
$submittedForm->ParentID = $this->ID;
|
||||||
$submittedForm->Recipient = $this->EmailTo;
|
$submittedForm->Recipient = $this->EmailTo;
|
||||||
$submittedForm->write();
|
$submittedForm->write();
|
||||||
|
|
||||||
|
// email values
|
||||||
$values = array();
|
$values = array();
|
||||||
$recipientAddresses = array();
|
$recipientAddresses = array();
|
||||||
$sendCopy = false;
|
$sendCopy = false;
|
||||||
|
|
||||||
$attachments = array();
|
$attachments = array();
|
||||||
|
|
||||||
|
|
||||||
$submittedFields = new DataObjectSet();
|
$submittedFields = new DataObjectSet();
|
||||||
|
|
||||||
foreach($this->Fields() as $field) {
|
foreach($this->Fields() as $field) {
|
||||||
$submittedField = new SubmittedFormField();
|
$submittedField = new SubmittedFormField();
|
||||||
$submittedField->ParentID = $submittedForm->ID;
|
$submittedField->ParentID = $submittedForm->ID;
|
||||||
$submittedField->Name = $field->Name;
|
$submittedField->Name = $field->Name;
|
||||||
$submittedField->Title = $field->Title;
|
$submittedField->Title = $field->Title;
|
||||||
|
|
||||||
if( $field->hasMethod( 'getValueFromData' ) )
|
if($field->hasMethod( 'getValueFromData' )) {
|
||||||
$submittedField->Value = $field->getValueFromData( $data );
|
$submittedField->Value = $field->getValueFromData( $data );
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
if(isset($data[$field->Name])) $submittedField->Value = $data[$field->Name];
|
if(isset($data[$field->Name])) $submittedField->Value = $data[$field->Name];
|
||||||
|
}
|
||||||
|
|
||||||
$submittedField->write();
|
$submittedField->write();
|
||||||
$submittedFields->push($submittedField);
|
$submittedFields->push($submittedField);
|
||||||
|
|
||||||
if(!empty( $data[$field->Name])){
|
if(!empty( $data[$field->Name])){
|
||||||
// execute the appropriate functionality based on the form field.
|
|
||||||
switch($field->ClassName){
|
switch($field->ClassName){
|
||||||
|
|
||||||
case "EditableEmailField" :
|
case "EditableEmailField" :
|
||||||
|
|
||||||
if($field->SendCopy){
|
if($field->SendCopy){
|
||||||
$recipientAddresses[] = $data[$field->Name];
|
$recipientAddresses[] = $data[$field->Name];
|
||||||
$sendCopy = true;
|
$sendCopy = true;
|
||||||
$values[$field->Title] = '<a style="white-space: nowrap" href="mailto:'.$data[$field->Name].'">'.$data[$field->Name].'</a>';
|
$values[$field->Title] = '<a style="white-space: nowrap" href="mailto:'.$data[$field->Name].'">'.$data[$field->Name].'</a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "EditableFileField" :
|
case "EditableFileField" :
|
||||||
@ -372,35 +450,34 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract email data
|
|
||||||
$emailData = array(
|
$emailData = array(
|
||||||
"Recipient" => $this->EmailTo,
|
|
||||||
"Sender" => Member::currentUser(),
|
"Sender" => Member::currentUser(),
|
||||||
"Fields" => $submittedFields,
|
"Fields" => $submittedFields,
|
||||||
);
|
);
|
||||||
|
|
||||||
if( $this->EmailOnSubmit ) {
|
// email users on submit. All have their own custom options
|
||||||
|
if($this->EmailRecipients()) {
|
||||||
$email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
|
$email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
|
||||||
$email->populateTemplate($emailData);
|
$email->populateTemplate($emailData);
|
||||||
$email->setTo( $this->EmailTo );
|
|
||||||
$email->setSubject( $this->Title );
|
|
||||||
|
|
||||||
// add attachments to email (<1MB)
|
|
||||||
if($attachments){
|
if($attachments){
|
||||||
foreach($attachments as $file){
|
foreach($attachments as $file){
|
||||||
$email->attachFile($filename,$filename);
|
$email->attachFile($filename,$filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
foreach($this->EmailRecipients() as $recipient) {
|
||||||
|
$email->populateTemplate($emailData);
|
||||||
|
$email->setFrom($recipient->EmailFrom);
|
||||||
|
$email->setBody($recipient->EmailBody);
|
||||||
|
$email->setSubject($recipient->EmailSubject);
|
||||||
|
$email->setTo($recipient->EmailAddress);
|
||||||
$email->send();
|
$email->send();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// send a copy to the author of the form
|
||||||
if($sendCopy) {
|
if($sendCopy) {
|
||||||
// send to each of email fields
|
|
||||||
$emailToSubmiter = new UserDefinedForm_SubmittedFormEmailToSubmitter($submittedFields);
|
$emailToSubmiter = new UserDefinedForm_SubmittedFormEmailToSubmitter($submittedFields);
|
||||||
$emailToSubmiter->populateTemplate($emailData);
|
$emailToSubmiter->setSubject($this->EmailOnSubmitSubject);
|
||||||
$emailToSubmiter->setSubject( $this->Title );
|
|
||||||
|
|
||||||
foreach($recipientAddresses as $addr) {
|
foreach($recipientAddresses as $addr) {
|
||||||
$emailToSubmiter->setBody($this->EmailMessageToSubmitter);
|
$emailToSubmiter->setBody($this->EmailMessageToSubmitter);
|
||||||
@ -437,33 +514,43 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Email that gets sent when a submission is made.
|
* A Form can have multiply members / emails to email the submission
|
||||||
* @package cms
|
* to and custom subjects
|
||||||
* @subpackage pagetypes
|
*
|
||||||
|
* @package userforms
|
||||||
|
*/
|
||||||
|
class UserDefinedForm_EmailRecipient extends DataObject {
|
||||||
|
|
||||||
|
static $db = array(
|
||||||
|
'EmailAddress' => 'Varchar(200)',
|
||||||
|
'EmailSubject' => 'Varchar(200)',
|
||||||
|
'EmailFrom' => 'Varchar(200)',
|
||||||
|
'EmailBody' => 'HTMLText'
|
||||||
|
);
|
||||||
|
|
||||||
|
static $has_one = array(
|
||||||
|
'Form' => 'UserDefinedForm'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Email that gets sent to the people listed in the Email Recipients
|
||||||
|
* when a submission is made
|
||||||
|
*
|
||||||
|
* @package userforms
|
||||||
*/
|
*/
|
||||||
class UserDefinedForm_SubmittedFormEmail extends Email {
|
class UserDefinedForm_SubmittedFormEmail extends Email {
|
||||||
protected $ss_template = "SubmittedFormEmail";
|
protected $ss_template = "SubmittedFormEmail";
|
||||||
protected $from = '$Sender.Email';
|
|
||||||
protected $to = '$Recipient.Email';
|
|
||||||
protected $subject = 'Submission of form';
|
|
||||||
protected $data;
|
protected $data;
|
||||||
|
|
||||||
function __construct($values) {
|
function __construct() {
|
||||||
$this->subject = _t('UserDefinedForm_SubmittedFormEmail.EMAILSUBJECT', 'Submission of form');
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->data = $values;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Data() {
|
|
||||||
return $this->data;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Email that gets sent to submitter when a submission is made.
|
* Email that gets sent to submitter when a submission is made.
|
||||||
* @package cms
|
*
|
||||||
* @subpackage pagetypes
|
* @package userforms
|
||||||
*/
|
*/
|
||||||
class UserDefinedForm_SubmittedFormEmailToSubmitter extends Email {
|
class UserDefinedForm_SubmittedFormEmailToSubmitter extends Email {
|
||||||
protected $ss_template = "SubmittedFormEmailToSubmitter";
|
protected $ss_template = "SubmittedFormEmailToSubmitter";
|
||||||
|
@ -200,25 +200,18 @@ class FieldEditor extends FormField {
|
|||||||
|
|
||||||
function FormOptions() {
|
function FormOptions() {
|
||||||
if($this->haveFormOptions){
|
if($this->haveFormOptions){
|
||||||
$fields = new FieldSet(
|
|
||||||
new EmailField( "{$this->name}[EmailTo]", _t('FieldEditor.EMAILSUBMISSION', 'Email submission to:'), $this->form->getRecord()->EmailTo ),
|
|
||||||
new CheckboxField( "{$this->name}[EmailOnSubmit]", _t('FieldEditor.EMAILONSUBMIT', 'Email form on submit:'), $this->form->getRecord()->EmailOnSubmit )
|
|
||||||
);
|
|
||||||
|
|
||||||
if($this->form->getRecord()->hasMethod('customFormActions')) {
|
if($this->form->getRecord()->hasMethod('customFormActions')) {
|
||||||
$newFields = $this->form->getRecord()->customFormActions($this->readonly);
|
$newFields = $this->form->getRecord()->customFormActions($this->readonly);
|
||||||
|
|
||||||
foreach( $newFields as $newField ) {
|
foreach( $newFields as $newField ) {
|
||||||
$newField->setName( "{$this->name}[{$newField->Name()}]" );
|
$newField->setName( "{$this->name}[{$newField->Name()}]" );
|
||||||
$fields->push( $newField );
|
}
|
||||||
|
if($this->readonly) {
|
||||||
|
$newFields = $newFields->makeReadonly();
|
||||||
|
}
|
||||||
|
return $newFields;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( $this->readonly )
|
|
||||||
$fields = $fields->makeReadonly();
|
|
||||||
|
|
||||||
return $fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -3,10 +3,6 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>$Subject</h1>
|
<h1>$Subject</h1>
|
||||||
<p>
|
|
||||||
<% _t('SUBMITTED','The following data was submitted to your website:') %>
|
|
||||||
|
|
||||||
</p>
|
|
||||||
$Body
|
$Body
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
|
Loading…
Reference in New Issue
Block a user