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