diff --git a/src/Controller/BroodsController.php b/src/Controller/BroodsController.php index d9d2cd8..80400f1 100644 --- a/src/Controller/BroodsController.php +++ b/src/Controller/BroodsController.php @@ -14,10 +14,17 @@ class BroodsController extends AppController $this->CRUD->index([ 'filters' => ['name', 'uuid', 'url', 'description', 'Organisations.id', 'trusted', 'pull', 'authkey'], 'quickFilters' => ['name', 'uuid', 'description'], + 'contextFilters' => [ + 'fields' => [ + 'pull', + 'Organisations.name', + ] + ], 'contain' => ['Organisations'] ]); - if ($this->ParamHandler->isRest()) { - return $this->restResponsePayload; + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; } $this->set('metaGroup', 'Sync'); } @@ -25,8 +32,9 @@ class BroodsController extends AppController public function add() { $this->CRUD->add(); - if ($this->ParamHandler->isRest()) { - return $this->restResponsePayload; + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; } $this->set('metaGroup', 'Sync'); $this->loadModel('Organisations'); @@ -41,8 +49,9 @@ class BroodsController extends AppController public function view($id) { $this->CRUD->view($id, ['contain' => ['Organisations']]); - if ($this->ParamHandler->isRest()) { - return $this->restResponsePayload; + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; } $this->set('metaGroup', 'Sync'); } @@ -50,8 +59,9 @@ class BroodsController extends AppController public function edit($id) { $this->CRUD->edit($id); - if ($this->ParamHandler->isRest()) { - return $this->restResponsePayload; + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; } $this->set('metaGroup', 'Sync'); $this->render('add'); @@ -60,8 +70,9 @@ class BroodsController extends AppController public function delete($id) { $this->CRUD->delete($id); - if ($this->ParamHandler->isRest()) { - return $this->restResponsePayload; + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; } $this->set('metaGroup', 'Sync'); } diff --git a/src/Model/Table/BroodsTable.php b/src/Model/Table/BroodsTable.php index 51424f1..243148b 100644 --- a/src/Model/Table/BroodsTable.php +++ b/src/Model/Table/BroodsTable.php @@ -44,6 +44,10 @@ class BroodsTable extends AppTable 'error' => __('Authentication failure'), 'reason' => __('Invalid user credentials.') ], + 404 => [ + 'error' => __('Not found'), + 'reason' => __('Incorrect URL or proxy error') + ], 405 => [ 'error' => __('Insufficient privileges'), 'reason' => __('The remote user account doesn\'t have the required privileges to synchronise.') diff --git a/templates/Broods/index.php b/templates/Broods/index.php index 9a3e103..8e873d4 100644 --- a/templates/Broods/index.php +++ b/templates/Broods/index.php @@ -3,7 +3,6 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'data' => [ 'data' => $data, 'top_bar' => [ - 'pull' => 'right', 'children' => [ [ 'type' => 'simple', @@ -11,11 +10,14 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'data' => [ 'type' => 'simple', 'text' => __('Add brood'), - 'class' => 'btn btn-primary', 'popover_url' => '/broods/add' ] ] ], + [ + 'type' => 'context_filters', + 'context_filters' => $filteringContexts + ], [ 'type' => 'search', 'button' => __('Filter'), @@ -67,15 +69,15 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'icon' => 'eye' ], [ - 'onclick' => 'populateAndLoadModal(\'/broods/edit/[onclick_params_data_path]\');', - 'onclick_params_data_path' => 'id', + 'open_modal' => '/broods/edit/[onclick_params_data_path]', + 'modal_params_data_path' => 'id', 'icon' => 'edit' ], [ - 'onclick' => 'populateAndLoadModal(\'/broods/delete/[onclick_params_data_path]\');', - 'onclick_params_data_path' => 'id', + 'open_modal' => '/broods/delete/[onclick_params_data_path]', + 'modal_params_data_path' => 'id', 'icon' => 'trash' - ] + ], ] ] ]); diff --git a/webroot/js/api-helper.js b/webroot/js/api-helper.js index 1a01f5d..3cff8d9 100644 --- a/webroot/js/api-helper.js +++ b/webroot/js/api-helper.js @@ -168,7 +168,7 @@ class AJAXApi { this.provideFeedback({ variant: 'danger', title: 'There has been a problem with the operation', - body: error + body: error.message }, true, skipFeedback); toReturn = Promise.reject(error); } finally { @@ -205,7 +205,7 @@ class AJAXApi { this.provideFeedback({ variant: 'danger', title: 'There has been a problem with the operation', - body: error + body: error.message }, true, skipFeedback); toReturn = Promise.reject(error); } finally { @@ -244,7 +244,7 @@ class AJAXApi { this.provideFeedback({ variant: 'danger', title: 'There has been a problem with the operation', - body: error + body: error.message }, true, skipFeedback); toReturn = Promise.reject(error); } finally { @@ -304,7 +304,7 @@ class AJAXApi { this.provideFeedback({ variant: 'danger', title: 'There has been a problem with the operation', - body: error + body: error.message }, true, feedbackShown); toReturn = Promise.reject(error); } @@ -312,7 +312,7 @@ class AJAXApi { this.provideFeedback({ variant: 'danger', title: 'There has been a problem with the operation', - body: error + body: error.message }, true, feedbackShown); toReturn = Promise.reject(error); } diff --git a/webroot/js/bootstrap-helper.js b/webroot/js/bootstrap-helper.js index 2e42a69..698911c 100644 --- a/webroot/js/bootstrap-helper.js +++ b/webroot/js/bootstrap-helper.js @@ -120,6 +120,7 @@ class UIFactory { promise.finally(() => { loadingOverlay.hide() }) + return promise } } diff --git a/webroot/js/main.js b/webroot/js/main.js index fa86bf3..333074c 100644 --- a/webroot/js/main.js +++ b/webroot/js/main.js @@ -27,35 +27,51 @@ function executeStateDependencyChecks(dependenceSourceSelector) { } function testConnection(id) { - $.ajax({ - url: '/broods/testConnection/' + id, - type: 'GET', - beforeSend: function () { - $("#connection_test_" + id).html('Running test...'); - }, - error: function(){ - $("#connection_test_" + id).html('Internal error'); - }, - success: function(result) { - var html = ''; - if (result['error']) { - html += 'Status: OK (' + $("").text(result['ping']).html() + ' ms)
'; - html += 'Status: Error: ' + result['error'] + ''; - html += 'Reason: ' + result['reason'] + ''; - } else { - html += 'Status: OK (' + $("").text(result['ping']).html() + ' ms)
'; - html += 'Remote: ' + $("").text(result['response']['application']).html() + ' v' + $("").text(result['response']['version']).html() + '
'; - html += 'User: ' + $("").text(result['response']['user']).html() + ' (' + $("").text(result['response']['role']['name']).html() + ')' + '
'; - var canSync = result['response']['role']['perm_admin'] || result['response']['role']['perm_sync']; - if (canSync) { - html += 'Sync permission: Yes
'; - } else { - html += 'Sync permission: No
'; - } - } - $("#connection_test_" + id).html(html); - } + $container = $(`#connection_test_${id}`) + UI.overlayUntilResolve( + $container[0], + AJAXApi.quickFetchJSON(`/broods/testConnection/${id}`), + {text: 'Running test'} + ).then(result => { + const $testResult = attachTestConnectionResultHtml(result, $container) + $(`#connection_test_${id}`).append($testResult) }) + .catch((error) => { + const $testResult = attachTestConnectionResultHtml(error.message, $container) + $(`#connection_test_${id}`).append($testResult) + }) +} + +function attachTestConnectionResultHtml(result, $container) { + function getKVHtml(key, value, valueClasses=[], extraValue='') { + return $('
').append( + $('').text(key + ': '), + $('').addClass(valueClasses).text(value), + $('').text(extraValue.length > 0 ? ` (${extraValue})` : '') + ) + } + $container.find('div.tester-result').remove() + $testResultDiv = $('
'); + if (typeof result !== 'object') { + $testResultDiv.append(getKVHtml('Internal error', result, ['text-danger font-weight-bold'])) + } else { + if (result['error']) { + $testResultDiv.append( + getKVHtml('Status', 'OK', ['text-danger'], `${result['ping']} ms`), + getKVHtml('Status', `Error: ${result['error']}`, ['text-danger']), + getKVHtml('Reason', result['reason'], ['text-danger']) + ) + } else { + const canSync = result['response']['role']['perm_admin'] || result['response']['role']['perm_sync']; + $testResultDiv.append( + getKVHtml('Status', 'OK', ['text-success'], `${result['ping']} ms`), + getKVHtml('Remote', `${result['response']['application']} v${result['response']['version']}`), + getKVHtml('User', result['response']['user'], [], result['response']['role']['name']), + getKVHtml('Sync permission', (canSync ? 'Yes' : 'No'), [(canSync ? 'text-success' : 'text-danger')]), + ) + } + } + return $testResultDiv } var UI