1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms; |
4
|
|
|
|
5
|
|
|
use Colymba\BulkManager\BulkManager; |
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
7
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader; |
8
|
|
|
use SilverStripe\Forms\CheckboxField; |
9
|
|
|
use SilverStripe\Forms\CompositeField; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\GridField\GridField; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldButtonRow; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
15
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
17
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
18
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
19
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
20
|
|
|
use SilverStripe\Forms\GridField\GridFieldExportButton; |
21
|
|
|
use SilverStripe\Forms\GridField\GridFieldPageCount; |
22
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator; |
23
|
|
|
use SilverStripe\Forms\GridField\GridFieldPrintButton; |
24
|
|
|
use SilverStripe\Forms\GridField\GridFieldSortableHeader; |
25
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader; |
26
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField; |
27
|
|
|
use SilverStripe\Forms\LabelField; |
28
|
|
|
use SilverStripe\Forms\LiteralField; |
29
|
|
|
use SilverStripe\Forms\TextField; |
30
|
|
|
use SilverStripe\ORM\ArrayList; |
31
|
|
|
use SilverStripe\ORM\DB; |
32
|
|
|
use SilverStripe\UserForms\Extension\UserFormFieldEditorExtension; |
33
|
|
|
use SilverStripe\UserForms\Extension\UserFormValidator; |
34
|
|
|
use SilverStripe\UserForms\Form\UserFormsGridFieldFilterHeader; |
35
|
|
|
use SilverStripe\UserForms\Model\Recipient\EmailRecipient; |
36
|
|
|
use SilverStripe\UserForms\Model\Recipient\UserFormRecipientItemRequest; |
37
|
|
|
use SilverStripe\UserForms\Model\Submission\SubmittedForm; |
38
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
39
|
|
|
use SilverStripe\View\Requirements; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Defines the user defined functionality to be applied to any {@link DataObject} |
43
|
|
|
* |
44
|
|
|
*/ |
45
|
|
|
trait UserForm |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* Built in extensions required by this page. |
49
|
|
|
* |
50
|
|
|
* @config |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private static $extensions = [ |
54
|
|
|
UserFormFieldEditorExtension::class |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string Required Identifier |
59
|
|
|
*/ |
60
|
|
|
private static $required_identifier = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private static $email_template_directory = 'userforms/templates/email/'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Should this module automatically upgrade on dev/build? |
69
|
|
|
* |
70
|
|
|
* @config |
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
private static $upgrade_on_build = true; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set this to true to disable automatic inclusion of CSS files |
77
|
|
|
* @config |
78
|
|
|
* @var bool |
79
|
|
|
*/ |
80
|
|
|
private static $block_default_userforms_css = false; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set this to true to disable automatic inclusion of JavaScript files |
84
|
|
|
* @config |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
private static $block_default_userforms_js = false; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var array Fields on the user defined form page. |
91
|
|
|
*/ |
92
|
|
|
private static $db = [ |
93
|
|
|
'SubmitButtonText' => 'Varchar', |
94
|
|
|
'ClearButtonText' => 'Varchar', |
95
|
|
|
'OnCompleteMessage' => 'HTMLText', |
96
|
|
|
'ShowClearButton' => 'Boolean', |
97
|
|
|
'DisableSaveSubmissions' => 'Boolean', |
98
|
|
|
'EnableLiveValidation' => 'Boolean', |
99
|
|
|
'DisplayErrorMessagesAtTop' => 'Boolean', |
100
|
|
|
'DisableAuthenicatedFinishAction' => 'Boolean', |
101
|
|
|
'DisableCsrfSecurityToken' => 'Boolean' |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var array Default values of variables when this page is created |
106
|
|
|
*/ |
107
|
|
|
private static $defaults = [ |
108
|
|
|
'Content' => '$UserDefinedForm', |
109
|
|
|
'DisableSaveSubmissions' => 0, |
110
|
|
|
'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>' |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @var array |
115
|
|
|
*/ |
116
|
|
|
private static $has_many = [ |
117
|
|
|
'Submissions' => SubmittedForm::class, |
118
|
|
|
'EmailRecipients' => EmailRecipient::class |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
private static $cascade_deletes = [ |
122
|
|
|
'EmailRecipients', |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @var array |
127
|
|
|
* @config |
128
|
|
|
*/ |
129
|
|
|
private static $casting = [ |
130
|
|
|
'ErrorContainerID' => 'Text' |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Error container selector which matches the element for grouped messages |
135
|
|
|
* |
136
|
|
|
* @var string |
137
|
|
|
* @config |
138
|
|
|
*/ |
139
|
|
|
private static $error_container_id = 'error-container'; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* The configuration used to determine whether a confirmation message is to |
143
|
|
|
* appear when navigating away from a partially completed form. |
144
|
|
|
* |
145
|
|
|
* @var boolean |
146
|
|
|
* @config |
147
|
|
|
*/ |
148
|
|
|
private static $enable_are_you_sure = true; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @var bool |
152
|
|
|
* @config |
153
|
|
|
*/ |
154
|
|
|
private static $recipients_warning_enabled = false; |
155
|
|
|
|
156
|
|
|
private static $non_live_permissions = ['SITETREE_VIEW_ALL']; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Temporary storage of field ids when the form is duplicated. |
160
|
|
|
* Example layout: array('EditableCheckbox3' => 'EditableCheckbox14') |
161
|
|
|
* @var array |
162
|
|
|
*/ |
163
|
|
|
protected $fieldsFromTo = []; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return FieldList |
167
|
|
|
*/ |
168
|
|
|
public function getCMSFields() |
169
|
|
|
{ |
170
|
|
|
Requirements::css( |
171
|
|
|
ModuleLoader::getModule('silverstripe/userforms') |
172
|
|
|
->getRelativeResourcePath('client/dist/styles/userforms-cms.css') |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
|
|
|
|
176
|
|
|
|
177
|
|
|
// remove |
178
|
|
|
$fields->removeByName('OnCompleteMessageLabel'); |
179
|
|
|
$fields->removeByName('OnCompleteMessage'); |
180
|
|
|
$fields->removeByName('Fields'); |
181
|
|
|
$fields->removeByName('EmailRecipients'); |
182
|
|
|
|
183
|
|
|
// define tabs |
184
|
|
|
$fields->findOrMakeTab('Root.FormOptions', _t(__CLASS__.'.CONFIGURATION', 'Configuration')); |
185
|
|
|
$fields->findOrMakeTab('Root.Recipients', _t(__CLASS__.'.RECIPIENTS', 'Recipients')); |
186
|
|
|
$fields->findOrMakeTab('Root.Submissions', _t(__CLASS__.'.SUBMISSIONS', 'Submissions')); |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
// text to show on complete |
190
|
|
|
$onCompleteFieldSet = CompositeField::create( |
191
|
|
|
$label = LabelField::create( |
192
|
|
|
'OnCompleteMessageLabel', |
193
|
|
|
_t(__CLASS__.'.ONCOMPLETELABEL', 'Show on completion') |
194
|
|
|
), |
195
|
|
|
$editor = HTMLEditorField::create( |
196
|
|
|
'OnCompleteMessage', |
197
|
|
|
'', |
198
|
|
|
_t(__CLASS__.'.ONCOMPLETEMESSAGE', $this->OnCompleteMessage) |
|
|
|
|
199
|
|
|
) |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
$onCompleteFieldSet->addExtraClass('field'); |
203
|
|
|
|
204
|
|
|
$editor->setRows(3); |
205
|
|
|
$label->addExtraClass('left'); |
206
|
|
|
|
207
|
|
|
// Define config for email recipients |
208
|
|
|
$emailRecipientsConfig = GridFieldConfig_RecordEditor::create(10); |
209
|
|
|
$emailRecipientsConfig->getComponentByType(GridFieldAddNewButton::class) |
|
|
|
|
210
|
|
|
->setButtonName( |
211
|
|
|
_t(__CLASS__.'.ADDEMAILRECIPIENT', 'Add Email Recipient') |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
// who do we email on submission |
215
|
|
|
$emailRecipients = GridField::create( |
216
|
|
|
'EmailRecipients', |
217
|
|
|
'', |
218
|
|
|
$this->EmailRecipients(), |
|
|
|
|
219
|
|
|
$emailRecipientsConfig |
220
|
|
|
); |
221
|
|
|
$emailRecipients |
|
|
|
|
222
|
|
|
->getConfig() |
223
|
|
|
->getComponentByType(GridFieldDetailForm::class) |
224
|
|
|
->setItemRequestClass(UserFormRecipientItemRequest::class); |
225
|
|
|
|
226
|
|
|
$fields->addFieldsToTab('Root.FormOptions', $onCompleteFieldSet); |
227
|
|
|
$fields->addFieldToTab('Root.Recipients', $emailRecipients); |
228
|
|
|
$fields->addFieldsToTab('Root.FormOptions', $this->getFormOptions()); |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
// view the submissions |
232
|
|
|
// make sure a numeric not a empty string is checked against this int column for SQL server |
233
|
|
|
$parentID = (!empty($this->ID)) ? (int) $this->ID : 0; |
|
|
|
|
234
|
|
|
|
235
|
|
|
// get a list of all field names and values used for print and export CSV views of the GridField below. |
236
|
|
|
$columnSQL = <<<SQL |
237
|
|
|
SELECT "SubmittedFormField"."Name" as "Name", COALESCE("EditableFormField"."Title", "SubmittedFormField"."Title") as "Title", COALESCE("EditableFormField"."Sort", 999) AS "Sort" |
238
|
|
|
FROM "SubmittedFormField" |
239
|
|
|
LEFT JOIN "SubmittedForm" ON "SubmittedForm"."ID" = "SubmittedFormField"."ParentID" |
240
|
|
|
LEFT JOIN "EditableFormField" ON "EditableFormField"."Name" = "SubmittedFormField"."Name" AND "EditableFormField"."ParentID" = '$parentID' |
241
|
|
|
WHERE "SubmittedForm"."ParentID" = '$parentID' |
242
|
|
|
ORDER BY "Sort", "Title" |
243
|
|
|
SQL; |
244
|
|
|
// Sanitise periods in title |
245
|
|
|
$columns = array(); |
246
|
|
|
|
247
|
|
|
foreach (DB::query($columnSQL)->map() as $name => $title) { |
248
|
|
|
$columns[$name] = trim(strtr($title, '.', ' ')); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$config = GridFieldConfig::create(); |
252
|
|
|
$config->addComponent(new GridFieldToolbarHeader()); |
253
|
|
|
$config->addComponent($sort = new GridFieldSortableHeader()); |
254
|
|
|
$config->addComponent($filter = new UserFormsGridFieldFilterHeader()); |
255
|
|
|
$config->addComponent(new GridFieldDataColumns()); |
256
|
|
|
$config->addComponent(new GridFieldEditButton()); |
257
|
|
|
$config->addComponent(new GridFieldDeleteAction()); |
258
|
|
|
$config->addComponent(new GridFieldPageCount('toolbar-header-right')); |
259
|
|
|
$config->addComponent($pagination = new GridFieldPaginator(25)); |
260
|
|
|
$config->addComponent(new GridFieldDetailForm()); |
261
|
|
|
$config->addComponent(new GridFieldButtonRow('after')); |
262
|
|
|
$config->addComponent($export = new GridFieldExportButton('buttons-after-left')); |
263
|
|
|
$config->addComponent($print = new GridFieldPrintButton('buttons-after-left')); |
264
|
|
|
|
265
|
|
|
// show user form items in the summary tab |
266
|
|
|
$summaryarray = array( |
267
|
|
|
'ID' => 'ID', |
268
|
|
|
'Created' => 'Created', |
269
|
|
|
'LastEdited' => 'Last Edited' |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
foreach (EditableFormField::get()->filter(array('ParentID' => $parentID)) as $eff) { |
273
|
|
|
if ($eff->ShowInSummary) { |
274
|
|
|
$summaryarray[$eff->Name] = $eff->Title ?: $eff->Name; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$config->getComponentByType(GridFieldDataColumns::class)->setDisplayFields($summaryarray); |
|
|
|
|
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Support for {@link https://github.com/colymba/GridFieldBulkEditingTools} |
282
|
|
|
*/ |
283
|
|
|
if (class_exists(BulkManager::class)) { |
284
|
|
|
$config->addComponent(new BulkManager); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$sort->setThrowExceptionOnBadDataType(false); |
288
|
|
|
$filter->setThrowExceptionOnBadDataType(false); |
289
|
|
|
$pagination->setThrowExceptionOnBadDataType(false); |
290
|
|
|
|
291
|
|
|
// attach every column to the print view form |
292
|
|
|
$columns['Created'] = 'Created'; |
293
|
|
|
$columns['SubmittedBy.Email'] = 'Submitter'; |
294
|
|
|
$filter->setColumns($columns); |
295
|
|
|
|
296
|
|
|
// print configuration |
297
|
|
|
|
298
|
|
|
$print->setPrintHasHeader(true); |
299
|
|
|
$print->setPrintColumns($columns); |
300
|
|
|
|
301
|
|
|
// export configuration |
302
|
|
|
$export->setCsvHasHeader(true); |
303
|
|
|
$export->setExportColumns($columns); |
304
|
|
|
|
305
|
|
|
$submissions = GridField::create( |
306
|
|
|
'Submissions', |
307
|
|
|
'', |
308
|
|
|
$this->Submissions()->sort('Created', 'DESC'), |
|
|
|
|
309
|
|
|
$config |
310
|
|
|
); |
311
|
|
|
$fields->addFieldToTab('Root.Submissions', $submissions); |
312
|
|
|
$fields->addFieldToTab( |
313
|
|
|
'Root.FormOptions', |
314
|
|
|
CheckboxField::create( |
315
|
|
|
'DisableSaveSubmissions', |
316
|
|
|
_t(__CLASS__.'.SAVESUBMISSIONS', 'Disable Saving Submissions to Server') |
317
|
|
|
) |
318
|
|
|
); |
319
|
|
|
}); |
320
|
|
|
|
321
|
|
|
$fields = parent::getCMSFields(); |
322
|
|
|
|
323
|
|
|
if ($this->EmailRecipients()->Count() == 0 && static::config()->recipients_warning_enabled) { |
|
|
|
|
324
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create( |
325
|
|
|
'EmailRecipientsWarning', |
326
|
|
|
'<p class="message warning">' . _t( |
327
|
|
|
__CLASS__.'.NORECIPIENTS', |
328
|
|
|
'Warning: You have not configured any recipients. Form submissions may be missed.' |
329
|
|
|
) |
330
|
|
|
. '</p>' |
331
|
|
|
), 'Title'); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return $fields; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Allow overriding the EmailRecipients on a {@link DataExtension} |
339
|
|
|
* so you can customise who receives an email. |
340
|
|
|
* Converts the RelationList to an ArrayList so that manipulation |
341
|
|
|
* of the original source data isn't possible. |
342
|
|
|
* |
343
|
|
|
* @return ArrayList |
344
|
|
|
*/ |
345
|
|
|
public function FilteredEmailRecipients($data = null, $form = null) |
346
|
|
|
{ |
347
|
|
|
$recipients = ArrayList::create($this->EmailRecipients()->toArray()); |
|
|
|
|
348
|
|
|
|
349
|
|
|
// Filter by rules |
350
|
|
|
$recipients = $recipients->filterByCallback(function ($recipient) use ($data, $form) { |
351
|
|
|
/** @var EmailRecipient $recipient */ |
352
|
|
|
return $recipient->canSend($data, $form); |
353
|
|
|
}); |
354
|
|
|
|
355
|
|
|
$this->extend('updateFilteredEmailRecipients', $recipients, $data, $form); |
|
|
|
|
356
|
|
|
|
357
|
|
|
return $recipients; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Custom options for the form. You can extend the built in options by |
362
|
|
|
* using {@link updateFormOptions()} |
363
|
|
|
* |
364
|
|
|
* @return FieldList |
365
|
|
|
*/ |
366
|
|
|
public function getFormOptions() |
367
|
|
|
{ |
368
|
|
|
$submit = ($this->SubmitButtonText) ? $this->SubmitButtonText : _t(__CLASS__.'.SUBMITBUTTON', 'Submit'); |
|
|
|
|
369
|
|
|
$clear = ($this->ClearButtonText) ? $this->ClearButtonText : _t(__CLASS__.'.CLEARBUTTON', 'Clear'); |
|
|
|
|
370
|
|
|
|
371
|
|
|
$options = FieldList::create( |
372
|
|
|
TextField::create('SubmitButtonText', _t(__CLASS__.'.TEXTONSUBMIT', 'Text on submit button:'), $submit), |
373
|
|
|
TextField::create('ClearButtonText', _t(__CLASS__.'.TEXTONCLEAR', 'Text on clear button:'), $clear), |
374
|
|
|
CheckboxField::create('ShowClearButton', _t(__CLASS__.'.SHOWCLEARFORM', 'Show Clear Form Button'), $this->ShowClearButton), |
|
|
|
|
375
|
|
|
CheckboxField::create('EnableLiveValidation', _t(__CLASS__.'.ENABLELIVEVALIDATION', 'Enable live validation')), |
376
|
|
|
CheckboxField::create('DisplayErrorMessagesAtTop', _t(__CLASS__.'.DISPLAYERRORMESSAGESATTOP', 'Display error messages above the form?')), |
377
|
|
|
CheckboxField::create('DisableCsrfSecurityToken', _t(__CLASS__.'.DISABLECSRFSECURITYTOKEN', 'Disable CSRF Token')), |
378
|
|
|
CheckboxField::create('DisableAuthenicatedFinishAction', _t(__CLASS__.'.DISABLEAUTHENICATEDFINISHACTION', 'Disable Authentication on finish action')) |
379
|
|
|
); |
380
|
|
|
|
381
|
|
|
$this->extend('updateFormOptions', $options); |
|
|
|
|
382
|
|
|
|
383
|
|
|
return $options; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Get the HTML id of the error container displayed above the form. |
388
|
|
|
* |
389
|
|
|
* @return string |
390
|
|
|
*/ |
391
|
|
|
public function getErrorContainerID() |
392
|
|
|
{ |
393
|
|
|
return $this->config()->get('error_container_id'); |
|
|
|
|
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Validate formfields |
398
|
|
|
*/ |
399
|
|
|
public function getCMSValidator() |
400
|
|
|
{ |
401
|
|
|
return UserFormValidator::create(); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.