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() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Set the visibility of an individual form field |
||
| 159 | * |
||
| 160 | * @param bool |
||
| 161 | */ |
||
| 162 | 1 | public function setReadonly($readonly = true) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Returns whether this field is readonly |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | 1 | private function isReadonly() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return FieldList |
||
| 179 | */ |
||
| 180 | 1 | public function getCMSFields() |
|
| 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 | )); |
||
| 316 | |||
| 317 | // Custom rules |
||
| 318 | $customRulesConfig = GridFieldConfig::create() |
||
| 319 | ->addComponents( |
||
| 320 | $editableColumns, |
||
| 321 | new GridFieldButtonRow(), |
||
| 322 | new GridFieldToolbarHeader(), |
||
| 323 | new GridFieldAddNewInlineButton(), |
||
| 324 | new GridFieldDeleteAction() |
||
| 325 | ); |
||
| 326 | |||
| 327 | return new FieldList( |
||
| 328 | DropdownField::create('ShowOnLoad', |
||
| 329 | _t('EditableFormField.INITIALVISIBILITY', 'Initial visibility'), |
||
| 330 | array( |
||
| 331 | 1 => 'Show', |
||
| 332 | 0 => 'Hide', |
||
| 333 | ) |
||
| 334 | ), |
||
| 335 | DropdownField::create('DisplayRulesConjunction', |
||
| 336 | _t('EditableFormField.DISPLAYIF', 'Toggle visibility when'), |
||
| 337 | array( |
||
| 338 | 'Or' => _t('UserDefinedForm.SENDIFOR', 'Any conditions are true'), |
||
| 339 | 'And' => _t('UserDefinedForm.SENDIFAND', 'All conditions are true'), |
||
| 340 | ) |
||
| 341 | ), |
||
| 342 | GridField::create( |
||
| 343 | 'DisplayRules', |
||
| 344 | _t('EditableFormField.CUSTOMRULES', 'Custom Rules'), |
||
| 345 | $this->DisplayRules(), |
||
| 346 | $customRulesConfig |
||
| 347 | ) |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | |||
| 351 | 45 | public function onBeforeWrite() |
|
| 352 | { |
||
| 353 | 45 | parent::onBeforeWrite(); |
|
| 354 | |||
| 355 | // Set a field name. |
||
| 356 | 45 | if (!$this->Name) { |
|
| 357 | // New random name |
||
| 358 | 26 | $this->Name = $this->generateName(); |
|
| 359 | 45 | } elseif ($this->Name === 'Field') { |
|
| 360 | throw new ValidationException('Field name cannot be "Field"'); |
||
| 361 | } |
||
| 362 | |||
| 363 | 45 | if (!$this->Sort && $this->ParentID) { |
|
| 364 | 42 | $parentID = $this->ParentID; |
|
| 365 | 42 | $this->Sort = EditableFormField::get() |
|
| 366 | 42 | ->filter('ParentID', $parentID) |
|
| 367 | 42 | ->max('Sort') + 1; |
|
| 368 | 42 | } |
|
| 369 | 45 | } |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Generate a new non-conflicting Name value |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | 26 | protected function generateName() |
|
| 377 | { |
||
| 378 | do { |
||
| 379 | // Generate a new random name after this class |
||
| 380 | 26 | $class = get_class($this); |
|
| 381 | 26 | $entropy = substr(sha1(uniqid()), 0, 5); |
|
| 382 | 26 | $name = "{$class}_{$entropy}"; |
|
| 383 | |||
| 384 | // Check if it conflicts |
||
| 385 | 26 | $exists = EditableFormField::get()->filter('Name', $name)->count() > 0; |
|
| 386 | 26 | } while ($exists); |
|
| 387 | 26 | return $name; |
|
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function getSetsOwnError() |
||
| 396 | { |
||
| 397 | return false; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Return whether a user can delete this form field |
||
| 402 | * based on whether they can edit the page |
||
| 403 | * |
||
| 404 | * @param Member $member |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | 1 | public function canDelete($member = null) |
|
| 408 | { |
||
| 409 | 1 | return $this->canEdit($member); |
|
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Return whether a user can edit this form field |
||
| 414 | * based on whether they can edit the page |
||
| 415 | * |
||
| 416 | * @param Member $member |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | 1 | public function canEdit($member = null) |
|
| 420 | { |
||
| 421 | 1 | $parent = $this->Parent(); |
|
| 422 | 1 | if ($parent && $parent->exists()) { |
|
| 423 | 1 | return $parent->canEdit($member) && !$this->isReadonly(); |
|
| 424 | } elseif (!$this->exists() && Controller::has_curr()) { |
||
| 425 | // This is for GridFieldOrderableRows support as it checks edit permissions on |
||
| 426 | // singleton of the class. Allows editing of User Defined Form pages by |
||
| 427 | // 'Content Authors' and those with permission to edit the UDF page. (ie. CanEditType/EditorGroups) |
||
| 428 | // This is to restore User Forms 2.x backwards compatibility. |
||
| 429 | $controller = Controller::curr(); |
||
| 430 | if ($controller && $controller instanceof CMSPageEditController) { |
||
| 431 | $parent = $controller->getRecord($controller->currentPageID()); |
||
| 432 | // Only allow this behaviour on pages using UserFormFieldEditorExtension, such |
||
| 433 | // as UserDefinedForm page type. |
||
| 434 | if ($parent && $parent->hasExtension('UserFormFieldEditorExtension')) { |
||
| 435 | return $parent->canEdit($member); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | // Fallback to secure admin permissions |
||
| 441 | return parent::canEdit($member); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Return whether a user can view this form field |
||
| 446 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
| 447 | * |
||
| 448 | * @param Member $member |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | 1 | public function canView($member = null) |
|
| 452 | { |
||
| 453 | 1 | $parent = $this->Parent(); |
|
| 454 | 1 | if ($parent && $parent->exists()) { |
|
| 455 | 1 | return $parent->canView($member); |
|
| 456 | } |
||
| 457 | |||
| 458 | return true; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Return whether a user can create an object of this type |
||
| 463 | * |
||
| 464 | * @param Member $member |
||
| 465 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | 3 | View Code Duplication | public function canCreate($member = null) |
| 469 | { |
||
| 470 | // Check parent page |
||
| 471 | 3 | $parent = $this->getCanCreateContext(func_get_args()); |
|
| 472 | 3 | if ($parent) { |
|
| 473 | return $parent->canEdit($member); |
||
| 474 | } |
||
| 475 | |||
| 476 | // Fall back to secure admin permissions |
||
| 477 | 3 | return parent::canCreate($member); |
|
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Helper method to check the parent for this object |
||
| 482 | * |
||
| 483 | * @param array $args List of arguments passed to canCreate |
||
| 484 | * @return SiteTree Parent page instance |
||
| 485 | */ |
||
| 486 | 3 | View Code Duplication | protected function getCanCreateContext($args) |
| 487 | { |
||
| 488 | // Inspect second parameter to canCreate for a 'Parent' context |
||
| 489 | 3 | if (isset($args[1]['Parent'])) { |
|
| 490 | return $args[1]['Parent']; |
||
| 491 | } |
||
| 492 | // Hack in currently edited page if context is missing |
||
| 493 | 3 | if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
|
| 494 | return Controller::curr()->currentPage(); |
||
| 495 | } |
||
| 496 | |||
| 497 | // No page being edited |
||
| 498 | 3 | return null; |
|
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Check if can publish |
||
| 503 | * |
||
| 504 | * @param Member $member |
||
| 505 | * @return bool |
||
| 506 | */ |
||
| 507 | public function canPublish($member = null) |
||
| 508 | { |
||
| 509 | return $this->canEdit($member); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Check if can unpublish |
||
| 514 | * |
||
| 515 | * @param Member $member |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | public function canUnpublish($member = null) |
||
| 519 | { |
||
| 520 | return $this->canDelete($member); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Publish this Form Field to the live site |
||
| 525 | * |
||
| 526 | * Wrapper for the {@link Versioned} publish function |
||
| 527 | * |
||
| 528 | * @param string $fromStage |
||
| 529 | * @param string $toStage |
||
| 530 | * @param bool $createNewVersion |
||
| 531 | */ |
||
| 532 | 10 | public function doPublish($fromStage, $toStage, $createNewVersion = false) |
|
| 533 | { |
||
| 534 | 10 | $this->publish($fromStage, $toStage, $createNewVersion); |
|
| 535 | 10 | $this->publishRules($fromStage, $toStage, $createNewVersion); |
|
| 536 | 10 | } |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Publish all field rules |
||
| 540 | * |
||
| 541 | * @param string $fromStage |
||
| 542 | * @param string $toStage |
||
| 543 | * @param bool $createNewVersion |
||
| 544 | */ |
||
| 545 | 10 | View Code Duplication | protected function publishRules($fromStage, $toStage, $createNewVersion) |
| 546 | { |
||
| 547 | 10 | $seenRuleIDs = array(); |
|
| 548 | |||
| 549 | // Don't forget to publish the related custom rules... |
||
| 550 | 10 | foreach ($this->DisplayRules() as $rule) { |
|
| 551 | 1 | $seenRuleIDs[] = $rule->ID; |
|
| 552 | 1 | $rule->doPublish($fromStage, $toStage, $createNewVersion); |
|
| 553 | 1 | $rule->destroy(); |
|
| 554 | 10 | } |
|
| 555 | |||
| 556 | // remove any orphans from the "fromStage" |
||
| 557 | 10 | $rules = Versioned::get_by_stage('EditableCustomRule', $toStage) |
|
| 558 | 10 | ->filter('ParentID', $this->ID); |
|
| 559 | |||
| 560 | 10 | if (!empty($seenRuleIDs)) { |
|
| 561 | 1 | $rules = $rules->exclude('ID', $seenRuleIDs); |
|
| 562 | 1 | } |
|
| 563 | |||
| 564 | 10 | foreach ($rules as $rule) { |
|
| 565 | 1 | $rule->deleteFromStage($toStage); |
|
| 566 | 10 | } |
|
| 567 | 10 | } |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Delete this field from a given stage |
||
| 571 | * |
||
| 572 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
| 573 | */ |
||
| 574 | 1 | public function doDeleteFromStage($stage) |
|
| 575 | { |
||
| 576 | // Remove custom rules in this stage |
||
| 577 | 1 | $rules = Versioned::get_by_stage('EditableCustomRule', $stage) |
|
| 578 | 1 | ->filter('ParentID', $this->ID); |
|
| 579 | 1 | foreach ($rules as $rule) { |
|
| 580 | $rule->deleteFromStage($stage); |
||
| 581 | 1 | } |
|
| 582 | |||
| 583 | // Remove record |
||
| 584 | 1 | $this->deleteFromStage($stage); |
|
| 585 | 1 | } |
|
| 586 | |||
| 587 | /** |
||
| 588 | * checks whether record is new, copied from SiteTree |
||
| 589 | */ |
||
| 590 | public function isNew() |
||
| 591 | { |
||
| 592 | if (empty($this->ID)) { |
||
| 593 | return true; |
||
| 594 | } |
||
| 595 | |||
| 596 | if (is_numeric($this->ID)) { |
||
| 597 | return false; |
||
| 598 | } |
||
| 599 | |||
| 600 | return stripos($this->ID, 'new') === 0; |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * checks if records is changed on stage |
||
| 605 | * @return boolean |
||
| 606 | */ |
||
| 607 | public function getIsModifiedOnStage() |
||
| 608 | { |
||
| 609 | // new unsaved fields could be never be published |
||
| 610 | if ($this->isNew()) { |
||
| 611 | return false; |
||
| 612 | } |
||
| 613 | |||
| 614 | $stageVersion = Versioned::get_versionnumber_by_stage('EditableFormField', 'Stage', $this->ID); |
||
| 615 | $liveVersion = Versioned::get_versionnumber_by_stage('EditableFormField', 'Live', $this->ID); |
||
| 616 | |||
| 617 | return ($stageVersion && $stageVersion != $liveVersion); |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @deprecated since version 4.0 |
||
| 622 | */ |
||
| 623 | public function getSettings() |
||
| 624 | { |
||
| 625 | Deprecation::notice('4.0', 'getSettings is deprecated'); |
||
| 626 | return (!empty($this->CustomSettings)) ? unserialize($this->CustomSettings) : array(); |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @deprecated since version 4.0 |
||
| 631 | */ |
||
| 632 | public function setSettings($settings = array()) |
||
| 633 | { |
||
| 634 | Deprecation::notice('4.0', 'setSettings is deprecated'); |
||
| 635 | $this->CustomSettings = serialize($settings); |
||
| 636 | } |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @deprecated since version 4.0 |
||
| 640 | */ |
||
| 641 | public function setSetting($key, $value) |
||
| 642 | { |
||
| 643 | Deprecation::notice('4.0', "setSetting({$key}) is deprecated"); |
||
| 644 | $settings = $this->getSettings(); |
||
| 645 | $settings[$key] = $value; |
||
| 646 | |||
| 647 | $this->setSettings($settings); |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Set the allowed css classes for the extraClass custom setting |
||
| 652 | * |
||
| 653 | * @param array $allowed The permissible CSS classes to add |
||
| 654 | */ |
||
| 655 | public function setAllowedCss(array $allowed) |
||
| 656 | { |
||
| 657 | if (is_array($allowed)) { |
||
| 658 | foreach ($allowed as $k => $v) { |
||
| 659 | self::$allowed_css[$k] = (!is_null($v)) ? $v : $k; |
||
| 660 | } |
||
| 661 | } |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @deprecated since version 4.0 |
||
| 666 | */ |
||
| 667 | public function getSetting($setting) |
||
| 668 | { |
||
| 669 | Deprecation::notice("4.0", "getSetting({$setting}) is deprecated"); |
||
| 670 | |||
| 671 | $settings = $this->getSettings(); |
||
| 672 | if (isset($settings) && count($settings) > 0) { |
||
| 673 | if (isset($settings[$setting])) { |
||
| 674 | return $settings[$setting]; |
||
| 675 | } |
||
| 676 | } |
||
| 677 | return ''; |
||
| 678 | } |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Get the path to the icon for this field type, relative to the site root. |
||
| 682 | * |
||
| 683 | * @return string |
||
| 684 | */ |
||
| 685 | public function getIcon() |
||
| 686 | { |
||
| 687 | return USERFORMS_DIR . '/images/' . strtolower($this->class) . '.png'; |
||
| 688 | } |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Return whether or not this field has addable options |
||
| 692 | * such as a dropdown field or radio set |
||
| 693 | * |
||
| 694 | * @return bool |
||
| 695 | */ |
||
| 696 | public function getHasAddableOptions() |
||
| 697 | { |
||
| 698 | return false; |
||
| 699 | } |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Return whether or not this field needs to show the extra |
||
| 703 | * options dropdown list |
||
| 704 | * |
||
| 705 | * @return bool |
||
| 706 | */ |
||
| 707 | public function showExtraOptions() |
||
| 708 | { |
||
| 709 | return true; |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
| 714 | * |
||
| 715 | * @return string |
||
| 716 | */ |
||
| 717 | 18 | public function getEscapedTitle() |
|
| 718 | { |
||
| 719 | 18 | return Convert::raw2xml($this->Title); |
|
| 720 | } |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
| 724 | * |
||
| 725 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
| 726 | * or groups |
||
| 727 | * |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | public function getFieldNumber() |
||
| 731 | { |
||
| 732 | // Check if exists |
||
| 733 | if (!$this->exists()) { |
||
| 734 | return null; |
||
| 735 | } |
||
| 736 | // Check parent |
||
| 737 | $form = $this->Parent(); |
||
| 738 | if (!$form || !$form->exists() || !($fields = $form->Fields())) { |
||
| 739 | return null; |
||
| 740 | } |
||
| 741 | |||
| 742 | $prior = 0; // Number of prior group at this level |
||
| 743 | $stack = array(); // Current stack of nested groups, where the top level = the page |
||
| 744 | foreach ($fields->map('ID', 'ClassName') as $id => $className) { |
||
| 745 | if ($className === 'EditableFormStep') { |
||
| 746 | $priorPage = empty($stack) ? $prior : $stack[0]; |
||
| 747 | $stack = array($priorPage + 1); |
||
| 748 | $prior = 0; |
||
| 749 | } elseif ($className === 'EditableFieldGroup') { |
||
| 750 | $stack[] = $prior + 1; |
||
| 751 | $prior = 0; |
||
| 752 | } elseif ($className === 'EditableFieldGroupEnd') { |
||
| 753 | $prior = array_pop($stack); |
||
| 754 | } |
||
| 755 | if ($id == $this->ID) { |
||
| 756 | return implode('.', $stack); |
||
| 757 | } |
||
| 758 | } |
||
| 759 | return null; |
||
| 760 | } |
||
| 761 | |||
| 762 | public function getCMSTitle() |
||
| 766 | |||
| 767 | /** |
||
| 768 | * @deprecated since version 4.0 |
||
| 769 | */ |
||
| 770 | public function getFieldName($field = false) |
||
| 771 | { |
||
| 772 | Deprecation::notice('4.0', "getFieldName({$field}) is deprecated"); |
||
| 773 | return ($field) ? "Fields[".$this->ID."][".$field."]" : "Fields[".$this->ID."]"; |
||
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 777 | * @deprecated since version 4.0 |
||
| 778 | */ |
||
| 779 | public function getSettingName($field) |
||
| 780 | { |
||
| 781 | Deprecation::notice('4.0', "getSettingName({$field}) is deprecated"); |
||
| 782 | $name = $this->getFieldName('CustomSettings'); |
||
| 783 | |||
| 784 | return $name . '[' . $field .']'; |
||
| 785 | } |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Append custom validation fields to the default 'Validation' |
||
| 789 | * section in the editable options view |
||
| 790 | * |
||
| 791 | * @return FieldList |
||
| 792 | */ |
||
| 793 | public function getFieldValidationOptions() |
||
| 794 | { |
||
| 795 | $fields = new FieldList( |
||
| 796 | CheckboxField::create('Required', _t('EditableFormField.REQUIRED', 'Is this field Required?')) |
||
| 797 | ->setDescription(_t('EditableFormField.REQUIRED_DESCRIPTION', 'Please note that conditional fields can\'t be required')), |
||
| 798 | TextField::create('CustomErrorMessage', _t('EditableFormField.CUSTOMERROR', 'Custom Error Message')) |
||
| 799 | ); |
||
| 800 | |||
| 801 | $this->extend('updateFieldValidationOptions', $fields); |
||
| 802 | |||
| 803 | return $fields; |
||
| 804 | } |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Return a FormField to appear on the front end. Implement on |
||
| 808 | * your subclass. |
||
| 809 | * |
||
| 810 | * @return FormField |
||
| 811 | */ |
||
| 812 | public function getFormField() |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Updates a formfield with extensions |
||
| 819 | * |
||
| 820 | * @param FormField $field |
||
| 821 | */ |
||
| 822 | 20 | public function doUpdateFormField($field) |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Updates a formfield with the additional metadata specified by this field |
||
| 831 | * |
||
| 832 | * @param FormField $field |
||
| 833 | */ |
||
| 834 | 20 | protected function updateFormField($field) |
|
| 835 | { |
||
| 836 | // set the error / formatting messages |
||
| 837 | 20 | $field->setCustomValidationMessage($this->getErrorMessage()->RAW()); |
|
| 838 | |||
| 839 | // set the right title on this field |
||
| 840 | 20 | if ($this->RightTitle) { |
|
| 841 | // Since this field expects raw html, safely escape the user data prior |
||
| 842 | 1 | $field->setRightTitle(Convert::raw2xml($this->RightTitle)); |
|
| 843 | 1 | } |
|
| 844 | |||
| 845 | // if this field is required add some |
||
| 846 | 20 | if ($this->Required) { |
|
| 847 | // Required validation can conflict so add the Required validation messages as input attributes |
||
| 848 | 3 | $errorMessage = $this->getErrorMessage()->HTML(); |
|
| 849 | 3 | $field->addExtraClass('requiredField'); |
|
| 850 | 3 | $field->setAttribute('data-rule-required', 'true'); |
|
| 851 | 3 | $field->setAttribute('data-msg-required', $errorMessage); |
|
| 852 | |||
| 853 | 3 | if ($identifier = UserDefinedForm::config()->required_identifier) { |
|
| 854 | 1 | $title = $field->Title() . " <span class='required-identifier'>". $identifier . "</span>"; |
|
| 855 | 1 | $field->setTitle($title); |
|
| 856 | 1 | } |
|
| 857 | 3 | } |
|
| 858 | |||
| 859 | // if this field has an extra class |
||
| 860 | 20 | if ($this->ExtraClass) { |
|
| 861 | 1 | $field->addExtraClass($this->ExtraClass); |
|
| 862 | 1 | } |
|
| 863 | |||
| 864 | // if ShowOnLoad is false hide the field |
||
| 865 | 20 | if (!$this->ShowOnLoad) { |
|
| 866 | $field->addExtraClass($this->ShowOnLoadNice()); |
||
| 867 | } |
||
| 868 | |||
| 869 | // if this field has a placeholder |
||
| 870 | 20 | if ($this->Placeholder) { |
|
| 871 | $field->setAttribute('placeholder', $this->Placeholder); |
||
| 872 | } |
||
| 873 | 20 | } |
|
| 874 | |||
| 875 | /** |
||
| 876 | * Return the instance of the submission field class |
||
| 877 | * |
||
| 878 | * @return SubmittedFormField |
||
| 879 | */ |
||
| 880 | 2 | public function getSubmittedFormField() |
|
| 884 | |||
| 885 | |||
| 886 | /** |
||
| 887 | * Show this form field (and its related value) in the reports and in emails. |
||
| 888 | * |
||
| 889 | * @return bool |
||
| 890 | */ |
||
| 891 | 2 | public function showInReports() |
|
| 895 | |||
| 896 | /** |
||
| 897 | * Return the error message for this field. Either uses the custom |
||
| 898 | * one (if provided) or the default SilverStripe message |
||
| 899 | * |
||
| 900 | * @return Varchar |
||
| 901 | */ |
||
| 902 | 20 | public function getErrorMessage() |
|
| 912 | |||
| 913 | /** |
||
| 914 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
| 915 | * to the field proper |
||
| 916 | * |
||
| 917 | * @param array $data Unserialised data |
||
| 918 | */ |
||
| 919 | 2 | public function migrateSettings($data) |
|
| 920 | { |
||
| 921 | // Map 'Show' / 'Hide' to boolean |
||
| 922 | 2 | if (isset($data['ShowOnLoad'])) { |
|
| 923 | 2 | $this->ShowOnLoad = $data['ShowOnLoad'] === '' || ($data['ShowOnLoad'] && $data['ShowOnLoad'] !== 'Hide'); |
|
| 924 | 2 | unset($data['ShowOnLoad']); |
|
| 925 | 2 | } |
|
| 926 | |||
| 927 | // Migrate all other settings |
||
| 928 | 2 | foreach ($data as $key => $value) { |
|
| 929 | 2 | if ($this->hasField($key)) { |
|
| 930 | 2 | $this->setField($key, $value); |
|
| 931 | 2 | } |
|
| 932 | 2 | } |
|
| 933 | 2 | } |
|
| 934 | |||
| 935 | /** |
||
| 936 | * Get the formfield to use when editing this inline in gridfield |
||
| 937 | * |
||
| 938 | * @param string $column name of column |
||
| 939 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
| 940 | * @return FormField |
||
| 941 | */ |
||
| 942 | public function getInlineClassnameField($column, $fieldClasses) |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Get the formfield to use when editing the title inline |
||
| 949 | * |
||
| 950 | * @param string $column |
||
| 951 | * @return FormField |
||
| 952 | */ |
||
| 953 | public function getInlineTitleField($column) |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Get the JS expression for selecting the holder for this field |
||
| 962 | * |
||
| 963 | * @return string |
||
| 964 | */ |
||
| 965 | public function getSelectorHolder() |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Returns only the JS identifier of a string, less the $(), which can be inserted elsewhere, for example when you |
||
| 972 | * want to perform selections on multiple selectors |
||
| 973 | * @return string |
||
| 974 | */ |
||
| 975 | 9 | public function getSelectorOnly() |
|
| 979 | |||
| 980 | /** |
||
| 981 | * Gets the JS expression for selecting the value for this field |
||
| 982 | * |
||
| 983 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
| 984 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
| 985 | * |
||
| 986 | * @return string |
||
| 987 | */ |
||
| 988 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) |
||
| 992 | |||
| 993 | /** |
||
| 994 | * @return string |
||
| 995 | */ |
||
| 996 | 2 | public function getSelectorFieldOnly() |
|
| 1000 | |||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Get the list of classes that can be selected and used as data-values |
||
| 1004 | * |
||
| 1005 | * @param $includeLiterals Set to false to exclude non-data fields |
||
| 1006 | * @return array |
||
| 1007 | */ |
||
| 1008 | 2 | public function getEditableFieldClasses($includeLiterals = true) |
|
| 1009 | { |
||
| 1010 | 2 | $classes = ClassInfo::getValidSubClasses('EditableFormField'); |
|
| 1011 | |||
| 1012 | // Remove classes we don't want to display in the dropdown. |
||
| 1013 | 2 | $editableFieldClasses = array(); |
|
| 1014 | 2 | foreach ($classes as $class) { |
|
| 1015 | // Skip abstract / hidden classes |
||
| 1016 | 2 | if (Config::inst()->get($class, 'abstract', Config::UNINHERITED) || Config::inst()->get($class, 'hidden') |
|
| 1017 | 2 | ) { |
|
| 1018 | 2 | continue; |
|
| 1019 | } |
||
| 1020 | |||
| 1021 | 2 | if (!$includeLiterals && Config::inst()->get($class, 'literal')) { |
|
| 1022 | continue; |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | 2 | $singleton = singleton($class); |
|
| 1026 | 2 | if (!$singleton->canCreate()) { |
|
| 1027 | continue; |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | 2 | $editableFieldClasses[$class] = $singleton->i18n_singular_name(); |
|
| 1031 | 2 | } |
|
| 1032 | |||
| 1033 | 2 | asort($editableFieldClasses); |
|
| 1034 | 2 | return $editableFieldClasses; |
|
| 1035 | } |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * @return EditableFormFieldValidator |
||
| 1039 | */ |
||
| 1040 | public function getCMSValidator() |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Determine effective display rules for this field. |
||
| 1048 | * |
||
| 1049 | * @return SS_List |
||
| 1050 | */ |
||
| 1051 | 11 | public function EffectiveDisplayRules() |
|
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Extracts info from DisplayRules into array so UserDefinedForm->buildWatchJS can run through it. |
||
| 1061 | * @return array|null |
||
| 1062 | */ |
||
| 1063 | 10 | public function formatDisplayRules() |
|
| 1064 | 1 | { |
|
| 1065 | 9 | $holderSelector = $this->getSelectorOnly(); |
|
| 1066 | $result = array( |
||
| 1067 | 9 | 'targetFieldID' => $holderSelector, |
|
| 1068 | 10 | 'conjunction' => $this->DisplayRulesConjunctionNice(), |
|
| 1069 | 9 | 'selectors' => array(), |
|
| 1070 | 9 | 'events' => array(), |
|
| 1071 | 9 | 'operations' => array(), |
|
| 1072 | 9 | 'initialState' => $this->ShowOnLoadNice(), |
|
| 1073 | 9 | 'view' => array(), |
|
| 1074 | 9 | 'opposite' => array(), |
|
| 1075 | 9 | ); |
|
| 1076 | // Check for field dependencies / default |
||
| 1077 | /** @var EditableCustomRule $rule */ |
||
| 1078 | 9 | foreach ($this->EffectiveDisplayRules() as $rule) { |
|
| 1079 | // Get the field which is effected |
||
| 1080 | /** @var EditableFormField $formFieldWatch */ |
||
| 1081 | 1 | $formFieldWatch = DataObject::get_by_id('EditableFormField', $rule->ConditionFieldID); |
|
| 1082 | // Skip deleted fields |
||
| 1083 | 1 | if (! $formFieldWatch) { |
|
| 1084 | continue; |
||
| 1085 | } |
||
| 1086 | 1 | $fieldToWatch = $formFieldWatch->getSelectorFieldOnly(); |
|
| 1087 | |||
| 1088 | 1 | $expression = $rule->buildExpression(); |
|
| 1089 | 1 | if (! in_array($fieldToWatch, $result['selectors'])) { |
|
| 1090 | 1 | $result['selectors'][] = $fieldToWatch; |
|
| 1091 | 1 | } |
|
| 1092 | 1 | if (! in_array($expression['event'], $result['events'])) { |
|
| 1093 | 1 | $result['events'][] = $expression['event']; |
|
| 1094 | 1 | } |
|
| 1095 | 1 | $result['operations'][] = $expression['operation']; |
|
| 1096 | |||
| 1097 | // View/Show should read |
||
| 1098 | 1 | $opposite = ($result['initialState'] === 'hide') ? 'show' : 'hide'; |
|
| 1099 | 1 | $result['view'] = $rule->toggleDisplayText($result['initialState']); |
|
| 1100 | 1 | $result['opposite'] = $rule->toggleDisplayText($opposite); |
|
| 1101 | 9 | } |
|
| 1102 | |||
| 1103 | 9 | return (count($result['selectors'])) ? $result : null; |
|
| 1104 | } |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Replaces the set DisplayRulesConjunction with their JS logical operators |
||
| 1108 | * @return string |
||
| 1109 | */ |
||
| 1110 | 9 | public function DisplayRulesConjunctionNice() |
|
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Replaces boolean ShowOnLoad with its JS string equivalent |
||
| 1117 | * @return string |
||
| 1118 | */ |
||
| 1119 | 9 | public function ShowOnLoadNice() |
|
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Returns whether this is of type EditableCheckBoxField |
||
| 1126 | * @return bool |
||
| 1127 | */ |
||
| 1128 | 2 | public function isCheckBoxField() |
|
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Returns whether this is of type EditableRadioField |
||
| 1135 | * @return bool |
||
| 1136 | */ |
||
| 1137 | 2 | public function isRadioField() |
|
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Determined is this is of type EditableCheckboxGroupField |
||
| 1144 | * @return bool |
||
| 1145 | */ |
||
| 1146 | public function isCheckBoxGroupField() |
||
| 1150 | } |
||
| 1151 |
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.