From cb1a0e7c7c9f390fe0e518e85946b2c0a1049272 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 26 Oct 2022 18:24:05 +1300 Subject: [PATCH] Port search kit fix Port of https://github.com/civicrm/civicrm-core/pull/24758 (which will be in stable releases from 5.56 - but we will be on 5.54.x until 2023. Bug: T295412 Change-Id: I369b720c5ba6080c0a0f6dc8e275e55ba4dbcc5a --- .../Action/SearchDisplay/GetSearchTasks.php | 52 ++++++++++++++++--- .../crmSearchTaskApiBatch.ctrl.js | 28 ++++++++++ .../crmSearchTasks/crmSearchTaskApiBatch.html | 13 +++++ .../crmSearchTaskDelete.ctrl.js | 22 -------- .../crmSearchTasks/crmSearchTaskDelete.html | 13 ----- .../crmSearchTasks.component.js | 10 ++-- 6 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskApiBatch.ctrl.js create mode 100644 drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskApiBatch.html delete mode 100644 drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskDelete.ctrl.js delete mode 100644 drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskDelete.html diff --git a/drupal/sites/all/modules/civicrm/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php b/drupal/sites/all/modules/civicrm/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php index 9db93923b5..4531bf2fe2 100644 --- a/drupal/sites/all/modules/civicrm/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php +++ b/drupal/sites/all/modules/civicrm/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php @@ -79,10 +79,16 @@ public function _run(\Civi\Api4\Generic\Result $result) { if (array_key_exists('delete', $entity['actions'])) { $tasks[$entity['name']]['delete'] = [ - 'module' => 'crmSearchTasks', 'title' => E::ts('Delete %1', [1 => $entity['title_plural']]), 'icon' => 'fa-trash', - 'uiDialog' => ['templateUrl' => '~/crmSearchTasks/crmSearchTaskDelete.html'], + 'apiBatch' => [ + 'action' => 'delete', + 'params' => NULL, + 'confirmMsg' => E::ts('Are you sure you want to delete %1 %2?'), + 'runMsg' => E::ts('Deleting %1 %2...'), + 'successMsg' => E::ts('Successfully deleted %1 %2.'), + 'errorMsg' => E::ts('An error occurred while attempting to delete %1 %2.'), + ], ]; } @@ -168,17 +174,49 @@ public function _run(\Civi\Api4\Generic\Result $result) { $null, $null, $null, 'civicrm_searchKitTasks' ); - usort($tasks[$entity['name']], function($a, $b) { - return strnatcasecmp($a['title'], $b['title']); - }); - foreach ($tasks[$entity['name']] as $name => &$task) { $task['name'] = $name; // Add default for number of rows action requires $task += ['number' => '> 0']; } - $result->exchangeArray(array_values($tasks[$entity['name']])); + usort($tasks[$entity['name']], function($a, $b) { + return strnatcasecmp($a['title'], $b['title']); + }); + + $result->exchangeArray($tasks[$entity['name']]); + } + + public static function fields() { + return [ + [ + 'name' => 'name', + ], + [ + 'name' => 'module', + ], + [ + 'name' => 'title', + ], + [ + 'name' => 'icon', + ], + [ + 'number' => 'icon', + ], + [ + 'name' => 'apiBatch', + 'data_type' => 'Array', + ], + [ + 'name' => 'uiDialog', + 'data_type' => 'Array', + ], + [ + 'name' => 'crmPopup', + 'data_type' => 'Array', + ], + ]; } } diff --git a/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskApiBatch.ctrl.js b/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskApiBatch.ctrl.js new file mode 100644 index 0000000000..cb06c22f53 --- /dev/null +++ b/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskApiBatch.ctrl.js @@ -0,0 +1,28 @@ +(function(angular, $, _) { + "use strict"; + + // Generic controller for running an ApiBatch task + angular.module('crmSearchTasks').controller('crmSearchTaskApiBatch', function($scope, searchTaskBaseTrait) { + var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), + // Combine this controller with model properties (ids, entity, entityInfo) and searchTaskBaseTrait + ctrl = angular.extend(this, $scope.model, searchTaskBaseTrait); + + this.entityTitle = this.getEntityTitle(); + + // If no confirmation message, skip straight to processing + if (!ctrl.apiBatch.confirmMsg) { + ctrl.start(ctrl.apiBatch.params); + } + + this.onSuccess = function() { + CRM.alert(ts(ctrl.apiBatch.successMsg, {1: ctrl.ids.length, 2: ctrl.entityTitle}), ts('%1 Complete', {1: ctrl.taskTitle}), 'success'); + this.close(); + }; + + this.onError = function() { + CRM.alert(ts(ctrl.apiBatch.errorMsg, {1: ctrl.ids.length, 2: ctrl.entityTitle}), ts('Error'), 'error'); + this.cancel(); + }; + + }); +})(angular, CRM.$, CRM._); diff --git a/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskApiBatch.html b/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskApiBatch.html new file mode 100644 index 0000000000..a4ce4c5aab --- /dev/null +++ b/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskApiBatch.html @@ -0,0 +1,13 @@ +
+
+

