diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 1095852..790e96c 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -567,7 +567,6 @@ class CRUDComponent extends Component foreach ($ids as $id) { $data = $this->Table->get($id); $success = $this->Table->delete($data); - $success = true; if ($success) { $bulkSuccesses++; } @@ -585,7 +584,11 @@ class CRUDComponent extends Component Inflector::pluralize($this->ObjectAlias) ) ); - $this->setResponseForController('delete', $bulkSuccesses, $message, $data); + $additionalData = []; + if ($bulkSuccesses > 0) { + $additionalData['redirect'] = Router::url(['controller' => $this->Controller->getName(), 'action' => 'index']); + } + $this->setResponseForController('delete', $bulkSuccesses, $message, $data, null, $additionalData); } $this->Controller->set('metaGroup', 'ContactDB'); $this->Controller->set('scope', 'users'); @@ -738,13 +741,16 @@ class CRUDComponent extends Component $this->Controller->render('/genericTemplates/tag'); } - public function setResponseForController($action, $success, $message, $data = [], $errors = null) + public function setResponseForController($action, $success, $message, $data = [], $errors = null, $additionalData = []) { if ($success) { if ($this->Controller->ParamHandler->isRest()) { $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); } elseif ($this->Controller->ParamHandler->isAjax()) { - $this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxSuccessResponse($this->ObjectAlias, $action, $data, $message); + if ($additionalData['redirect']) { // If a redirection occors, we need to make sure the flash message gets displayed + $this->Controller->Flash->success($message); + } + $this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxSuccessResponse($this->ObjectAlias, $action, $data, $message, $additionalData); } else { $this->Controller->Flash->success($message); $this->Controller->redirect($this->Controller->referer()); @@ -753,6 +759,9 @@ class CRUDComponent extends Component if ($this->Controller->ParamHandler->isRest()) { $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); } elseif ($this->Controller->ParamHandler->isAjax()) { + if ($additionalData['redirect']) { // If a redirection occors, we need to make sure the flash message gets displayed + $this->Controller->Flash->error($message); + } $this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, $action, $data, $message, !is_null($errors) ? $errors : $data->getErrors()); } else { $this->Controller->Flash->error($message); diff --git a/webroot/js/bootstrap-helper.js b/webroot/js/bootstrap-helper.js index 4762df5..1d5a2ae 100644 --- a/webroot/js/bootstrap-helper.js +++ b/webroot/js/bootstrap-helper.js @@ -185,10 +185,14 @@ class UIFactory { submissionReloaderModal(url, reloadUrl, $reloadedElement, $statusNode=null) { const successCallback = function ([data, modalObject]) { UI.reload(reloadUrl, $reloadedElement, $statusNode) - if (data.additionalData !== undefined && data.additionalData.displayOnSuccess !== undefined) { - UI.modal({ - rawHtml: data.additionalData.displayOnSuccess - }) + if (data.additionalData !== undefined) { + if (data.additionalData.displayOnSuccess !== undefined) { + UI.modal({ + rawHtml: data.additionalData.displayOnSuccess + }) + } else if (data.additionalData.redirect !== undefined) { + window.location = data.additionalData.redirect + } } } return UI.submissionModal(url, successCallback)