These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SilverStripe\UserForms\Model; |
||
4 | |||
5 | use SilverStripe\CMS\Controllers\CMSMain; |
||
6 | use SilverStripe\CMS\Controllers\CMSPageEditController; |
||
7 | use SilverStripe\Control\Controller; |
||
8 | use SilverStripe\Core\ClassInfo; |
||
9 | use SilverStripe\Core\Config\Config; |
||
10 | use SilverStripe\Core\Convert; |
||
11 | use SilverStripe\Core\Manifest\ModuleLoader; |
||
12 | use SilverStripe\Forms\CheckboxField; |
||
13 | use SilverStripe\Forms\DropdownField; |
||
14 | use SilverStripe\Forms\FieldList; |
||
15 | use SilverStripe\Forms\GridField\GridField; |
||
16 | use SilverStripe\Forms\GridField\GridFieldButtonRow; |
||
17 | use SilverStripe\Forms\GridField\GridFieldConfig; |
||
18 | use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
||
19 | use SilverStripe\Forms\GridField\GridFieldToolbarHeader; |
||
20 | use SilverStripe\Forms\LabelField; |
||
21 | use SilverStripe\Forms\LiteralField; |
||
22 | use SilverStripe\Forms\ReadonlyField; |
||
23 | use SilverStripe\Forms\SegmentField; |
||
24 | use SilverStripe\Forms\TabSet; |
||
25 | use SilverStripe\Forms\TextField; |
||
26 | use SilverStripe\ORM\ArrayList; |
||
27 | use SilverStripe\ORM\DataObject; |
||
28 | use SilverStripe\ORM\DB; |
||
29 | use SilverStripe\ORM\FieldType\DBField; |
||
30 | use SilverStripe\ORM\ValidationException; |
||
31 | use SilverStripe\UserForms\Extension\UserFormFieldEditorExtension; |
||
32 | use SilverStripe\UserForms\Model\UserDefinedForm; |
||
33 | use SilverStripe\UserForms\Model\EditableCustomRule; |
||
34 | use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup; |
||
35 | use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd; |
||
36 | use SilverStripe\UserForms\Model\EditableFormField\EditableFormStep; |
||
37 | use SilverStripe\UserForms\Model\Submission\SubmittedFormField; |
||
38 | use SilverStripe\UserForms\Modifier\DisambiguationSegmentFieldModifier; |
||
39 | use SilverStripe\UserForms\Modifier\UnderscoreSegmentFieldModifier; |
||
40 | use SilverStripe\UserForms\UserForm; |
||
41 | use SilverStripe\Versioned\Versioned; |
||
42 | use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; |
||
43 | use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
||
44 | |||
45 | /** |
||
46 | * Represents the base class of a editable form field |
||
47 | * object like {@link EditableTextField}. |
||
48 | * |
||
49 | * @package userforms |
||
50 | * |
||
51 | * @property string $Name |
||
52 | * @property string $Title |
||
53 | * @property string $Default |
||
54 | * @property int $Sort |
||
55 | * @property bool $Required |
||
56 | * @property string $CustomErrorMessage |
||
57 | * @property boolean $ShowOnLoad |
||
58 | * @property string $DisplayRulesConjunction |
||
59 | * @method UserDefinedForm Parent() Parent page |
||
60 | * @method DataList DisplayRules() List of EditableCustomRule objects |
||
61 | * @mixin Versioned |
||
62 | */ |
||
63 | class EditableFormField extends DataObject |
||
64 | { |
||
65 | /** |
||
66 | * Set to true to hide from class selector |
||
67 | * |
||
68 | * @config |
||
69 | * @var bool |
||
70 | */ |
||
71 | private static $hidden = false; |
||
72 | |||
73 | /** |
||
74 | * Define this field as abstract (not inherited) |
||
75 | * |
||
76 | * @config |
||
77 | * @var bool |
||
78 | */ |
||
79 | private static $abstract = true; |
||
80 | |||
81 | /** |
||
82 | * Flag this field type as non-data (e.g. literal, header, html) |
||
83 | * |
||
84 | * @config |
||
85 | * @var bool |
||
86 | */ |
||
87 | private static $literal = false; |
||
88 | |||
89 | /** |
||
90 | * Default sort order |
||
91 | * |
||
92 | * @config |
||
93 | * @var string |
||
94 | */ |
||
95 | private static $default_sort = '"Sort"'; |
||
96 | |||
97 | /** |
||
98 | * A list of CSS classes that can be added |
||
99 | * |
||
100 | * @var array |
||
101 | */ |
||
102 | public static $allowed_css = []; |
||
103 | |||
104 | /** |
||
105 | * Set this to true to enable placeholder field for any given class |
||
106 | * @config |
||
107 | * @var bool |
||
108 | */ |
||
109 | private static $has_placeholder = false; |
||
110 | |||
111 | /** |
||
112 | * @config |
||
113 | * @var array |
||
114 | */ |
||
115 | private static $summary_fields = [ |
||
116 | 'Title' |
||
117 | ]; |
||
118 | |||
119 | /** |
||
120 | * @config |
||
121 | * @var array |
||
122 | */ |
||
123 | private static $db = [ |
||
124 | 'Name' => 'Varchar', |
||
125 | 'Title' => 'Varchar(255)', |
||
126 | 'Default' => 'Varchar(255)', |
||
127 | 'Sort' => 'Int', |
||
128 | 'Required' => 'Boolean', |
||
129 | 'CustomErrorMessage' => 'Varchar(255)', |
||
130 | 'ExtraClass' => 'Text', |
||
131 | 'RightTitle' => 'Varchar(255)', |
||
132 | 'ShowOnLoad' => 'Boolean(1)', |
||
133 | 'ShowInSummary' => 'Boolean', |
||
134 | 'Placeholder' => 'Varchar(255)', |
||
135 | 'DisplayRulesConjunction' => 'Enum("And,Or","Or")', |
||
136 | ]; |
||
137 | |||
138 | private static $table_name = 'EditableFormField'; |
||
139 | |||
140 | |||
141 | private static $defaults = [ |
||
142 | 'ShowOnLoad' => true, |
||
143 | ]; |
||
144 | |||
145 | |||
146 | /** |
||
147 | * @config |
||
148 | * @var array |
||
149 | */ |
||
150 | private static $has_one = [ |
||
151 | 'Parent' => DataObject::class, |
||
152 | ]; |
||
153 | |||
154 | /** |
||
155 | * Built in extensions required |
||
156 | * |
||
157 | * @config |
||
158 | * @var array |
||
159 | */ |
||
160 | private static $extensions = [ |
||
161 | Versioned::class . "('Stage', 'Live')" |
||
162 | ]; |
||
163 | |||
164 | /** |
||
165 | * @config |
||
166 | * @var array |
||
167 | */ |
||
168 | private static $has_many = [ |
||
169 | 'DisplayRules' => EditableCustomRule::class . '.Parent' |
||
170 | ]; |
||
171 | |||
172 | private static $owns = [ |
||
173 | 'DisplayRules', |
||
174 | ]; |
||
175 | |||
176 | private static $cascade_deletes = [ |
||
177 | 'DisplayRules', |
||
178 | ]; |
||
179 | |||
180 | /** |
||
181 | * @var bool |
||
182 | */ |
||
183 | protected $readonly; |
||
184 | |||
185 | /** |
||
186 | * Property holds the JS event which gets fired for this type of element |
||
187 | * |
||
188 | * @var string |
||
189 | */ |
||
190 | protected $jsEventHandler = 'change'; |
||
191 | |||
192 | /** |
||
193 | * Returns the jsEventHandler property for the current object. Bearing in mind it could've been overridden. |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getJsEventHandler() |
||
197 | { |
||
198 | return $this->jsEventHandler; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Set the visibility of an individual form field |
||
203 | * |
||
204 | * @param bool |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function setReadonly($readonly = true) |
||
208 | { |
||
209 | $this->readonly = $readonly; |
||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Returns whether this field is readonly |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | private function isReadonly() |
||
219 | { |
||
220 | return $this->readonly; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return FieldList |
||
225 | */ |
||
226 | public function getCMSFields() |
||
227 | { |
||
228 | $fields = FieldList::create(TabSet::create('Root')); |
||
229 | |||
230 | // Main tab |
||
231 | $fields->addFieldsToTab( |
||
232 | 'Root.Main', |
||
233 | [ |
||
234 | ReadonlyField::create( |
||
235 | 'Type', |
||
236 | _t(__CLASS__.'.TYPE', 'Type'), |
||
237 | $this->i18n_singular_name() |
||
238 | ), |
||
239 | CheckboxField::create('ShowInSummary', _t(__CLASS__.'.SHOWINSUMMARY', 'Show in summary gridfield')), |
||
240 | LiteralField::create( |
||
241 | 'MergeField', |
||
242 | '<div class="form-group field readonly">' . |
||
243 | '<label class="left form__field-label" for="Form_ItemEditForm_MergeField">' |
||
244 | . _t(__CLASS__.'.MERGEFIELDNAME', 'Merge field') |
||
245 | . '</label>' |
||
246 | . '<div class="form__field-holder">' |
||
247 | . '<span class="readonly" id="Form_ItemEditForm_MergeField">$' . $this->Name . '</span>' |
||
248 | . '</div>' |
||
249 | . '</div>' |
||
250 | ), |
||
251 | TextField::create('Title', _t(__CLASS__.'.TITLE', 'Title')), |
||
252 | TextField::create('Default', _t(__CLASS__.'.DEFAULT', 'Default value')), |
||
253 | TextField::create('RightTitle', _t(__CLASS__.'.RIGHTTITLE', 'Right title')), |
||
254 | SegmentField::create('Name', _t(__CLASS__.'.NAME', 'Name'))->setModifiers([ |
||
255 | UnderscoreSegmentFieldModifier::create()->setDefault('FieldName'), |
||
256 | DisambiguationSegmentFieldModifier::create(), |
||
257 | ])->setPreview($this->Name) |
||
258 | ] |
||
259 | ); |
||
260 | $fields->fieldByName('Root.Main')->setTitle(_t('SilverStripe\\CMS\\Model\\SiteTree.TABMAIN', 'Main')); |
||
261 | |||
262 | // Custom settings |
||
263 | if (!empty(self::$allowed_css)) { |
||
264 | $cssList = []; |
||
265 | foreach (self::$allowed_css as $k => $v) { |
||
266 | if (!is_array($v)) { |
||
267 | $cssList[$k]=$v; |
||
268 | } elseif ($k === $this->ClassName) { |
||
269 | $cssList = array_merge($cssList, $v); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | $fields->addFieldToTab( |
||
274 | 'Root.Main', |
||
275 | DropdownField::create( |
||
276 | 'ExtraClass', |
||
277 | _t(__CLASS__.'.EXTRACLASS_TITLE', 'Extra Styling/Layout'), |
||
278 | $cssList |
||
279 | )->setDescription(_t( |
||
280 | __CLASS__.'.EXTRACLASS_SELECT', |
||
281 | 'Select from the list of allowed styles' |
||
282 | )) |
||
283 | ); |
||
284 | } else { |
||
285 | $fields->addFieldToTab( |
||
286 | 'Root.Main', |
||
287 | TextField::create( |
||
288 | 'ExtraClass', |
||
289 | _t(__CLASS__.'.EXTRACLASS_Title', 'Extra CSS classes') |
||
290 | )->setDescription(_t( |
||
291 | __CLASS__.'.EXTRACLASS_MULTIPLE', |
||
292 | 'Separate each CSS class with a single space' |
||
293 | )) |
||
294 | ); |
||
295 | } |
||
296 | |||
297 | // Validation |
||
298 | $validationFields = $this->getFieldValidationOptions(); |
||
299 | if ($validationFields && $validationFields->count()) { |
||
300 | $fields->addFieldsToTab('Root.Validation', $validationFields); |
||
301 | $fields->fieldByName('Root.Validation')->setTitle(_t(__CLASS__.'.VALIDATION', 'Validation')); |
||
302 | } |
||
303 | |||
304 | // Add display rule fields |
||
305 | $displayFields = $this->getDisplayRuleFields(); |
||
306 | if ($displayFields && $displayFields->count()) { |
||
307 | $fields->addFieldsToTab('Root.DisplayRules', $displayFields); |
||
308 | } |
||
309 | |||
310 | // Placeholder |
||
311 | if ($this->config()->has_placeholder) { |
||
312 | $fields->addFieldToTab( |
||
313 | 'Root.Main', |
||
314 | TextField::create( |
||
315 | 'Placeholder', |
||
316 | _t(__CLASS__.'.PLACEHOLDER', 'Placeholder') |
||
317 | ) |
||
318 | ); |
||
319 | } |
||
320 | |||
321 | $this->extend('updateCMSFields', $fields); |
||
322 | |||
323 | return $fields; |
||
324 | } |
||
325 | |||
326 | |||
327 | public function requireDefaultRecords() |
||
328 | { |
||
329 | parent::requireDefaultRecords(); |
||
330 | |||
331 | // make sure to migrate the class across (prior to v5.x) |
||
332 | DB::query("UPDATE EditableFormField SET ParentClass = 'Page' WHERE ParentClass IS NULL"); |
||
333 | DB::query("UPDATE EditableFormField_Live SET ParentClass = 'Page' WHERE ParentClass IS NULL"); |
||
334 | DB::query("UPDATE EditableFormField_Versions SET ParentClass = 'Page' WHERE ParentClass IS NULL"); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Return fields to display on the 'Display Rules' tab |
||
339 | * |
||
340 | * @return FieldList |
||
341 | */ |
||
342 | protected function getDisplayRuleFields() |
||
343 | { |
||
344 | // Check display rules |
||
345 | if ($this->Required) { |
||
346 | return new FieldList( |
||
347 | LabelField::create( |
||
348 | _t( |
||
349 | __CLASS__.'.DISPLAY_RULES_DISABLED', |
||
350 | 'Display rules are not enabled for required fields. Please uncheck "Is this field Required?" under "Validation" to re-enable.' |
||
351 | ) |
||
352 | ) |
||
353 | ->addExtraClass('message warning') |
||
354 | ); |
||
355 | } |
||
356 | |||
357 | $allowedClasses = array_keys($this->getEditableFieldClasses(false)); |
||
358 | $editableColumns = new GridFieldEditableColumns(); |
||
359 | $editableColumns->setDisplayFields([ |
||
360 | 'ConditionFieldID' => function ($record, $column, $grid) use ($allowedClasses) { |
||
361 | return DropdownField::create($column, '', EditableFormField::get()->filter([ |
||
362 | 'ParentID' => $this->ParentID, |
||
363 | 'ClassName' => $allowedClasses, |
||
364 | ])->exclude([ |
||
365 | 'ID' => $this->ID, |
||
366 | ])->map('ID', 'Title')); |
||
367 | }, |
||
368 | 'ConditionOption' => function ($record, $column, $grid) { |
||
369 | $options = Config::inst()->get(EditableCustomRule::class, 'condition_options'); |
||
370 | |||
371 | return DropdownField::create($column, '', $options); |
||
372 | }, |
||
373 | 'FieldValue' => function ($record, $column, $grid) { |
||
374 | return TextField::create($column); |
||
375 | } |
||
376 | ]); |
||
377 | |||
378 | // Custom rules |
||
379 | $customRulesConfig = GridFieldConfig::create() |
||
380 | ->addComponents( |
||
381 | $editableColumns, |
||
382 | new GridFieldButtonRow(), |
||
383 | new GridFieldToolbarHeader(), |
||
384 | new GridFieldAddNewInlineButton(), |
||
385 | new GridFieldDeleteAction() |
||
386 | ); |
||
387 | |||
388 | return new FieldList( |
||
389 | DropdownField::create( |
||
390 | 'ShowOnLoad', |
||
391 | _t(__CLASS__.'.INITIALVISIBILITY', 'Initial visibility'), |
||
392 | [ |
||
393 | 1 => 'Show', |
||
394 | 0 => 'Hide', |
||
395 | ] |
||
396 | ), |
||
397 | DropdownField::create( |
||
398 | 'DisplayRulesConjunction', |
||
399 | _t(__CLASS__.'.DISPLAYIF', 'Toggle visibility when'), |
||
400 | [ |
||
401 | 'Or' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFOR', 'Any conditions are true'), |
||
402 | 'And' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFAND', 'All conditions are true'), |
||
403 | ] |
||
404 | ), |
||
405 | GridField::create( |
||
406 | 'DisplayRules', |
||
407 | _t(__CLASS__.'.CUSTOMRULES', 'Custom Rules'), |
||
408 | $this->DisplayRules(), |
||
409 | $customRulesConfig |
||
410 | ) |
||
411 | ); |
||
412 | } |
||
413 | |||
414 | public function onBeforeWrite() |
||
415 | { |
||
416 | parent::onBeforeWrite(); |
||
417 | |||
418 | // Set a field name. |
||
419 | if (!$this->Name) { |
||
420 | // New random name |
||
421 | $this->Name = $this->generateName(); |
||
422 | } elseif ($this->Name === 'Field') { |
||
423 | throw new ValidationException('Field name cannot be "Field"'); |
||
424 | } |
||
425 | |||
426 | if (!$this->Sort && $this->ParentID) { |
||
427 | $parentID = $this->ParentID; |
||
428 | $this->Sort = EditableFormField::get() |
||
429 | ->filter('ParentID', $parentID) |
||
430 | ->max('Sort') + 1; |
||
431 | } |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Generate a new non-conflicting Name value |
||
436 | * |
||
437 | * @return string |
||
438 | */ |
||
439 | protected function generateName() |
||
440 | { |
||
441 | do { |
||
442 | // Generate a new random name after this class (handles namespaces) |
||
443 | $classNamePieces = explode('\\', static::class); |
||
444 | $class = array_pop($classNamePieces); |
||
445 | $entropy = substr(sha1(uniqid()), 0, 5); |
||
446 | $name = "{$class}_{$entropy}"; |
||
447 | |||
448 | // Check if it conflicts |
||
449 | $exists = EditableFormField::get()->filter('Name', $name)->count() > 0; |
||
450 | } while ($exists); |
||
451 | return $name; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
456 | * |
||
457 | * @return bool |
||
458 | */ |
||
459 | public function getSetsOwnError() |
||
460 | { |
||
461 | return false; |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Return whether a user can delete this form field |
||
466 | * based on whether they can edit the page |
||
467 | * |
||
468 | * @param Member $member |
||
469 | * @return bool |
||
470 | */ |
||
471 | public function canDelete($member = null) |
||
472 | { |
||
473 | return $this->canEdit($member); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Return whether a user can edit this form field |
||
478 | * based on whether they can edit the page |
||
479 | * |
||
480 | * @param Member $member |
||
481 | * @return bool |
||
482 | */ |
||
483 | public function canEdit($member = null) |
||
484 | { |
||
485 | $parent = $this->Parent(); |
||
486 | if ($parent && $parent->exists()) { |
||
487 | return $parent->canEdit($member) && !$this->isReadonly(); |
||
488 | } elseif (!$this->exists() && Controller::has_curr()) { |
||
489 | // This is for GridFieldOrderableRows support as it checks edit permissions on |
||
490 | // singleton of the class. Allows editing of User Defined Form pages by |
||
491 | // 'Content Authors' and those with permission to edit the UDF page. (ie. CanEditType/EditorGroups) |
||
492 | // This is to restore User Forms 2.x backwards compatibility. |
||
493 | $controller = Controller::curr(); |
||
494 | if ($controller && $controller instanceof CMSPageEditController) { |
||
495 | $parent = $controller->getRecord($controller->currentPageID()); |
||
496 | // Only allow this behaviour on pages using UserFormFieldEditorExtension, such |
||
497 | // as UserDefinedForm page type. |
||
498 | if ($parent && $parent->hasExtension(UserFormFieldEditorExtension::class)) { |
||
499 | return $parent->canEdit($member); |
||
500 | } |
||
501 | } |
||
502 | } |
||
503 | |||
504 | // Fallback to secure admin permissions |
||
505 | return parent::canEdit($member); |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Return whether a user can view this form field |
||
510 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
511 | * |
||
512 | * @param Member $member |
||
513 | * @return bool |
||
514 | */ |
||
515 | public function canView($member = null) |
||
516 | { |
||
517 | $parent = $this->Parent(); |
||
518 | if ($parent && $parent->exists()) { |
||
519 | return $parent->canView($member); |
||
520 | } |
||
521 | |||
522 | return true; |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Return whether a user can create an object of this type |
||
527 | * |
||
528 | * @param Member $member |
||
529 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
530 | * @return bool |
||
531 | */ |
||
532 | View Code Duplication | public function canCreate($member = null, $context = []) |
|
533 | { |
||
534 | // Check parent page |
||
535 | $parent = $this->getCanCreateContext(func_get_args()); |
||
536 | if ($parent) { |
||
537 | return $parent->canEdit($member); |
||
538 | } |
||
539 | |||
540 | // Fall back to secure admin permissions |
||
541 | return parent::canCreate($member); |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Helper method to check the parent for this object |
||
546 | * |
||
547 | * @param array $args List of arguments passed to canCreate |
||
548 | * @return SiteTree Parent page instance |
||
549 | */ |
||
550 | View Code Duplication | protected function getCanCreateContext($args) |
|
551 | { |
||
552 | // Inspect second parameter to canCreate for a 'Parent' context |
||
553 | if (isset($args[1]['Parent'])) { |
||
554 | return $args[1]['Parent']; |
||
555 | } |
||
556 | // Hack in currently edited page if context is missing |
||
557 | if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
||
558 | return Controller::curr()->currentPage(); |
||
559 | } |
||
560 | |||
561 | // No page being edited |
||
562 | return null; |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * checks whether record is new, copied from SiteTree |
||
567 | */ |
||
568 | public function isNew() |
||
569 | { |
||
570 | if (empty($this->ID)) { |
||
571 | return true; |
||
572 | } |
||
573 | |||
574 | if (is_numeric($this->ID)) { |
||
575 | return false; |
||
576 | } |
||
577 | |||
578 | return stripos($this->ID, 'new') === 0; |
||
579 | } |
||
580 | |||
581 | /** |
||
582 | * Set the allowed css classes for the extraClass custom setting |
||
583 | * |
||
584 | * @param array $allowed The permissible CSS classes to add |
||
585 | */ |
||
586 | public function setAllowedCss(array $allowed) |
||
587 | { |
||
588 | if (is_array($allowed)) { |
||
589 | foreach ($allowed as $k => $v) { |
||
590 | self::$allowed_css[$k] = (!is_null($v)) ? $v : $k; |
||
591 | } |
||
592 | } |
||
593 | } |
||
594 | |||
595 | /** |
||
596 | * Get the path to the icon for this field type, relative to the site root. |
||
597 | * |
||
598 | * @return string |
||
599 | */ |
||
600 | public function getIcon() |
||
601 | { |
||
602 | return ModuleLoader::getModule('silverstripe/userforms') |
||
603 | ->getRelativeResourcePath('images/' . strtolower($this->class) . '.png'); |
||
0 ignored issues
–
show
|
|||
604 | } |
||
605 | |||
606 | /** |
||
607 | * Return whether or not this field has addable options |
||
608 | * such as a dropdown field or radio set |
||
609 | * |
||
610 | * @return bool |
||
611 | */ |
||
612 | public function getHasAddableOptions() |
||
613 | { |
||
614 | return false; |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * Return whether or not this field needs to show the extra |
||
619 | * options dropdown list |
||
620 | * |
||
621 | * @return bool |
||
622 | */ |
||
623 | public function showExtraOptions() |
||
624 | { |
||
625 | return true; |
||
626 | } |
||
627 | |||
628 | /** |
||
629 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | public function getEscapedTitle() |
||
634 | { |
||
635 | return Convert::raw2xml($this->Title); |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
640 | * |
||
641 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
642 | * or groups |
||
643 | * |
||
644 | * @return string |
||
645 | */ |
||
646 | public function getFieldNumber() |
||
647 | { |
||
648 | // Check if exists |
||
649 | if (!$this->exists()) { |
||
650 | return null; |
||
651 | } |
||
652 | // Check parent |
||
653 | $form = $this->Parent(); |
||
654 | if (!$form || !$form->exists() || !($fields = $form->Fields())) { |
||
655 | return null; |
||
656 | } |
||
657 | |||
658 | $prior = 0; // Number of prior group at this level |
||
659 | $stack = []; // Current stack of nested groups, where the top level = the page |
||
660 | foreach ($fields->map('ID', 'ClassName') as $id => $className) { |
||
661 | if ($className === EditableFormStep::class) { |
||
662 | $priorPage = empty($stack) ? $prior : $stack[0]; |
||
663 | $stack = array($priorPage + 1); |
||
664 | $prior = 0; |
||
665 | } elseif ($className === EditableFieldGroup::class) { |
||
666 | $stack[] = $prior + 1; |
||
667 | $prior = 0; |
||
668 | } elseif ($className === EditableFieldGroupEnd::class) { |
||
669 | $prior = array_pop($stack); |
||
670 | } |
||
671 | if ($id == $this->ID) { |
||
672 | return implode('.', $stack); |
||
673 | } |
||
674 | } |
||
675 | return null; |
||
676 | } |
||
677 | |||
678 | public function getCMSTitle() |
||
679 | { |
||
680 | return $this->i18n_singular_name() . ' (' . $this->Title . ')'; |
||
681 | } |
||
682 | |||
683 | /** |
||
684 | * Append custom validation fields to the default 'Validation' |
||
685 | * section in the editable options view |
||
686 | * |
||
687 | * @return FieldList |
||
688 | */ |
||
689 | public function getFieldValidationOptions() |
||
690 | { |
||
691 | $fields = new FieldList( |
||
692 | CheckboxField::create('Required', _t(__CLASS__.'.REQUIRED', 'Is this field Required?')) |
||
693 | ->setDescription(_t(__CLASS__.'.REQUIRED_DESCRIPTION', 'Please note that conditional fields can\'t be required')), |
||
694 | TextField::create('CustomErrorMessage', _t(__CLASS__.'.CUSTOMERROR', 'Custom Error Message')) |
||
695 | ); |
||
696 | |||
697 | $this->extend('updateFieldValidationOptions', $fields); |
||
698 | |||
699 | return $fields; |
||
700 | } |
||
701 | |||
702 | /** |
||
703 | * Return a FormField to appear on the front end. Implement on |
||
704 | * your subclass. |
||
705 | * |
||
706 | * @return FormField |
||
707 | */ |
||
708 | public function getFormField() |
||
709 | { |
||
710 | user_error("Please implement a getFormField() on your EditableFormClass ". $this->ClassName, E_USER_ERROR); |
||
711 | } |
||
712 | |||
713 | /** |
||
714 | * Updates a formfield with extensions |
||
715 | * |
||
716 | * @param FormField $field |
||
717 | */ |
||
718 | public function doUpdateFormField($field) |
||
719 | { |
||
720 | $this->extend('beforeUpdateFormField', $field); |
||
721 | $this->updateFormField($field); |
||
722 | $this->extend('afterUpdateFormField', $field); |
||
723 | } |
||
724 | |||
725 | /** |
||
726 | * Updates a formfield with the additional metadata specified by this field |
||
727 | * |
||
728 | * @param FormField $field |
||
729 | */ |
||
730 | protected function updateFormField($field) |
||
731 | { |
||
732 | // set the error / formatting messages |
||
733 | $field->setCustomValidationMessage($this->getErrorMessage()->RAW()); |
||
734 | |||
735 | // set the right title on this field |
||
736 | if ($this->RightTitle) { |
||
737 | // Since this field expects raw html, safely escape the user data prior |
||
738 | $field->setRightTitle(Convert::raw2xml($this->RightTitle)); |
||
739 | } |
||
740 | |||
741 | // if this field is required add some |
||
742 | if ($this->Required) { |
||
743 | // Required validation can conflict so add the Required validation messages as input attributes |
||
744 | $errorMessage = $this->getErrorMessage()->HTML(); |
||
745 | $field->addExtraClass('requiredField'); |
||
746 | $field->setAttribute('data-rule-required', 'true'); |
||
747 | $field->setAttribute('data-msg-required', $errorMessage); |
||
748 | |||
749 | if ($identifier = UserDefinedForm::config()->required_identifier) { |
||
750 | $title = $field->Title() . " <span class='required-identifier'>". $identifier . "</span>"; |
||
751 | $field->setTitle($title); |
||
752 | } |
||
753 | } |
||
754 | |||
755 | // if this field has an extra class |
||
756 | if ($this->ExtraClass) { |
||
757 | $field->addExtraClass($this->ExtraClass); |
||
758 | } |
||
759 | |||
760 | // if ShowOnLoad is false hide the field |
||
761 | if (!$this->ShowOnLoad) { |
||
762 | $field->addExtraClass($this->ShowOnLoadNice()); |
||
763 | } |
||
764 | |||
765 | // if this field has a placeholder |
||
766 | if ($this->Placeholder) { |
||
767 | $field->setAttribute('placeholder', $this->Placeholder); |
||
768 | } |
||
769 | } |
||
770 | |||
771 | /** |
||
772 | * Return the instance of the submission field class |
||
773 | * |
||
774 | * @return SubmittedFormField |
||
775 | */ |
||
776 | public function getSubmittedFormField() |
||
777 | { |
||
778 | return SubmittedFormField::create(); |
||
779 | } |
||
780 | |||
781 | |||
782 | /** |
||
783 | * Show this form field (and its related value) in the reports and in emails. |
||
784 | * |
||
785 | * @return bool |
||
786 | */ |
||
787 | public function showInReports() |
||
788 | { |
||
789 | return true; |
||
790 | } |
||
791 | |||
792 | /** |
||
793 | * Return the error message for this field. Either uses the custom |
||
794 | * one (if provided) or the default SilverStripe message |
||
795 | * |
||
796 | * @return Varchar |
||
797 | */ |
||
798 | public function getErrorMessage() |
||
799 | { |
||
800 | $title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'"); |
||
801 | $standard = _t('SilverStripe\\Forms\\Form.FIELDISREQUIRED', '{field} is required.', ['field' => $title]); |
||
802 | |||
803 | // only use CustomErrorMessage if it has a non empty value |
||
804 | $errorMessage = (!empty($this->CustomErrorMessage)) ? $this->CustomErrorMessage : $standard; |
||
805 | |||
806 | return DBField::create_field('Varchar', $errorMessage); |
||
807 | } |
||
808 | |||
809 | /** |
||
810 | * Get the formfield to use when editing this inline in gridfield |
||
811 | * |
||
812 | * @param string $column name of column |
||
813 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
814 | * @return FormField |
||
815 | */ |
||
816 | public function getInlineClassnameField($column, $fieldClasses) |
||
817 | { |
||
818 | return DropdownField::create($column, false, $fieldClasses); |
||
819 | } |
||
820 | |||
821 | /** |
||
822 | * Get the formfield to use when editing the title inline |
||
823 | * |
||
824 | * @param string $column |
||
825 | * @return FormField |
||
826 | */ |
||
827 | public function getInlineTitleField($column) |
||
828 | { |
||
829 | return TextField::create($column, false) |
||
830 | ->setAttribute('placeholder', _t(__CLASS__.'.TITLE', 'Title')) |
||
831 | ->setAttribute('data-placeholder', _t(__CLASS__.'.TITLE', 'Title')); |
||
832 | } |
||
833 | |||
834 | /** |
||
835 | * Get the JS expression for selecting the holder for this field |
||
836 | * |
||
837 | * @return string |
||
838 | */ |
||
839 | public function getSelectorHolder() |
||
840 | { |
||
841 | return sprintf('$("%s")', $this->getSelectorOnly()); |
||
842 | } |
||
843 | |||
844 | /** |
||
845 | * Returns only the JS identifier of a string, less the $(), which can be inserted elsewhere, for example when you |
||
846 | * want to perform selections on multiple selectors |
||
847 | * @return string |
||
848 | */ |
||
849 | public function getSelectorOnly() |
||
850 | { |
||
851 | return "#{$this->Name}"; |
||
852 | } |
||
853 | |||
854 | /** |
||
855 | * Gets the JS expression for selecting the value for this field |
||
856 | * |
||
857 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
858 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
859 | * |
||
860 | * @return string |
||
861 | */ |
||
862 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
863 | { |
||
864 | return sprintf("$(%s)", $this->getSelectorFieldOnly()); |
||
865 | } |
||
866 | |||
867 | /** |
||
868 | * @return string |
||
869 | */ |
||
870 | public function getSelectorFieldOnly() |
||
871 | { |
||
872 | return "[name='{$this->Name}']"; |
||
873 | } |
||
874 | |||
875 | |||
876 | /** |
||
877 | * Get the list of classes that can be selected and used as data-values |
||
878 | * |
||
879 | * @param $includeLiterals Set to false to exclude non-data fields |
||
880 | * @return array |
||
881 | */ |
||
882 | public function getEditableFieldClasses($includeLiterals = true) |
||
883 | { |
||
884 | $classes = ClassInfo::getValidSubClasses(EditableFormField::class); |
||
885 | |||
886 | // Remove classes we don't want to display in the dropdown. |
||
887 | $editableFieldClasses = []; |
||
888 | foreach ($classes as $class) { |
||
889 | // Skip abstract / hidden classes |
||
890 | if (Config::inst()->get($class, 'abstract', Config::UNINHERITED) |
||
891 | || Config::inst()->get($class, 'hidden') |
||
892 | ) { |
||
893 | continue; |
||
894 | } |
||
895 | |||
896 | if (!$includeLiterals && Config::inst()->get($class, 'literal')) { |
||
897 | continue; |
||
898 | } |
||
899 | |||
900 | $singleton = singleton($class); |
||
901 | if (!$singleton->canCreate()) { |
||
902 | continue; |
||
903 | } |
||
904 | |||
905 | $editableFieldClasses[$class] = $singleton->i18n_singular_name(); |
||
906 | } |
||
907 | |||
908 | asort($editableFieldClasses); |
||
909 | return $editableFieldClasses; |
||
910 | } |
||
911 | |||
912 | /** |
||
913 | * @return EditableFormField\Validator |
||
914 | */ |
||
915 | public function getCMSValidator() |
||
916 | { |
||
917 | return EditableFormField\Validator::create() |
||
918 | ->setRecord($this); |
||
919 | } |
||
920 | |||
921 | /** |
||
922 | * Determine effective display rules for this field. |
||
923 | * |
||
924 | * @return SS_List |
||
925 | */ |
||
926 | public function EffectiveDisplayRules() |
||
927 | { |
||
928 | if ($this->Required) { |
||
929 | return ArrayList::create(); |
||
930 | } |
||
931 | return $this->DisplayRules(); |
||
932 | } |
||
933 | |||
934 | /** |
||
935 | * Extracts info from DisplayRules into array so UserDefinedForm->buildWatchJS can run through it. |
||
936 | * @return array|null |
||
937 | */ |
||
938 | public function formatDisplayRules() |
||
939 | { |
||
940 | $holderSelector = $this->getSelectorOnly(); |
||
941 | $result = [ |
||
942 | 'targetFieldID' => $holderSelector, |
||
943 | 'conjunction' => $this->DisplayRulesConjunctionNice(), |
||
944 | 'selectors' => [], |
||
945 | 'events' => [], |
||
946 | 'operations' => [], |
||
947 | 'initialState' => $this->ShowOnLoadNice(), |
||
948 | 'view' => [], |
||
949 | 'opposite' => [], |
||
950 | ]; |
||
951 | |||
952 | // Check for field dependencies / default |
||
953 | /** @var EditableCustomRule $rule */ |
||
954 | foreach ($this->EffectiveDisplayRules() as $rule) { |
||
955 | // Get the field which is effected |
||
956 | /** @var EditableFormField $formFieldWatch */ |
||
957 | $formFieldWatch = DataObject::get_by_id(EditableFormField::class, $rule->ConditionFieldID); |
||
958 | // Skip deleted fields |
||
959 | if (! $formFieldWatch) { |
||
960 | continue; |
||
961 | } |
||
962 | $fieldToWatch = $formFieldWatch->getSelectorFieldOnly(); |
||
963 | |||
964 | $expression = $rule->buildExpression(); |
||
965 | if (!in_array($fieldToWatch, $result['selectors'])) { |
||
966 | $result['selectors'][] = $fieldToWatch; |
||
967 | } |
||
968 | if (!in_array($expression['event'], $result['events'])) { |
||
969 | $result['events'][] = $expression['event']; |
||
970 | } |
||
971 | $result['operations'][] = $expression['operation']; |
||
972 | |||
973 | // View/Show should read |
||
974 | $opposite = ($result['initialState'] === 'hide') ? 'show' : 'hide'; |
||
975 | $result['view'] = $rule->toggleDisplayText($result['initialState']); |
||
976 | $result['opposite'] = $rule->toggleDisplayText($opposite); |
||
977 | } |
||
978 | |||
979 | return (count($result['selectors'])) ? $result : null; |
||
980 | } |
||
981 | |||
982 | /** |
||
983 | * Replaces the set DisplayRulesConjunction with their JS logical operators |
||
984 | * @return string |
||
985 | */ |
||
986 | public function DisplayRulesConjunctionNice() |
||
987 | { |
||
988 | return (strtolower($this->DisplayRulesConjunction) === 'or') ? '||' : '&&'; |
||
989 | } |
||
990 | |||
991 | /** |
||
992 | * Replaces boolean ShowOnLoad with its JS string equivalent |
||
993 | * @return string |
||
994 | */ |
||
995 | public function ShowOnLoadNice() |
||
996 | { |
||
997 | return ($this->ShowOnLoad) ? 'show' : 'hide'; |
||
998 | } |
||
999 | |||
1000 | /** |
||
1001 | * Returns whether this is of type EditableCheckBoxField |
||
1002 | * @return bool |
||
1003 | */ |
||
1004 | public function isCheckBoxField() |
||
1005 | { |
||
1006 | return false; |
||
1007 | } |
||
1008 | |||
1009 | /** |
||
1010 | * Returns whether this is of type EditableRadioField |
||
1011 | * @return bool |
||
1012 | */ |
||
1013 | public function isRadioField() |
||
1014 | { |
||
1015 | return false; |
||
1016 | } |
||
1017 | |||
1018 | /** |
||
1019 | * Determined is this is of type EditableCheckboxGroupField |
||
1020 | * @return bool |
||
1021 | */ |
||
1022 | public function isCheckBoxGroupField() |
||
1023 | { |
||
1024 | return false; |
||
1025 | } |
||
1026 | } |
||
1027 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.