Total Complexity | 101 |
Total Lines | 679 |
Duplicated Lines | 0 % |
Changes | 14 | ||
Bugs | 4 | Features | 1 |
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 |
||
33 | class ActionsGridFieldItemRequest extends DataExtension |
||
34 | { |
||
35 | use Configurable; |
||
36 | |||
37 | /** |
||
38 | * @config |
||
39 | * @var boolean |
||
40 | */ |
||
41 | private static $enable_save_prev_next = true; |
||
42 | |||
43 | /** |
||
44 | * @config |
||
45 | * @var boolean |
||
46 | */ |
||
47 | private static $enable_save_close = true; |
||
48 | |||
49 | /** |
||
50 | * @config |
||
51 | * @var boolean |
||
52 | */ |
||
53 | private static $enable_delete_right = true; |
||
54 | |||
55 | /** |
||
56 | * @config |
||
57 | * @var boolean |
||
58 | */ |
||
59 | private static $enable_utils_prev_next = false; |
||
|
|||
60 | |||
61 | /** |
||
62 | * @var array Allowed controller actions |
||
63 | */ |
||
64 | private static $allowed_actions = array( |
||
65 | 'doSaveAndClose', |
||
66 | 'doSaveAndNext', |
||
67 | 'doSaveAndPrev', |
||
68 | 'doCustomAction', // For CustomAction |
||
69 | 'doCustomLink', // For CustomLink |
||
70 | ); |
||
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | protected function getAvailableActions($actions) |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Updates the detail form to include new form actions and buttons |
||
86 | * |
||
87 | * Reorganize things a bit |
||
88 | * |
||
89 | * @param Form The ItemEditForm object |
||
90 | */ |
||
91 | public function updateItemEditForm($form) |
||
92 | { |
||
93 | $itemRequest = $this->owner; |
||
94 | $record = $itemRequest->record; |
||
95 | if (!$record) { |
||
96 | $record = $form->getRecord(); |
||
97 | } |
||
98 | if (!$record) { |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | // We get the actions as defined on our record |
||
103 | $CMSActions = $record->getCMSActions(); |
||
104 | |||
105 | // address Silverstripe bug when SiteTree buttons are broken |
||
106 | // @link https://github.com/silverstripe/silverstripe-cms/issues/2702 |
||
107 | $CMSActions->setForm($form); |
||
108 | |||
109 | // We can the actions from the GridFieldDetailForm_ItemRequest |
||
110 | // It sets the Save and Delete buttons + Right Group |
||
111 | $actions = $form->Actions(); |
||
112 | |||
113 | // The default button group that contains the Save or Create action |
||
114 | // @link https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/extend_cms_interface/#extending-the-cms-actions |
||
115 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
116 | |||
117 | // If it doesn't exist, push to default group |
||
118 | if (!$MajorActions) { |
||
119 | $MajorActions = $actions; |
||
120 | } |
||
121 | |||
122 | // Push our actions that are otherwise ignored by SilverStripe |
||
123 | foreach ($CMSActions as $action) { |
||
124 | $actions->push($action); |
||
125 | } |
||
126 | |||
127 | // We create a Drop-Up menu afterwards because it may already exist in the $CMSActions |
||
128 | // and we don't want to duplicate it |
||
129 | $this->processDropUpMenu($actions); |
||
130 | |||
131 | // Add extension hook |
||
132 | $record->extend('onBeforeUpdateCMSActions', $actions); |
||
133 | |||
134 | $ActionMenus = $actions->fieldByName('ActionMenus'); |
||
135 | // Re-insert ActionMenus to make sure they always follow the buttons |
||
136 | if ($ActionMenus) { |
||
137 | $actions->remove($ActionMenus); |
||
138 | $actions->push($ActionMenus); |
||
139 | } |
||
140 | |||
141 | // We have a 4.4 setup, before that there was no RightGroup |
||
142 | $RightGroup = $actions->fieldByName('RightGroup'); |
||
143 | |||
144 | // Insert again to make sure our actions are properly placed after apply changes |
||
145 | if ($RightGroup) { |
||
146 | $actions->remove($RightGroup); |
||
147 | $actions->push($RightGroup); |
||
148 | } |
||
149 | |||
150 | if (self::config()->enable_save_close) { |
||
151 | $this->addSaveAndClose($actions, $record); |
||
152 | } |
||
153 | |||
154 | if (self::config()->enable_save_prev_next) { |
||
155 | $this->addSaveNextAndPrevious($actions, $record); |
||
156 | } |
||
157 | |||
158 | if (self::config()->enable_delete_right) { |
||
159 | $this->moveCancelAndDelete($actions, $record); |
||
160 | } |
||
161 | |||
162 | // Add extension hook |
||
163 | $record->extend('onAfterUpdateCMSActions', $actions); |
||
164 | } |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Collect all Drop-Up actions into a menu. |
||
169 | * @param FieldList $actions |
||
170 | * @return void |
||
171 | */ |
||
172 | protected function processDropUpMenu($actions) |
||
173 | { |
||
174 | // The Drop-up container may already exist |
||
175 | $dropUpContainer = $actions->fieldByName('ActionMenus.MoreOptions'); |
||
176 | foreach ($actions as $action) { |
||
177 | if ($action->hasMethod('getDropUp') && $action->getDropUp()) { |
||
178 | if (!$dropUpContainer) { |
||
179 | $dropUpContainer = $this->createDropUpContainer($actions); |
||
180 | } |
||
181 | $action->getContainerFieldList()->removeByName($action->getName()); |
||
182 | $dropUpContainer->push($action); |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Prepares a Drop-Up menu |
||
189 | * @param FieldList $actions |
||
190 | * @return Tab |
||
191 | */ |
||
192 | protected function createDropUpContainer($actions) |
||
193 | { |
||
194 | $rootTabSet = new TabSet('ActionMenus'); |
||
195 | $dropUpContainer = new Tab( |
||
196 | 'MoreOptions', |
||
197 | _t(__CLASS__ . '.MoreOptions', 'More options', 'Expands a view for more buttons') |
||
198 | ); |
||
199 | $dropUpContainer->addExtraClass('popover-actions-simulate'); |
||
200 | $rootTabSet->push($dropUpContainer); |
||
201 | $rootTabSet->addExtraClass('ss-ui-action-tabset action-menus noborder'); |
||
202 | |||
203 | $actions->insertBefore('RightGroup', $rootTabSet); |
||
204 | return $dropUpContainer; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param FieldList $actions |
||
209 | * @param DataObject $record |
||
210 | * @return void |
||
211 | */ |
||
212 | public function moveCancelAndDelete(FieldList $actions, DataObject $record) |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param DataObject $record |
||
256 | * @return int |
||
257 | */ |
||
258 | public function getCustomPreviousRecordID(DataObject $record) |
||
259 | { |
||
260 | if ($record->hasMethod('PrevRecord')) { |
||
261 | return $record->PrevRecord()->ID ?? 0; |
||
262 | } |
||
263 | return $this->owner->getPreviousRecordID(); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param DataObject $record |
||
268 | * @return int |
||
269 | */ |
||
270 | public function getCustomNextRecordID(DataObject $record) |
||
271 | { |
||
272 | if ($record->hasMethod('NextRecord')) { |
||
273 | return $record->NextRecord()->ID ?? 0; |
||
274 | } |
||
275 | return $this->owner->getNextRecordID(); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param FieldList $actions |
||
280 | * @param DataObject $record |
||
281 | * @return void |
||
282 | */ |
||
283 | public function addSaveNextAndPrevious(FieldList $actions, DataObject $record) |
||
284 | { |
||
285 | if (!$record->canEdit()) { |
||
286 | return; |
||
287 | } |
||
288 | if (!$record->ID) { |
||
289 | return; |
||
290 | } |
||
291 | |||
292 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
293 | |||
294 | // If it doesn't exist, push to default group |
||
295 | if (!$MajorActions) { |
||
296 | $MajorActions = $actions; |
||
297 | } |
||
298 | |||
299 | // TODO: check why with paginator, after the first page, getPreviousRecordID/getNextRecordID tend to not work properly |
||
300 | $getPreviousRecordID = $this->getCustomPreviousRecordID($record); |
||
301 | $getNextRecordID = $this->getCustomNextRecordID($record); |
||
302 | |||
303 | // Coupling for HasPrevNextUtils |
||
304 | if (Controller::has_curr()) { |
||
305 | $request = Controller::curr()->getRequest(); |
||
306 | $routeParams = $request->routeParams(); |
||
307 | $routeParams['PreviousRecordID'] = $getPreviousRecordID; |
||
308 | $routeParams['NextRecordID'] = $getNextRecordID; |
||
309 | $request->setRouteParams($routeParams); |
||
310 | } |
||
311 | |||
312 | if ($getPreviousRecordID) { |
||
313 | $doSaveAndPrev = new FormAction('doSaveAndPrev', _t('ActionsGridFieldItemRequest.SAVEANDPREVIOUS', 'Save and Previous')); |
||
314 | $doSaveAndPrev->addExtraClass($this->getBtnClassForRecord($record)); |
||
315 | $doSaveAndPrev->addExtraClass('font-icon-angle-double-left'); |
||
316 | $doSaveAndPrev->setUseButtonTag(true); |
||
317 | $MajorActions->push($doSaveAndPrev); |
||
318 | } |
||
319 | if ($getNextRecordID) { |
||
320 | $doSaveAndNext = new FormAction('doSaveAndNext', _t('ActionsGridFieldItemRequest.SAVEANDNEXT', 'Save and Next')); |
||
321 | $doSaveAndNext->addExtraClass($this->getBtnClassForRecord($record)); |
||
322 | $doSaveAndNext->addExtraClass('font-icon-angle-double-right'); |
||
323 | $doSaveAndNext->setUseButtonTag(true); |
||
324 | $MajorActions->push($doSaveAndNext); |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @param FieldList $actions |
||
330 | * @param DataObject $record |
||
331 | * @return void |
||
332 | */ |
||
333 | public function addSaveAndClose(FieldList $actions, DataObject $record) |
||
334 | { |
||
335 | if (!$record->canEdit()) { |
||
336 | return; |
||
337 | } |
||
338 | if (!$record->ID && !$record->canCreate()) { |
||
339 | return; |
||
340 | } |
||
341 | |||
342 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
343 | |||
344 | // If it doesn't exist, push to default group |
||
345 | if (!$MajorActions) { |
||
346 | $MajorActions = $actions; |
||
347 | } |
||
348 | |||
349 | if ($record->ID) { |
||
350 | $label = _t('ActionsGridFieldItemRequest.SAVEANDCLOSE', 'Save and Close'); |
||
351 | } else { |
||
352 | $label = _t('ActionsGridFieldItemRequest.CREATEANDCLOSE', 'Create and Close'); |
||
353 | } |
||
354 | $saveAndClose = new FormAction('doSaveAndClose', $label); |
||
355 | $saveAndClose->addExtraClass($this->getBtnClassForRecord($record)); |
||
356 | $saveAndClose->setAttribute('data-text-alternate', $label); |
||
357 | if ($record->ID) { |
||
358 | $saveAndClose->setAttribute('data-btn-alternate-add', 'btn-primary'); |
||
359 | $saveAndClose->setAttribute('data-btn-alternate-remove', 'btn-outline-primary'); |
||
360 | } |
||
361 | $saveAndClose->addExtraClass('font-icon-level-up'); |
||
362 | $saveAndClose->setUseButtonTag(true); |
||
363 | $MajorActions->push($saveAndClose); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * New and existing records have different classes |
||
368 | * |
||
369 | * @param DataObject $record |
||
370 | * @return string |
||
371 | */ |
||
372 | protected function getBtnClassForRecord(DataObject $record) |
||
373 | { |
||
374 | if ($record->ID) { |
||
375 | return 'btn-outline-primary'; |
||
376 | } |
||
377 | return 'btn-primary'; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Forward a given action to a DataObject |
||
382 | * |
||
383 | * Action must be declared in getCMSActions to be called |
||
384 | * |
||
385 | * @param string $action |
||
386 | * @param array $data |
||
387 | * @param Form $form |
||
388 | * @return HTTPResponse|DBHTMLText|string |
||
389 | */ |
||
390 | protected function forwardActionToRecord($action, $data = [], $form = null) |
||
391 | { |
||
392 | $controller = $this->getToplevelController(); |
||
393 | |||
394 | // We have an item request |
||
395 | $record = null; |
||
396 | if ($this->owner instanceof GridFieldDetailForm_ItemRequest) { |
||
397 | $record = $this->owner->record; |
||
398 | } elseif ($controller instanceof LeftAndMain) { |
||
399 | if (empty($data['ClassName']) || empty($data['ID'])) { |
||
400 | throw new Exception("Submitted data does not contain and ID and a ClassName"); |
||
401 | } |
||
402 | $record = DataObject::get_by_id($data['ClassName'], $data['ID']); |
||
403 | } elseif ($controller->hasMethod("getRecord")) { |
||
404 | $record = $controller->getRecord(); |
||
405 | } |
||
406 | |||
407 | if (!$record) { |
||
408 | throw new Exception("No record to handle the action $action on " . get_class($controller)); |
||
409 | } |
||
410 | $definedActions = $record->getCMSActions(); |
||
411 | // Check if the action is indeed available |
||
412 | $clickedAction = null; |
||
413 | if (!empty($definedActions)) { |
||
414 | foreach ($definedActions as $definedAction) { |
||
415 | $definedActionName = $definedAction->getName(); |
||
416 | if ($definedAction->hasMethod('actionName')) { |
||
417 | $definedActionName = $definedAction->actionName(); |
||
418 | } |
||
419 | if ($definedActionName == $action) { |
||
420 | $clickedAction = $definedAction; |
||
421 | } |
||
422 | } |
||
423 | } |
||
424 | if (!$clickedAction) { |
||
425 | $class = get_class($record); |
||
426 | $availableActions = implode(',', $this->getAvailableActions($definedActions)); |
||
427 | if (!$availableActions) { |
||
428 | $availableActions = "(no available actions, please check getCMSActions)"; |
||
429 | } |
||
430 | return $this->owner->httpError(403, 'Action not available on ' . $class . '. It must be one of : ' . $availableActions); |
||
431 | } |
||
432 | $message = null; |
||
433 | $error = false; |
||
434 | |||
435 | // Check record BEFORE the action |
||
436 | // It can be deleted by the action and it will return to the list |
||
437 | $isNewRecord = $record->ID == 0; |
||
438 | |||
439 | try { |
||
440 | $result = $record->$action($data, $form, $controller); |
||
441 | |||
442 | // We have a response |
||
443 | if ($result && $result instanceof HTTPResponse) { |
||
444 | return $result; |
||
445 | } |
||
446 | |||
447 | if ($result === false) { |
||
448 | // Result returned an error (false) |
||
449 | $error = true; |
||
450 | $message = _t( |
||
451 | 'ActionsGridFieldItemRequest.FAILED', |
||
452 | 'Action {action} failed on {name}', |
||
453 | array( |
||
454 | 'action' => $clickedAction->getTitle(), |
||
455 | 'name' => $record->i18n_singular_name(), |
||
456 | ) |
||
457 | ); |
||
458 | } elseif (is_string($result)) { |
||
459 | // Result is a message |
||
460 | $message = $result; |
||
461 | } |
||
462 | } catch (Exception $ex) { |
||
463 | $error = true; |
||
464 | $message = $ex->getMessage(); |
||
465 | } |
||
466 | |||
467 | // Build default message |
||
468 | if (!$message) { |
||
469 | $message = _t( |
||
470 | 'ActionsGridFieldItemRequest.DONE', |
||
471 | 'Action {action} was done on {name}', |
||
472 | array( |
||
473 | 'action' => $clickedAction->getTitle(), |
||
474 | 'name' => $record->i18n_singular_name(), |
||
475 | ) |
||
476 | ); |
||
477 | } |
||
478 | $status = 'good'; |
||
479 | if ($error) { |
||
480 | $status = 'bad'; |
||
481 | } |
||
482 | |||
483 | // Progressive actions return array with json data |
||
484 | if (method_exists($clickedAction, 'getProgressive') && $clickedAction->getProgressive()) { |
||
485 | $response = $controller->getResponse(); |
||
486 | $response->addHeader('Content-Type', 'application/json'); |
||
487 | if ($result) { |
||
488 | $response->setBody(json_encode($result)); |
||
489 | } |
||
490 | return $response; |
||
491 | } |
||
492 | |||
493 | // We don't have a form, simply return the result |
||
494 | if (!$form) { |
||
495 | if ($error) { |
||
496 | return $this->owner->httpError(403, $message); |
||
497 | } |
||
498 | return $message; |
||
499 | } |
||
500 | if (Director::is_ajax()) { |
||
501 | $controller = $this->getToplevelController(); |
||
502 | $controller->getResponse()->addHeader('X-Status', rawurlencode($message)); |
||
503 | if (method_exists($clickedAction, 'getShouldRefresh') && $clickedAction->getShouldRefresh()) { |
||
504 | $controller->getResponse()->addHeader('X-Reload', "true"); |
||
505 | } |
||
506 | // 4xx status makes a red box |
||
507 | if ($error) { |
||
508 | $controller->getResponse()->setStatusCode(400); |
||
509 | } |
||
510 | } else { |
||
511 | $form->sessionMessage($message, $status, ValidationResult::CAST_HTML); |
||
512 | } |
||
513 | // Redirect after action |
||
514 | return $this->redirectAfterAction($isNewRecord, $record); |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Handles custom links |
||
519 | * |
||
520 | * Use CustomLink with default behaviour to trigger this |
||
521 | * |
||
522 | * See: |
||
523 | * DefaultLink::getModelLink |
||
524 | * GridFieldCustomLink::getLink |
||
525 | * |
||
526 | * @param HTTPRequest $request |
||
527 | * @return HTTPResponse|DBHTMLText|string |
||
528 | */ |
||
529 | public function doCustomLink(HTTPRequest $request) |
||
530 | { |
||
531 | $action = $request->getVar('CustomLink'); |
||
532 | return $this->forwardActionToRecord($action); |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Handles custom actions |
||
537 | * |
||
538 | * Use CustomAction class to trigger this |
||
539 | * |
||
540 | * Nested actions are submitted like this |
||
541 | * [action_doCustomAction] => Array |
||
542 | * ( |
||
543 | * [doTestAction] => 1 |
||
544 | * ) |
||
545 | * |
||
546 | * @param array The form data |
||
547 | * @param Form The form object |
||
548 | * @return HTTPResponse|DBHTMLText|string |
||
549 | */ |
||
550 | public function doCustomAction($data, $form) |
||
551 | { |
||
552 | $action = key($data['action_doCustomAction']); |
||
553 | return $this->forwardActionToRecord($action, $data, $form); |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Saves the form and goes back to list view |
||
558 | * |
||
559 | * @param array The form data |
||
560 | * @param Form The form object |
||
561 | */ |
||
562 | public function doSaveAndClose($data, $form) |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Saves the form and goes back to the next item |
||
573 | * |
||
574 | * @param array The form data |
||
575 | * @param Form The form object |
||
576 | */ |
||
577 | public function doSaveAndNext($data, $form) |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * Saves the form and goes to the previous item |
||
600 | * |
||
601 | * @param array The form data |
||
602 | * @param Form The form object |
||
603 | */ |
||
604 | public function doSaveAndPrev($data, $form) |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Gets the top level controller. |
||
627 | * |
||
628 | * @return Controller |
||
629 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
630 | * because it is a protected method and not visible to a decorator! |
||
631 | */ |
||
632 | protected function getToplevelController() |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Gets the back link |
||
649 | * |
||
650 | * @return string |
||
651 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
652 | * because it is a protected method and not visible to a decorator! |
||
653 | */ |
||
654 | public function getBackLink() |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * Response object for this request after a successful save |
||
675 | * |
||
676 | * @param bool $isNewRecord True if this record was just created |
||
677 | * @param DataObject $record |
||
678 | * @return HTTPResponse|DBHTMLText|string |
||
679 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
680 | * because it is a protected method and not visible to a decorator! |
||
681 | */ |
||
682 | protected function redirectAfterAction($isNewRecord, $record = null) |
||
712 | } |
||
713 | } |
||
714 | } |
||
715 |