Total Complexity | 75 |
Total Lines | 550 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 1 |
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 |
||
30 | class ActionsGridFieldItemRequest extends DataExtension |
||
31 | { |
||
32 | use Configurable; |
||
33 | |||
34 | /** |
||
35 | * @config |
||
36 | * @var boolean |
||
37 | */ |
||
38 | private static $enable_save_prev_next = true; |
||
39 | |||
40 | /** |
||
41 | * @config |
||
42 | * @var boolean |
||
43 | */ |
||
44 | private static $enable_save_close = true; |
||
45 | |||
46 | /** |
||
47 | * @config |
||
48 | * @var boolean |
||
49 | */ |
||
50 | private static $enable_delete_right = true; |
||
51 | |||
52 | /** |
||
53 | * @config |
||
54 | * @var boolean |
||
55 | */ |
||
56 | private static $enable_utils_prev_next = false; |
||
|
|||
57 | |||
58 | /** |
||
59 | * @var array Allowed controller actions |
||
60 | */ |
||
61 | private static $allowed_actions = array( |
||
62 | 'doSaveAndClose', |
||
63 | 'doSaveAndNext', |
||
64 | 'doSaveAndPrev', |
||
65 | 'doCustomAction', // For CustomAction |
||
66 | 'doCustomLink', // For CustomLink |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * @return array |
||
71 | */ |
||
72 | protected function getAvailableActions($actions) |
||
73 | { |
||
74 | $list = []; |
||
75 | foreach ($actions as $action) { |
||
76 | $list[] = $action->getName(); |
||
77 | } |
||
78 | return $list; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Updates the detail form to include new form actions and buttons |
||
83 | * |
||
84 | * Reorganize things a bit |
||
85 | * |
||
86 | * @param Form The ItemEditForm object |
||
87 | */ |
||
88 | public function updateItemEditForm($form) |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param FieldList $actions |
||
150 | * @param DataObject $record |
||
151 | * @return void |
||
152 | */ |
||
153 | public function moveCancelAndDelete(FieldList $actions, DataObject $record) |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | public function getCustomPreviousRecordID(DataObject $record) |
||
196 | { |
||
197 | if ($record->hasMethod('PrevRecord')) { |
||
198 | return $record->PrevRecord()->ID ?? 0; |
||
199 | } |
||
200 | $this->owner->getPreviousRecordID(); |
||
201 | } |
||
202 | |||
203 | public function getCustomNextRecordID(DataObject $record) |
||
204 | { |
||
205 | if ($record->hasMethod('NextRecord')) { |
||
206 | return $record->NextRecord()->ID ?? 0; |
||
207 | } |
||
208 | $this->owner->getNextRecordID(); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param FieldList $actions |
||
213 | * @param DataObject $record |
||
214 | * @return void |
||
215 | */ |
||
216 | public function addSaveNextAndPrevious(FieldList $actions, DataObject $record) |
||
217 | { |
||
218 | if (!$record->canEdit()) { |
||
219 | return; |
||
220 | } |
||
221 | if (!$record->ID) { |
||
222 | return; |
||
223 | } |
||
224 | |||
225 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
226 | |||
227 | // If it doesn't exist, push to default group |
||
228 | if (!$MajorActions) { |
||
229 | $MajorActions = $actions; |
||
230 | } |
||
231 | |||
232 | // TODO: check why with paginator, after the first page, getPreviousRecordID/getNextRecordID tend to not work properly |
||
233 | $getPreviousRecordID = $this->getCustomPreviousRecordID($record); |
||
234 | $getNextRecordID = $this->getCustomNextRecordID($record); |
||
235 | |||
236 | // Coupling for HasPrevNextUtils |
||
237 | if (Controller::has_curr()) { |
||
238 | $request = Controller::curr()->getRequest(); |
||
239 | $routeParams = $request->routeParams(); |
||
240 | $routeParams['PreviousRecordID'] = $getPreviousRecordID; |
||
241 | $routeParams['NextRecordID'] = $getNextRecordID; |
||
242 | $request->setRouteParams($routeParams); |
||
243 | } |
||
244 | |||
245 | if ($getPreviousRecordID) { |
||
246 | $doSaveAndPrev = new FormAction('doSaveAndPrev', _t('ActionsGridFieldItemRequest.SAVEANDPREVIOUS', 'Save and Previous')); |
||
247 | $doSaveAndPrev->addExtraClass($this->getBtnClassForRecord($record)); |
||
248 | $doSaveAndPrev->addExtraClass('font-icon-angle-double-left'); |
||
249 | $doSaveAndPrev->setUseButtonTag(true); |
||
250 | $MajorActions->push($doSaveAndPrev); |
||
251 | } |
||
252 | if ($getNextRecordID) { |
||
253 | $doSaveAndNext = new FormAction('doSaveAndNext', _t('ActionsGridFieldItemRequest.SAVEANDNEXT', 'Save and Next')); |
||
254 | $doSaveAndNext->addExtraClass($this->getBtnClassForRecord($record)); |
||
255 | $doSaveAndNext->addExtraClass('font-icon-angle-double-right'); |
||
256 | $doSaveAndNext->setUseButtonTag(true); |
||
257 | $MajorActions->push($doSaveAndNext); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param FieldList $actions |
||
263 | * @param DataObject $record |
||
264 | * @return void |
||
265 | */ |
||
266 | public function addSaveAndClose(FieldList $actions, DataObject $record) |
||
267 | { |
||
268 | if (!$record->canEdit()) { |
||
269 | return; |
||
270 | } |
||
271 | |||
272 | $MajorActions = $actions->fieldByName('MajorActions'); |
||
273 | |||
274 | // If it doesn't exist, push to default group |
||
275 | if (!$MajorActions) { |
||
276 | $MajorActions = $actions; |
||
277 | } |
||
278 | |||
279 | if ($record->ID) { |
||
280 | $label = _t('ActionsGridFieldItemRequest.SAVEANDCLOSE', 'Save and Close'); |
||
281 | } else { |
||
282 | $label = _t('ActionsGridFieldItemRequest.CREATEANDCLOSE', 'Create and Close'); |
||
283 | } |
||
284 | $saveAndClose = new FormAction('doSaveAndClose', $label); |
||
285 | $saveAndClose->addExtraClass($this->getBtnClassForRecord($record)); |
||
286 | $saveAndClose->setAttribute('data-text-alternate', $label); |
||
287 | if ($record->ID) { |
||
288 | $saveAndClose->setAttribute('data-btn-alternate-add', 'btn-primary'); |
||
289 | $saveAndClose->setAttribute('data-btn-alternate-remove', 'btn-outline-primary'); |
||
290 | } |
||
291 | $saveAndClose->addExtraClass('font-icon-level-up'); |
||
292 | $saveAndClose->setUseButtonTag(true); |
||
293 | $MajorActions->push($saveAndClose); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * New and existing records have different classes |
||
298 | * |
||
299 | * @param DataObject $record |
||
300 | * @return string |
||
301 | */ |
||
302 | protected function getBtnClassForRecord(DataObject $record) |
||
303 | { |
||
304 | if ($record->ID) { |
||
305 | return 'btn-outline-primary'; |
||
306 | } |
||
307 | return 'btn-primary'; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Forward a given action to a DataObject |
||
312 | * |
||
313 | * Action must be declared in getCMSActions to be called |
||
314 | * |
||
315 | * @param string $action |
||
316 | * @param array $data |
||
317 | * @param Form $form |
||
318 | * @return HTTPResponse|DBHTMLText |
||
319 | */ |
||
320 | protected function forwardActionToRecord($action, $data = [], $form = null) |
||
321 | { |
||
322 | $controller = $this->getToplevelController(); |
||
323 | $record = $this->owner->record; |
||
324 | $definedActions = $record->getCMSActions(); |
||
325 | // Check if the action is indeed available |
||
326 | $clickedAction = null; |
||
327 | if (!empty($definedActions)) { |
||
328 | foreach ($definedActions as $definedAction) { |
||
329 | $definedActionName = $definedAction->getName(); |
||
330 | if ($definedAction->hasMethod('actionName')) { |
||
331 | $definedActionName = $definedAction->actionName(); |
||
332 | } |
||
333 | if ($definedActionName == $action) { |
||
334 | $clickedAction = $definedAction; |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | if (!$clickedAction) { |
||
339 | $class = get_class($record); |
||
340 | return $this->owner->httpError(403, 'Action not available on ' . $class . '. It must be one of :' . implode(',', $this->getAvailableActions($definedActions))); |
||
341 | } |
||
342 | $message = null; |
||
343 | $error = false; |
||
344 | |||
345 | // Check record BEFORE the action |
||
346 | // It can be deleted by the action and it will return to the list |
||
347 | $isNewRecord = $record->ID == 0; |
||
348 | |||
349 | try { |
||
350 | $result = $record->$action($data, $form, $controller); |
||
351 | |||
352 | // We have a response |
||
353 | if ($result && $result instanceof HTTPResponse) { |
||
354 | return $result; |
||
355 | } |
||
356 | |||
357 | if ($result === false) { |
||
358 | // Result returned an error (false) |
||
359 | $error = true; |
||
360 | $message = _t( |
||
361 | 'ActionsGridFieldItemRequest.FAILED', |
||
362 | 'Action {action} failed on {name}', |
||
363 | array( |
||
364 | 'action' => $clickedAction->getTitle(), |
||
365 | 'name' => $record->i18n_singular_name(), |
||
366 | ) |
||
367 | ); |
||
368 | } elseif (is_string($result)) { |
||
369 | // Result is a message |
||
370 | $message = $result; |
||
371 | } |
||
372 | } catch (Exception $ex) { |
||
373 | $error = true; |
||
374 | $message = $ex->getMessage(); |
||
375 | } |
||
376 | |||
377 | // Build default message |
||
378 | if (!$message) { |
||
379 | $message = _t( |
||
380 | 'ActionsGridFieldItemRequest.DONE', |
||
381 | 'Action {action} was done on {name}', |
||
382 | array( |
||
383 | 'action' => $clickedAction->getTitle(), |
||
384 | 'name' => $record->i18n_singular_name(), |
||
385 | ) |
||
386 | ); |
||
387 | } |
||
388 | $status = 'good'; |
||
389 | if ($error) { |
||
390 | $status = 'bad'; |
||
391 | } |
||
392 | // We don't have a form, simply return the result |
||
393 | if (!$form) { |
||
394 | if ($error) { |
||
395 | return $this->owner->httpError(403, $message); |
||
396 | } |
||
397 | return $message; |
||
398 | } |
||
399 | if (Director::is_ajax()) { |
||
400 | $controller = $this->getToplevelController(); |
||
401 | $controller->getResponse()->addHeader('X-Status', rawurlencode($message)); |
||
402 | if (method_exists($clickedAction, 'getShouldRefresh') && $clickedAction->getShouldRefresh()) { |
||
403 | $controller->getResponse()->addHeader('X-Reload', true); |
||
404 | } |
||
405 | } else { |
||
406 | $form->sessionMessage($message, $status, ValidationResult::CAST_HTML); |
||
407 | } |
||
408 | // Redirect after action |
||
409 | return $this->redirectAfterAction($isNewRecord); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Handles custom links |
||
414 | * |
||
415 | * Use CustomLink with default behaviour to trigger this |
||
416 | * |
||
417 | * See: |
||
418 | * DefaultLink::getModelLink |
||
419 | * GridFieldCustomLink::getLink |
||
420 | * |
||
421 | * @param HTTPRequest $request |
||
422 | * @return HTTPResponse|DBHTMLText |
||
423 | */ |
||
424 | public function doCustomLink(HTTPRequest $request) |
||
425 | { |
||
426 | $action = $request->getVar('CustomLink'); |
||
427 | return $this->forwardActionToRecord($action); |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * Handles custom actions |
||
432 | * |
||
433 | * Use CustomAction class to trigger this |
||
434 | * |
||
435 | * @param array The form data |
||
436 | * @param Form The form object |
||
437 | * @return HTTPResponse|DBHTMLText |
||
438 | */ |
||
439 | public function doCustomAction($data, $form) |
||
440 | { |
||
441 | $action = key($data['action_doCustomAction']); |
||
442 | return $this->forwardActionToRecord($action, $data, $form); |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Saves the form and goes back to list view |
||
447 | * |
||
448 | * @param array The form data |
||
449 | * @param Form The form object |
||
450 | */ |
||
451 | public function doSaveAndClose($data, $form) |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Saves the form and goes back to the next item |
||
462 | * |
||
463 | * @param array The form data |
||
464 | * @param Form The form object |
||
465 | */ |
||
466 | public function doSaveAndNext($data, $form) |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Saves the form and goes to the previous item |
||
489 | * |
||
490 | * @param array The form data |
||
491 | * @param Form The form object |
||
492 | */ |
||
493 | public function doSaveAndPrev($data, $form) |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Gets the top level controller. |
||
516 | * |
||
517 | * @return Controller |
||
518 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
519 | * because it is a protected method and not visible to a decorator! |
||
520 | */ |
||
521 | protected function getToplevelController() |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Gets the back link |
||
532 | * |
||
533 | * @return string |
||
534 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
535 | * because it is a protected method and not visible to a decorator! |
||
536 | */ |
||
537 | public function getBackLink() |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Response object for this request after a successful save |
||
558 | * |
||
559 | * @param bool $isNewRecord True if this record was just created |
||
560 | * @return HTTPResponse|DBHTMLText |
||
561 | * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
||
562 | * because it is a protected method and not visible to a decorator! |
||
563 | */ |
||
564 | protected function redirectAfterAction($isNewRecord) |
||
580 | } |
||
581 | } |
||
582 | } |
||
583 |