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