| Total Complexity | 100 |
| Total Lines | 781 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 1 | Features | 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 |
||
| 29 | class GridFieldDetailForm_ItemRequest extends RequestHandler |
||
| 30 | { |
||
| 31 | use GridFieldStateAware; |
||
| 32 | |||
| 33 | private static $allowed_actions = [ |
||
| 34 | 'edit', |
||
| 35 | 'view', |
||
| 36 | 'ItemEditForm' |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The default form actions available to this item request |
||
| 41 | * |
||
| 42 | * e.g [ |
||
| 43 | * 'showPagination': true, |
||
| 44 | * 'showAdd': true |
||
| 45 | * ] |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private static $formActions = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * |
||
| 53 | * @var GridField |
||
| 54 | */ |
||
| 55 | protected $gridField; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * |
||
| 59 | * @var GridFieldDetailForm |
||
| 60 | */ |
||
| 61 | protected $component; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var DataObject |
||
| 65 | */ |
||
| 66 | protected $record; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * This represents the current parent RequestHandler (which does not necessarily need to be a Controller). |
||
| 70 | * It allows us to traverse the RequestHandler chain upwards to reach the Controller stack. |
||
| 71 | * |
||
| 72 | * @var RequestHandler |
||
| 73 | */ |
||
| 74 | protected $popupController; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $popupFormName; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var String |
||
| 84 | */ |
||
| 85 | protected $template = null; |
||
| 86 | |||
| 87 | private static $url_handlers = [ |
||
| 88 | '$Action!' => '$Action', |
||
| 89 | '' => 'edit', |
||
| 90 | ]; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * |
||
| 94 | * @param GridField $gridField |
||
| 95 | * @param GridFieldDetailForm $component |
||
| 96 | * @param DataObject $record |
||
| 97 | * @param RequestHandler $requestHandler |
||
| 98 | * @param string $popupFormName |
||
| 99 | */ |
||
| 100 | public function __construct($gridField, $component, $record, $requestHandler, $popupFormName) |
||
| 108 | } |
||
| 109 | |||
| 110 | public function Link($action = null) |
||
| 111 | { |
||
| 112 | return Controller::join_links( |
||
| 113 | $this->gridField->Link('item'), |
||
| 114 | $this->record->ID ? $this->record->ID : 'new', |
||
| 115 | $action |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param HTTPRequest $request |
||
| 121 | * @return mixed |
||
| 122 | */ |
||
| 123 | public function view($request) |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param HTTPRequest $request |
||
| 153 | * @return mixed |
||
| 154 | */ |
||
| 155 | public function edit($request) |
||
| 156 | { |
||
| 157 | $controller = $this->getToplevelController(); |
||
| 158 | $form = $this->ItemEditForm(); |
||
| 159 | |||
| 160 | $return = $this->customise([ |
||
| 161 | 'Backlink' => $controller->hasMethod('Backlink') ? $controller->Backlink() : $controller->Link(), |
||
| 162 | 'ItemEditForm' => $form, |
||
| 163 | ])->renderWith($this->getTemplates()); |
||
| 164 | |||
| 165 | if ($request->isAjax()) { |
||
| 166 | return $return; |
||
| 167 | } else { |
||
| 168 | // If not requested by ajax, we need to render it within the controller context+template |
||
| 169 | return $controller->customise([ |
||
| 170 | // TODO CMS coupling |
||
| 171 | 'Content' => $return, |
||
| 172 | ]); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Builds an item edit form. The arguments to getCMSFields() are the popupController and |
||
| 178 | * popupFormName, however this is an experimental API and may change. |
||
| 179 | * |
||
| 180 | * @todo In the future, we will probably need to come up with a tigher object representing a partially |
||
| 181 | * complete controller with gaps for extra functionality. This, for example, would be a better way |
||
| 182 | * of letting Security/login put its log-in form inside a UI specified elsewhere. |
||
| 183 | * |
||
| 184 | * @return Form|HTTPResponse |
||
| 185 | */ |
||
| 186 | public function ItemEditForm() |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Build context for verifying canCreate |
||
| 291 | * @see GridFieldAddNewButton::getHTMLFragments() |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | protected function getCreateContext() |
||
| 296 | { |
||
| 297 | $gridField = $this->gridField; |
||
| 298 | $context = []; |
||
| 299 | if ($gridField->getList() instanceof RelationList) { |
||
| 300 | $record = $gridField->getForm()->getRecord(); |
||
| 301 | if ($record && $record instanceof DataObject) { |
||
| 302 | $context['Parent'] = $record; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | return $context; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return CompositeField Returns the right aligned toolbar group field along with its FormAction's |
||
| 310 | */ |
||
| 311 | protected function getRightGroupField() |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Build the set of form field actions for this DataObject |
||
| 380 | * |
||
| 381 | * @return FieldList |
||
| 382 | */ |
||
| 383 | protected function getFormActions() |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Traverse the nested RequestHandlers until we reach something that's not GridFieldDetailForm_ItemRequest. |
||
| 441 | * This allows us to access the Controller responsible for invoking the top-level GridField. |
||
| 442 | * This should be equivalent to getting the controller off the top of the controller stack via Controller::curr(), |
||
| 443 | * but allows us to avoid accessing the global state. |
||
| 444 | * |
||
| 445 | * GridFieldDetailForm_ItemRequests are RequestHandlers, and as such they are not part of the controller stack. |
||
| 446 | * |
||
| 447 | * @return Controller |
||
| 448 | */ |
||
| 449 | protected function getToplevelController() |
||
| 456 | } |
||
| 457 | |||
| 458 | protected function getBackLink() |
||
| 459 | { |
||
| 460 | // TODO Coupling with CMS |
||
| 461 | $backlink = ''; |
||
| 462 | $toplevelController = $this->getToplevelController(); |
||
| 463 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
| 464 | if ($toplevelController->hasMethod('Backlink')) { |
||
| 465 | $backlink = $toplevelController->Backlink(); |
||
| 466 | } elseif ($this->popupController->hasMethod('Breadcrumbs')) { |
||
| 467 | $parents = $this->popupController->Breadcrumbs(false); |
||
| 468 | if ($parents && $parents = $parents->items) { |
||
| 469 | $backlink = array_pop($parents)->Link; |
||
| 470 | } |
||
| 471 | } |
||
| 472 | } |
||
| 473 | if (!$backlink) { |
||
| 474 | $backlink = $toplevelController->Link(); |
||
| 475 | } |
||
| 476 | |||
| 477 | return $backlink; |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get the list of extra data from the $record as saved into it by |
||
| 482 | * {@see Form::saveInto()} |
||
| 483 | * |
||
| 484 | * Handles detection of falsey values explicitly saved into the |
||
| 485 | * DataObject by formfields |
||
| 486 | * |
||
| 487 | * @param DataObject $record |
||
| 488 | * @param SS_List $list |
||
| 489 | * @return array List of data to write to the relation |
||
| 490 | */ |
||
| 491 | protected function getExtraSavedData($record, $list) |
||
| 492 | { |
||
| 493 | // Skip extra data if not ManyManyList |
||
| 494 | if (!($list instanceof ManyManyList)) { |
||
| 495 | return null; |
||
| 496 | } |
||
| 497 | |||
| 498 | $data = []; |
||
| 499 | foreach ($list->getExtraFields() as $field => $dbSpec) { |
||
| 500 | $savedField = "ManyMany[{$field}]"; |
||
| 501 | if ($record->hasField($savedField)) { |
||
| 502 | $data[$field] = $record->getField($savedField); |
||
| 503 | } |
||
| 504 | } |
||
| 505 | return $data; |
||
| 506 | } |
||
| 507 | |||
| 508 | public function doSave($data, $form) |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Gets the edit link for a record |
||
| 545 | * |
||
| 546 | * @param int $id The ID of the record in the GridField |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | public function getEditLink($id) |
||
| 550 | { |
||
| 551 | $link = Controller::join_links( |
||
| 552 | $this->gridField->Link(), |
||
| 553 | 'item', |
||
| 554 | $id |
||
| 555 | ); |
||
| 556 | |||
| 557 | return $this->getStateManager()->addStateToURL($this->gridField, $link); |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @param int $offset The offset from the current record |
||
| 562 | * @return int|bool |
||
| 563 | */ |
||
| 564 | private function getAdjacentRecordID($offset) |
||
| 565 | { |
||
| 566 | $gridField = $this->getGridField(); |
||
| 567 | $list = $gridField->getManipulatedList(); |
||
| 568 | $state = $gridField->getState(false); |
||
| 569 | $gridStateStr = $this->getStateManager()->getStateFromRequest($this->gridField, $this->getRequest()); |
||
| 570 | if (!empty($gridStateStr)) { |
||
| 571 | $state->setValue($gridStateStr); |
||
| 572 | } |
||
| 573 | $data = $state->getData(); |
||
| 574 | $paginator = $data->getData('GridFieldPaginator'); |
||
| 575 | if (!$paginator) { |
||
| 576 | return false; |
||
| 577 | } |
||
| 578 | |||
| 579 | $currentPage = $paginator->getData('currentPage'); |
||
| 580 | $itemsPerPage = $paginator->getData('itemsPerPage'); |
||
| 581 | |||
| 582 | $limit = $itemsPerPage + 2; |
||
| 583 | $limitOffset = max(0, $itemsPerPage * ($currentPage-1) -1); |
||
| 584 | |||
| 585 | $map = $list->limit($limit, $limitOffset)->column('ID'); |
||
| 586 | $index = array_search($this->record->ID, $map); |
||
| 587 | return isset($map[$index+$offset]) ? $map[$index+$offset] : false; |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Gets the ID of the previous record in the list. |
||
| 592 | * |
||
| 593 | * @return int |
||
| 594 | */ |
||
| 595 | public function getPreviousRecordID() |
||
| 596 | { |
||
| 597 | return $this->getAdjacentRecordID(-1); |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Gets the ID of the next record in the list. |
||
| 602 | * |
||
| 603 | * @return int |
||
| 604 | */ |
||
| 605 | public function getNextRecordID() |
||
| 606 | { |
||
| 607 | return $this->getAdjacentRecordID(1); |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Response object for this request after a successful save |
||
| 612 | * |
||
| 613 | * @param bool $isNewRecord True if this record was just created |
||
| 614 | * @return HTTPResponse|DBHTMLText |
||
| 615 | */ |
||
| 616 | protected function redirectAfterSave($isNewRecord) |
||
| 617 | { |
||
| 618 | $controller = $this->getToplevelController(); |
||
| 619 | if ($isNewRecord) { |
||
| 620 | return $controller->redirect($this->Link()); |
||
| 621 | } elseif ($this->gridField->getList()->byID($this->record->ID)) { |
||
| 622 | // Return new view, as we can't do a "virtual redirect" via the CMS Ajax |
||
| 623 | // to the same URL (it assumes that its content is already current, and doesn't reload) |
||
| 624 | return $this->edit($controller->getRequest()); |
||
| 625 | } else { |
||
| 626 | // Changes to the record properties might've excluded the record from |
||
| 627 | // a filtered list, so return back to the main view if it can't be found |
||
| 628 | $url = $controller->getRequest()->getURL(); |
||
| 629 | $noActionURL = $controller->removeAction($url); |
||
| 630 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); |
||
| 631 | return $controller->redirect($noActionURL, 302); |
||
| 632 | } |
||
| 633 | } |
||
| 634 | |||
| 635 | public function httpError($errorCode, $errorMessage = null) |
||
| 636 | { |
||
| 637 | $controller = $this->getToplevelController(); |
||
| 638 | return $controller->httpError($errorCode, $errorMessage); |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Loads the given form data into the underlying dataobject and relation |
||
| 643 | * |
||
| 644 | * @param array $data |
||
| 645 | * @param Form $form |
||
| 646 | * @throws ValidationException On error |
||
| 647 | * @return DataObject Saved record |
||
| 648 | */ |
||
| 649 | protected function saveFormIntoRecord($data, $form) |
||
| 650 | { |
||
| 651 | $list = $this->gridField->getList(); |
||
| 652 | |||
| 653 | // Check object matches the correct classname |
||
| 654 | if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { |
||
| 655 | $newClassName = $data['ClassName']; |
||
| 656 | // The records originally saved attribute was overwritten by $form->saveInto($record) before. |
||
| 657 | // This is necessary for newClassInstance() to work as expected, and trigger change detection |
||
| 658 | // on the ClassName attribute |
||
| 659 | $this->record->setClassName($this->record->ClassName); |
||
| 660 | // Replace $record with a new instance |
||
| 661 | $this->record = $this->record->newClassInstance($newClassName); |
||
| 662 | } |
||
| 663 | |||
| 664 | // Save form and any extra saved data into this dataobject. |
||
| 665 | // Set writeComponents = true to write has-one relations / join records |
||
| 666 | $form->saveInto($this->record); |
||
| 667 | // https://github.com/silverstripe/silverstripe-assets/issues/365 |
||
| 668 | $this->record->write(); |
||
| 669 | $this->extend('onAfterSave', $this->record); |
||
| 670 | |||
| 671 | $extraData = $this->getExtraSavedData($this->record, $list); |
||
| 672 | $list->add($this->record, $extraData); |
||
| 673 | |||
| 674 | return $this->record; |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @param array $data |
||
| 679 | * @param Form $form |
||
| 680 | * @return HTTPResponse |
||
| 681 | * @throws ValidationException |
||
| 682 | */ |
||
| 683 | public function doDelete($data, $form) |
||
| 684 | { |
||
| 685 | $title = $this->record->Title; |
||
| 686 | if (!$this->record->canDelete()) { |
||
| 687 | throw new ValidationException( |
||
| 688 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.DeletePermissionsFailure', "No delete permissions") |
||
| 689 | ); |
||
| 690 | } |
||
| 691 | $this->record->delete(); |
||
| 692 | |||
| 693 | $message = _t( |
||
| 694 | 'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Deleted', |
||
| 695 | 'Deleted {type} {name}', |
||
| 696 | [ |
||
| 697 | 'type' => $this->record->i18n_singular_name(), |
||
| 698 | 'name' => htmlspecialchars($title, ENT_QUOTES) |
||
| 699 | ] |
||
| 700 | ); |
||
| 701 | |||
| 702 | $toplevelController = $this->getToplevelController(); |
||
| 703 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
| 704 | $backForm = $toplevelController->getEditForm(); |
||
| 705 | $backForm->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
||
| 706 | } else { |
||
| 707 | $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
||
| 708 | } |
||
| 709 | |||
| 710 | //when an item is deleted, redirect to the parent controller |
||
| 711 | $controller = $this->getToplevelController(); |
||
| 712 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh |
||
| 713 | |||
| 714 | return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section |
||
| 715 | } |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @param string $template |
||
| 719 | * @return $this |
||
| 720 | */ |
||
| 721 | public function setTemplate($template) |
||
| 722 | { |
||
| 723 | $this->template = $template; |
||
| 724 | return $this; |
||
| 725 | } |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | public function getTemplate() |
||
| 731 | { |
||
| 732 | return $this->template; |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Get list of templates to use |
||
| 737 | * |
||
| 738 | * @return array |
||
| 739 | */ |
||
| 740 | public function getTemplates() |
||
| 741 | { |
||
| 742 | $templates = SSViewer::get_templates_by_class($this, '', __CLASS__); |
||
| 743 | // Prefer any custom template |
||
| 744 | if ($this->getTemplate()) { |
||
| 745 | array_unshift($templates, $this->getTemplate()); |
||
| 746 | } |
||
| 747 | return $templates; |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * @return Controller |
||
| 752 | */ |
||
| 753 | public function getController() |
||
| 754 | { |
||
| 755 | return $this->popupController; |
||
| 756 | } |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @return GridField |
||
| 760 | */ |
||
| 761 | public function getGridField() |
||
| 764 | } |
||
| 765 | |||
| 766 | /** |
||
| 767 | * @return DataObject |
||
| 768 | */ |
||
| 769 | public function getRecord() |
||
| 770 | { |
||
| 771 | return $this->record; |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * CMS-specific functionality: Passes through navigation breadcrumbs |
||
| 776 | * to the template, and includes the currently edited record (if any). |
||
| 777 | * see {@link LeftAndMain->Breadcrumbs()} for details. |
||
| 778 | * |
||
| 779 | * @param boolean $unlinked |
||
| 780 | * @return ArrayList |
||
| 781 | */ |
||
| 782 | public function Breadcrumbs($unlinked = false) |
||
| 810 | } |
||
| 811 | } |
||
| 812 |
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