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 | 2 | public function getJsEventHandler() |
|
| 153 | { |
||
| 154 | 2 | return $this->jsEventHandler; |
|
| 155 | } |
||
| 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 | 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 | ), |
||
| 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 | )); |
||
| 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 | 44 | public function onBeforeWrite() |
|
| 352 | { |
||
| 353 | 44 | parent::onBeforeWrite(); |
|
| 354 | |||
| 355 | // Set a field name. |
||
| 356 | 44 | if (!$this->Name) { |
|
| 357 | // New random name |
||
| 358 | 26 | $this->Name = $this->generateName(); |
|
| 359 | 44 | } elseif ($this->Name === 'Field') { |
|
| 360 | throw new ValidationException('Field name cannot be "Field"'); |
||
| 361 | } |
||
| 362 | |||
| 363 | 44 | if (!$this->Sort && $this->ParentID) { |
|
| 364 | 41 | $parentID = $this->ParentID; |
|
| 365 | 41 | $this->Sort = EditableFormField::get() |
|
| 366 | 41 | ->filter('ParentID', $parentID) |
|
| 367 | 41 | ->max('Sort') + 1; |
|
| 368 | } |
||
| 369 | 44 | } |
|
| 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) |
| 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 | } |
||
| 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 | 17 | public function getEscapedTitle() |
|
| 718 | { |
||
| 719 | 17 | 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() |
||
| 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() |
||
| 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() |
||
| 813 | { |
||
| 814 | user_error("Please implement a getFormField() on your EditableFormClass ". $this->ClassName, E_USER_ERROR); |
||
| 815 | } |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Updates a formfield with extensions |
||
| 819 | * |
||
| 820 | * @param FormField $field |
||
| 821 | */ |
||
| 822 | 19 | public function doUpdateFormField($field) |
|
| 823 | { |
||
| 824 | 19 | $this->extend('beforeUpdateFormField', $field); |
|
| 825 | 19 | $this->updateFormField($field); |
|
| 826 | 19 | $this->extend('afterUpdateFormField', $field); |
|
| 827 | 19 | } |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Updates a formfield with the additional metadata specified by this field |
||
| 831 | * |
||
| 832 | * @param FormField $field |
||
| 833 | */ |
||
| 834 | 19 | protected function updateFormField($field) |
|
| 835 | { |
||
| 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 | 19 | 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) |
|
| 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) |
|
| 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 | 9 | public function formatDisplayRules() |
|
| 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.