1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* A Form can have multiply members / emails to email the submission |
6
|
|
|
* to and custom subjects |
7
|
|
|
* |
8
|
|
|
* @package userforms |
9
|
|
|
*/ |
10
|
|
|
class UserDefinedForm_EmailRecipient extends DataObject |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private static $db = array( |
|
|
|
|
14
|
|
|
'EmailAddress' => 'Varchar(200)', |
15
|
|
|
'EmailSubject' => 'Varchar(200)', |
16
|
|
|
'EmailFrom' => 'Varchar(200)', |
17
|
|
|
'EmailReplyTo' => 'Varchar(200)', |
18
|
|
|
'EmailBody' => 'Text', |
19
|
|
|
'EmailBodyHtml' => 'HTMLText', |
20
|
|
|
'EmailTemplate' => 'Varchar', |
21
|
|
|
'SendPlain' => 'Boolean', |
22
|
|
|
'HideFormData' => 'Boolean', |
23
|
|
|
'CustomRulesCondition' => 'Enum("And,Or")' |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
private static $has_one = array( |
|
|
|
|
27
|
|
|
'Form' => 'UserDefinedForm', |
28
|
|
|
'SendEmailFromField' => 'EditableFormField', |
29
|
|
|
'SendEmailToField' => 'EditableFormField', |
30
|
|
|
'SendEmailSubjectField' => 'EditableFormField' |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
private static $has_many = array( |
|
|
|
|
34
|
|
|
'CustomRules' => 'UserDefinedForm_EmailRecipientCondition' |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
private static $summary_fields = array( |
|
|
|
|
38
|
|
|
'EmailAddress', |
39
|
|
|
'EmailSubject', |
40
|
|
|
'EmailFrom' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Setting this to true will allow you to select "risky" fields as |
45
|
|
|
* email recipient, such as free-text entry fields. |
46
|
|
|
* |
47
|
|
|
* It's advisable to leave this off. |
48
|
|
|
* |
49
|
|
|
* @config |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
private static $allow_unbound_recipient_fields = false; |
|
|
|
|
53
|
|
|
|
54
|
|
|
public function summaryFields() |
55
|
|
|
{ |
56
|
|
|
$fields = parent::summaryFields(); |
57
|
|
|
if (isset($fields['EmailAddress'])) { |
58
|
|
|
$fields['EmailAddress'] = _t('UserDefinedForm.EMAILADDRESS', 'Email'); |
59
|
|
|
} |
60
|
|
|
if (isset($fields['EmailSubject'])) { |
61
|
|
|
$fields['EmailSubject'] = _t('UserDefinedForm.EMAILSUBJECT', 'Subject'); |
62
|
|
|
} |
63
|
|
|
if (isset($fields['EmailFrom'])) { |
64
|
|
|
$fields['EmailFrom'] = _t('UserDefinedForm.EMAILFROM', 'From'); |
65
|
|
|
} |
66
|
|
|
return $fields; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get instance of UserDefinedForm when editing in getCMSFields |
71
|
|
|
* |
72
|
|
|
* @return UserDefinedFrom |
73
|
|
|
*/ |
74
|
1 |
|
protected function getFormParent() |
75
|
|
|
{ |
76
|
1 |
|
$formID = $this->FormID |
|
|
|
|
77
|
1 |
|
? $this->FormID |
|
|
|
|
78
|
1 |
|
: Session::get('CMSMain.currentPage'); |
79
|
1 |
|
return UserDefinedForm::get()->byID($formID); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getTitle() |
83
|
|
|
{ |
84
|
|
|
if ($this->EmailAddress) { |
|
|
|
|
85
|
|
|
return $this->EmailAddress; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
if ($this->EmailSubject) { |
|
|
|
|
88
|
|
|
return $this->EmailSubject; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
return parent::getTitle(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Generate a gridfield config for editing filter rules |
95
|
|
|
* |
96
|
|
|
* @return GridFieldConfig |
97
|
|
|
*/ |
98
|
1 |
|
protected function getRulesConfig() |
99
|
|
|
{ |
100
|
1 |
|
$formFields = $this->getFormParent()->Fields(); |
101
|
|
|
|
102
|
1 |
|
$config = GridFieldConfig::create() |
103
|
1 |
|
->addComponents( |
104
|
1 |
|
new GridFieldButtonRow('before'), |
105
|
1 |
|
new GridFieldToolbarHeader(), |
106
|
1 |
|
new GridFieldAddNewInlineButton(), |
107
|
1 |
|
new GridFieldDeleteAction(), |
108
|
1 |
|
$columns = new GridFieldEditableColumns() |
109
|
|
|
); |
110
|
|
|
|
111
|
1 |
|
$columns->setDisplayFields(array( |
112
|
|
|
'ConditionFieldID' => function ($record, $column, $grid) use ($formFields) { |
|
|
|
|
113
|
|
|
return DropdownField::create($column, false, $formFields->map('ID', 'Title')); |
114
|
1 |
|
}, |
115
|
1 |
|
'ConditionOption' => function ($record, $column, $grid) { |
|
|
|
|
116
|
|
|
$options = UserDefinedForm_EmailRecipientCondition::config()->condition_options; |
117
|
|
|
return DropdownField::create($column, false, $options); |
118
|
1 |
|
}, |
119
|
1 |
|
'ConditionValue' => function ($record, $column, $grid) { |
|
|
|
|
120
|
|
|
return TextField::create($column); |
121
|
1 |
|
} |
122
|
|
|
)); |
123
|
|
|
|
124
|
1 |
|
return $config; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return FieldList |
129
|
|
|
*/ |
130
|
1 |
|
public function getCMSFields() |
131
|
|
|
{ |
132
|
1 |
|
Requirements::javascript(USERFORMS_DIR . '/javascript/Recipient.js'); |
133
|
|
|
|
134
|
|
|
// Determine optional field values |
135
|
1 |
|
$form = $this->getFormParent(); |
136
|
|
|
|
137
|
|
|
// predefined choices are also candidates |
138
|
1 |
|
$multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', $form->ID); |
139
|
|
|
|
140
|
|
|
// if they have email fields then we could send from it |
141
|
1 |
|
$validEmailFromFields = EditableEmailField::get()->filter('ParentID', $form->ID); |
142
|
|
|
|
143
|
|
|
// For the subject, only one-line entry boxes make sense |
144
|
1 |
|
$validSubjectFields = ArrayList::create( |
145
|
1 |
|
EditableTextField::get() |
146
|
1 |
|
->filter('ParentID', $form->ID) |
147
|
1 |
|
->exclude('Rows:GreaterThan', 1) |
148
|
1 |
|
->toArray() |
149
|
|
|
); |
150
|
1 |
|
$validSubjectFields->merge($multiOptionFields); |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
// Check valid email-recipient fields |
154
|
1 |
|
if ($this->config()->allow_unbound_recipient_fields) { |
155
|
|
|
// To address can only be email fields or multi option fields |
156
|
|
|
$validEmailToFields = ArrayList::create($validEmailFromFields->toArray()); |
157
|
|
|
$validEmailToFields->merge($multiOptionFields); |
158
|
|
|
} else { |
159
|
|
|
// To address cannot be unbound, so restrict to pre-defined lists |
160
|
1 |
|
$validEmailToFields = $multiOptionFields; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Build fieldlist |
164
|
1 |
|
$fields = FieldList::create(Tabset::create('Root')->addExtraClass('EmailRecipientForm')); |
165
|
|
|
|
166
|
|
|
// Configuration fields |
167
|
1 |
|
$fields->addFieldsToTab('Root.EmailDetails', array( |
168
|
|
|
// Subject |
169
|
1 |
|
FieldGroup::create( |
170
|
1 |
|
TextField::create('EmailSubject', _t('UserDefinedForm.TYPESUBJECT', 'Type subject')) |
171
|
1 |
|
->setAttribute('style', 'min-width: 400px;'), |
172
|
1 |
|
DropdownField::create( |
173
|
1 |
|
'SendEmailSubjectFieldID', |
174
|
1 |
|
_t('UserDefinedForm.SELECTAFIELDTOSETSUBJECT', '.. or select a field to use as the subject'), |
175
|
1 |
|
$validSubjectFields->map('ID', 'Title') |
176
|
1 |
|
)->setEmptyString('') |
177
|
|
|
) |
178
|
1 |
|
->setTitle(_t('UserDefinedForm.EMAILSUBJECT', 'Email subject')), |
179
|
|
|
|
180
|
|
|
// To |
181
|
1 |
|
FieldGroup::create( |
182
|
1 |
|
TextField::create('EmailAddress', _t('UserDefinedForm.TYPETO', 'Type to address')) |
183
|
1 |
|
->setAttribute('style', 'min-width: 400px;'), |
184
|
1 |
|
DropdownField::create( |
185
|
1 |
|
'SendEmailToFieldID', |
186
|
1 |
|
_t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'), |
187
|
1 |
|
$validEmailToFields->map('ID', 'Title') |
188
|
1 |
|
)->setEmptyString(' ') |
189
|
|
|
) |
190
|
1 |
|
->setTitle(_t('UserDefinedForm.SENDEMAILTO', 'Send email to')) |
191
|
1 |
|
->setDescription(_t( |
192
|
1 |
|
'UserDefinedForm.SENDEMAILTO_DESCRIPTION', |
193
|
1 |
|
'You may enter multiple email addresses as a comma separated list.' |
194
|
|
|
)), |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
// From |
198
|
1 |
|
TextField::create('EmailFrom', _t('UserDefinedForm.FROMADDRESS', 'Send email from')) |
199
|
1 |
|
->setDescription(_t( |
200
|
1 |
|
'UserDefinedForm.EmailFromContent', |
201
|
|
|
"The from address allows you to set who the email comes from. On most servers this ". |
202
|
|
|
"will need to be set to an email address on the same domain name as your site. ". |
203
|
|
|
"For example on yoursite.com the from address may need to be [email protected]. ". |
204
|
1 |
|
"You can however, set any email address you wish as the reply to address." |
205
|
|
|
)), |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
// Reply-To |
209
|
1 |
|
FieldGroup::create( |
210
|
1 |
|
TextField::create('EmailReplyTo', _t('UserDefinedForm.TYPEREPLY', 'Type reply address')) |
211
|
1 |
|
->setAttribute('style', 'min-width: 400px;'), |
212
|
1 |
|
DropdownField::create( |
213
|
1 |
|
'SendEmailFromFieldID', |
214
|
1 |
|
_t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'), |
215
|
1 |
|
$validEmailFromFields->map('ID', 'Title') |
216
|
1 |
|
)->setEmptyString(' ') |
217
|
|
|
) |
218
|
1 |
|
->setTitle(_t('UserDefinedForm.REPLYADDRESS', 'Email for reply to')) |
219
|
1 |
|
->setDescription(_t( |
220
|
1 |
|
'UserDefinedForm.REPLYADDRESS_DESCRIPTION', |
221
|
1 |
|
'The email address which the recipient is able to \'reply\' to.' |
222
|
|
|
)) |
223
|
|
|
)); |
224
|
|
|
|
225
|
1 |
|
$fields->fieldByName('Root.EmailDetails')->setTitle(_t('UserDefinedForm_EmailRecipient.EMAILDETAILSTAB', 'Email Details')); |
226
|
|
|
|
227
|
|
|
// Only show the preview link if the recipient has been saved. |
228
|
1 |
|
if (!empty($this->EmailTemplate)) { |
|
|
|
|
229
|
|
|
$preview = sprintf( |
230
|
|
|
'<p><a href="%s" target="_blank" class="ss-ui-button">%s</a></p><em>%s</em>', |
231
|
|
|
Controller::join_links( |
232
|
|
|
singleton('CMSPageEditController')->getEditForm()->FormAction(), |
233
|
|
|
"field/EmailRecipients/item/{$this->ID}/preview" |
234
|
|
|
), |
235
|
|
|
_t('UserDefinedForm.PREVIEW_EMAIL', 'Preview email'), |
236
|
|
|
_t('UserDefinedForm.PREVIEW_EMAIL_DESCRIPTION', 'Note: Unsaved changes will not appear in the preview.') |
237
|
|
|
); |
238
|
|
|
} else { |
239
|
1 |
|
$preview = sprintf( |
240
|
1 |
|
'<em>%s</em>', |
241
|
1 |
|
_t( |
242
|
1 |
|
'UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE', |
243
|
1 |
|
'You can preview this email once you have saved the Recipient.' |
244
|
|
|
) |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// Email templates |
249
|
1 |
|
$fields->addFieldsToTab('Root.EmailContent', array( |
250
|
1 |
|
CheckboxField::create('HideFormData', _t('UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?')), |
251
|
1 |
|
CheckboxField::create( |
252
|
1 |
|
'SendPlain', |
253
|
1 |
|
_t('UserDefinedForm.SENDPLAIN', 'Send email as plain text? (HTML will be stripped)') |
254
|
|
|
), |
255
|
1 |
|
DropdownField::create( |
256
|
1 |
|
'EmailTemplate', |
257
|
1 |
|
_t('UserDefinedForm.EMAILTEMPLATE', 'Email template'), |
258
|
1 |
|
$this->getEmailTemplateDropdownValues() |
259
|
1 |
|
)->addExtraClass('toggle-html-only'), |
260
|
1 |
|
HTMLEditorField::create('EmailBodyHtml', _t('UserDefinedForm.EMAILBODYHTML', 'Body')) |
261
|
1 |
|
->addExtraClass('toggle-html-only'), |
262
|
1 |
|
TextareaField::create('EmailBody', _t('UserDefinedForm.EMAILBODY', 'Body')) |
263
|
1 |
|
->addExtraClass('toggle-plain-only'), |
264
|
1 |
|
LiteralField::create( |
265
|
1 |
|
'EmailPreview', |
266
|
1 |
|
'<div id="EmailPreview" class="field toggle-html-only">' . $preview . '</div>' |
267
|
|
|
) |
268
|
|
|
)); |
269
|
|
|
|
270
|
1 |
|
$fields->fieldByName('Root.EmailContent')->setTitle(_t('UserDefinedForm_EmailRecipient.EMAILCONTENTTAB', 'Email Content')); |
271
|
|
|
|
272
|
|
|
// Custom rules for sending this field |
273
|
1 |
|
$grid = new GridField( |
274
|
1 |
|
"CustomRules", |
275
|
1 |
|
_t('EditableFormField.CUSTOMRULES', 'Custom Rules'), |
276
|
1 |
|
$this->CustomRules(), |
|
|
|
|
277
|
1 |
|
$this->getRulesConfig() |
278
|
|
|
); |
279
|
1 |
|
$grid->setDescription(_t( |
280
|
1 |
|
'UserDefinedForm.RulesDescription', |
281
|
1 |
|
'Emails will only be sent to the recipient if the custom rules are met. If no rules are defined, this receipient will receive notifications for every submission.' |
282
|
|
|
)); |
283
|
1 |
|
$fields->addFieldsToTab('Root.CustomRules', array( |
284
|
1 |
|
new DropdownField( |
285
|
1 |
|
'CustomRulesCondition', |
286
|
1 |
|
_t('UserDefinedForm.SENDIF', 'Send condition'), |
287
|
|
|
array( |
288
|
1 |
|
'Or' => _t('UserDefinedForm.SENDIFOR', 'Any conditions are true'), |
289
|
1 |
|
'And' => _t('UserDefinedForm.SENDIFAND', 'All conditions are true') |
290
|
|
|
) |
291
|
|
|
), |
292
|
1 |
|
$grid |
293
|
|
|
)); |
294
|
|
|
|
295
|
1 |
|
$fields->fieldByName('Root.CustomRules')->setTitle(_t('UserDefinedForm_EmailRecipient.CUSTOMRULESTAB', 'Custom Rules')); |
296
|
|
|
|
297
|
1 |
|
$this->extend('updateCMSFields', $fields); |
298
|
1 |
|
return $fields; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Return whether a user can create an object of this type |
303
|
|
|
* |
304
|
|
|
* @param Member $member |
305
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check |
|
|
|
|
306
|
|
|
* @return bool |
307
|
|
|
*/ |
308
|
|
View Code Duplication |
public function canCreate($member = null) |
|
|
|
|
309
|
|
|
{ |
310
|
|
|
// Check parent page |
311
|
|
|
$parent = $this->getCanCreateContext(func_get_args()); |
312
|
|
|
if ($parent) { |
313
|
|
|
return $parent->canEdit($member); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
// Fall back to secure admin permissions |
317
|
|
|
return parent::canCreate($member); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Helper method to check the parent for this object |
322
|
|
|
* |
323
|
|
|
* @param array $args List of arguments passed to canCreate |
324
|
|
|
* @return SiteTree Parent page instance |
325
|
|
|
*/ |
326
|
|
View Code Duplication |
protected function getCanCreateContext($args) |
|
|
|
|
327
|
|
|
{ |
328
|
|
|
// Inspect second parameter to canCreate for a 'Parent' context |
329
|
|
|
if (isset($args[1]['Form'])) { |
330
|
|
|
return $args[1]['Form']; |
331
|
|
|
} |
332
|
|
|
// Hack in currently edited page if context is missing |
333
|
|
|
if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
334
|
|
|
return Controller::curr()->currentPage(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
// No page being edited |
338
|
|
|
return null; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param Member |
343
|
|
|
* |
344
|
|
|
* @return boolean |
345
|
|
|
*/ |
346
|
|
|
public function canView($member = null) |
347
|
|
|
{ |
348
|
|
|
return $this->Form()->canView($member); |
|
|
|
|
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param Member |
353
|
|
|
* |
354
|
|
|
* @return boolean |
355
|
|
|
*/ |
356
|
1 |
|
public function canEdit($member = null) |
357
|
|
|
{ |
358
|
1 |
|
return $this->Form()->canEdit($member); |
|
|
|
|
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @param Member |
363
|
|
|
* |
364
|
|
|
* @return boolean |
365
|
|
|
*/ |
366
|
1 |
|
public function canDelete($member = null) |
367
|
|
|
{ |
368
|
1 |
|
return $this->canEdit($member); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/* |
372
|
|
|
* Determine if this recipient may receive notifications for this submission |
373
|
|
|
* |
374
|
|
|
* @param array $data |
375
|
|
|
* @param Form $form |
376
|
|
|
* @return bool |
377
|
|
|
*/ |
378
|
2 |
|
public function canSend($data, $form) |
|
|
|
|
379
|
|
|
{ |
380
|
|
|
// Skip if no rules configured |
381
|
2 |
|
$customRules = $this->CustomRules(); |
|
|
|
|
382
|
2 |
|
if (!$customRules->count()) { |
383
|
2 |
|
return true; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
// Check all rules |
387
|
1 |
|
$isAnd = $this->CustomRulesCondition === 'And'; |
|
|
|
|
388
|
1 |
|
foreach ($customRules as $customRule) { |
389
|
|
|
/** @var UserDefinedForm_EmailRecipientCondition $customRule */ |
390
|
1 |
|
$matches = $customRule->matches($data); |
391
|
1 |
|
if ($isAnd && !$matches) { |
|
|
|
|
392
|
1 |
|
return false; |
393
|
|
|
} |
394
|
1 |
|
if (!$isAnd && $matches) { |
395
|
1 |
|
return true; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
// Once all rules are checked |
400
|
1 |
|
return $isAnd; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Make sure the email template saved against the recipient exists on the file system. |
405
|
|
|
* |
406
|
|
|
* @param string |
407
|
|
|
* |
408
|
|
|
* @return boolean |
409
|
|
|
*/ |
410
|
2 |
|
public function emailTemplateExists($template = '') |
411
|
|
|
{ |
412
|
2 |
|
$t = ($template ? $template : $this->EmailTemplate); |
|
|
|
|
413
|
|
|
|
414
|
2 |
|
return in_array($t, $this->getEmailTemplateDropdownValues()); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Get the email body for the current email format |
419
|
|
|
* |
420
|
|
|
* @return string |
421
|
|
|
*/ |
422
|
2 |
|
public function getEmailBodyContent() |
423
|
|
|
{ |
424
|
2 |
|
return $this->SendPlain ? $this->EmailBody : $this->EmailBodyHtml; |
|
|
|
|
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Gets a list of email templates suitable for populating the email template dropdown. |
429
|
|
|
* |
430
|
|
|
* @return array |
431
|
|
|
*/ |
432
|
4 |
|
public function getEmailTemplateDropdownValues() |
433
|
|
|
{ |
434
|
4 |
|
$templates = array(); |
435
|
|
|
|
436
|
4 |
|
$finder = new SS_FileFinder(); |
437
|
4 |
|
$finder->setOption('name_regex', '/^.*\.ss$/'); |
438
|
|
|
|
439
|
4 |
|
$found = $finder->find(BASE_PATH . '/' . UserDefinedForm::config()->email_template_directory); |
440
|
|
|
|
441
|
4 |
|
foreach ($found as $key => $value) { |
442
|
4 |
|
$template = pathinfo($value); |
443
|
|
|
|
444
|
4 |
|
$templates[$template['filename']] = $template['filename']; |
445
|
|
|
} |
446
|
|
|
|
447
|
4 |
|
return $templates; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.