Total Complexity | 70 |
Total Lines | 521 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like ActionsGridFieldItemRequest 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 ActionsGridFieldItemRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class ActionsGridFieldItemRequest extends DataExtension |
||
31 | { |
||
32 | use Configurable; |
||
33 | |||
34 | /** |
||
35 | * @config |
||
36 | * @var boolean |
||
37 | */ |
||
38 | private static $enable_save_prev_next = true; |
||
39 | |||
40 | /** |
||
41 | * @config |
||
42 | * @var boolean |
||
43 | */ |
||
44 | private static $enable_save_close = true; |
||
45 | |||
46 | /** |
||
47 | * @config |
||
48 | * @var boolean |
||
49 | */ |
||
50 | private static $enable_delete_right = true; |
||
51 | |||
52 | /** |
||
53 | * @config |
||
54 | * @var boolean |
||
55 | */ |
||
56 | private static $enable_utils_prev_next = false; |
||
|
|||
57 | |||
58 | /** |
||
59 | * @var array Allowed controller actions |
||
60 | */ |
||
61 | private static $allowed_actions = array( |
||
62 | 'doSaveAndClose', |
||
63 | 'doSaveAndNext', |
||
64 | 'doSaveAndPrev', |
||
65 | 'doCustomAction', // For CustomAction |
||
66 | 'doCustomLink', // For CustomLink |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * @return array |
||
71 | */ |
||
72 | protected function getAvailableActions($actions) |
||
73 | { |
||
74 | $list = []; |
||
75 | foreach ($actions as $action) { |
||
76 | $list[] = $action->getName(); |
||
77 | } |
||
78 | return $list; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Updates the detail form to include new form actions and buttons |
||
83 | * |
||
84 | * Reorganize things a bit |
||
85 | * |
||
86 | * @param Form The ItemEditForm object |
||
87 | */ |
||
88 | public function updateItemEditForm($form) |
||
89 | { |
||
90 | $itemRequest = $this->owner; |
||
91 | $record = $itemRequest->record; |
||
92 | if (!$record) { |
||
93 | $record = $form->getRecord(); |
||
94 | } |
||
95 | if (!$record) { |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | // We get the actions as defined on our record |
||
100 | $CMSActions = $record->getCMSActions(); |
||
101 | |||
102 | // We can the actions from the GridFieldDetailForm_ItemRequest |
||
103 | // It sets the Save and Delete buttons + Right Group |
||
104 | $actions = $form->Actions(); |
||
105 | |||
106 | // The default button group that contains the Save or Create action |
||
107 | // @link https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/extend_cms_interface/#extending-the-cms-actions |
||
108 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
109 | |||
110 | // If it doesn't exist, push to default group |
||
111 | if (!$MajorActions) { |
||
112 | $MajorActions = $actions; |
||
113 | } |
||
114 | |||
115 | // Push our actions that are otherwise ignored by SilverStripe |
||
116 | foreach ($CMSActions as $action) { |
||
117 | $actions->push($action); |
||
118 | } |
||
119 | |||
120 | // Add extension hook |
||
121 | $record->extend('onBeforeUpdateCMSActions', $actions); |
||
122 | |||
123 | // We have a 4.4 setup, before that there was no RightGroup |
||
124 | $RightGroup = $actions->fieldByName('RightGroup'); |
||
125 | |||
126 | // Insert again to make sure our actions are properly placed after apply changes |
||
127 | if ($RightGroup) { |
||
128 | $actions->remove($RightGroup); |
||
129 | $actions->push($RightGroup); |
||
130 | } |
||
131 | |||
132 | if (self::config()->enable_save_close) { |
||
133 | $this->addSaveAndClose($actions, $record); |
||
134 | } |
||
135 | |||
136 | if (self::config()->enable_save_prev_next) { |
||
137 | $this->addSaveNextAndPrevious($actions, $record); |
||
138 | } |
||
139 | |||
140 | if (self::config()->enable_delete_right) { |
||
141 | $this->moveCancelAndDelete($actions, $record); |
||
142 | } |
||
143 | |||
144 | // Add extension hook |
||
145 | $record->extend('onAfterUpdateCMSActions', $actions); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param FieldList $actions |
||
150 | * @param DataObject $record |
||
151 | * @return void |
||
152 | */ |
||
153 | public function moveCancelAndDelete(FieldList $actions, DataObject $record) |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param FieldList $actions |
||
195 | * @param DataObject $record |
||
196 | * @return void |
||
197 | */ |
||
198 | public function addSaveNextAndPrevious(FieldList $actions, DataObject $record) |
||
199 | { |
||
200 | if (!$record->canEdit()) { |
||
201 | return; |
||
202 | } |
||
203 | if (!$record->ID) { |
||
204 | return; |
||
205 | } |
||
206 | |||
207 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
208 | |||
209 | // If it doesn't exist, push to default group |
||
210 | if (!$MajorActions) { |
||
211 | $MajorActions = $actions; |
||
212 | } |
||
213 | |||
214 | $getPreviousRecordID = $this->owner->getPreviousRecordID(); |
||
215 | $getNextRecordID = $this->owner->getNextRecordID(); |
||
216 | |||
217 | // Coupling for HasPrevNextUtils |
||
218 | if (Controller::has_curr()) { |
||
219 | $request = Controller::curr()->getRequest(); |
||
220 | $routeParams = $request->routeParams(); |
||
221 | $routeParams['PreviousRecordID'] = $getPreviousRecordID; |
||
222 | $routeParams['NextRecordID'] = $getNextRecordID; |
||
223 | $request->setRouteParams($routeParams); |
||
224 | } |
||
225 | |||
226 | if ($getPreviousRecordID) { |
||
227 | $doSaveAndPrev = new FormAction('doSaveAndPrev', _t('ActionsGridFieldItemRequest.SAVEANDPREVIOUS', 'Save and Previous')); |
||
228 | $doSaveAndPrev->addExtraClass($this->getBtnClassForRecord($record)); |
||
229 | $doSaveAndPrev->addExtraClass('font-icon-angle-double-left'); |
||
230 | $doSaveAndPrev->setUseButtonTag(true); |
||
231 | $MajorActions->push($doSaveAndPrev); |
||
232 | } |
||
233 | if ($getNextRecordID) { |
||
234 | $doSaveAndNext = new FormAction('doSaveAndNext', _t('ActionsGridFieldItemRequest.SAVEANDNEXT', 'Save and Next')); |
||
235 | $doSaveAndNext->addExtraClass($this->getBtnClassForRecord($record)); |
||
236 | $doSaveAndNext->addExtraClass('font-icon-angle-double-right'); |
||
237 | $doSaveAndNext->setUseButtonTag(true); |
||
238 | $MajorActions->push($doSaveAndNext); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param FieldList $actions |
||
244 | * @param DataObject $record |
||
245 | * @return void |
||
246 | */ |
||
247 | public function addSaveAndClose(FieldList $actions, DataObject $record) |
||
248 | { |
||
249 | if (!$record->canEdit()) { |
||
250 | return; |
||
251 | } |
||
252 | |||
253 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
254 | |||
255 | // If it doesn't exist, push to default group |
||
256 | if (!$MajorActions) { |
||
257 | $MajorActions = $actions; |
||
258 | } |
||
259 | |||
260 | if ($record->ID) { |
||
261 | $label = _t('ActionsGridFieldItemRequest.SAVEANDCLOSE', 'Save and Close'); |
||
262 | } else { |
||
263 | $label = _t('ActionsGridFieldItemRequest.CREATEANDCLOSE', 'Create and Close'); |
||
264 | } |
||
265 | $saveAndClose = new FormAction('doSaveAndClose', $label); |
||
266 | $saveAndClose->addExtraClass($this->getBtnClassForRecord($record)); |
||
267 | $saveAndClose->addExtraClass('font-icon-level-up'); |
||
268 | $saveAndClose->setUseButtonTag(true); |
||
269 | $MajorActions->push($saveAndClose); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * New and existing records have different classes |
||
274 | * |
||
275 | * @param DataObject $record |
||
276 | * @return string |
||
277 | */ |
||
278 | protected function getBtnClassForRecord(DataObject $record) |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Forward a given action to a DataObject |
||
288 | * |
||
289 | * Action must be declared in getCMSActions to be called |
||
290 | * |
||
291 | * @param string $action |
||
292 | * @param array $data |
||
293 | * @param Form $form |
||
294 | * @return HTTPResponse|DBHTMLText |
||
295 | */ |
||
296 | protected function forwardActionToRecord($action, $data = [], $form = null) |
||
297 | { |
||
298 | $controller = $this->getToplevelController(); |
||
299 | $record = $this->owner->record; |
||
300 | $definedActions = $record->getCMSActions(); |
||
301 | // Check if the action is indeed available |
||
302 | $clickedAction = null; |
||
303 | if (!empty($definedActions)) { |
||
304 | foreach ($definedActions as $definedAction) { |
||
305 | $definedActionName = $definedAction->getName(); |
||
306 | if ($definedAction->hasMethod('actionName')) { |
||
307 | $definedActionName = $definedAction->actionName(); |
||
308 | } |
||
309 | if ($definedActionName == $action) { |
||
310 | $clickedAction = $definedAction; |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | if (!$clickedAction) { |
||
315 | $class = get_class($record); |
||
316 | return $this->owner->httpError(403, 'Action not available on ' . $class . '. It must be one of :' . implode(',', $this->getAvailableActions($definedActions))); |
||
317 | } |
||
318 | $message = null; |
||
319 | $error = false; |
||
320 | try { |
||
321 | $result = $record->$action($data, $form, $controller); |
||
322 | |||
323 | // We have a response |
||
324 | if ($result && $result instanceof HTTPResponse) { |
||
325 | return $result; |
||
326 | } |
||
327 | |||
328 | if ($result === false) { |
||
329 | // Result returned an error (false) |
||
330 | $error = true; |
||
331 | $message = _t( |
||
332 | 'ActionsGridFieldItemRequest.FAILED', |
||
333 | 'Action {action} failed on {name}', |
||
334 | array( |
||
335 | 'action' => $clickedAction->getTitle(), |
||
336 | 'name' => $record->i18n_singular_name(), |
||
337 | ) |
||
338 | ); |
||
339 | } elseif (is_string($result)) { |
||
340 | // Result is a message |
||
341 | $message = $result; |
||
342 | } |
||
343 | } catch (Exception $ex) { |
||
344 | $error = true; |
||
345 | $message = $ex->getMessage(); |
||
346 | } |
||
347 | $isNewRecord = $record->ID == 0; |
||
348 | // Build default message |
||
349 | if (!$message) { |
||
350 | $message = _t( |
||
351 | 'ActionsGridFieldItemRequest.DONE', |
||
352 | 'Action {action} was done on {name}', |
||
353 | array( |
||
354 | 'action' => $clickedAction->getTitle(), |
||
355 | 'name' => $record->i18n_singular_name(), |
||
356 | ) |
||
357 | ); |
||
358 | } |
||
359 | $status = 'good'; |
||
360 | if ($error) { |
||
361 | $status = 'bad'; |
||
362 | } |
||
363 | // We don't have a form, simply return the result |
||
364 | if (!$form) { |
||
365 | if ($error) { |
||
366 | return $this->owner->httpError(403, $message); |
||
367 | } |
||
368 | return $message; |
||
369 | } |
||
370 | if (Director::is_ajax()) { |
||
371 | $controller = $this->getToplevelController(); |
||
372 | $controller->getResponse()->addHeader('X-Status', rawurlencode($message)); |
||
373 | if (method_exists($clickedAction, 'getShouldRefresh') && $clickedAction->getShouldRefresh()) { |
||
374 | $controller->getResponse()->addHeader('X-Reload', true); |
||
375 | } |
||
376 | } else { |
||
377 | $form->sessionMessage($message, $status, ValidationResult::CAST_HTML); |
||
378 | } |
||
379 | // Redirect after action |
||
380 | return $this->redirectAfterAction($isNewRecord); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Handles custom links |
||
385 | * |
||
386 | * Use CustomLink with default behaviour to trigger this |
||
387 | * |
||
388 | * See: |
||
389 | * DefaultLink::getModelLink |
||
390 | * GridFieldCustomLink::getLink |
||
391 | * |
||
392 | * @param HTTPRequest $request |
||
393 | * @return HTTPResponse|DBHTMLText |
||
394 | */ |
||
395 | public function doCustomLink(HTTPRequest $request) |
||
396 | { |
||
397 | $action = $request->getVar('CustomLink'); |
||
398 | return $this->forwardActionToRecord($action); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Handles custom actions |
||
403 | * |
||
404 | * Use CustomAction class to trigger this |
||
405 | * |
||
406 | * @param array The form data |
||
407 | * @param Form The form object |
||
408 | * @return HTTPResponse|DBHTMLText |
||
409 | */ |
||
410 | public function doCustomAction($data, $form) |
||
411 | { |
||
412 | $action = key($data['action_doCustomAction']); |
||
413 | return $this->forwardActionToRecord($action, $data, $form); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Saves the form and goes back to list view |
||
418 | * |
||
419 | * @param array The form data |
||
420 | * @param Form The form object |
||
421 | */ |
||
422 | public function doSaveAndClose($data, $form) |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Saves the form and goes back to the next item |
||
433 | * |
||
434 | * @param array The form data |
||
435 | * @param Form The form object |
||
436 | */ |
||
437 | public function doSaveAndNext($data, $form) |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Saves the form and goes to the previous item |
||
460 | * |
||
461 | * @param array The form data |
||
462 | * @param Form The form object |
||
463 | */ |
||
464 | public function doSaveAndPrev($data, $form) |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Gets the top level controller. |
||
487 | * |
||
488 | * @return Controller |
||
489 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
490 | * because it is a protected method and not visible to a decorator! |
||
491 | */ |
||
492 | protected function getToplevelController() |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Gets the back link |
||
503 | * |
||
504 | * @return string |
||
505 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
506 | * because it is a protected method and not visible to a decorator! |
||
507 | */ |
||
508 | public function getBackLink() |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Response object for this request after a successful save |
||
529 | * |
||
530 | * @param bool $isNewRecord True if this record was just created |
||
531 | * @return HTTPResponse|DBHTMLText |
||
532 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
533 | * because it is a protected method and not visible to a decorator! |
||
534 | */ |
||
535 | protected function redirectAfterAction($isNewRecord) |
||
554 |