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 |
||
| 16 | class EditableMultipleOptionField extends EditableFormField { |
||
|
|
|||
| 17 | |||
| 18 | /** |
||
| 19 | * Define this field as abstract (not inherited) |
||
| 20 | * |
||
| 21 | * @config |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | private static $abstract = true; |
||
| 25 | |||
| 26 | private static $has_many = array( |
||
| 27 | "Options" => "EditableOption" |
||
| 28 | ); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return FieldList |
||
| 32 | */ |
||
| 33 | public function getCMSFields() { |
||
| 34 | $fields = parent::getCMSFields(); |
||
| 35 | |||
| 36 | $editableColumns = new GridFieldEditableColumns(); |
||
| 37 | $editableColumns->setDisplayFields(array( |
||
| 38 | 'Title' => array( |
||
| 39 | 'title' => _t('EditableMultipleOptionField.TITLE', 'Title'), |
||
| 40 | 'callback' => function($record, $column, $grid) { |
||
| 41 | return TextField::create($column); |
||
| 42 | } |
||
| 43 | ), |
||
| 44 | 'Default' => array( |
||
| 45 | 'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'), |
||
| 46 | 'callback' => function($record, $column, $grid) { |
||
| 47 | return CheckboxField::create($column); |
||
| 48 | } |
||
| 49 | ) |
||
| 50 | )); |
||
| 51 | |||
| 52 | $optionsConfig = GridFieldConfig::create() |
||
| 53 | ->addComponents( |
||
| 54 | new GridFieldToolbarHeader(), |
||
| 55 | new GridFieldTitleHeader(), |
||
| 56 | $editableColumns, |
||
| 57 | new GridFieldButtonRow(), |
||
| 58 | new GridFieldAddNewInlineButton(), |
||
| 59 | new GridFieldDeleteAction() |
||
| 60 | ); |
||
| 61 | |||
| 62 | $optionsGrid = GridField::create( |
||
| 63 | 'Options', |
||
| 64 | _t('EditableFormField.CUSTOMOPTIONS', 'Options'), |
||
| 65 | $this->Options(), |
||
| 66 | $optionsConfig |
||
| 67 | ); |
||
| 68 | |||
| 69 | $fields->insertAfter(new Tab('Options', _t('EditableMultipleOptionField.OPTIONSTAB','Options')), 'Main'); |
||
| 70 | $fields->addFieldToTab('Root.Options', $optionsGrid); |
||
| 71 | |||
| 72 | return $fields; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Publishing Versioning support. |
||
| 77 | * |
||
| 78 | * When publishing it needs to handle copying across / publishing |
||
| 79 | * each of the individual field options |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
||
| 84 | $live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID"); |
||
| 85 | |||
| 86 | if($live) { |
||
| 87 | foreach($live as $option) { |
||
| 88 | $option->delete(); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | if($this->Options()) { |
||
| 93 | foreach($this->Options() as $option) { |
||
| 94 | $option->publish($fromStage, $toStage, $createNewVersion); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | parent::doPublish($fromStage, $toStage, $createNewVersion); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Unpublishing Versioning support |
||
| 103 | * |
||
| 104 | * When unpublishing the field it has to remove all options attached |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public function doDeleteFromStage($stage) { |
||
| 109 | // Remove options |
||
| 110 | $options = Versioned::get_by_stage('EditableOption', $stage) |
||
| 111 | ->filter('ParentID', $this->ID); |
||
| 112 | foreach($options as $option) { |
||
| 113 | $option->deleteFromStage($stage); |
||
| 114 | } |
||
| 115 | |||
| 116 | parent::doDeleteFromStage($stage); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Deletes all the options attached to this field before deleting the |
||
| 121 | * field. Keeps stray options from floating around |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function delete() { |
||
| 126 | $options = $this->Options(); |
||
| 127 | |||
| 128 | if($options) { |
||
| 129 | foreach($options as $option) { |
||
| 130 | $option->delete(); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | parent::delete(); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Duplicate a pages content. We need to make sure all the fields attached |
||
| 139 | * to that page go with it |
||
| 140 | * |
||
| 141 | * @return DataObject |
||
| 142 | */ |
||
| 143 | public function duplicate($doWrite = true) { |
||
| 144 | $clonedNode = parent::duplicate(); |
||
| 145 | |||
| 146 | View Code Duplication | foreach($this->Options() as $field) { |
|
| 147 | $newField = $field->duplicate(false); |
||
| 148 | $newField->ParentID = $clonedNode->ID; |
||
| 149 | $newField->Version = 0; |
||
| 150 | $newField->write(); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $clonedNode; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return whether or not this field has addable options such as a |
||
| 158 | * {@link EditableDropdownField} or {@link EditableRadioField} |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function getHasAddableOptions() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Gets map of field options suitable for use in a form |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | protected function getOptionsMap() { |
||
| 172 | $optionSet = $this->Options(); |
||
| 173 | $optionMap = $optionSet->map('EscapedTitle', 'Title'); |
||
| 174 | if($optionMap instanceof SS_Map) { |
||
| 175 | return $optionMap->toArray(); |
||
| 176 | } |
||
| 177 | return $optionMap; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns all default options |
||
| 182 | * |
||
| 183 | * @return SS_List |
||
| 184 | */ |
||
| 185 | protected function getDefaultOptions() { |
||
| 188 | } |
||
| 189 |
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.