| Total Complexity | 139 |
| Total Lines | 959 |
| Duplicated Lines | 0 % |
| Changes | 39 | ||
| Bugs | 13 | Features | 3 |
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 |
||
| 44 | class ActionsGridFieldItemRequest extends DataExtension |
||
| 45 | { |
||
| 46 | use Configurable; |
||
| 47 | use Extensible; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @config |
||
| 51 | * @var boolean |
||
| 52 | */ |
||
| 53 | private static $enable_save_prev_next = true; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @config |
||
| 57 | * @var boolean |
||
| 58 | */ |
||
| 59 | private static $enable_save_close = true; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @config |
||
| 63 | * @var boolean |
||
| 64 | */ |
||
| 65 | private static $enable_delete_right = true; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @config |
||
| 69 | * @var boolean |
||
| 70 | */ |
||
| 71 | private static $enable_utils_prev_next = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array<string> Allowed controller actions |
||
| 75 | */ |
||
| 76 | private static $allowed_actions = [ |
||
| 77 | 'doSaveAndClose', |
||
| 78 | 'doSaveAndNext', |
||
| 79 | 'doSaveAndPrev', |
||
| 80 | 'doCustomAction', // For CustomAction |
||
| 81 | 'doCustomLink', // For CustomLink |
||
| 82 | ]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param FieldList $actions |
||
| 86 | * @return array<string> |
||
| 87 | */ |
||
| 88 | protected function getAvailableActions($actions) |
||
| 89 | { |
||
| 90 | $list = []; |
||
| 91 | foreach ($actions as $action) { |
||
| 92 | if (is_a($action, CompositeField::class)) { |
||
| 93 | $list = array_merge($list, $this->getAvailableActions($action->FieldList())); |
||
| 94 | } else { |
||
| 95 | $list[] = $action->getName(); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | return $list; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * This module does not interact with the /schema/SearchForm endpoint |
||
| 103 | * and therefore all requests for these urls don't need any special treatement |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | protected function isSearchFormRequest(): bool |
||
| 108 | { |
||
| 109 | if (!Controller::has_curr()) { |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | $curr = Controller::curr(); |
||
| 113 | if ($curr) { |
||
| 114 | return str_contains($curr->getRequest()->getURL(), '/schema/SearchForm'); |
||
| 115 | } |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Called by CMSMain, typically in the CMS or in the SiteConfig admin |
||
| 121 | * CMSMain already uses getCMSActions so we are good to go with anything defined there |
||
| 122 | * |
||
| 123 | * @param Form $form |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | public function updateEditForm(Form $form) |
||
| 127 | { |
||
| 128 | // Ignore search form requests |
||
| 129 | if ($this->isSearchFormRequest()) { |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | |||
| 133 | $actions = $form->Actions(); |
||
| 134 | |||
| 135 | // We create a Drop-Up menu afterwards because it may already exist in the $CMSActions |
||
| 136 | // and we don't want to duplicate it |
||
| 137 | $this->processDropUpMenu($actions); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return FieldList|false |
||
| 142 | */ |
||
| 143 | public function recordCmsUtils() |
||
| 144 | { |
||
| 145 | $record = $this->owner->getRecord(); |
||
| 146 | if ($record->hasMethod('getCMSUtils')) { |
||
| 147 | $utils = $record->getCMSUtils(); |
||
| 148 | $this->extend('onCMSUtils', $utils, $record); |
||
| 149 | $record->extend('onCMSUtils', $utils); |
||
| 150 | return $utils; |
||
| 151 | } |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Called by GridField_ItemRequest |
||
| 157 | * GridField_ItemRequest defines its own set of actions so we need to add ours |
||
| 158 | * We add our custom save&close, save&next and other tweaks |
||
| 159 | * Actions can be made readonly after this extension point |
||
| 160 | * @param FieldList $actions |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function updateFormActions($actions) |
||
| 164 | { |
||
| 165 | // Ignore search form requests |
||
| 166 | if ($this->isSearchFormRequest()) { |
||
| 167 | return; |
||
| 168 | } |
||
| 169 | |||
| 170 | $record = $this->owner->getRecord(); |
||
| 171 | |||
| 172 | // We get the actions as defined on our record |
||
| 173 | /** @var FieldList $CMSActions */ |
||
| 174 | $CMSActions = $record->getCMSActions(); |
||
| 175 | |||
| 176 | // The default button group that contains the Save or Create action |
||
| 177 | // @link https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/extend_cms_interface/#extending-the-cms-actions |
||
| 178 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
| 179 | |||
| 180 | // If it doesn't exist, push to default group |
||
| 181 | if (!$MajorActions) { |
||
| 182 | $MajorActions = $actions; |
||
| 183 | } |
||
| 184 | |||
| 185 | // Push our actions that are otherwise ignored by SilverStripe |
||
| 186 | foreach ($CMSActions as $action) { |
||
| 187 | // Avoid duplicated actions (eg: when added by SilverStripe\Versioned\VersionedGridFieldItemRequest) |
||
| 188 | if ($actions->fieldByName($action->getName())) { |
||
| 189 | continue; |
||
| 190 | } |
||
| 191 | $actions->push($action); |
||
| 192 | } |
||
| 193 | |||
| 194 | // We create a Drop-Up menu afterwards because it may already exist in the $CMSActions |
||
| 195 | // and we don't want to duplicate it |
||
| 196 | $this->processDropUpMenu($actions); |
||
| 197 | |||
| 198 | // Add extension hook |
||
| 199 | $this->extend('onBeforeUpdateCMSActions', $actions, $record); |
||
| 200 | $record->extend('onBeforeUpdateCMSActions', $actions); |
||
| 201 | |||
| 202 | $ActionMenus = $actions->fieldByName('ActionMenus'); |
||
| 203 | // Re-insert ActionMenus to make sure they always follow the buttons |
||
| 204 | if ($ActionMenus) { |
||
| 205 | $actions->remove($ActionMenus); |
||
| 206 | $actions->push($ActionMenus); |
||
| 207 | } |
||
| 208 | |||
| 209 | // We have a 4.4 setup, before that there was no RightGroup |
||
| 210 | $RightGroup = $this->getRightGroupActions($actions); |
||
| 211 | |||
| 212 | // Insert again to make sure our actions are properly placed after apply changes |
||
| 213 | if ($RightGroup) { |
||
| 214 | $actions->remove($RightGroup); |
||
| 215 | $actions->push($RightGroup); |
||
| 216 | } |
||
| 217 | |||
| 218 | $opts = [ |
||
| 219 | 'save_close' => self::config()->enable_save_close, |
||
| 220 | 'save_prev_next' => self::config()->enable_save_prev_next, |
||
| 221 | 'delete_right' => self::config()->enable_delete_right, |
||
| 222 | ]; |
||
| 223 | if ($record->hasMethod('getCMSActionsOptions')) { |
||
| 224 | $opts = array_merge($opts, $record->getCMSActionsOptions()); |
||
| 225 | } |
||
| 226 | |||
| 227 | if ($opts['save_close']) { |
||
| 228 | $this->addSaveAndClose($actions, $record); |
||
| 229 | } |
||
| 230 | |||
| 231 | if ($opts['save_prev_next']) { |
||
| 232 | $this->addSaveNextAndPrevious($actions, $record); |
||
| 233 | } |
||
| 234 | |||
| 235 | if ($opts['delete_right']) { |
||
| 236 | $this->moveCancelAndDelete($actions, $record); |
||
| 237 | } |
||
| 238 | |||
| 239 | // Fix gridstate being lost when running custom actions |
||
| 240 | if (method_exists($this->owner, 'getStateManager')) { |
||
| 241 | $request = $this->owner->getRequest(); |
||
| 242 | $stateManager = $this->owner->getStateManager(); |
||
| 243 | $gridField = $this->owner->getGridField(); |
||
| 244 | $state = $stateManager->getStateFromRequest($gridField, $request); |
||
| 245 | $actions->push(new HiddenField($stateManager->getStateKey($gridField), null, $state)); |
||
| 246 | } |
||
| 247 | |||
| 248 | // Add extension hook |
||
| 249 | $this->extend('onAfterUpdateCMSActions', $actions, $record); |
||
| 250 | $record->extend('onAfterUpdateCMSActions', $actions); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Collect all Drop-Up actions into a menu. |
||
| 255 | * @param FieldList $actions |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | protected function processDropUpMenu($actions) |
||
| 259 | { |
||
| 260 | // The Drop-up container may already exist |
||
| 261 | /** @var ?Tab $dropUpContainer */ |
||
| 262 | $dropUpContainer = $actions->fieldByName('ActionMenus.MoreOptions'); |
||
| 263 | foreach ($actions as $action) { |
||
| 264 | //@phpstan-ignore-next-line |
||
| 265 | if ($action->hasMethod('getDropUp') && $action->getDropUp()) { |
||
| 266 | if (!$dropUpContainer) { |
||
| 267 | $dropUpContainer = $this->createDropUpContainer($actions); |
||
| 268 | } |
||
| 269 | $action->getContainerFieldList()->removeByName($action->getName()); |
||
| 270 | $dropUpContainer->push($action); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Prepares a Drop-Up menu |
||
| 277 | * @param FieldList $actions |
||
| 278 | * @return Tab |
||
| 279 | */ |
||
| 280 | protected function createDropUpContainer($actions) |
||
| 281 | { |
||
| 282 | $rootTabSet = new TabSet('ActionMenus'); |
||
| 283 | $dropUpContainer = new Tab( |
||
| 284 | 'MoreOptions', |
||
| 285 | _t(__CLASS__ . '.MoreOptions', 'More options', 'Expands a view for more buttons') |
||
| 286 | ); |
||
| 287 | $dropUpContainer->addExtraClass('popover-actions-simulate'); |
||
| 288 | $rootTabSet->push($dropUpContainer); |
||
| 289 | $rootTabSet->addExtraClass('ss-ui-action-tabset action-menus noborder'); |
||
| 290 | |||
| 291 | $actions->insertBefore('RightGroup', $rootTabSet); |
||
| 292 | |||
| 293 | return $dropUpContainer; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Check if a record can be edited/created/exists |
||
| 298 | * @param DataObject $record |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | protected function checkCan($record) |
||
| 302 | { |
||
| 303 | if (!$record->canEdit() || (!$record->ID && !$record->canCreate())) { |
||
| 304 | return false; |
||
| 305 | } |
||
| 306 | |||
| 307 | return true; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param FieldList $actions |
||
| 312 | * @param DataObject $record |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | public function moveCancelAndDelete(FieldList $actions, DataObject $record) |
||
| 316 | { |
||
| 317 | // We have a 4.4 setup, before that there was no RightGroup |
||
| 318 | $RightGroup = $actions->fieldByName('RightGroup'); |
||
| 319 | |||
| 320 | // Move delete at the end |
||
| 321 | $deleteAction = $actions->fieldByName('action_doDelete'); |
||
| 322 | if ($deleteAction) { |
||
| 323 | // Move at the end of the stack |
||
| 324 | $actions->remove($deleteAction); |
||
| 325 | $actions->push($deleteAction); |
||
| 326 | |||
| 327 | if (!$RightGroup) { |
||
| 328 | // Only necessary pre 4.4 |
||
| 329 | $deleteAction->addExtraClass('align-right'); |
||
| 330 | } |
||
| 331 | // Set custom title |
||
| 332 | if ($record->hasMethod('getDeleteButtonTitle')) { |
||
| 333 | //@phpstan-ignore-next-line |
||
| 334 | $deleteAction->setTitle($record->getDeleteButtonTitle()); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | // Move cancel at the end |
||
| 338 | $cancelButton = $actions->fieldByName('cancelbutton'); |
||
| 339 | if ($cancelButton) { |
||
| 340 | // Move at the end of the stack |
||
| 341 | $actions->remove($cancelButton); |
||
| 342 | $actions->push($cancelButton); |
||
| 343 | if (!$RightGroup) { |
||
| 344 | // Only necessary pre 4.4 |
||
| 345 | $cancelButton->addExtraClass('align-right'); |
||
| 346 | } |
||
| 347 | // Set custom titlte |
||
| 348 | if ($record->hasMethod('getCancelButtonTitle')) { |
||
| 349 | //@phpstan-ignore-next-line |
||
| 350 | $cancelButton->setTitle($record->getCancelButtonTitle()); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param DataObject $record |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function useCustomPrevNext(DataObject $record): bool |
||
| 360 | { |
||
| 361 | if (self::config()->enable_custom_prevnext) { |
||
| 362 | return $record->hasMethod('PrevRecord') && $record->hasMethod('NextRecord'); |
||
| 363 | } |
||
| 364 | return false; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param DataObject $record |
||
| 369 | * @return int |
||
| 370 | */ |
||
| 371 | public function getCustomPreviousRecordID(DataObject $record) |
||
| 372 | { |
||
| 373 | // This will overwrite state provided record |
||
| 374 | if ($this->useCustomPrevNext($record)) { |
||
| 375 | //@phpstan-ignore-next-line |
||
| 376 | return $record->PrevRecord()->ID ?? 0; |
||
| 377 | } |
||
| 378 | return $this->owner->getPreviousRecordID(); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param DataObject $record |
||
| 383 | * @return int |
||
| 384 | */ |
||
| 385 | public function getCustomNextRecordID(DataObject $record) |
||
| 386 | { |
||
| 387 | |||
| 388 | // This will overwrite state provided record |
||
| 389 | if ($this->useCustomPrevNext($record)) { |
||
| 390 | //@phpstan-ignore-next-line |
||
| 391 | return $record->NextRecord()->ID ?? 0; |
||
| 392 | } |
||
| 393 | return $this->owner->getNextRecordID(); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param FieldList $actions |
||
| 398 | * @return CompositeField|FieldList |
||
| 399 | */ |
||
| 400 | protected function getMajorActions(FieldList $actions) |
||
| 401 | { |
||
| 402 | /** @var ?CompositeField $MajorActions */ |
||
| 403 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
| 404 | |||
| 405 | // If it doesn't exist, push to default group |
||
| 406 | if (!$MajorActions) { |
||
| 407 | $MajorActions = $actions; |
||
| 408 | } |
||
| 409 | return $MajorActions; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param FieldList $actions |
||
| 414 | * @return CompositeField |
||
| 415 | */ |
||
| 416 | protected function getRightGroupActions(FieldList $actions) |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param FieldList $actions |
||
| 425 | * @param DataObject $record |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | public function addSaveNextAndPrevious(FieldList $actions, DataObject $record) |
||
| 429 | { |
||
| 430 | if (!$record->canEdit() || !$record->ID) { |
||
| 431 | return; |
||
| 432 | } |
||
| 433 | |||
| 434 | $MajorActions = $this->getMajorActions($actions); |
||
| 435 | |||
| 436 | // @link https://github.com/silverstripe/silverstripe-framework/issues/10742 |
||
| 437 | $getPreviousRecordID = $this->getCustomPreviousRecordID($record); |
||
| 438 | $getNextRecordID = $this->getCustomNextRecordID($record); |
||
| 439 | $isCustom = $this->useCustomPrevNext($record); |
||
| 440 | |||
| 441 | // Coupling for HasPrevNextUtils |
||
| 442 | if (Controller::has_curr()) { |
||
| 443 | $prevLink = $nextLink = null; |
||
| 444 | if (!$isCustom && $this->owner instanceof GridFieldDetailForm_ItemRequest) { |
||
| 445 | if ($getPreviousRecordID) { |
||
| 446 | $prevLink = $this->getPublicEditLinkForAdjacentRecord(-1); |
||
| 447 | } |
||
| 448 | if ($getNextRecordID) { |
||
| 449 | $nextLink = $this->getPublicEditLinkForAdjacentRecord(+1); |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | /** @var HTTPRequest $request */ |
||
| 454 | $request = Controller::curr()->getRequest(); |
||
| 455 | $routeParams = $request->routeParams(); |
||
| 456 | $recordClass = get_class($record); |
||
| 457 | $routeParams['cmsactions'][$recordClass]['PreviousRecordID'] = $getPreviousRecordID; |
||
| 458 | $routeParams['cmsactions'][$recordClass]['NextRecordID'] = $getNextRecordID; |
||
| 459 | $routeParams['cmsactions'][$recordClass]['PrevRecordLink'] = $prevLink; |
||
| 460 | $routeParams['cmsactions'][$recordClass]['NextRecordLink'] = $nextLink; |
||
| 461 | $request->setRouteParams($routeParams); |
||
| 462 | } |
||
| 463 | |||
| 464 | if ($getPreviousRecordID) { |
||
| 465 | $doSaveAndPrev = new FormAction('doSaveAndPrev', _t('ActionsGridFieldItemRequest.SAVEANDPREVIOUS', 'Save and Previous')); |
||
| 466 | $doSaveAndPrev->addExtraClass($this->getBtnClassForRecord($record)); |
||
| 467 | $doSaveAndPrev->addExtraClass('font-icon-angle-double-left btn-mobile-collapse'); |
||
| 468 | $doSaveAndPrev->setUseButtonTag(true); |
||
| 469 | $MajorActions->push($doSaveAndPrev); |
||
| 470 | } |
||
| 471 | if ($getNextRecordID) { |
||
| 472 | $doSaveAndNext = new FormAction('doSaveAndNext', _t('ActionsGridFieldItemRequest.SAVEANDNEXT', 'Save and Next')); |
||
| 473 | $doSaveAndNext->addExtraClass($this->getBtnClassForRecord($record)); |
||
| 474 | $doSaveAndNext->addExtraClass('font-icon-angle-double-right btn-mobile-collapse'); |
||
| 475 | $doSaveAndNext->setUseButtonTag(true); |
||
| 476 | $MajorActions->push($doSaveAndNext); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | public function getPublicEditLinkForAdjacentRecord(int $offset): ?string |
||
| 481 | { |
||
| 482 | $this->owner->getStateManager(); |
||
| 483 | $reflObject = new ReflectionObject($this->owner); |
||
| 484 | $reflMethod = $reflObject->getMethod('getEditLinkForAdjacentRecord'); |
||
| 485 | $reflMethod->setAccessible(true); |
||
| 486 | |||
| 487 | try { |
||
| 488 | return $reflMethod->invoke($this->owner, $offset); |
||
| 489 | } catch (Exception $e) { |
||
| 490 | return null; |
||
| 491 | } |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param FieldList $actions |
||
| 496 | * @param DataObject $record |
||
| 497 | * @return void |
||
| 498 | */ |
||
| 499 | public function addSaveAndClose(FieldList $actions, DataObject $record) |
||
| 500 | { |
||
| 501 | if (!$this->checkCan($record)) { |
||
| 502 | return; |
||
| 503 | } |
||
| 504 | |||
| 505 | $MajorActions = $this->getMajorActions($actions); |
||
| 506 | |||
| 507 | if ($record->ID) { |
||
| 508 | $label = _t('ActionsGridFieldItemRequest.SAVEANDCLOSE', 'Save and Close'); |
||
| 509 | } else { |
||
| 510 | $label = _t('ActionsGridFieldItemRequest.CREATEANDCLOSE', 'Create and Close'); |
||
| 511 | } |
||
| 512 | $saveAndClose = new FormAction('doSaveAndClose', $label); |
||
| 513 | $saveAndClose->addExtraClass($this->getBtnClassForRecord($record)); |
||
| 514 | $saveAndClose->setAttribute('data-text-alternate', $label); |
||
| 515 | if ($record->ID) { |
||
| 516 | $saveAndClose->setAttribute('data-btn-alternate-add', 'btn-primary'); |
||
| 517 | $saveAndClose->setAttribute('data-btn-alternate-remove', 'btn-outline-primary'); |
||
| 518 | } |
||
| 519 | $saveAndClose->addExtraClass('font-icon-level-up btn-mobile-collapse'); |
||
| 520 | $saveAndClose->setUseButtonTag(true); |
||
| 521 | $MajorActions->push($saveAndClose); |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * New and existing records have different classes |
||
| 526 | * |
||
| 527 | * @param DataObject $record |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | protected function getBtnClassForRecord(DataObject $record) |
||
| 531 | { |
||
| 532 | if ($record->ID) { |
||
| 533 | return 'btn-outline-primary'; |
||
| 534 | } |
||
| 535 | return 'btn-primary'; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $action |
||
| 540 | * @param array<FormField>|FieldList $definedActions |
||
| 541 | * @return mixed|CompositeField|null |
||
| 542 | */ |
||
| 543 | protected static function findAction($action, $definedActions) |
||
| 544 | { |
||
| 545 | $result = null; |
||
| 546 | |||
| 547 | foreach ($definedActions as $definedAction) { |
||
| 548 | if (is_a($definedAction, CompositeField::class)) { |
||
| 549 | $result = self::findAction($action, $definedAction->FieldList()); |
||
| 550 | if ($result) { |
||
| 551 | break; |
||
| 552 | } |
||
| 553 | } |
||
| 554 | |||
| 555 | $definedActionName = $definedAction->getName(); |
||
| 556 | |||
| 557 | if ($definedAction->hasMethod('actionName')) { |
||
| 558 | //@phpstan-ignore-next-line |
||
| 559 | $definedActionName = $definedAction->actionName(); |
||
| 560 | } |
||
| 561 | if ($definedActionName === $action) { |
||
| 562 | $result = $definedAction; |
||
| 563 | break; |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | return $result; |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Forward a given action to a DataObject |
||
| 572 | * |
||
| 573 | * Action must be declared in getCMSActions to be called |
||
| 574 | * |
||
| 575 | * @param string $action |
||
| 576 | * @param array<string,mixed> $data |
||
| 577 | * @param Form $form |
||
| 578 | * @return HTTPResponse|DBHTMLText|string |
||
| 579 | * @throws HTTPResponse_Exception |
||
| 580 | */ |
||
| 581 | protected function forwardActionToRecord($action, $data = [], $form = null) |
||
| 582 | { |
||
| 583 | $controller = $this->getToplevelController(); |
||
| 584 | |||
| 585 | // We have an item request or a controller that can provide a record |
||
| 586 | $record = null; |
||
| 587 | if ($this->owner->hasMethod('ItemEditForm')) { |
||
| 588 | // It's a request handler. Don't check for a specific class as it may be subclassed |
||
| 589 | $record = $this->owner->record; |
||
| 590 | } elseif ($controller->hasMethod('save_siteconfig')) { |
||
| 591 | // Check for any type of siteconfig controller |
||
| 592 | $record = SiteConfig::current_site_config(); |
||
| 593 | } elseif (!empty($data['ClassName']) && !empty($data['ID'])) { |
||
| 594 | $record = DataObject::get_by_id($data['ClassName'], $data['ID']); |
||
| 595 | } elseif ($controller->hasMethod("getRecord")) { |
||
| 596 | // LeftAndMain requires an id |
||
| 597 | if ($controller instanceof LeftAndMain && !empty($data['ID'])) { |
||
| 598 | $record = $controller->getRecord($data['ID']); |
||
| 599 | } elseif ($controller instanceof ModelAdmin) { |
||
| 600 | // Otherwise fallback to singleton |
||
| 601 | $record = DataObject::singleton($controller->getModelClass()); |
||
| 602 | } |
||
| 603 | } |
||
| 604 | |||
| 605 | if (!$record) { |
||
| 606 | throw new Exception("No record to handle the action $action on " . get_class($controller)); |
||
| 607 | } |
||
| 608 | $definedActions = $record->getCMSActions(); |
||
| 609 | // Check if the action is indeed available |
||
| 610 | $clickedAction = null; |
||
| 611 | if (!empty($definedActions)) { |
||
| 612 | $clickedAction = self::findAction($action, $definedActions); |
||
| 613 | } |
||
| 614 | if (!$clickedAction) { |
||
| 615 | $class = get_class($record); |
||
| 616 | $availableActions = implode(',', $this->getAvailableActions($definedActions)); |
||
| 617 | if (!$availableActions) { |
||
| 618 | $availableActions = "(no available actions, please check getCMSActions)"; |
||
| 619 | } |
||
| 620 | |||
| 621 | return $this->owner->httpError(403, sprintf( |
||
| 622 | 'Action not available on %s. It must be one of : %s', |
||
| 623 | $class, |
||
| 624 | $availableActions |
||
| 625 | )); |
||
| 626 | } |
||
| 627 | $message = null; |
||
| 628 | $error = false; |
||
| 629 | |||
| 630 | // Check record BEFORE the action |
||
| 631 | // It can be deleted by the action, and it will return to the list |
||
| 632 | $isNewRecord = $record->ID === 0; |
||
| 633 | |||
| 634 | try { |
||
| 635 | $result = $record->$action($data, $form, $controller); |
||
| 636 | |||
| 637 | // We have a response |
||
| 638 | if ($result instanceof HTTPResponse) { |
||
| 639 | return $result; |
||
| 640 | } |
||
| 641 | |||
| 642 | if ($result === false) { |
||
| 643 | // Result returned an error (false) |
||
| 644 | $error = true; |
||
| 645 | $message = _t( |
||
| 646 | 'ActionsGridFieldItemRequest.FAILED', |
||
| 647 | 'Action {action} failed on {name}', |
||
| 648 | ['action' => $clickedAction->getTitle(), 'name' => $record->i18n_singular_name()] |
||
| 649 | ); |
||
| 650 | } elseif (is_string($result)) { |
||
| 651 | // Result is a message |
||
| 652 | $message = $result; |
||
| 653 | } |
||
| 654 | } catch (Exception $ex) { |
||
| 655 | $result = null; |
||
| 656 | $error = true; |
||
| 657 | $message = $ex->getMessage(); |
||
| 658 | } |
||
| 659 | |||
| 660 | // Build default message |
||
| 661 | if (!$message) { |
||
| 662 | $message = _t( |
||
| 663 | 'ActionsGridFieldItemRequest.DONE', |
||
| 664 | 'Action {action} was done on {name}', |
||
| 665 | ['action' => $clickedAction->getTitle(), 'name' => $record->i18n_singular_name()] |
||
| 666 | ); |
||
| 667 | } |
||
| 668 | $status = 'good'; |
||
| 669 | if ($error) { |
||
| 670 | $status = 'bad'; |
||
| 671 | } |
||
| 672 | |||
| 673 | // Progressive actions return array with json data |
||
| 674 | //@phpstan-ignore-next-line |
||
| 675 | if (method_exists($clickedAction, 'getProgressive') && $clickedAction->getProgressive()) { |
||
| 676 | $response = $controller->getResponse(); |
||
| 677 | $response->addHeader('Content-Type', 'application/json'); |
||
| 678 | if ($result) { |
||
| 679 | $encodedResult = json_encode($result); |
||
| 680 | if (!$encodedResult) { |
||
| 681 | $encodedResult = json_last_error_msg(); |
||
| 682 | } |
||
| 683 | $response->setBody($encodedResult); |
||
| 684 | } |
||
| 685 | |||
| 686 | return $response; |
||
| 687 | } |
||
| 688 | |||
| 689 | // We don't have a form, simply return the result |
||
| 690 | if (!$form) { |
||
| 691 | if ($error) { |
||
| 692 | return $this->owner->httpError(403, $message); |
||
| 693 | } |
||
| 694 | |||
| 695 | return $message; |
||
| 696 | } |
||
| 697 | |||
| 698 | if (Director::is_ajax()) { |
||
| 699 | $controller->getResponse()->addHeader('X-Status', rawurlencode($message)); |
||
| 700 | |||
| 701 | //@phpstan-ignore-next-line |
||
| 702 | if (method_exists($clickedAction, 'getShouldRefresh') && $clickedAction->getShouldRefresh()) { |
||
| 703 | $controller->getResponse()->addHeader('X-Reload', "true"); |
||
| 704 | // Requires a ControllerURL as well, see https://github.com/silverstripe/silverstripe-admin/blob/a3aa41cea4c4df82050eef65ad5efcfae7bfde69/client/src/legacy/LeftAndMain.js#L773-L780 |
||
| 705 | $url = $controller->getReferer(); |
||
| 706 | $controller->getResponse()->addHeader('X-ControllerURL', $url); |
||
| 707 | } |
||
| 708 | // 4xx status makes a red box |
||
| 709 | if ($error) { |
||
| 710 | $controller->getResponse()->setStatusCode(400); |
||
| 711 | } |
||
| 712 | } else { |
||
| 713 | // If the controller support sessionMessage, use it instead of form |
||
| 714 | if ($controller->hasMethod('sessionMessage')) { |
||
| 715 | //@phpstan-ignore-next-line |
||
| 716 | $controller->sessionMessage($message, $status, ValidationResult::CAST_HTML); |
||
| 717 | } else { |
||
| 718 | $form->sessionMessage($message, $status, ValidationResult::CAST_HTML); |
||
| 719 | } |
||
| 720 | } |
||
| 721 | |||
| 722 | // Custom redirect |
||
| 723 | //@phpstan-ignore-next-line |
||
| 724 | if (method_exists($clickedAction, 'getRedirectURL') && $clickedAction->getRedirectURL()) { |
||
| 725 | $controller->getResponse()->addHeader('X-Reload', "true"); // we probably need a full ui refresh |
||
| 726 | //@phpstan-ignore-next-line |
||
| 727 | return $controller->redirect($clickedAction->getRedirectURL()); |
||
| 728 | } |
||
| 729 | |||
| 730 | // Redirect after action |
||
| 731 | return $this->redirectAfterAction($isNewRecord, $record); |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Handles custom links |
||
| 736 | * |
||
| 737 | * Use CustomLink with default behaviour to trigger this |
||
| 738 | * |
||
| 739 | * See: |
||
| 740 | * DefaultLink::getModelLink |
||
| 741 | * GridFieldCustomLink::getLink |
||
| 742 | * |
||
| 743 | * @param HTTPRequest $request |
||
| 744 | * @return HTTPResponse|DBHTMLText|string |
||
| 745 | * @throws Exception |
||
| 746 | */ |
||
| 747 | public function doCustomLink(HTTPRequest $request) |
||
| 748 | { |
||
| 749 | $action = $request->getVar('CustomLink'); |
||
| 750 | return $this->forwardActionToRecord($action); |
||
| 751 | } |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Handles custom actions |
||
| 755 | * |
||
| 756 | * Use CustomAction class to trigger this |
||
| 757 | * |
||
| 758 | * Nested actions are submitted like this |
||
| 759 | * [action_doCustomAction] => Array |
||
| 760 | * ( |
||
| 761 | * [doTestAction] => 1 |
||
| 762 | * ) |
||
| 763 | * |
||
| 764 | * @param array<string,mixed> $data The form data |
||
| 765 | * @param Form $form The form object |
||
| 766 | * @return HTTPResponse|DBHTMLText|string |
||
| 767 | * @throws Exception |
||
| 768 | */ |
||
| 769 | public function doCustomAction($data, $form) |
||
| 770 | { |
||
| 771 | $action = key($data['action_doCustomAction']); |
||
| 772 | return $this->forwardActionToRecord($action, $data, $form); |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Saves the form and goes back to list view |
||
| 777 | * |
||
| 778 | * @param array<string,mixed> $data The form data |
||
| 779 | * @param Form $form The form object |
||
| 780 | * @return HTTPResponse |
||
| 781 | */ |
||
| 782 | public function doSaveAndClose($data, $form) |
||
| 783 | { |
||
| 784 | $this->owner->doSave($data, $form); |
||
| 785 | // Redirect after save |
||
| 786 | $controller = $this->getToplevelController(); |
||
| 787 | |||
| 788 | $link = $this->getBackLink(); |
||
| 789 | |||
| 790 | // Doesn't seem to be needed anymore |
||
| 791 | // $link = $this->addGridState($link, $data); |
||
| 792 | |||
| 793 | $controller->getResponse()->addHeader("X-Pjax", "Content"); |
||
| 794 | |||
| 795 | return $controller->redirect($link); |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @param string $dir prev|next |
||
| 800 | * @param array<string,mixed> $data The form data |
||
| 801 | * @param Form|null $form |
||
| 802 | * @return void |
||
| 803 | */ |
||
| 804 | protected function doSaveAndAdjacent(string $dir, array $data, ?Form $form) |
||
| 805 | { |
||
| 806 | $record = $this->owner->record; |
||
| 807 | $this->owner->doSave($data, $form); |
||
| 808 | // Redirect after save |
||
| 809 | $controller = $this->getToplevelController(); |
||
| 810 | $controller->getResponse()->addHeader("X-Pjax", "Content"); |
||
| 811 | |||
| 812 | $class = get_class($record); |
||
| 813 | if (!$class) { |
||
| 814 | throw new Exception("Could not get class"); |
||
| 815 | } |
||
| 816 | |||
| 817 | $method = match ($dir) { |
||
| 818 | 'prev' => 'getCustomPreviousRecordID', |
||
| 819 | 'next' => 'getCustomNextRecordID', |
||
| 820 | }; |
||
| 821 | |||
| 822 | $offset = match ($dir) { |
||
| 823 | 'prev' => -1, |
||
| 824 | 'next' => +1, |
||
| 825 | }; |
||
| 826 | |||
| 827 | $adjRecordID = $this->$method($record); |
||
| 828 | |||
| 829 | /** @var ?DataObject $adj */ |
||
| 830 | $adj = $class::get()->byID($adjRecordID); |
||
| 831 | |||
| 832 | $useCustom = $this->useCustomPrevNext($record); |
||
| 833 | $link = $this->getPublicEditLinkForAdjacentRecord($offset); |
||
| 834 | if (!$link || $useCustom) { |
||
| 835 | $link = $this->owner->getEditLink($adjRecordID); |
||
| 836 | $link = $this->addGridState($link, $data); |
||
| 837 | } |
||
| 838 | |||
| 839 | // Link to a specific tab if set, see cms-actions.js |
||
| 840 | if ($adj && !empty($data['_activetab'])) { |
||
| 841 | $link .= sprintf('#%s', $data['_activetab']); |
||
| 842 | } |
||
| 843 | |||
| 844 | return $controller->redirect($link); |
||
| 845 | } |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Saves the form and goes back to the next item |
||
| 849 | * |
||
| 850 | * @param array<string,mixed> $data The form data |
||
| 851 | * @param Form $form The form object |
||
| 852 | * @return HTTPResponse |
||
| 853 | */ |
||
| 854 | public function doSaveAndNext($data, $form) |
||
| 855 | { |
||
| 856 | return $this->doSaveAndAdjacent('next', $data, $form); |
||
| 857 | } |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Saves the form and goes to the previous item |
||
| 861 | * |
||
| 862 | * @param array<string,mixed> $data The form data |
||
| 863 | * @param Form $form The form object |
||
| 864 | * @return HTTPResponse |
||
| 865 | */ |
||
| 866 | public function doSaveAndPrev($data, $form) |
||
| 867 | { |
||
| 868 | return $this->doSaveAndAdjacent('prev', $data, $form); |
||
| 869 | } |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Check if we can remove this safely |
||
| 873 | * @deprecated |
||
| 874 | * @param string $url |
||
| 875 | * @param array<mixed> $data |
||
| 876 | * @return string |
||
| 877 | */ |
||
| 878 | protected function addGridState($url, $data) |
||
| 890 | } |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Gets the top level controller. |
||
| 894 | * |
||
| 895 | * @return Controller |
||
| 896 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
| 897 | * because it is a protected method and not visible to a decorator! |
||
| 898 | */ |
||
| 899 | protected function getToplevelController() |
||
| 900 | { |
||
| 901 | if ($this->isLeftAndMain($this->owner)) { |
||
| 902 | return $this->owner; |
||
| 903 | } |
||
| 904 | if (!$this->owner->hasMethod("getController")) { |
||
| 905 | return Controller::curr(); |
||
| 906 | } |
||
| 907 | $controller = $this->owner->getController(); |
||
| 908 | while ($controller instanceof GridFieldDetailForm_ItemRequest) { |
||
| 909 | $controller = $controller->getController(); |
||
| 910 | } |
||
| 911 | |||
| 912 | return $controller; |
||
| 913 | } |
||
| 914 | |||
| 915 | /** |
||
| 916 | * @param Controller $controller |
||
| 917 | * @return boolean |
||
| 918 | */ |
||
| 919 | protected function isLeftAndMain($controller) |
||
| 922 | } |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Gets the back link |
||
| 926 | * |
||
| 927 | * @return string |
||
| 928 | */ |
||
| 929 | public function getBackLink() |
||
| 946 | } |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Response object for this request after a successful save |
||
| 950 | * |
||
| 951 | * @param bool $isNewRecord True if this record was just created |
||
| 952 | * @param DataObject $record |
||
| 953 | * @return HTTPResponse|DBHTMLText|string |
||
| 954 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
| 955 | * because it is a protected method and not visible to a decorator! |
||
| 956 | */ |
||
| 957 | protected function redirectAfterAction($isNewRecord, $record = null) |
||
| 1003 | } |
||
| 1004 | } |
||
| 1005 |
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