| Total Complexity | 85 |
| Total Lines | 759 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GridFieldDetailForm_ItemRequest 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 GridFieldDetailForm_ItemRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class GridFieldDetailForm_ItemRequest extends RequestHandler |
||
| 29 | { |
||
| 30 | |||
| 31 | private static $allowed_actions = array( |
||
| 32 | 'edit', |
||
| 33 | 'view', |
||
| 34 | 'ItemEditForm' |
||
| 35 | ); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * |
||
| 39 | * @var GridField |
||
| 40 | */ |
||
| 41 | protected $gridField; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * |
||
| 45 | * @var GridFieldDetailForm |
||
| 46 | */ |
||
| 47 | protected $component; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var DataObject |
||
| 51 | */ |
||
| 52 | protected $record; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * This represents the current parent RequestHandler (which does not necessarily need to be a Controller). |
||
| 56 | * It allows us to traverse the RequestHandler chain upwards to reach the Controller stack. |
||
| 57 | * |
||
| 58 | * @var RequestHandler |
||
| 59 | */ |
||
| 60 | protected $popupController; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $popupFormName; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var String |
||
| 70 | */ |
||
| 71 | protected $template = null; |
||
| 72 | |||
| 73 | private static $url_handlers = array( |
||
| 74 | '$Action!' => '$Action', |
||
| 75 | '' => 'edit', |
||
| 76 | ); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * |
||
| 80 | * @param GridField $gridField |
||
| 81 | * @param GridFieldDetailForm $component |
||
| 82 | * @param DataObject $record |
||
| 83 | * @param RequestHandler $requestHandler |
||
| 84 | * @param string $popupFormName |
||
| 85 | */ |
||
| 86 | public function __construct($gridField, $component, $record, $requestHandler, $popupFormName) |
||
| 94 | } |
||
| 95 | |||
| 96 | public function Link($action = null) |
||
| 97 | { |
||
| 98 | return Controller::join_links( |
||
| 99 | $this->gridField->Link('item'), |
||
| 100 | $this->record->ID ? $this->record->ID : 'new', |
||
| 101 | $action |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param HTTPRequest $request |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | public function view($request) |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param HTTPRequest $request |
||
| 135 | * @return mixed |
||
| 136 | */ |
||
| 137 | public function edit($request) |
||
| 138 | { |
||
| 139 | $controller = $this->getToplevelController(); |
||
| 140 | $form = $this->ItemEditForm(); |
||
| 141 | |||
| 142 | $return = $this->customise(array( |
||
| 143 | 'Backlink' => $controller->hasMethod('Backlink') ? $controller->Backlink() : $controller->Link(), |
||
| 144 | 'ItemEditForm' => $form, |
||
| 145 | ))->renderWith($this->getTemplates()); |
||
| 146 | |||
| 147 | if ($request->isAjax()) { |
||
| 148 | return $return; |
||
| 149 | } else { |
||
| 150 | // If not requested by ajax, we need to render it within the controller context+template |
||
| 151 | return $controller->customise(array( |
||
| 152 | // TODO CMS coupling |
||
| 153 | 'Content' => $return, |
||
| 154 | )); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Builds an item edit form. The arguments to getCMSFields() are the popupController and |
||
| 160 | * popupFormName, however this is an experimental API and may change. |
||
| 161 | * |
||
| 162 | * @todo In the future, we will probably need to come up with a tigher object representing a partially |
||
| 163 | * complete controller with gaps for extra functionality. This, for example, would be a better way |
||
| 164 | * of letting Security/login put its log-in form inside a UI specified elsewhere. |
||
| 165 | * |
||
| 166 | * @return Form|HTTPResponse |
||
| 167 | */ |
||
| 168 | public function ItemEditForm() |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return CompositeField Returns the right aligned toolbar group field along with its FormAction's |
||
| 276 | */ |
||
| 277 | private function getRightGroupField() |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Build the set of form field actions for this DataObject |
||
| 317 | * |
||
| 318 | * @return FieldList |
||
| 319 | */ |
||
| 320 | protected function getFormActions() |
||
| 321 | { |
||
| 322 | $canEdit = $this->record->canEdit(); |
||
| 323 | $canDelete = $this->record->canDelete(); |
||
| 324 | $actions = FieldList::create(); |
||
| 325 | if ($this->record->ID !== 0) { // existing record |
||
| 326 | if ($canEdit) { |
||
| 327 | $actions->push( |
||
| 328 | FormAction::create( |
||
| 329 | 'doSave', |
||
| 330 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Save', 'Save') |
||
| 331 | ) |
||
| 332 | ->setUseButtonTag(true) |
||
| 333 | ->addExtraClass('btn-primary font-icon-save') |
||
| 334 | ); |
||
| 335 | } |
||
| 336 | |||
| 337 | if ($canDelete) { |
||
| 338 | $actions->push( |
||
| 339 | FormAction::create( |
||
| 340 | 'doDelete', |
||
| 341 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Delete', 'Delete') |
||
| 342 | ) |
||
| 343 | ->setUseButtonTag(true) |
||
| 344 | ->addExtraClass('btn-outline-danger btn-hide-outline font-icon-trash-bin action--delete') |
||
| 345 | ); |
||
| 346 | } |
||
| 347 | |||
| 348 | $gridState = $this->getRequest()->requestVar('gridState'); |
||
| 349 | $this->gridField->getState(false)->setValue($gridState); |
||
| 350 | $actions->push(HiddenField::create('gridState', null, $gridState)); |
||
| 351 | |||
| 352 | $actions->push($this->getRightGroupField()); |
||
| 353 | } else { // adding new record |
||
| 354 | // Change the Save label to 'Create' |
||
| 355 | $actions->push( |
||
| 356 | FormAction::create( |
||
| 357 | 'doSave', |
||
| 358 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Create', 'Create') |
||
| 359 | ) |
||
| 360 | ->setUseButtonTag(true) |
||
| 361 | ->addExtraClass('btn-primary font-icon-plus-thin') |
||
| 362 | ); |
||
| 363 | |||
| 364 | // Add a Cancel link which is a button-like link and link back to one level up. |
||
| 365 | $crumbs = $this->Breadcrumbs(); |
||
| 366 | if ($crumbs && $crumbs->count() >= 2) { |
||
| 367 | $oneLevelUp = $crumbs->offsetGet($crumbs->count() - 2); |
||
| 368 | $text = sprintf( |
||
| 369 | "<a class=\"%s\" href=\"%s\">%s</a>", |
||
| 370 | "crumb btn btn-secondary cms-panel-link", // CSS classes |
||
| 371 | $oneLevelUp->Link, // url |
||
| 372 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.CancelBtn', 'Cancel') // label |
||
| 373 | ); |
||
| 374 | $actions->push(new LiteralField('cancelbutton', $text)); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | $this->extend('updateFormActions', $actions); |
||
| 379 | |||
| 380 | if (isset($rightGroup)) { |
||
| 381 | $actions->push($rightGroup); |
||
| 382 | } |
||
| 383 | |||
| 384 | return $actions; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Traverse the nested RequestHandlers until we reach something that's not GridFieldDetailForm_ItemRequest. |
||
| 389 | * This allows us to access the Controller responsible for invoking the top-level GridField. |
||
| 390 | * This should be equivalent to getting the controller off the top of the controller stack via Controller::curr(), |
||
| 391 | * but allows us to avoid accessing the global state. |
||
| 392 | * |
||
| 393 | * GridFieldDetailForm_ItemRequests are RequestHandlers, and as such they are not part of the controller stack. |
||
| 394 | * |
||
| 395 | * @return Controller |
||
| 396 | */ |
||
| 397 | protected function getToplevelController() |
||
| 398 | { |
||
| 399 | $c = $this->popupController; |
||
| 400 | while ($c && $c instanceof GridFieldDetailForm_ItemRequest) { |
||
| 401 | $c = $c->getController(); |
||
| 402 | } |
||
| 403 | return $c; |
||
| 404 | } |
||
| 405 | |||
| 406 | protected function getBackLink() |
||
| 407 | { |
||
| 408 | // TODO Coupling with CMS |
||
| 409 | $backlink = ''; |
||
| 410 | $toplevelController = $this->getToplevelController(); |
||
| 411 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
| 412 | if ($toplevelController->hasMethod('Backlink')) { |
||
| 413 | $backlink = $toplevelController->Backlink(); |
||
| 414 | } elseif ($this->popupController->hasMethod('Breadcrumbs')) { |
||
| 415 | $parents = $this->popupController->Breadcrumbs(false)->items; |
||
| 416 | $backlink = array_pop($parents)->Link; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | if (!$backlink) { |
||
| 420 | $backlink = $toplevelController->Link(); |
||
| 421 | } |
||
| 422 | |||
| 423 | return $backlink; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get the list of extra data from the $record as saved into it by |
||
| 428 | * {@see Form::saveInto()} |
||
| 429 | * |
||
| 430 | * Handles detection of falsey values explicitly saved into the |
||
| 431 | * DataObject by formfields |
||
| 432 | * |
||
| 433 | * @param DataObject $record |
||
| 434 | * @param SS_List $list |
||
| 435 | * @return array List of data to write to the relation |
||
| 436 | */ |
||
| 437 | protected function getExtraSavedData($record, $list) |
||
| 438 | { |
||
| 439 | // Skip extra data if not ManyManyList |
||
| 440 | if (!($list instanceof ManyManyList)) { |
||
| 441 | return null; |
||
| 442 | } |
||
| 443 | |||
| 444 | $data = array(); |
||
| 445 | foreach ($list->getExtraFields() as $field => $dbSpec) { |
||
| 446 | $savedField = "ManyMany[{$field}]"; |
||
| 447 | if ($record->hasField($savedField)) { |
||
| 448 | $data[$field] = $record->getField($savedField); |
||
| 449 | } |
||
| 450 | } |
||
| 451 | return $data; |
||
| 452 | } |
||
| 453 | |||
| 454 | public function doSave($data, $form) |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Goes to the previous record |
||
| 486 | * @param array $data The form data |
||
| 487 | * @param Form $form The Form object |
||
| 488 | * @return HTTPResponse |
||
| 489 | */ |
||
| 490 | public function doPrevious($data, $form) |
||
| 491 | { |
||
| 492 | $this->getToplevelController()->getResponse()->addHeader('X-Pjax', 'Content'); |
||
| 493 | $link = $this->getEditLink($this->getPreviousRecordID()); |
||
| 494 | return $this->redirect($link); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Goes to the next record |
||
| 499 | * @param array $data The form data |
||
| 500 | * @param Form $form The Form object |
||
| 501 | * @return HTTPResponse |
||
| 502 | */ |
||
| 503 | public function doNext($data, $form) |
||
| 504 | { |
||
| 505 | $this->getToplevelController()->getResponse()->addHeader('X-Pjax', 'Content'); |
||
| 506 | $link = $this->getEditLink($this->getNextRecordID()); |
||
| 507 | return $this->redirect($link); |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Creates a new record. If you're already creating a new record, |
||
| 512 | * this forces the URL to change. |
||
| 513 | * |
||
| 514 | * @param array $data The form data |
||
| 515 | * @param Form $form The Form object |
||
| 516 | * @return HTTPResponse |
||
| 517 | */ |
||
| 518 | public function doNew($data, $form) |
||
| 519 | { |
||
| 520 | return $this->redirect(Controller::join_links($this->gridField->Link('item'), 'new')); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Gets the edit link for a record |
||
| 525 | * |
||
| 526 | * @param int $id The ID of the record in the GridField |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public function getEditLink($id) |
||
| 530 | { |
||
| 531 | return Controller::join_links( |
||
| 532 | $this->gridField->Link(), |
||
| 533 | 'item', |
||
| 534 | $id, |
||
| 535 | '?gridState=' . urlencode($this->gridField->getState(false)->Value()) |
||
| 536 | ); |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @param int $offset The offset from the current record |
||
| 541 | * @return int|bool |
||
| 542 | */ |
||
| 543 | private function getAdjacentRecordID($offset) |
||
| 544 | { |
||
| 545 | $gridField = $this->getGridField(); |
||
| 546 | $gridStateStr = $this->getRequest()->requestVar('gridState'); |
||
| 547 | $state = $gridField->getState(false); |
||
| 548 | $state->setValue($gridStateStr); |
||
| 549 | $data = $state->getData(); |
||
| 550 | $paginator = $data->getData('GridFieldPaginator'); |
||
| 551 | if (!$paginator) { |
||
| 552 | return false; |
||
| 553 | } |
||
| 554 | |||
| 555 | $currentPage = $paginator->getData('currentPage'); |
||
| 556 | $itemsPerPage = $paginator->getData('itemsPerPage'); |
||
| 557 | |||
| 558 | $limit = $itemsPerPage + 2; |
||
| 559 | $limitOffset = max(0, $itemsPerPage * ($currentPage-1) -1); |
||
| 560 | |||
| 561 | $map = $gridField->getManipulatedList()->limit($limit, $limitOffset)->column('ID'); |
||
| 562 | $index = array_search($this->record->ID, $map); |
||
| 563 | return isset($map[$index+$offset]) ? $map[$index+$offset] : false; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Gets the ID of the previous record in the list. |
||
| 568 | * |
||
| 569 | * @return int |
||
| 570 | */ |
||
| 571 | public function getPreviousRecordID() |
||
| 572 | { |
||
| 573 | return $this->getAdjacentRecordID(-1); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Gets the ID of the next record in the list. |
||
| 578 | * |
||
| 579 | * @return int |
||
| 580 | */ |
||
| 581 | public function getNextRecordID() |
||
| 582 | { |
||
| 583 | return $this->getAdjacentRecordID(1); |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Response object for this request after a successful save |
||
| 588 | * |
||
| 589 | * @param bool $isNewRecord True if this record was just created |
||
| 590 | * @return HTTPResponse|DBHTMLText |
||
| 591 | */ |
||
| 592 | protected function redirectAfterSave($isNewRecord) |
||
| 593 | { |
||
| 594 | $controller = $this->getToplevelController(); |
||
| 595 | if ($isNewRecord) { |
||
| 596 | return $controller->redirect($this->Link()); |
||
| 597 | } elseif ($this->gridField->getList()->byID($this->record->ID)) { |
||
| 598 | // Return new view, as we can't do a "virtual redirect" via the CMS Ajax |
||
| 599 | // to the same URL (it assumes that its content is already current, and doesn't reload) |
||
| 600 | return $this->edit($controller->getRequest()); |
||
| 601 | } else { |
||
| 602 | // Changes to the record properties might've excluded the record from |
||
| 603 | // a filtered list, so return back to the main view if it can't be found |
||
| 604 | $url = $controller->getRequest()->getURL(); |
||
| 605 | $noActionURL = $controller->removeAction($url); |
||
| 606 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); |
||
| 607 | return $controller->redirect($noActionURL, 302); |
||
| 608 | } |
||
| 609 | } |
||
| 610 | |||
| 611 | public function httpError($errorCode, $errorMessage = null) |
||
| 612 | { |
||
| 613 | $controller = $this->getToplevelController(); |
||
| 614 | return $controller->httpError($errorCode, $errorMessage); |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Loads the given form data into the underlying dataobject and relation |
||
| 619 | * |
||
| 620 | * @param array $data |
||
| 621 | * @param Form $form |
||
| 622 | * @throws ValidationException On error |
||
| 623 | * @return DataObject Saved record |
||
| 624 | */ |
||
| 625 | protected function saveFormIntoRecord($data, $form) |
||
| 626 | { |
||
| 627 | $list = $this->gridField->getList(); |
||
| 628 | |||
| 629 | // Check object matches the correct classname |
||
| 630 | if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { |
||
| 631 | $newClassName = $data['ClassName']; |
||
| 632 | // The records originally saved attribute was overwritten by $form->saveInto($record) before. |
||
| 633 | // This is necessary for newClassInstance() to work as expected, and trigger change detection |
||
| 634 | // on the ClassName attribute |
||
| 635 | $this->record->setClassName($this->record->ClassName); |
||
| 636 | // Replace $record with a new instance |
||
| 637 | $this->record = $this->record->newClassInstance($newClassName); |
||
| 638 | } |
||
| 639 | |||
| 640 | // Save form and any extra saved data into this dataobject |
||
| 641 | $form->saveInto($this->record); |
||
| 642 | $this->record->write(); |
||
| 643 | $this->extend('onAfterSave', $this->record); |
||
| 644 | |||
| 645 | $extraData = $this->getExtraSavedData($this->record, $list); |
||
| 646 | $list->add($this->record, $extraData); |
||
| 647 | |||
| 648 | return $this->record; |
||
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @param array $data |
||
| 653 | * @param Form $form |
||
| 654 | * @return HTTPResponse |
||
| 655 | * @throws ValidationException |
||
| 656 | */ |
||
| 657 | public function doDelete($data, $form) |
||
| 658 | { |
||
| 659 | $title = $this->record->Title; |
||
| 660 | if (!$this->record->canDelete()) { |
||
| 661 | throw new ValidationException( |
||
| 662 | _t( |
||
| 663 | 'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.DeletePermissionsFailure', |
||
| 664 | "No delete permissions" |
||
| 665 | ) |
||
| 666 | ); |
||
| 667 | } |
||
| 668 | $this->record->delete(); |
||
| 669 | |||
| 670 | $message = _t( |
||
| 671 | 'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Deleted', |
||
| 672 | 'Deleted {type} {name}', |
||
| 673 | [ |
||
| 674 | 'type' => $this->record->i18n_singular_name(), |
||
| 675 | 'name' => htmlspecialchars($title, ENT_QUOTES) |
||
| 676 | ] |
||
| 677 | ); |
||
| 678 | |||
| 679 | $toplevelController = $this->getToplevelController(); |
||
| 680 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
| 681 | $backForm = $toplevelController->getEditForm(); |
||
| 682 | $backForm->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
||
| 683 | } else { |
||
| 684 | $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
||
| 685 | } |
||
| 686 | |||
| 687 | //when an item is deleted, redirect to the parent controller |
||
| 688 | $controller = $this->getToplevelController(); |
||
| 689 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh |
||
| 690 | |||
| 691 | return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @param string $template |
||
| 696 | * @return $this |
||
| 697 | */ |
||
| 698 | public function setTemplate($template) |
||
| 699 | { |
||
| 700 | $this->template = $template; |
||
| 701 | return $this; |
||
| 702 | } |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @return string |
||
| 706 | */ |
||
| 707 | public function getTemplate() |
||
| 708 | { |
||
| 709 | return $this->template; |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Get list of templates to use |
||
| 714 | * |
||
| 715 | * @return array |
||
| 716 | */ |
||
| 717 | public function getTemplates() |
||
| 718 | { |
||
| 719 | $templates = SSViewer::get_templates_by_class($this, '', __CLASS__); |
||
| 720 | // Prefer any custom template |
||
| 721 | if ($this->getTemplate()) { |
||
| 722 | array_unshift($templates, $this->getTemplate()); |
||
| 723 | } |
||
| 724 | return $templates; |
||
| 725 | } |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @return Controller |
||
| 729 | */ |
||
| 730 | public function getController() |
||
| 731 | { |
||
| 732 | return $this->popupController; |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @return GridField |
||
| 737 | */ |
||
| 738 | public function getGridField() |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @return DataObject |
||
| 745 | */ |
||
| 746 | public function getRecord() |
||
| 747 | { |
||
| 748 | return $this->record; |
||
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * CMS-specific functionality: Passes through navigation breadcrumbs |
||
| 753 | * to the template, and includes the currently edited record (if any). |
||
| 754 | * see {@link LeftAndMain->Breadcrumbs()} for details. |
||
| 755 | * |
||
| 756 | * @param boolean $unlinked |
||
| 757 | * @return ArrayList |
||
| 758 | */ |
||
| 759 | public function Breadcrumbs($unlinked = false) |
||
| 787 | } |
||
| 788 | } |
||
| 789 |
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