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 | * @var array |
||
10 | */ |
||
11 | private static $has_many = array( |
||
12 | 'Fields' => 'EditableFormField' |
||
13 | ); |
||
14 | |||
15 | /** |
||
16 | * Adds the field editor to the page. |
||
17 | * |
||
18 | * @return FieldList |
||
19 | */ |
||
20 | public function updateCMSFields(FieldList $fields) { |
||
21 | $fieldEditor = $this->getFieldEditorGrid(); |
||
22 | |||
23 | $fields->insertAfter(new Tab('FormFields', _t('UserFormFieldEditorExtension.FORMFIELDS', 'Form Fields')), 'Main'); |
||
24 | $fields->addFieldToTab('Root.FormFields', $fieldEditor); |
||
25 | |||
26 | return $fields; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Gets the field editor, for adding and removing EditableFormFields. |
||
31 | * |
||
32 | * @return GridField |
||
33 | */ |
||
34 | public function getFieldEditorGrid() { |
||
35 | Requirements::javascript(USERFORMS_DIR . '/javascript/FieldEditor.js'); |
||
36 | |||
37 | $fields = $this->owner->Fields(); |
||
38 | |||
39 | $this->createInitialFormStep(true); |
||
40 | |||
41 | $editableColumns = new GridFieldEditableColumns(); |
||
42 | $fieldClasses = singleton('EditableFormField')->getEditableFieldClasses(); |
||
43 | $editableColumns->setDisplayFields(array( |
||
44 | 'ClassName' => function($record, $column, $grid) use ($fieldClasses) { |
||
45 | if($record instanceof EditableFormField) { |
||
46 | return $record->getInlineClassnameField($column, $fieldClasses); |
||
47 | } |
||
48 | }, |
||
49 | 'Title' => function($record, $column, $grid) { |
||
50 | if($record instanceof EditableFormField) { |
||
51 | return $record->getInlineTitleField($column); |
||
52 | } |
||
53 | } |
||
54 | )); |
||
55 | |||
56 | $config = GridFieldConfig::create() |
||
57 | ->addComponents( |
||
58 | $editableColumns, |
||
59 | new GridFieldButtonRow(), |
||
60 | GridFieldAddClassesButton::create('EditableTextField') |
||
61 | ->setButtonName(_t('UserFormFieldEditorExtension.ADD_FIELD', 'Add Field')) |
||
62 | ->setButtonClass('ss-ui-action-constructive'), |
||
63 | GridFieldAddClassesButton::create('EditableFormStep') |
||
64 | ->setButtonName(_t('UserFormFieldEditorExtension.ADD_PAGE_BREAK', 'Add Page Break')), |
||
65 | GridFieldAddClassesButton::create(array('EditableFieldGroup', 'EditableFieldGroupEnd')) |
||
66 | ->setButtonName(_t('UserFormFieldEditorExtension.ADD_FIELD_GROUP', 'Add Field Group')), |
||
67 | new GridFieldEditButton(), |
||
68 | new GridFieldDeleteAction(), |
||
69 | new GridFieldToolbarHeader(), |
||
70 | new GridFieldOrderableRows('Sort'), |
||
71 | new GridFieldDetailForm() |
||
72 | ); |
||
73 | |||
74 | $fieldEditor = GridField::create( |
||
75 | 'Fields', |
||
76 | _t('UserDefinedForm.FIELDS', 'Fields'), |
||
77 | $fields, |
||
78 | $config |
||
79 | )->addExtraClass('uf-field-editor'); |
||
80 | |||
81 | return $fieldEditor; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * A UserForm must have at least one step. |
||
86 | * If no steps exist, create an initial step, and put all fields inside it. |
||
87 | * |
||
88 | * @param bool $force |
||
89 | * @return void |
||
90 | */ |
||
91 | public function createInitialFormStep($force = false) { |
||
92 | // Only invoke once saved |
||
93 | if(!$this->owner->exists()) { |
||
94 | return; |
||
95 | } |
||
96 | |||
97 | // Check if first field is a step |
||
98 | $fields = $this->owner->Fields(); |
||
99 | $firstField = $fields->first(); |
||
100 | if($firstField instanceof EditableFormStep) { |
||
101 | return; |
||
102 | } |
||
103 | |||
104 | // Don't create steps on write if there are no formfields, as this |
||
105 | // can create duplicate first steps during publish of new records |
||
106 | if(!$force && !$firstField) { |
||
107 | return; |
||
108 | } |
||
109 | |||
110 | // Re-apply sort to each field starting at 2 |
||
111 | $next = 2; |
||
112 | foreach($fields as $field) { |
||
113 | $field->Sort = $next++; |
||
114 | $field->write(); |
||
115 | } |
||
116 | |||
117 | // Add step |
||
118 | $step = EditableFormStep::create(); |
||
119 | $step->Title = _t('EditableFormStep.TITLE_FIRST', 'First Page'); |
||
120 | $step->Sort = 1; |
||
121 | $step->write(); |
||
122 | $fields->add($step); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Ensure that at least one page exists at the start |
||
127 | */ |
||
128 | public function onAfterWrite() { |
||
131 | |||
132 | /** |
||
133 | * @see SiteTree::doPublish |
||
134 | * @param Page $original |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | public function onAfterPublish($original) { |
||
153 | |||
154 | /** |
||
155 | * @see SiteTree::doUnpublish |
||
156 | * @param Page $page |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function onAfterUnpublish($page) { |
||
161 | foreach($page->Fields() as $field) { |
||
162 | $field->doDeleteFromStage('Live'); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @see SiteTree::duplicate |
||
168 | * @param DataObject $newPage |
||
169 | * |
||
170 | * @return DataObject |
||
171 | */ |
||
172 | public function onAfterDuplicate($newPage) { |
||
173 | // List of EditableFieldGroups, where the |
||
174 | // key of the array is the ID of the old end group |
||
175 | $fieldGroups = array(); |
||
176 | foreach($this->owner->Fields() as $field) { |
||
177 | $newField = $field->duplicate(false); |
||
178 | $newField->ParentID = $newPage->ID; |
||
179 | $newField->ParentClass = $newPage->ClassName; |
||
180 | $newField->Version = 0; |
||
181 | $newField->write(); |
||
182 | |||
183 | // If we encounter a group start, record it for later use |
||
184 | if($field instanceof EditableFieldGroup) { |
||
185 | $fieldGroups[$field->EndID] = $newField; |
||
186 | } |
||
187 | |||
188 | // If we encounter an end group, link it back to the group start |
||
189 | if($field instanceof EditableFieldGroupEnd && isset($fieldGroups[$field->ID])) { |
||
190 | $groupStart = $fieldGroups[$field->ID]; |
||
191 | $groupStart->EndID = $newField->ID; |
||
192 | $groupStart->write(); |
||
193 | } |
||
194 | |||
195 | View Code Duplication | foreach ($field->DisplayRules() as $customRule) { |
|
196 | $newRule = $customRule->duplicate(false); |
||
197 | $newRule->ParentID = $newField->ID; |
||
198 | $newRule->Version = 0; |
||
199 | $newRule->write(); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | return $newPage; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @see SiteTree::getIsModifiedOnStage |
||
208 | * @param boolean $isModified |
||
209 | * |
||
210 | * @return boolean |
||
211 | */ |
||
212 | public function getIsModifiedOnStage($isModified) { |
||
224 | |||
225 | /** |
||
226 | * @see SiteTree::doRevertToLive |
||
227 | * @param Page $page |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | public function onAfterRevertToLive($page) { |
||
237 | } |
||
238 |
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.