From 691032551b59e91aacf806d532ceb2d4c52ffb77 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Thu, 14 Jan 2021 11:33:51 +0100 Subject: [PATCH] chg: Added support of displayOnSuccess, non-dismissable modals and some house cleaning --- src/Controller/AuthKeysController.php | 6 +- src/Controller/Component/CRUDComponent.php | 24 +++++--- .../Component/RestResponseComponent.php | 5 +- templates/AuthKeys/authkey_display.php | 18 ++++-- templates/AuthKeys/index.php | 1 - .../element/genericElements/genericModal.php | 6 +- webroot/js/bootstrap-helper.js | 56 +++++++++++++------ 7 files changed, 80 insertions(+), 36 deletions(-) diff --git a/src/Controller/AuthKeysController.php b/src/Controller/AuthKeysController.php index 27bb57d..ca562b0 100644 --- a/src/Controller/AuthKeysController.php +++ b/src/Controller/AuthKeysController.php @@ -17,7 +17,7 @@ class AuthKeysController extends AppController public function index() { $this->CRUD->index([ - 'filters' => ['users.username', 'authkey', 'comment', 'users.id'], + 'filters' => ['Users.username', 'authkey', 'comment', 'Users.id'], 'quickFilters' => ['authkey', 'comment'], 'contain' => ['Users'], 'exclude_fields' => ['authkey'] @@ -45,7 +45,9 @@ class AuthKeysController extends AppController $this->CRUD->add([ 'displayOnSuccess' => 'authkey_display' ]); - $responsePayload = $this->CRUD->getResponsePayload(); + $responsePayload = $this->CRUD->getResponsePayload([ + 'displayOnSuccess' => 'authkey_display' + ]); if (!empty($responsePayload)) { return $responsePayload; } diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index faafe66..47e3650 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -6,6 +6,7 @@ use Cake\Controller\Component; use Cake\Error\Debugger; use Cake\Utility\Hash; use Cake\Utility\Inflector; +use Cake\View\ViewBuilder; class CRUDComponent extends Component { @@ -108,17 +109,16 @@ class CRUDComponent extends Component $this->saveMetaFields($data->id, $input); } if ($this->Controller->ParamHandler->isRest()) { - $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); + $this->Controller->restResponsePayload = $this->RestResponse->viewData($savedData, 'json'); } else if ($this->Controller->ParamHandler->isAjax()) { - $this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'add', $savedData, $message); + if (!empty($params['displayOnSuccess'])) { + $displayOnSuccess = $this->renderViewInVariable($params['displayOnSuccess'], ['entity' => $data]); + $this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'add', $savedData, $message, ['displayOnSuccess' => $displayOnSuccess]); + } else { + $this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'add', $savedData, $message); + } } else { $this->Controller->Flash->success($message); - if (!empty($params['displayOnSuccess'])) { - $this->Controller->set('entity', $data); - $this->Controller->set('referer', $this->Controller->referer()); - $this->Controller->render($params['displayOnSuccess']); - return; - } if (empty($params['redirect'])) { $this->Controller->redirect(['action' => 'view', $data->id]); } else { @@ -503,4 +503,12 @@ class CRUDComponent extends Component return $this->Table->find()->distinct([$field])->all()->extract($field)->toList(); } } + + private function renderViewInVariable($templateRelativeName, $data) + { + $builder = new ViewBuilder(); + $builder->disableAutoLayout()->setTemplate("{$this->TableAlias}/{$templateRelativeName}"); + $view = $builder->build($data); + return $view->render(); + } } diff --git a/src/Controller/Component/RestResponseComponent.php b/src/Controller/Component/RestResponseComponent.php index 94b6efc..ef9bf53 100644 --- a/src/Controller/Component/RestResponseComponent.php +++ b/src/Controller/Component/RestResponseComponent.php @@ -420,7 +420,7 @@ class RestResponseComponent extends Component return $this->__sendResponse($response, 200, $format); } - public function ajaxSuccessResponse($ObjectAlias, $action, $entity, $message) + public function ajaxSuccessResponse($ObjectAlias, $action, $entity, $message, $additionalData=[]) { $action = $this->__dissectAdminRouting($action); $response = [ @@ -429,6 +429,9 @@ class RestResponseComponent extends Component 'data' => $entity->toArray(), 'url' => $this->__generateURL($action, $ObjectAlias, $entity->id) ]; + if (!empty($additionalData)) { + $response['additionalData'] = $additionalData; + } return $this->viewData($response); } diff --git a/templates/AuthKeys/authkey_display.php b/templates/AuthKeys/authkey_display.php index e131596..2072d17 100644 --- a/templates/AuthKeys/authkey_display.php +++ b/templates/AuthKeys/authkey_display.php @@ -1,5 +1,13 @@ -

-

-

-

%s', __('Authkey'), h($entity->authkey_raw)) ?>

- +element('genericElements/genericModal', [ + 'title' => __('Authkey created'), + 'body' => sprintf( + '

%s

%s

%s

', + __('Please make sure that you note down the authkey below, this is the only time the authkey is shown in plain text, so make sure you save it. If you lose the key, simply remove the entry and generate a new one.'), + __('Cerebrate will use the first and the last 4 digit for identification purposes.'), + sprintf('%s: %s', __('Authkey'), h($entity->authkey_raw)) + ), + 'actionButton' => sprintf('%s', __('I have noted down my key, take me back now')), + 'noCancel' => true, + 'staticBackdrop' => true, +]); diff --git a/templates/AuthKeys/index.php b/templates/AuthKeys/index.php index 0964ca1..b5fbc35 100644 --- a/templates/AuthKeys/index.php +++ b/templates/AuthKeys/index.php @@ -10,7 +10,6 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'data' => [ 'type' => 'simple', 'text' => __('Add authentication key'), - 'class' => 'btn btn-primary', 'popover_url' => '/authKeys/add' ] ] diff --git a/templates/element/genericElements/genericModal.php b/templates/element/genericElements/genericModal.php index b12ec15..36c71a8 100644 --- a/templates/element/genericElements/genericModal.php +++ b/templates/element/genericElements/genericModal.php @@ -1,4 +1,4 @@ -