Total Complexity | 46 |
Total Lines | 269 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GridFieldSortableHeader 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.
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 GridFieldSortableHeader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class GridFieldSortableHeader extends AbstractGridFieldComponent implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider, GridField_StateProvider |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * See {@link setThrowExceptionOnBadDataType()} |
||
26 | * |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $throwExceptionOnBadDataType = true; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | public $fieldSorting = []; |
||
35 | |||
36 | /** |
||
37 | * Determine what happens when this component is used with a list that isn't {@link SS_Filterable}. |
||
38 | * |
||
39 | * - true: An exception is thrown |
||
40 | * - false: This component will be ignored - it won't make any changes to the GridField. |
||
41 | * |
||
42 | * By default, this is set to true so that it's clearer what's happening, but the predefined |
||
43 | * {@link GridFieldConfig} subclasses set this to false for flexibility. |
||
44 | * |
||
45 | * @param bool $throwExceptionOnBadDataType |
||
46 | * @return $this |
||
47 | */ |
||
48 | public function setThrowExceptionOnBadDataType($throwExceptionOnBadDataType) |
||
49 | { |
||
50 | $this->throwExceptionOnBadDataType = $throwExceptionOnBadDataType; |
||
51 | return $this; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * See {@link setThrowExceptionOnBadDataType()} |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | public function getThrowExceptionOnBadDataType() |
||
60 | { |
||
61 | return $this->throwExceptionOnBadDataType; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Check that this dataList is of the right data type. |
||
66 | * Returns false if it's a bad data type, and if appropriate, throws an exception. |
||
67 | * |
||
68 | * @param SS_List $dataList |
||
69 | * @return bool |
||
70 | */ |
||
71 | protected function checkDataType($dataList) |
||
72 | { |
||
73 | if ($dataList instanceof Sortable) { |
||
74 | return true; |
||
75 | } else { |
||
76 | if ($this->throwExceptionOnBadDataType) { |
||
77 | throw new LogicException( |
||
78 | static::class . " expects an SS_Sortable list to be passed to the GridField." |
||
79 | ); |
||
80 | } |
||
81 | return false; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Specify sorting with fieldname as the key, and actual fieldname to sort as value. |
||
87 | * Example: array("MyCustomTitle"=>"Title", "MyCustomBooleanField" => "ActualBooleanField") |
||
88 | * |
||
89 | * @param array $sorting |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function setFieldSorting($sorting) |
||
93 | { |
||
94 | $this->fieldSorting = $sorting; |
||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return array |
||
100 | */ |
||
101 | public function getFieldSorting() |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Returns the header row providing titles with sort buttons |
||
108 | * |
||
109 | * @param GridField $gridField |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getHTMLFragments($gridField) |
||
113 | { |
||
114 | $list = $gridField->getList(); |
||
115 | if (!$this->checkDataType($list)) { |
||
116 | return null; |
||
117 | } |
||
118 | /** @var Sortable $list */ |
||
119 | $forTemplate = new ArrayData([]); |
||
120 | $forTemplate->Fields = new ArrayList; |
||
|
|||
121 | |||
122 | $state = $this->getState($gridField); |
||
123 | $columns = $gridField->getColumns(); |
||
124 | $currentColumn = 0; |
||
125 | |||
126 | $schema = DataObject::getSchema(); |
||
127 | foreach ($columns as $columnField) { |
||
128 | $currentColumn++; |
||
129 | $metadata = $gridField->getColumnMetadata($columnField); |
||
130 | $fieldName = str_replace('.', '-', $columnField ?: ''); |
||
131 | $title = $metadata['title']; |
||
132 | |||
133 | if (isset($this->fieldSorting[$columnField]) && $this->fieldSorting[$columnField]) { |
||
134 | $columnField = $this->fieldSorting[$columnField]; |
||
135 | } |
||
136 | |||
137 | $allowSort = ($title && $list->canSortBy($columnField)); |
||
138 | |||
139 | if (!$allowSort && strpos((string) $columnField, '.') !== false) { |
||
140 | // we have a relation column with dot notation |
||
141 | // @see DataObject::relField for approximation |
||
142 | $parts = explode('.', (string) $columnField); |
||
143 | $tmpItem = singleton($list->dataClass()); |
||
144 | for ($idx = 0; $idx < sizeof($parts ?: []); $idx++) { |
||
145 | $methodName = $parts[$idx]; |
||
146 | if ($tmpItem instanceof SS_List) { |
||
147 | // It's impossible to sort on a HasManyList/ManyManyList |
||
148 | break; |
||
149 | } elseif ($tmpItem && method_exists($tmpItem, 'hasMethod') && $tmpItem->hasMethod($methodName)) { |
||
150 | // The part is a relation name, so get the object/list from it |
||
151 | $tmpItem = $tmpItem->$methodName(); |
||
152 | } elseif ($tmpItem instanceof DataObject |
||
153 | && $schema->fieldSpec($tmpItem, $methodName, DataObjectSchema::DB_ONLY) |
||
154 | ) { |
||
155 | // Else, if we've found a database field at the end of the chain, we can sort on it. |
||
156 | // If a method is applied further to this field (E.g. 'Cost.Currency') then don't try to sort. |
||
157 | $allowSort = $idx === sizeof($parts ?: []) - 1; |
||
158 | break; |
||
159 | } else { |
||
160 | // If neither method nor field, then unable to sort |
||
161 | break; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | if ($allowSort) { |
||
167 | $dir = 'asc'; |
||
168 | if ($state->SortColumn(null) == $columnField && $state->SortDirection('asc') == 'asc') { |
||
169 | $dir = 'desc'; |
||
170 | } |
||
171 | |||
172 | $field = GridField_FormAction::create( |
||
173 | $gridField, |
||
174 | 'SetOrder' . $fieldName, |
||
175 | $title, |
||
176 | "sort$dir", |
||
177 | ['SortColumn' => $columnField] |
||
178 | )->addExtraClass('grid-field__sort'); |
||
179 | |||
180 | if ($state->SortColumn(null) == $columnField) { |
||
181 | $field->addExtraClass('ss-gridfield-sorted'); |
||
182 | |||
183 | if ($state->SortDirection('asc') == 'asc') { |
||
184 | $field->addExtraClass('ss-gridfield-sorted-asc'); |
||
185 | } else { |
||
186 | $field->addExtraClass('ss-gridfield-sorted-desc'); |
||
187 | } |
||
188 | } |
||
189 | } else { |
||
190 | if ($currentColumn == count($columns ?: [])) { |
||
191 | $filter = $gridField->getConfig()->getComponentByType(GridFieldFilterHeader::class); |
||
192 | |||
193 | if ($filter && $filter->useLegacyFilterHeader && $filter->canFilterAnyColumns($gridField)) { |
||
194 | $field = new LiteralField( |
||
195 | $fieldName, |
||
196 | sprintf( |
||
197 | '<button type="button" name="showFilter" aria-label="%s" title="%s"' . |
||
198 | ' class="btn btn-secondary font-icon-search btn--no-text btn--icon-large grid-field__filter-open"></button>', |
||
199 | _t('SilverStripe\\Forms\\GridField\\GridField.OpenFilter', "Open search and filter"), |
||
200 | _t('SilverStripe\\Forms\\GridField\\GridField.OpenFilter', "Open search and filter") |
||
201 | ) |
||
202 | ); |
||
203 | } else { |
||
204 | $field = new LiteralField($fieldName, '<span class="non-sortable">' . $title . '</span>'); |
||
205 | } |
||
206 | } else { |
||
207 | $field = new LiteralField($fieldName, '<span class="non-sortable">' . $title . '</span>'); |
||
208 | } |
||
209 | } |
||
210 | $forTemplate->Fields->push($field); |
||
211 | } |
||
212 | |||
213 | $template = SSViewer::get_templates_by_class($this, '_Row', __CLASS__); |
||
214 | return [ |
||
215 | 'header' => $forTemplate->renderWith($template), |
||
216 | ]; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * |
||
221 | * @param GridField $gridField |
||
222 | * @return array |
||
223 | */ |
||
224 | public function getActions($gridField) |
||
225 | { |
||
226 | if (!$this->checkDataType($gridField->getList())) { |
||
227 | return []; |
||
228 | } |
||
229 | |||
230 | return ['sortasc', 'sortdesc']; |
||
231 | } |
||
232 | |||
233 | public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
||
250 | } |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Returns the manipulated (sorted) DataList. Field names will simply add an |
||
255 | * 'ORDER BY' clause, relation names will add appropriate joins to the |
||
256 | * {@link DataQuery} first. |
||
257 | * |
||
258 | * @param GridField $gridField |
||
259 | * @param SS_List $dataList |
||
260 | * @return SS_List |
||
261 | */ |
||
262 | public function getManipulatedData(GridField $gridField, SS_List $dataList) |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Extract state data from the parent gridfield |
||
279 | * @param GridField $gridField |
||
280 | * @return GridState_Data |
||
281 | */ |
||
282 | private function getState(GridField $gridField): GridState_Data |
||
285 | } |
||
286 | |||
287 | public function initDefaultState(GridState_Data $data): void |
||
290 | } |
||
291 | } |
||
292 |