| Total Complexity | 86 |
| Total Lines | 733 |
| 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) |
||
| 110 | { |
||
| 111 | if (!$this->record->canView()) { |
||
| 112 | $this->httpError(403); |
||
| 113 | } |
||
| 114 | |||
| 115 | $controller = $this->getToplevelController(); |
||
| 116 | |||
| 117 | $form = $this->ItemEditForm(); |
||
| 118 | $form->makeReadonly(); |
||
| 119 | |||
| 120 | $data = new ArrayData(array( |
||
| 121 | 'Backlink' => $controller->Link(), |
||
| 122 | 'ItemEditForm' => $form |
||
| 123 | )); |
||
| 124 | $return = $data->renderWith($this->getTemplates()); |
||
| 125 | |||
| 126 | if ($request->isAjax()) { |
||
| 127 | return $return; |
||
| 128 | } else { |
||
| 129 | return $controller->customise(array('Content' => $return)); |
||
| 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() |
||
| 169 | { |
||
| 170 | $list = $this->gridField->getList(); |
||
| 171 | |||
| 172 | if (empty($this->record)) { |
||
| 173 | $controller = $this->getToplevelController(); |
||
| 174 | $url = $controller->getRequest()->getURL(); |
||
| 175 | $noActionURL = $controller->removeAction($url); |
||
| 176 | $controller->getResponse()->removeHeader('Location'); //clear the existing redirect |
||
| 177 | return $controller->redirect($noActionURL, 302); |
||
| 178 | } |
||
| 179 | |||
| 180 | $canView = $this->record->canView(); |
||
| 181 | $canEdit = $this->record->canEdit(); |
||
| 182 | $canDelete = $this->record->canDelete(); |
||
| 183 | $canCreate = $this->record->canCreate(); |
||
| 184 | |||
| 185 | if (!$canView) { |
||
| 186 | $controller = $this->getToplevelController(); |
||
| 187 | // TODO More friendly error |
||
| 188 | return $controller->httpError(403); |
||
| 189 | } |
||
| 190 | |||
| 191 | // Build actions |
||
| 192 | $actions = $this->getFormActions(); |
||
| 193 | |||
| 194 | // If we are creating a new record in a has-many list, then |
||
| 195 | // pre-populate the record's foreign key. |
||
| 196 | if ($list instanceof HasManyList && !$this->record->isInDB()) { |
||
| 197 | $key = $list->getForeignKey(); |
||
| 198 | $id = $list->getForeignID(); |
||
| 199 | $this->record->$key = $id; |
||
| 200 | } |
||
| 201 | |||
| 202 | $fields = $this->component->getFields(); |
||
| 203 | if (!$fields) { |
||
| 204 | $fields = $this->record->getCMSFields(); |
||
| 205 | } |
||
| 206 | |||
| 207 | // If we are creating a new record in a has-many list, then |
||
| 208 | // Disable the form field as it has no effect. |
||
| 209 | if ($list instanceof HasManyList) { |
||
| 210 | $key = $list->getForeignKey(); |
||
| 211 | |||
| 212 | if ($field = $fields->dataFieldByName($key)) { |
||
| 213 | $fields->makeFieldReadonly($field); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | $form = new Form( |
||
| 218 | $this, |
||
| 219 | 'ItemEditForm', |
||
| 220 | $fields, |
||
| 221 | $actions, |
||
| 222 | $this->component->getValidator() |
||
| 223 | ); |
||
| 224 | |||
| 225 | $form->loadDataFrom($this->record, $this->record->ID == 0 ? Form::MERGE_IGNORE_FALSEISH : Form::MERGE_DEFAULT); |
||
| 226 | |||
| 227 | if ($this->record->ID && !$canEdit) { |
||
| 228 | // Restrict editing of existing records |
||
| 229 | $form->makeReadonly(); |
||
| 230 | // Hack to re-enable delete button if user can delete |
||
| 231 | if ($canDelete) { |
||
| 232 | $form->Actions()->fieldByName('action_doDelete')->setReadonly(false); |
||
| 233 | } |
||
| 234 | } elseif (!$this->record->ID && !$canCreate) { |
||
| 235 | // Restrict creation of new records |
||
| 236 | $form->makeReadonly(); |
||
| 237 | } |
||
| 238 | |||
| 239 | // Load many_many extraData for record. |
||
| 240 | // Fields with the correct 'ManyMany' namespace need to be added manually through getCMSFields(). |
||
| 241 | if ($list instanceof ManyManyList) { |
||
| 242 | $extraData = $list->getExtraData('', $this->record->ID); |
||
| 243 | $form->loadDataFrom(array('ManyMany' => $extraData)); |
||
| 244 | } |
||
| 245 | |||
| 246 | // TODO Coupling with CMS |
||
| 247 | $toplevelController = $this->getToplevelController(); |
||
| 248 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
| 249 | // Always show with base template (full width, no other panels), |
||
| 250 | // regardless of overloaded CMS controller templates. |
||
| 251 | // TODO Allow customization, e.g. to display an edit form alongside a search form from the CMS controller |
||
| 252 | $form->setTemplate([ |
||
| 253 | 'type' => 'Includes', |
||
| 254 | 'SilverStripe\\Admin\\LeftAndMain_EditForm', |
||
| 255 | ]); |
||
| 256 | $form->addExtraClass('cms-content cms-edit-form center fill-height flexbox-area-grow'); |
||
| 257 | $form->setAttribute('data-pjax-fragment', 'CurrentForm Content'); |
||
| 258 | if ($form->Fields()->hasTabSet()) { |
||
| 259 | $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
||
| 260 | $form->addExtraClass('cms-tabset'); |
||
| 261 | } |
||
| 262 | |||
| 263 | $form->Backlink = $this->getBackLink(); |
||
| 264 | } |
||
| 265 | |||
| 266 | $cb = $this->component->getItemEditFormCallback(); |
||
| 267 | if ($cb) { |
||
| 268 | $cb($form, $this); |
||
| 269 | } |
||
| 270 | $this->extend("updateItemEditForm", $form); |
||
| 271 | return $form; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return CompositeField Returns the right aligned toolbar group field along with its FormAction's |
||
| 276 | */ |
||
| 277 | protected function getRightGroupField() |
||
| 278 | { |
||
| 279 | $rightGroup = CompositeField::create()->setName('RightGroup'); |
||
| 280 | $rightGroup->addExtraClass('ml-auto'); |
||
| 281 | $rightGroup->setFieldHolderTemplate(get_class($rightGroup) . '_holder_buttongroup'); |
||
| 282 | |||
| 283 | $previousAndNextGroup = CompositeField::create()->setName('PreviousAndNextGroup'); |
||
| 284 | $previousAndNextGroup->addExtraClass('circular-group mr-2'); |
||
| 285 | $previousAndNextGroup->setFieldHolderTemplate(get_class($previousAndNextGroup) . '_holder_buttongroup'); |
||
| 286 | |||
| 287 | /** @var GridFieldDetailForm $component */ |
||
| 288 | $component = $this->gridField->getConfig()->getComponentByType(GridFieldDetailForm::class); |
||
| 289 | $gridState = $this->getRequest()->requestVar('gridState'); |
||
| 290 | if ($component && $component->getShowPagination()) { |
||
| 291 | $previousAndNextGroup->push(FormAction::create('doPrevious') |
||
| 292 | ->setUseButtonTag(true) |
||
| 293 | ->setAttribute('data-grid-state', $gridState) |
||
| 294 | ->setDisabled(!$this->getPreviousRecordID()) |
||
| 295 | ->addExtraClass('btn btn-secondary font-icon-left-open action--previous discard-confirmation')); |
||
| 296 | |||
| 297 | $previousAndNextGroup->push(FormAction::create('doNext') |
||
| 298 | ->setUseButtonTag(true) |
||
| 299 | ->setAttribute('data-grid-state', $gridState) |
||
| 300 | ->setDisabled(!$this->getNextRecordID()) |
||
| 301 | ->addExtraClass('btn btn-secondary font-icon-right-open action--next discard-confirmation')); |
||
| 302 | } |
||
| 303 | |||
| 304 | $rightGroup->push($previousAndNextGroup); |
||
| 305 | |||
| 306 | if ($component && $component->getShowAdd()) { |
||
| 307 | $rightGroup->push(FormAction::create('doNew') |
||
| 308 | ->setUseButtonTag(true) |
||
| 309 | ->setAttribute('data-grid-state', $this->getRequest()->getVar('gridState')) |
||
| 310 | ->addExtraClass('btn btn-primary font-icon-plus-thin circular action--new discard-confirmation')); |
||
| 311 | } |
||
| 312 | |||
| 313 | return $rightGroup; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Build the set of form field actions for this DataObject |
||
| 318 | * |
||
| 319 | * @return FieldList |
||
| 320 | */ |
||
| 321 | protected function getFormActions() |
||
| 322 | { |
||
| 323 | $actions = new FieldList(); |
||
| 324 | |||
| 325 | if ($this->record->ID !== 0) { // existing record |
||
| 326 | if ($this->record->canEdit()) { |
||
| 327 | $actions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Save', 'Save')) |
||
| 328 | ->setUseButtonTag(true) |
||
| 329 | ->addExtraClass('btn-primary font-icon-save')); |
||
| 330 | } |
||
| 331 | |||
| 332 | if ($this->record->canDelete()) { |
||
| 333 | $actions->push(FormAction::create('doDelete', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Delete', 'Delete')) |
||
| 334 | ->setUseButtonTag(true) |
||
| 335 | ->addExtraClass('btn-outline-danger btn-hide-outline font-icon-trash-bin action--delete')); |
||
| 336 | } |
||
| 337 | |||
| 338 | $gridState = $this->getRequest()->requestVar('gridState'); |
||
| 339 | $this->gridField->getState(false)->setValue($gridState); |
||
| 340 | $actions->push(HiddenField::create('gridState', null, $gridState)); |
||
| 341 | |||
| 342 | $actions->push($this->getRightGroupField()); |
||
| 343 | } else { // adding new record |
||
| 344 | //Change the Save label to 'Create' |
||
| 345 | $actions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Create', 'Create')) |
||
| 346 | ->setUseButtonTag(true) |
||
| 347 | ->addExtraClass('btn-primary font-icon-plus-thin')); |
||
| 348 | |||
| 349 | // Add a Cancel link which is a button-like link and link back to one level up. |
||
| 350 | $crumbs = $this->Breadcrumbs(); |
||
| 351 | if ($crumbs && $crumbs->count() >= 2) { |
||
| 352 | $oneLevelUp = $crumbs->offsetGet($crumbs->count() - 2); |
||
| 353 | $text = sprintf( |
||
| 354 | "<a class=\"%s\" href=\"%s\">%s</a>", |
||
| 355 | "crumb btn btn-secondary cms-panel-link", // CSS classes |
||
| 356 | $oneLevelUp->Link, // url |
||
| 357 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.CancelBtn', 'Cancel') // label |
||
| 358 | ); |
||
| 359 | $actions->push(new LiteralField('cancelbutton', $text)); |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | $this->extend('updateFormActions', $actions); |
||
| 364 | |||
| 365 | return $actions; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Traverse the nested RequestHandlers until we reach something that's not GridFieldDetailForm_ItemRequest. |
||
| 370 | * This allows us to access the Controller responsible for invoking the top-level GridField. |
||
| 371 | * This should be equivalent to getting the controller off the top of the controller stack via Controller::curr(), |
||
| 372 | * but allows us to avoid accessing the global state. |
||
| 373 | * |
||
| 374 | * GridFieldDetailForm_ItemRequests are RequestHandlers, and as such they are not part of the controller stack. |
||
| 375 | * |
||
| 376 | * @return Controller |
||
| 377 | */ |
||
| 378 | protected function getToplevelController() |
||
| 379 | { |
||
| 380 | $c = $this->popupController; |
||
| 381 | while ($c && $c instanceof GridFieldDetailForm_ItemRequest) { |
||
| 382 | $c = $c->getController(); |
||
| 383 | } |
||
| 384 | return $c; |
||
| 385 | } |
||
| 386 | |||
| 387 | protected function getBackLink() |
||
| 388 | { |
||
| 389 | // TODO Coupling with CMS |
||
| 390 | $backlink = ''; |
||
| 391 | $toplevelController = $this->getToplevelController(); |
||
| 392 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
| 393 | if ($toplevelController->hasMethod('Backlink')) { |
||
| 394 | $backlink = $toplevelController->Backlink(); |
||
| 395 | } elseif ($this->popupController->hasMethod('Breadcrumbs')) { |
||
| 396 | $parents = $this->popupController->Breadcrumbs(false)->items; |
||
| 397 | $backlink = array_pop($parents)->Link; |
||
| 398 | } |
||
| 399 | } |
||
| 400 | if (!$backlink) { |
||
| 401 | $backlink = $toplevelController->Link(); |
||
| 402 | } |
||
| 403 | |||
| 404 | return $backlink; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get the list of extra data from the $record as saved into it by |
||
| 409 | * {@see Form::saveInto()} |
||
| 410 | * |
||
| 411 | * Handles detection of falsey values explicitly saved into the |
||
| 412 | * DataObject by formfields |
||
| 413 | * |
||
| 414 | * @param DataObject $record |
||
| 415 | * @param SS_List $list |
||
| 416 | * @return array List of data to write to the relation |
||
| 417 | */ |
||
| 418 | protected function getExtraSavedData($record, $list) |
||
| 419 | { |
||
| 420 | // Skip extra data if not ManyManyList |
||
| 421 | if (!($list instanceof ManyManyList)) { |
||
| 422 | return null; |
||
| 423 | } |
||
| 424 | |||
| 425 | $data = array(); |
||
| 426 | foreach ($list->getExtraFields() as $field => $dbSpec) { |
||
| 427 | $savedField = "ManyMany[{$field}]"; |
||
| 428 | if ($record->hasField($savedField)) { |
||
| 429 | $data[$field] = $record->getField($savedField); |
||
| 430 | } |
||
| 431 | } |
||
| 432 | return $data; |
||
| 433 | } |
||
| 434 | |||
| 435 | public function doSave($data, $form) |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Goes to the previous record |
||
| 467 | * @param array $data The form data |
||
| 468 | * @param Form $form The Form object |
||
| 469 | * @return HTTPResponse |
||
| 470 | */ |
||
| 471 | public function doPrevious($data, $form) |
||
| 472 | { |
||
| 473 | $this->getToplevelController()->getResponse()->addHeader('X-Pjax', 'Content'); |
||
| 474 | $link = $this->getEditLink($this->getPreviousRecordID()); |
||
| 475 | return $this->redirect($link); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Goes to the next record |
||
| 480 | * @param array $data The form data |
||
| 481 | * @param Form $form The Form object |
||
| 482 | * @return HTTPResponse |
||
| 483 | */ |
||
| 484 | public function doNext($data, $form) |
||
| 485 | { |
||
| 486 | $this->getToplevelController()->getResponse()->addHeader('X-Pjax', 'Content'); |
||
| 487 | $link = $this->getEditLink($this->getNextRecordID()); |
||
| 488 | return $this->redirect($link); |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Creates a new record. If you're already creating a new record, |
||
| 493 | * this forces the URL to change. |
||
| 494 | * |
||
| 495 | * @param array $data The form data |
||
| 496 | * @param Form $form The Form object |
||
| 497 | * @return HTTPResponse |
||
| 498 | */ |
||
| 499 | public function doNew($data, $form) |
||
| 500 | { |
||
| 501 | return $this->redirect(Controller::join_links($this->gridField->Link('item'), 'new')); |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Gets the edit link for a record |
||
| 506 | * |
||
| 507 | * @param int $id The ID of the record in the GridField |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | public function getEditLink($id) |
||
| 511 | { |
||
| 512 | return Controller::join_links( |
||
| 513 | $this->gridField->Link(), |
||
| 514 | 'item', |
||
| 515 | $id, |
||
| 516 | '?gridState=' . urlencode($this->gridField->getState(false)->Value()) |
||
| 517 | ); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param int $offset The offset from the current record |
||
| 522 | * @return int|bool |
||
| 523 | */ |
||
| 524 | private function getAdjacentRecordID($offset) |
||
| 525 | { |
||
| 526 | $gridField = $this->getGridField(); |
||
| 527 | $gridStateStr = $this->getRequest()->requestVar('gridState'); |
||
| 528 | $state = $gridField->getState(false); |
||
| 529 | $state->setValue($gridStateStr); |
||
| 530 | $data = $state->getData(); |
||
| 531 | $paginator = $data->getData('GridFieldPaginator'); |
||
| 532 | if (!$paginator) { |
||
| 533 | return false; |
||
| 534 | } |
||
| 535 | |||
| 536 | $currentPage = $paginator->getData('currentPage'); |
||
| 537 | $itemsPerPage = $paginator->getData('itemsPerPage'); |
||
| 538 | |||
| 539 | $limit = $itemsPerPage + 2; |
||
| 540 | $limitOffset = max(0, $itemsPerPage * ($currentPage-1) -1); |
||
| 541 | |||
| 542 | $map = $gridField->getManipulatedList()->limit($limit, $limitOffset)->column('ID'); |
||
| 543 | $index = array_search($this->record->ID, $map); |
||
| 544 | return isset($map[$index+$offset]) ? $map[$index+$offset] : false; |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Gets the ID of the previous record in the list. |
||
| 549 | * |
||
| 550 | * @return int |
||
| 551 | */ |
||
| 552 | public function getPreviousRecordID() |
||
| 553 | { |
||
| 554 | return $this->getAdjacentRecordID(-1); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Gets the ID of the next record in the list. |
||
| 559 | * |
||
| 560 | * @return int |
||
| 561 | */ |
||
| 562 | public function getNextRecordID() |
||
| 563 | { |
||
| 564 | return $this->getAdjacentRecordID(1); |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Response object for this request after a successful save |
||
| 569 | * |
||
| 570 | * @param bool $isNewRecord True if this record was just created |
||
| 571 | * @return HTTPResponse|DBHTMLText |
||
| 572 | */ |
||
| 573 | protected function redirectAfterSave($isNewRecord) |
||
| 574 | { |
||
| 575 | $controller = $this->getToplevelController(); |
||
| 576 | if ($isNewRecord) { |
||
| 577 | return $controller->redirect($this->Link()); |
||
| 578 | } elseif ($this->gridField->getList()->byID($this->record->ID)) { |
||
| 579 | // Return new view, as we can't do a "virtual redirect" via the CMS Ajax |
||
| 580 | // to the same URL (it assumes that its content is already current, and doesn't reload) |
||
| 581 | return $this->edit($controller->getRequest()); |
||
| 582 | } else { |
||
| 583 | // Changes to the record properties might've excluded the record from |
||
| 584 | // a filtered list, so return back to the main view if it can't be found |
||
| 585 | $url = $controller->getRequest()->getURL(); |
||
| 586 | $noActionURL = $controller->removeAction($url); |
||
| 587 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); |
||
| 588 | return $controller->redirect($noActionURL, 302); |
||
| 589 | } |
||
| 590 | } |
||
| 591 | |||
| 592 | public function httpError($errorCode, $errorMessage = null) |
||
| 593 | { |
||
| 594 | $controller = $this->getToplevelController(); |
||
| 595 | return $controller->httpError($errorCode, $errorMessage); |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Loads the given form data into the underlying dataobject and relation |
||
| 600 | * |
||
| 601 | * @param array $data |
||
| 602 | * @param Form $form |
||
| 603 | * @throws ValidationException On error |
||
| 604 | * @return DataObject Saved record |
||
| 605 | */ |
||
| 606 | protected function saveFormIntoRecord($data, $form) |
||
| 607 | { |
||
| 608 | $list = $this->gridField->getList(); |
||
| 609 | |||
| 610 | // Check object matches the correct classname |
||
| 611 | if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { |
||
| 612 | $newClassName = $data['ClassName']; |
||
| 613 | // The records originally saved attribute was overwritten by $form->saveInto($record) before. |
||
| 614 | // This is necessary for newClassInstance() to work as expected, and trigger change detection |
||
| 615 | // on the ClassName attribute |
||
| 616 | $this->record->setClassName($this->record->ClassName); |
||
| 617 | // Replace $record with a new instance |
||
| 618 | $this->record = $this->record->newClassInstance($newClassName); |
||
| 619 | } |
||
| 620 | |||
| 621 | // Save form and any extra saved data into this dataobject |
||
| 622 | $form->saveInto($this->record); |
||
| 623 | $this->record->write(); |
||
| 624 | $this->extend('onAfterSave', $this->record); |
||
| 625 | |||
| 626 | $extraData = $this->getExtraSavedData($this->record, $list); |
||
| 627 | $list->add($this->record, $extraData); |
||
| 628 | |||
| 629 | return $this->record; |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param array $data |
||
| 634 | * @param Form $form |
||
| 635 | * @return HTTPResponse |
||
| 636 | * @throws ValidationException |
||
| 637 | */ |
||
| 638 | public function doDelete($data, $form) |
||
| 639 | { |
||
| 640 | $title = $this->record->Title; |
||
| 641 | if (!$this->record->canDelete()) { |
||
| 642 | throw new ValidationException( |
||
| 643 | _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.DeletePermissionsFailure', "No delete permissions") |
||
| 644 | ); |
||
| 645 | } |
||
| 646 | $this->record->delete(); |
||
| 647 | |||
| 648 | $message = _t( |
||
| 649 | 'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Deleted', |
||
| 650 | 'Deleted {type} {name}', |
||
| 651 | [ |
||
| 652 | 'type' => $this->record->i18n_singular_name(), |
||
| 653 | 'name' => htmlspecialchars($title, ENT_QUOTES) |
||
| 654 | ] |
||
| 655 | ); |
||
| 656 | |||
| 657 | $toplevelController = $this->getToplevelController(); |
||
| 658 | if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
||
| 659 | $backForm = $toplevelController->getEditForm(); |
||
| 660 | $backForm->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
||
| 661 | } else { |
||
| 662 | $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
||
| 663 | } |
||
| 664 | |||
| 665 | //when an item is deleted, redirect to the parent controller |
||
| 666 | $controller = $this->getToplevelController(); |
||
| 667 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh |
||
| 668 | |||
| 669 | return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param string $template |
||
| 674 | * @return $this |
||
| 675 | */ |
||
| 676 | public function setTemplate($template) |
||
| 677 | { |
||
| 678 | $this->template = $template; |
||
| 679 | return $this; |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @return string |
||
| 684 | */ |
||
| 685 | public function getTemplate() |
||
| 686 | { |
||
| 687 | return $this->template; |
||
| 688 | } |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Get list of templates to use |
||
| 692 | * |
||
| 693 | * @return array |
||
| 694 | */ |
||
| 695 | public function getTemplates() |
||
| 696 | { |
||
| 697 | $templates = SSViewer::get_templates_by_class($this, '', __CLASS__); |
||
| 698 | // Prefer any custom template |
||
| 699 | if ($this->getTemplate()) { |
||
| 700 | array_unshift($templates, $this->getTemplate()); |
||
| 701 | } |
||
| 702 | return $templates; |
||
| 703 | } |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @return Controller |
||
| 707 | */ |
||
| 708 | public function getController() |
||
| 709 | { |
||
| 710 | return $this->popupController; |
||
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @return GridField |
||
| 715 | */ |
||
| 716 | public function getGridField() |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * @return DataObject |
||
| 723 | */ |
||
| 724 | public function getRecord() |
||
| 725 | { |
||
| 726 | return $this->record; |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * CMS-specific functionality: Passes through navigation breadcrumbs |
||
| 731 | * to the template, and includes the currently edited record (if any). |
||
| 732 | * see {@link LeftAndMain->Breadcrumbs()} for details. |
||
| 733 | * |
||
| 734 | * @param boolean $unlinked |
||
| 735 | * @return ArrayList |
||
| 736 | */ |
||
| 737 | public function Breadcrumbs($unlinked = false) |
||
| 761 | } |
||
| 762 | } |
||
| 763 |
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