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