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