From e71c81f3dcde95182df1edcaed6403ae75490cbd Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 7 Feb 2023 18:24:57 -0500 Subject: [PATCH] SearchKit - Configurable action menu tasks per-search-display --- .../Action/SearchDisplay/GetSearchTasks.php | 18 +++--- .../SearchDisplayTasksSubscriber.php | 48 +++++++++++++++ .../searchAdminTasksConfig.component.js | 58 +++++++++++++++++++ .../common/searchAdminTasksConfig.html | 26 +++++++++ .../displays/searchAdminDisplayTable.html | 9 +-- 5 files changed, 145 insertions(+), 14 deletions(-) create mode 100644 ext/search_kit/Civi/Api4/Event/Subscriber/SearchDisplayTasksSubscriber.php create mode 100644 ext/search_kit/ang/crmSearchAdmin/displays/common/searchAdminTasksConfig.component.js create mode 100644 ext/search_kit/ang/crmSearchAdmin/displays/common/searchAdminTasksConfig.html diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php index b2ca9c97c0..f1fb340a88 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php @@ -206,13 +206,15 @@ class GetSearchTasks extends \Civi\Api4\Generic\AbstractAction { } } - // Call `hook_civicrm_searchKitTasks`. - // Note - this hook serves 2 purposes, both to augment this list of tasks AND to - // get a full list of Angular modules which provide tasks. That's why this hook needs - // the base-level array and not just the array of tasks for `$this->entity`. - // Although it may seem wasteful to have extensions add tasks for all possible entities and then - // discard most of it (all but the ones relevant to `$this->entity`), it's necessary to do it this way - // so that they can be declared as angular dependencies - see search_kit_civicrm_angularModules(). + // Call `hook_civicrm_searchKitTasks` which serves 3 purposes: + // 1. For extensions to augment this list of tasks + // 2. To allow tasks to be added/removed per search display + // Note: Use Events::W_LATE to do so after the tasks are filtered per search-display settings. + // 3. To get a full list of Angular modules which provide tasks. + // Note: That's why this hook needs the base-level array and not just the array of tasks for `$this->entity`. + // Although it may seem wasteful to have extensions add tasks for all possible entities and then + // discard most of it (all but the ones relevant to `$this->entity`), it's necessary to do it this way + // so that they can be declared as angular dependencies - see search_kit_civicrm_angularModules(). $null = NULL; $checkPermissions = $this->checkPermissions; $userId = $this->checkPermissions ? \CRM_Core_Session::getLoggedInContactID() : NULL; @@ -234,7 +236,7 @@ class GetSearchTasks extends \Civi\Api4\Generic\AbstractAction { $result->exchangeArray($tasks[$entity['name']]); } - public static function fields() { + public static function fields(): array { return [ [ 'name' => 'name', diff --git a/ext/search_kit/Civi/Api4/Event/Subscriber/SearchDisplayTasksSubscriber.php b/ext/search_kit/Civi/Api4/Event/Subscriber/SearchDisplayTasksSubscriber.php new file mode 100644 index 0000000000..832ce2808e --- /dev/null +++ b/ext/search_kit/Civi/Api4/Event/Subscriber/SearchDisplayTasksSubscriber.php @@ -0,0 +1,48 @@ + [ + ['filterTasksForDisplay', -50], + ], + ]; + } + + /** + * @param \Civi\Core\Event\GenericHookEvent $event + */ + public function filterTasksForDisplay(GenericHookEvent $event): void { + $enabledActions = $event->display['settings']['actions'] ?? NULL; + $entityName = $event->search['api_entity'] ?? NULL; + if ($entityName && is_array($enabledActions)) { + $event->tasks[$entityName] = array_intersect_key($event->tasks[$entityName], array_flip($enabledActions)); + } + } + +} diff --git a/ext/search_kit/ang/crmSearchAdmin/displays/common/searchAdminTasksConfig.component.js b/ext/search_kit/ang/crmSearchAdmin/displays/common/searchAdminTasksConfig.component.js new file mode 100644 index 0000000000..11887b9570 --- /dev/null +++ b/ext/search_kit/ang/crmSearchAdmin/displays/common/searchAdminTasksConfig.component.js @@ -0,0 +1,58 @@ +(function(angular, $, _) { + "use strict"; + + angular.module('crmSearchAdmin').component('searchAdminTasksConfig', { + bindings: { + display: '<', + apiEntity: '<', + apiParams: '<' + }, + templateUrl: '~/crmSearchAdmin/displays/common/searchAdminTasksConfig.html', + controller: function($scope, crmApi4, $timeout) { + var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), + ctrl = this; + + this.$onInit = function() { + crmApi4('SearchDisplay', 'getSearchTasks', { + entity: ctrl.apiEntity, + savedSearch: {api_entity: ctrl.apiEntity, api_params: ctrl.apiParams} + }).then(function(tasks) { + ctrl.allTasks = tasks; + }); + }; + + this.toggleActions = function() { + this.display.settings.actions = !this.display.settings.actions; + ctrl.menuOpen = false; + }; + + this.toggleTask = function(name) { + // Timeout waits for checkbox state to change, otherwise checkbox 'checked' property gets out-of-sync with angular model + $timeout(function() { + // Disabling one when all enabled, convert to array + if (typeof ctrl.display.settings.actions === 'boolean') { + ctrl.display.settings.actions = _.map(ctrl.allTasks, 'name'); + } + // Remove enabled task + if (ctrl.display.settings.actions.includes(name)) { + _.pull(ctrl.display.settings.actions, name); + } + // Add disabled task + else { + ctrl.display.settings.actions.push(name); + } + // All enabled, convert to boolean + if (ctrl.display.settings.actions.length === ctrl.allTasks.length) { + ctrl.display.settings.actions = true; + } + }); + }; + + this.isEnabled = function(name) { + return (typeof this.display.settings.actions === 'boolean') || this.display.settings.actions.includes(name); + }; + + } + }); + +})(angular, CRM.$, CRM._); diff --git a/ext/search_kit/ang/crmSearchAdmin/displays/common/searchAdminTasksConfig.html b/ext/search_kit/ang/crmSearchAdmin/displays/common/searchAdminTasksConfig.html new file mode 100644 index 0000000000..b6ae4adc2c --- /dev/null +++ b/ext/search_kit/ang/crmSearchAdmin/displays/common/searchAdminTasksConfig.html @@ -0,0 +1,26 @@ +
+ +
+
+
+ + +
+
diff --git a/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html b/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html index 9de37446b5..737dbdd479 100644 --- a/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html +++ b/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html @@ -10,12 +10,9 @@
-
- -
+ +
+
-- 2.25.1