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:
| 1 | <?php |
||
| 6 | class UserFormFieldEditorExtension extends DataExtension |
||
|
|
|||
| 7 | { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private static $has_many = array( |
||
| 13 | 'Fields' => 'EditableFormField' |
||
| 14 | ); |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Adds the field editor to the page. |
||
| 18 | * |
||
| 19 | * @return FieldList |
||
| 20 | */ |
||
| 21 | 2 | public function updateCMSFields(FieldList $fields) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Gets the field editor, for adding and removing EditableFormFields. |
||
| 33 | * |
||
| 34 | * @return GridField |
||
| 35 | */ |
||
| 36 | 2 | public function getFieldEditorGrid() |
|
| 37 | { |
||
| 38 | 2 | Requirements::javascript(USERFORMS_DIR . '/javascript/FieldEditor.js'); |
|
| 39 | |||
| 40 | 2 | $fields = $this->owner->Fields(); |
|
| 41 | |||
| 42 | 2 | $this->createInitialFormStep(true); |
|
| 43 | |||
| 44 | 2 | $editableColumns = new GridFieldEditableColumns(); |
|
| 45 | 2 | $fieldClasses = singleton('EditableFormField')->getEditableFieldClasses(); |
|
| 46 | 2 | $editableColumns->setDisplayFields(array( |
|
| 47 | 'ClassName' => function ($record, $column, $grid) use ($fieldClasses) { |
||
| 48 | if ($record instanceof EditableFormField) { |
||
| 49 | return $record->getInlineClassnameField($column, $fieldClasses); |
||
| 50 | } |
||
| 51 | 2 | }, |
|
| 52 | 2 | 'Title' => function ($record, $column, $grid) { |
|
| 53 | if ($record instanceof EditableFormField) { |
||
| 54 | return $record->getInlineTitleField($column); |
||
| 55 | } |
||
| 56 | 2 | } |
|
| 57 | )); |
||
| 58 | |||
| 59 | 2 | $config = GridFieldConfig::create() |
|
| 60 | 2 | ->addComponents( |
|
| 61 | $editableColumns, |
||
| 62 | 2 | new GridFieldButtonRow(), |
|
| 63 | 2 | GridFieldAddClassesButton::create('EditableTextField') |
|
| 64 | 2 | ->setButtonName(_t('UserFormFieldEditorExtension.ADD_FIELD', 'Add Field')) |
|
| 65 | 2 | ->setButtonClass('ss-ui-action-constructive'), |
|
| 66 | 2 | GridFieldAddClassesButton::create('EditableFormStep') |
|
| 67 | 2 | ->setButtonName(_t('UserFormFieldEditorExtension.ADD_PAGE_BREAK', 'Add Page Break')), |
|
| 68 | 2 | GridFieldAddClassesButton::create(array('EditableFieldGroup', 'EditableFieldGroupEnd')) |
|
| 69 | 2 | ->setButtonName(_t('UserFormFieldEditorExtension.ADD_FIELD_GROUP', 'Add Field Group')), |
|
| 70 | 2 | new GridFieldEditButton(), |
|
| 71 | 2 | new GridFieldDeleteAction(), |
|
| 72 | 2 | new GridFieldToolbarHeader(), |
|
| 73 | 2 | new GridFieldOrderableRows('Sort'), |
|
| 74 | 2 | new GridFieldDetailForm() |
|
| 75 | ); |
||
| 76 | |||
| 77 | 2 | $fieldEditor = GridField::create( |
|
| 78 | 2 | 'Fields', |
|
| 79 | 2 | _t('UserDefinedForm.FIELDS', 'Fields'), |
|
| 80 | $fields, |
||
| 81 | 2 | $config |
|
| 82 | 2 | )->addExtraClass('uf-field-editor'); |
|
| 83 | |||
| 84 | 2 | return $fieldEditor; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * A UserForm must have at least one step. |
||
| 89 | * If no steps exist, create an initial step, and put all fields inside it. |
||
| 90 | * |
||
| 91 | * @param bool $force |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | 41 | public function createInitialFormStep($force = false) |
|
| 95 | { |
||
| 96 | // Only invoke once saved |
||
| 97 | 41 | if (!$this->owner->exists()) { |
|
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | // Check if first field is a step |
||
| 102 | 41 | $fields = $this->owner->Fields(); |
|
| 103 | 41 | $firstField = $fields->first(); |
|
| 104 | 41 | if ($firstField instanceof EditableFormStep) { |
|
| 105 | 10 | return; |
|
| 106 | } |
||
| 107 | |||
| 108 | // Don't create steps on write if there are no formfields, as this |
||
| 109 | // can create duplicate first steps during publish of new records |
||
| 110 | 41 | if (!$force && !$firstField) { |
|
| 111 | 41 | return; |
|
| 112 | } |
||
| 113 | |||
| 114 | // Re-apply sort to each field starting at 2 |
||
| 115 | 2 | $next = 2; |
|
| 116 | 2 | foreach ($fields as $field) { |
|
| 117 | 2 | $field->Sort = $next++; |
|
| 118 | 2 | $field->write(); |
|
| 119 | } |
||
| 120 | |||
| 121 | // Add step |
||
| 122 | 2 | $step = EditableFormStep::create(); |
|
| 123 | 2 | $step->Title = _t('EditableFormStep.TITLE_FIRST', 'First Page'); |
|
| 124 | 2 | $step->Sort = 1; |
|
| 125 | 2 | $step->write(); |
|
| 126 | 2 | $fields->add($step); |
|
| 127 | 2 | } |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Ensure that at least one page exists at the start |
||
| 131 | */ |
||
| 132 | 41 | public function onAfterWrite() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @see SiteTree::doPublish |
||
| 139 | * @param Page $original |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | 10 | public function onAfterPublish($original) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @see SiteTree::doUnpublish |
||
| 176 | * @param Page $page |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | 1 | public function onAfterUnpublish($page) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @see SiteTree::duplicate |
||
| 189 | * @param DataObject $newPage |
||
| 190 | * |
||
| 191 | * @return DataObject |
||
| 192 | */ |
||
| 193 | 1 | public function onAfterDuplicate($newPage) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @see SiteTree::getIsModifiedOnStage |
||
| 230 | * @param boolean $isModified |
||
| 231 | * |
||
| 232 | * @return boolean |
||
| 233 | */ |
||
| 234 | public function getIsModifiedOnStage($isModified) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @see SiteTree::doRevertToLive |
||
| 250 | * @param Page $page |
||
| 251 | * |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | 1 | public function onAfterRevertToLive($page) |
|
| 261 | } |
||
| 262 |
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.