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