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