Skip to content

Commit

Permalink
ENH Add config to hide invisible fields from email output
Browse files Browse the repository at this point in the history
  • Loading branch information
xini committed Mar 15, 2022
1 parent 60cd3d0 commit 33e9a25
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
9 changes: 9 additions & 0 deletions code/Control/UserDefinedFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ public function process($data, $form)
}
}

// set visibility flag according to display rules
$submittedField->Displayed = $field->isDisplayed($data);

if (!empty($data[$field->Name])) {
if (in_array(EditableFileField::class, $field->getClassAncestry())) {
if (!empty($_FILES[$field->Name]['name'])) {
Expand Down Expand Up @@ -305,6 +308,8 @@ public function process($data, $form)
$submittedFields->push($submittedField);
}

$visibleSubmittedFields = $submittedFields->filter('Displayed', true);

$emailData = [
'Sender' => Security::getCurrentUser(),
'HideFormData' => false,
Expand Down Expand Up @@ -350,6 +355,10 @@ public function process($data, $form)
// This string substitution works for both HTML and plain text emails.
// $recipient->getEmailBodyContent() will retrieve the relevant version of the email
$emailData['Body'] = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields);
// only include visible fields if recipient visibility flag is set
if ((bool) $recipient->HideInvisibleFields) {
$emailData['Fields'] = $visibleSubmittedFields;
}

// Push the template data to the Email's data
foreach ($emailData as $key => $value) {
Expand Down
5 changes: 5 additions & 0 deletions code/Model/Recipient/EmailRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class EmailRecipient extends DataObject
'EmailTemplate' => 'Varchar',
'SendPlain' => 'Boolean',
'HideFormData' => 'Boolean',
'HideInvisibleFields' => 'Boolean',
'CustomRulesCondition' => 'Enum("And,Or")'
];

Expand Down Expand Up @@ -303,6 +304,10 @@ public function getCMSFields()
'HideFormData',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?')
),
CheckboxField::create(
'HideInvisibleFields',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.HIDEINVISIBLEFIELDS', 'Hide invisible fields from email?')
),
CheckboxField::create(
'SendPlain',
_t(
Expand Down
3 changes: 2 additions & 1 deletion code/Model/Submission/SubmittedFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class SubmittedFormField extends DataObject
private static $db = [
'Name' => 'Varchar',
'Value' => 'Text',
'Title' => 'Varchar(255)'
'Title' => 'Varchar(255)',
'Displayed' => 'Boolean',
];

private static $has_one = [
Expand Down
4 changes: 4 additions & 0 deletions docs/en/userguide/form-submissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ In this field you can add a custom message to add to the email

You can check this if you do not wish for the email recipient to see the form submission's data in the email.

#### Hide invisible fields from email?

You can check this if you want to hide the fields from the email that were invisible to the user according to the display rules set up for the form fields.

#### Send email as plain text?

You can check this if you want to remove all of the HTML from the email, this means the email
Expand Down
1 change: 1 addition & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ en:
EmailFromContent: 'The from address allows you to set who the email comes from. On most servers this will need to be set to an email address on the same domain name as your site. For example on yoursite.com the from address may need to be [email protected]. You can however, set any email address you wish as the reply to address.'
FROMADDRESS: 'Send email from'
HIDEFORMDATA: 'Hide form data from email?'
HIDEINVISIBLEFIELDS: 'Hide invisible fields from email?'
ORSELECTAFIELDTOUSEASFROM: '.. or select a field to use as reply to address'
ORSELECTAFIELDTOUSEASTO: '.. or select a field to use as the to address'
PLURALNAME: 'Base Pages'
Expand Down
10 changes: 5 additions & 5 deletions templates/email/SubmittedFormEmailPlain.ss
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
$Body.RAW

<% if not $HideFormData %>
*
<% loop $Fields %>
* <% if $Title %>$Title<% else %>$Name<% end_if %>
* $FormattedValue
<% end_loop %>
*
<% loop $Fields %>
* <% if $Title %>$Title<% else %>$Name<% end_if %>
* $FormattedValue
<% end_loop %>
<% end_if %>

0 comments on commit 33e9a25

Please sign in to comment.