From 41948044cdfdd33a940cc8c41f30fda35301d2ec Mon Sep 17 00:00:00 2001 From: mokaddem Date: Mon, 18 Jan 2021 17:26:35 +0100 Subject: [PATCH] fix: [sharinggroups] Possibility to remove/add org from sharinggroup --- src/Controller/SharingGroupsController.php | 48 ++++++++++++++++++++-- templates/SharingGroups/index.php | 1 - templates/SharingGroups/list_orgs.php | 5 ++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/Controller/SharingGroupsController.php b/src/Controller/SharingGroupsController.php index 5f246f7..75916ba 100644 --- a/src/Controller/SharingGroupsController.php +++ b/src/Controller/SharingGroupsController.php @@ -2,6 +2,7 @@ namespace App\Controller; use App\Controller\AppController; +use Cake\Utility\Inflector; use Cake\Utility\Hash; use Cake\Utility\Text; use \Cake\Database\Expression\QueryExpression; @@ -115,9 +116,12 @@ class SharingGroupsController extends AppController } if ($this->ParamHandler->isRest() || $this->ParamHandler->isAjax()) { if ($result) { - $this->RestResponse->saveSuccessResponse('SharingGroups', 'addOrg', $id, 'json', $message); + $savedData = $this->SharingGroups->get($id, [ + 'contain' => 'SharingGroupOrgs' + ]); + return $this->RestResponse->ajaxSuccessResponse(Inflector::singularize($this->SharingGroups->getAlias()), 'addOrg', $savedData, $message); } else { - $this->RestResponse->saveFailResponse('SharingGroups', 'addOrg', $id, $message, 'json'); + return $this->RestResponse->ajaxFailResponse(Inflector::singularize($this->SharingGroups->getAlias()), 'addOrg', $sharingGroup, $message);; } } else { if ($result) { @@ -131,9 +135,45 @@ class SharingGroupsController extends AppController $this->set(compact('dropdownData')); } - public function removeOrg($id) + public function removeOrg($id, $org_id) { - + $sharingGroup = $this->SharingGroups->get($id, [ + 'contain' => 'SharingGroupOrgs' + ]); + if ($this->request->is('post')) { + $org = $this->SharingGroups->SharingGroupOrgs->get($org_id); + $result = (bool)$this->SharingGroups->SharingGroupOrgs->unlink($sharingGroup, [$org]); + if ($result) { + $message = __('Organisation(s) removed from the sharing group.'); + } else { + $message = __('Organisation(s) could not be removed to the sharing group.'); + } + if ($this->ParamHandler->isRest() || $this->ParamHandler->isAjax()) { + if ($result) { + $savedData = $this->SharingGroups->get($id, [ + 'contain' => 'SharingGroupOrgs' + ]); + return $this->RestResponse->ajaxSuccessResponse(Inflector::singularize($this->SharingGroups->getAlias()), 'removeOrg', $savedData, $message); + } else { + return $this->RestResponse->ajaxFailResponse(Inflector::singularize($this->SharingGroups->getAlias()), 'removeOrg', $sharingGroup, $message); + ; + } + } else { + if ($result) { + $this->Flash->success($message); + } else { + $this->Flash->error($message); + } + $this->redirect(['action' => 'view', $id]); + } + } + $this->set('scope', 'sharing_groups'); + $this->set('id', $org_id); + $this->set('sharingGroup', $sharingGroup); + $this->set('deletionText', __('Are you sure you want to remove Organisation #{0} from Sharing group #{1}?', $org_id, $sharingGroup['id'])); + $this->set('postLinkParameters', ['action' => 'removeOrg', $id, $org_id]); + $this->viewBuilder()->setLayout('ajax'); + $this->render('/genericTemplates/delete'); } public function listOrgs($id) diff --git a/templates/SharingGroups/index.php b/templates/SharingGroups/index.php index 357e79f..6076c6c 100644 --- a/templates/SharingGroups/index.php +++ b/templates/SharingGroups/index.php @@ -10,7 +10,6 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'data' => [ 'type' => 'simple', 'text' => __('Add sharing group'), - 'class' => 'btn btn-primary', 'popover_url' => '/SharingGroups/add' ] ] diff --git a/templates/SharingGroups/list_orgs.php b/templates/SharingGroups/list_orgs.php index 0c6fd96..1c0658f 100644 --- a/templates/SharingGroups/list_orgs.php +++ b/templates/SharingGroups/list_orgs.php @@ -11,8 +11,8 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'data' => [ 'type' => 'simple', 'text' => __('Add member'), - 'class' => 'btn btn-primary', - 'popover_url' => '/sharingGroups/addOrg/' . h($sharing_group_id) + 'popover_url' => '/sharingGroups/addOrg/' . h($sharing_group_id), + 'reload_url' => '/sharingGroups/listOrgs/' . h($sharing_group_id) ] ] ], @@ -54,6 +54,7 @@ echo $this->element('genericElements/IndexTable/index_table', [ [ 'open_modal' => '/sharingGroups/removeOrg/' . h($sharing_group_id) . '/[onclick_params_data_path]', 'modal_params_data_path' => 'id', + 'reload_url' => '/sharingGroups/listOrgs/' . h($sharing_group_id), 'icon' => 'trash' ], ]