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