{{:: ts(model.apiBatch.confirmMsg, {1: model.ids.length, 2: $ctrl.entityTitle}) }}

+
+
+
{{:: ts(model.apiBatch.runMsg, {1: model.ids.length, 2: $ctrl.entityTitle}) }}
+ +
+ + + +
+
diff --git a/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskDelete.ctrl.js b/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskDelete.ctrl.js deleted file mode 100644 index ddce87d984..0000000000 --- a/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskDelete.ctrl.js +++ /dev/null @@ -1,22 +0,0 @@ -(function(angular, $, _) { - "use strict"; - - angular.module('crmSearchTasks').controller('crmSearchTaskDelete', function($scope, searchTaskBaseTrait) { - var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), - // Combine this controller with model properties (ids, entity, entityInfo) and searchTaskBaseTrait - ctrl = angular.extend(this, $scope.model, searchTaskBaseTrait); - - this.entityTitle = this.getEntityTitle(); - - this.onSuccess = function() { - CRM.alert(ts('Successfully deleted %1 %2.', {1: ctrl.ids.length, 2: ctrl.entityTitle}), ts('Deleted'), 'success'); - this.close(); - }; - - this.onError = function() { - CRM.alert(ts('An error occurred while attempting to delete %1 %2.', {1: ctrl.ids.length, 2: ctrl.entityTitle}), ts('Error'), 'error'); - this.cancel(); - }; - - }); -})(angular, CRM.$, CRM._); diff --git a/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskDelete.html b/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskDelete.html deleted file mode 100644 index e15415ce34..0000000000 --- a/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTaskDelete.html +++ /dev/null @@ -1,13 +0,0 @@ -
-
-

{{:: ts('Are you sure you want to delete %1 %2?', {1: model.ids.length, 2: $ctrl.entityTitle}) }}

-
-
-
{{:: ts('Deleting %1 %2...', {1: model.ids.length, 2: $ctrl.entityTitle}) }}
- -
- - - -
-
diff --git a/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTasks.component.js b/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTasks.component.js index 639bc4cfdd..83d189c5d6 100644 --- a/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTasks.component.js +++ b/drupal/sites/all/modules/civicrm/ext/search_kit/ang/crmSearchTasks/crmSearchTasks.component.js @@ -30,7 +30,7 @@ } initialized = true; crmApi4({ - entityInfo: ['Entity', 'get', {select: ['name', 'title', 'title_plural'], where: [['name', '=', ctrl.entity]]}, 0], + entityInfo: ['Entity', 'get', {select: ['name', 'title', 'title_plural', 'primary_key'], where: [['name', '=', ctrl.entity]]}, 0], tasks: ['SearchDisplay', 'getSearchTasks', {entity: ctrl.entity}] }).then(function(result) { ctrl.entityInfo = result.entityInfo; @@ -61,7 +61,9 @@ search: ctrl.search, display: ctrl.display, displayController: ctrl.displayController, - entityInfo: ctrl.entityInfo + entityInfo: ctrl.entityInfo, + taskTitle: action.title, + apiBatch: _.cloneDeep(action.apiBatch) }; // If action uses a crmPopup form if (action.crmPopup) { @@ -71,13 +73,13 @@ .on('crmFormSuccess', ctrl.refresh); } // If action uses dialogService - else if (action.uiDialog) { + else { var options = CRM.utils.adjustDialogDefaults({ autoOpen: false, dialogClass: 'crm-search-task-dialog', title: action.title }); - dialogService.open('crmSearchTask', action.uiDialog.templateUrl, data, options) + dialogService.open('crmSearchTask', (action.uiDialog && action.uiDialog.templateUrl) || '~/crmSearchTasks/crmSearchTaskApiBatch.html', data, options) // Reload results on success, do nothing on cancel .then(ctrl.refresh, _.noop); }