Total Complexity | 90 |
Total Lines | 746 |
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 | protected function getRightGroupField() |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Build the set of form field actions for this DataObject |
||
346 | * |
||
347 | * @return FieldList |
||
348 | */ |
||
349 | protected function getFormActions() |
||
350 | { |
||
351 | $canEdit = $this->record->canEdit(); |
||
352 | $canDelete = $this->record->canDelete(); |
||
353 | $actions = FieldList::create(); |
||
354 | |||
355 | if ($this->record->ID !== 0) { // existing record |
||
356 | if ($canEdit) { |
||
357 | $actions->push( |
||
358 | FormAction::create( |
||
359 | 'doSave', |
||
360 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Save', 'Save') |
||
361 | ) |
||
362 | ->setUseButtonTag(true) |
||
363 | ->addExtraClass('btn-primary font-icon-save') |
||
364 | ); |
||
365 | } |
||
366 | |||
367 | if ($canDelete) { |
||
368 | $actions->push( |
||
369 | FormAction::create( |
||
370 | 'doDelete', |
||
371 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Delete', 'Delete') |
||
372 | ) |
||
373 | ->setUseButtonTag(true) |
||
374 | ->addExtraClass('btn-outline-danger btn-hide-outline font-icon-trash-bin action--delete') |
||
375 | ); |
||
376 | } |
||
377 | |||
378 | $gridState = $this->getRequest()->requestVar('gridState'); |
||
379 | $this->gridField->getState(false)->setValue($gridState); |
||
380 | $actions->push(HiddenField::create('gridState', null, $gridState)); |
||
381 | |||
382 | $actions->push($this->getRightGroupField()); |
||
383 | } else { // adding new record |
||
384 | // Change the Save label to 'Create' |
||
385 | $actions->push( |
||
386 | FormAction::create( |
||
387 | 'doSave', |
||
388 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Create', 'Create') |
||
389 | ) |
||
390 | ->setUseButtonTag(true) |
||
391 | ->addExtraClass('btn-primary font-icon-plus-thin') |
||
392 | ); |
||
393 | |||
394 | // Add a Cancel link which is a button-like link and link back to one level up. |
||
395 | $crumbs = $this->Breadcrumbs(); |
||
396 | if ($crumbs && $crumbs->count() >= 2) { |
||
397 | $oneLevelUp = $crumbs->offsetGet($crumbs->count() - 2); |
||
398 | $text = sprintf( |
||
399 | "<a class=\"%s\" href=\"%s\">%s</a>", |
||
400 | "crumb btn btn-secondary cms-panel-link", // CSS classes |
||
401 | $oneLevelUp->Link, // url |
||
402 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.CancelBtn', 'Cancel') // label |
||
403 | ); |
||
404 | $actions->push(new LiteralField('cancelbutton', $text)); |
||
405 | } |
||
406 | } |
||
407 | |||
408 | $this->extend('updateFormActions', $actions); |
||
409 | |||
410 | return $actions; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Traverse the nested RequestHandlers until we reach something that's not GridFieldDetailForm_ItemRequest. |
||
415 | * This allows us to access the Controller responsible for invoking the top-level GridField. |
||
416 | * This should be equivalent to getting the controller off the top of the controller stack via Controller::curr(), |
||
417 | * but allows us to avoid accessing the global state. |
||
418 | * |
||
419 | * GridFieldDetailForm_ItemRequests are RequestHandlers, and as such they are not part of the controller stack. |
||
420 | * |
||
421 | * @return Controller |
||
422 | */ |
||
423 | protected function getToplevelController() |
||
424 | { |
||
425 | $c = $this->popupController; |
||
426 | while ($c && $c instanceof GridFieldDetailForm_ItemRequest) { |
||
427 | $c = $c->getController(); |
||
428 | } |
||
429 | return $c; |
||
430 | } |
||
431 | |||
432 | protected function getBackLink() |
||
433 | { |
||
434 | // TODO Coupling with CMS |
||
435 | $backlink = ''; |
||
436 | $toplevelController = $this->getToplevelController(); |
||
437 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
438 | if ($toplevelController->hasMethod('Backlink')) { |
||
439 | $backlink = $toplevelController->Backlink(); |
||
440 | } elseif ($this->popupController->hasMethod('Breadcrumbs')) { |
||
441 | $parents = $this->popupController->Breadcrumbs(false)->items; |
||
442 | $backlink = array_pop($parents)->Link; |
||
443 | } |
||
444 | } |
||
445 | if (!$backlink) { |
||
446 | $backlink = $toplevelController->Link(); |
||
447 | } |
||
448 | |||
449 | return $backlink; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Get the list of extra data from the $record as saved into it by |
||
454 | * {@see Form::saveInto()} |
||
455 | * |
||
456 | * Handles detection of falsey values explicitly saved into the |
||
457 | * DataObject by formfields |
||
458 | * |
||
459 | * @param DataObject $record |
||
460 | * @param SS_List $list |
||
461 | * @return array List of data to write to the relation |
||
462 | */ |
||
463 | protected function getExtraSavedData($record, $list) |
||
464 | { |
||
465 | // Skip extra data if not ManyManyList |
||
466 | if (!($list instanceof ManyManyList)) { |
||
467 | return null; |
||
468 | } |
||
469 | |||
470 | $data = array(); |
||
471 | foreach ($list->getExtraFields() as $field => $dbSpec) { |
||
472 | $savedField = "ManyMany[{$field}]"; |
||
473 | if ($record->hasField($savedField)) { |
||
474 | $data[$field] = $record->getField($savedField); |
||
475 | } |
||
476 | } |
||
477 | return $data; |
||
478 | } |
||
479 | |||
480 | public function doSave($data, $form) |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Gets the edit link for a record |
||
512 | * |
||
513 | * @param int $id The ID of the record in the GridField |
||
514 | * @return string |
||
515 | */ |
||
516 | public function getEditLink($id) |
||
517 | { |
||
518 | return Controller::join_links( |
||
519 | $this->gridField->Link(), |
||
520 | 'item', |
||
521 | $id, |
||
522 | '?gridState=' . urlencode($this->gridField->getState(false)->Value()) |
||
523 | ); |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * @param int $offset The offset from the current record |
||
528 | * @return int|bool |
||
529 | */ |
||
530 | private function getAdjacentRecordID($offset) |
||
531 | { |
||
532 | $gridField = $this->getGridField(); |
||
533 | $gridStateStr = $this->getRequest()->requestVar('gridState'); |
||
534 | $state = $gridField->getState(false); |
||
535 | $state->setValue($gridStateStr); |
||
536 | $data = $state->getData(); |
||
537 | $paginator = $data->getData('GridFieldPaginator'); |
||
538 | if (!$paginator) { |
||
539 | return false; |
||
540 | } |
||
541 | |||
542 | $currentPage = $paginator->getData('currentPage'); |
||
543 | $itemsPerPage = $paginator->getData('itemsPerPage'); |
||
544 | |||
545 | $limit = $itemsPerPage + 2; |
||
546 | $limitOffset = max(0, $itemsPerPage * ($currentPage-1) -1); |
||
547 | |||
548 | $map = $gridField->getManipulatedList()->limit($limit, $limitOffset)->column('ID'); |
||
549 | $index = array_search($this->record->ID, $map); |
||
550 | return isset($map[$index+$offset]) ? $map[$index+$offset] : false; |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * Gets the ID of the previous record in the list. |
||
555 | * |
||
556 | * @return int |
||
557 | */ |
||
558 | public function getPreviousRecordID() |
||
559 | { |
||
560 | return $this->getAdjacentRecordID(-1); |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Gets the ID of the next record in the list. |
||
565 | * |
||
566 | * @return int |
||
567 | */ |
||
568 | public function getNextRecordID() |
||
569 | { |
||
570 | return $this->getAdjacentRecordID(1); |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * Response object for this request after a successful save |
||
575 | * |
||
576 | * @param bool $isNewRecord True if this record was just created |
||
577 | * @return HTTPResponse|DBHTMLText |
||
578 | */ |
||
579 | protected function redirectAfterSave($isNewRecord) |
||
580 | { |
||
581 | $controller = $this->getToplevelController(); |
||
582 | if ($isNewRecord) { |
||
583 | return $controller->redirect($this->Link()); |
||
584 | } elseif ($this->gridField->getList()->byID($this->record->ID)) { |
||
585 | // Return new view, as we can't do a "virtual redirect" via the CMS Ajax |
||
586 | // to the same URL (it assumes that its content is already current, and doesn't reload) |
||
587 | return $this->edit($controller->getRequest()); |
||
588 | } else { |
||
589 | // Changes to the record properties might've excluded the record from |
||
590 | // a filtered list, so return back to the main view if it can't be found |
||
591 | $url = $controller->getRequest()->getURL(); |
||
592 | $noActionURL = $controller->removeAction($url); |
||
593 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); |
||
594 | return $controller->redirect($noActionURL, 302); |
||
595 | } |
||
596 | } |
||
597 | |||
598 | public function httpError($errorCode, $errorMessage = null) |
||
599 | { |
||
600 | $controller = $this->getToplevelController(); |
||
601 | return $controller->httpError($errorCode, $errorMessage); |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * Loads the given form data into the underlying dataobject and relation |
||
606 | * |
||
607 | * @param array $data |
||
608 | * @param Form $form |
||
609 | * @throws ValidationException On error |
||
610 | * @return DataObject Saved record |
||
611 | */ |
||
612 | protected function saveFormIntoRecord($data, $form) |
||
613 | { |
||
614 | $list = $this->gridField->getList(); |
||
615 | |||
616 | // Check object matches the correct classname |
||
617 | if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { |
||
618 | $newClassName = $data['ClassName']; |
||
619 | // The records originally saved attribute was overwritten by $form->saveInto($record) before. |
||
620 | // This is necessary for newClassInstance() to work as expected, and trigger change detection |
||
621 | // on the ClassName attribute |
||
622 | $this->record->setClassName($this->record->ClassName); |
||
623 | // Replace $record with a new instance |
||
624 | $this->record = $this->record->newClassInstance($newClassName); |
||
625 | } |
||
626 | |||
627 | // Save form and any extra saved data into this dataobject |
||
628 | $form->saveInto($this->record); |
||
629 | $this->record->write(); |
||
630 | $this->extend('onAfterSave', $this->record); |
||
631 | |||
632 | $extraData = $this->getExtraSavedData($this->record, $list); |
||
633 | $list->add($this->record, $extraData); |
||
634 | |||
635 | return $this->record; |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * @param array $data |
||
640 | * @param Form $form |
||
641 | * @return HTTPResponse |
||
642 | * @throws ValidationException |
||
643 | */ |
||
644 | public function doDelete($data, $form) |
||
645 | { |
||
646 | $title = $this->record->Title; |
||
647 | if (!$this->record->canDelete()) { |
||
648 | throw new ValidationException( |
||
649 | _t( |
||
650 | 'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.DeletePermissionsFailure', |
||
651 | "No delete permissions" |
||
652 | ) |
||
653 | ); |
||
654 | } |
||
655 | $this->record->delete(); |
||
656 | |||
657 | $message = _t( |
||
658 | 'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Deleted', |
||
659 | 'Deleted {type} {name}', |
||
660 | [ |
||
661 | 'type' => $this->record->i18n_singular_name(), |
||
662 | 'name' => htmlspecialchars($title, ENT_QUOTES) |
||
663 | ] |
||
664 | ); |
||
665 | |||
666 | $toplevelController = $this->getToplevelController(); |
||
667 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
668 | $backForm = $toplevelController->getEditForm(); |
||
669 | $backForm->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
||
670 | } else { |
||
671 | $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
||
672 | } |
||
673 | |||
674 | //when an item is deleted, redirect to the parent controller |
||
675 | $controller = $this->getToplevelController(); |
||
676 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh |
||
677 | |||
678 | return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section |
||
679 | } |
||
680 | |||
681 | /** |
||
682 | * @param string $template |
||
683 | * @return $this |
||
684 | */ |
||
685 | public function setTemplate($template) |
||
686 | { |
||
687 | $this->template = $template; |
||
688 | return $this; |
||
689 | } |
||
690 | |||
691 | /** |
||
692 | * @return string |
||
693 | */ |
||
694 | public function getTemplate() |
||
695 | { |
||
696 | return $this->template; |
||
697 | } |
||
698 | |||
699 | /** |
||
700 | * Get list of templates to use |
||
701 | * |
||
702 | * @return array |
||
703 | */ |
||
704 | public function getTemplates() |
||
705 | { |
||
706 | $templates = SSViewer::get_templates_by_class($this, '', __CLASS__); |
||
707 | // Prefer any custom template |
||
708 | if ($this->getTemplate()) { |
||
709 | array_unshift($templates, $this->getTemplate()); |
||
710 | } |
||
711 | return $templates; |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * @return Controller |
||
716 | */ |
||
717 | public function getController() |
||
718 | { |
||
719 | return $this->popupController; |
||
720 | } |
||
721 | |||
722 | /** |
||
723 | * @return GridField |
||
724 | */ |
||
725 | public function getGridField() |
||
728 | } |
||
729 | |||
730 | /** |
||
731 | * @return DataObject |
||
732 | */ |
||
733 | public function getRecord() |
||
734 | { |
||
735 | return $this->record; |
||
736 | } |
||
737 | |||
738 | /** |
||
739 | * CMS-specific functionality: Passes through navigation breadcrumbs |
||
740 | * to the template, and includes the currently edited record (if any). |
||
741 | * see {@link LeftAndMain->Breadcrumbs()} for details. |
||
742 | * |
||
743 | * @param boolean $unlinked |
||
744 | * @return ArrayList |
||
745 | */ |
||
746 | public function Breadcrumbs($unlinked = false) |
||
774 | } |
||
775 | } |
||
776 |
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