From: Coleman Watts Date: Thu, 11 Mar 2021 20:09:32 +0000 (-0500) Subject: SearchKit - Refactor tasks list to use API callback X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=06ea32c;p=civicrm-core.git SearchKit - Refactor tasks list to use API callback --- diff --git a/ext/search/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php b/ext/search/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php new file mode 100644 index 0000000000..a65b4d5364 --- /dev/null +++ b/ext/search/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php @@ -0,0 +1,95 @@ +checkPermissions)->addWhere('name', '=', $this->entity) + ->addSelect('name', 'title_plural') + ->setChain(['actions' => [$this->entity, 'getActions', ['where' => [['name', 'IN', ['update', 'delete']]]], 'name']]) + ->execute()->first(); + + if (!$entity) { + return; + } + $tasks = []; + + if (array_key_exists($entity['name'], \CRM_Export_BAO_Export::getComponents())) { + $tasks[] = [ + 'name' => 'export', + 'title' => E::ts('Export %1', [1 => $entity['title_plural']]), + 'icon' => 'fa-file-excel-o', + 'crmPopup' => [ + 'path' => "'civicrm/export/standalone'", + 'query' => "{entity: {$entity['name']}, id: ids.join(',')}", + ], + ]; + } + + if (array_key_exists('update', $entity['actions'])) { + $tasks[] = [ + 'name' => 'update', + 'title' => E::ts('Update %1', [1 => $entity['title_plural']]), + 'icon' => 'fa-save', + 'uiDialog' => ['templateUrl' => '~/crmSearchActions/crmSearchActionUpdate.html'], + ]; + } + + if (array_key_exists('delete', $entity['actions'])) { + $tasks[] = [ + 'name' => 'delete', + 'title' => E::ts('Delete %1', [1 => $entity['title_plural']]), + 'icon' => 'fa-trash', + 'uiDialog' => ['templateUrl' => '~/crmSearchActions/crmSearchActionDelete.html'], + ]; + } + + if ($entity['name'] === 'Contact') { + // Add contact tasks which support standalone mode (with a 'url' property) + $contactTasks = \CRM_Contact_Task::permissionedTaskTitles(\CRM_Core_Permission::getPermission()); + foreach (\CRM_Contact_Task::tasks() as $id => $task) { + if (isset($contactTasks[$id]) && !empty($task['url']) && $task['url'] !== 'civicrm/task/delete-contact') { + if ($task['url'] === 'civicrm/task/pick-profile') { + $task['title'] = E::ts('Profile Update'); + } + $tasks[] = [ + 'name' => 'contact.' . $id, + 'title' => $task['title'], + 'icon' => $task['icon'] ?? 'fa-gear', + 'crmPopup' => [ + 'path' => "'{$task['url']}'", + 'query' => "{cids: ids.join(',')}", + ], + ]; + } + } + } + + usort($tasks, function($a, $b) { + return strnatcasecmp($a['title'], $b['title']); + }); + + $result->exchangeArray($tasks); + } + +} diff --git a/ext/search/Civi/Api4/SearchDisplay.php b/ext/search/Civi/Api4/SearchDisplay.php index 2ea595cbf1..85e65602f1 100644 --- a/ext/search/Civi/Api4/SearchDisplay.php +++ b/ext/search/Civi/Api4/SearchDisplay.php @@ -20,4 +20,13 @@ class SearchDisplay extends Generic\DAOEntity { ->setCheckPermissions($checkPermissions); } + /** + * @param bool $checkPermissions + * @return Action\SearchDisplay\GetSearchTasks + */ + public static function getSearchTasks($checkPermissions = TRUE) { + return (new Action\SearchDisplay\GetSearchTasks(__CLASS__, __FUNCTION__)) + ->setCheckPermissions($checkPermissions); + } + } diff --git a/ext/search/Civi/Search/Actions.php b/ext/search/Civi/Search/Actions.php index f425b43e43..3593141df3 100644 --- a/ext/search/Civi/Search/Actions.php +++ b/ext/search/Civi/Search/Actions.php @@ -24,57 +24,8 @@ class Actions { */ public static function getActionSettings():array { return [ - 'tasks' => self::getTasks(), 'dateRanges' => \CRM_Utils_Array::makeNonAssociative(\CRM_Core_OptionGroup::values('relative_date_filters'), 'id', 'text'), ]; } - /** - * @return array - */ - public static function getTasks():array { - // Note: the placeholder %1 will be replaced with entity name on the clientside - $tasks = [ - 'export' => [ - 'title' => E::ts('Export %1'), - 'icon' => 'fa-file-excel-o', - 'entities' => array_keys(\CRM_Export_BAO_Export::getComponents()), - 'crmPopup' => [ - 'path' => "'civicrm/export/standalone'", - 'query' => "{entity: entity, id: ids.join(',')}", - ], - ], - 'update' => [ - 'title' => E::ts('Update %1'), - 'icon' => 'fa-save', - 'entities' => [], - 'uiDialog' => ['templateUrl' => '~/crmSearchActions/crmSearchActionUpdate.html'], - ], - 'delete' => [ - 'title' => E::ts('Delete %1'), - 'icon' => 'fa-trash', - 'entities' => [], - 'uiDialog' => ['templateUrl' => '~/crmSearchActions/crmSearchActionDelete.html'], - ], - ]; - - // Add contact tasks which support standalone mode (with a 'url' property) - $contactTasks = \CRM_Contact_Task::permissionedTaskTitles(\CRM_Core_Permission::getPermission()); - foreach (\CRM_Contact_Task::tasks() as $id => $task) { - if (isset($contactTasks[$id]) && !empty($task['url']) && $task['url'] !== 'civicrm/task/delete-contact') { - $tasks['contact.' . $id] = [ - 'title' => $task['title'], - 'entities' => ['Contact'], - 'icon' => $task['icon'] ?? 'fa-gear', - 'crmPopup' => [ - 'path' => "'{$task['url']}'", - 'query' => "{cids: ids.join(',')}", - ], - ]; - } - } - - return $tasks; - } - } diff --git a/ext/search/ang/crmSearchActions/crmSearchActions.component.js b/ext/search/ang/crmSearchActions/crmSearchActions.component.js index 46a9cdb2ae..2f3570eeaa 100644 --- a/ext/search/ang/crmSearchActions/crmSearchActions.component.js +++ b/ext/search/ang/crmSearchActions/crmSearchActions.component.js @@ -25,19 +25,10 @@ function initialize() { crmApi4({ entityInfo: ['Entity', 'get', {select: ['name', 'title', 'title_plural'], where: [['name', '=', ctrl.entity]]}, 0], - allowed: [ctrl.entity, 'getActions', {where: [['name', 'IN', ['update', 'delete']]]}, ['name']] + tasks: ['SearchDisplay', 'getSearchTasks', {entity: ctrl.entity}] }).then(function(result) { ctrl.entityInfo = result.entityInfo; - _.each(result.allowed, function(action) { - CRM.crmSearchActions.tasks[action].entities.push(ctrl.entity); - }); - var actions = _.transform(_.cloneDeep(CRM.crmSearchActions.tasks), function(actions, action) { - if (_.includes(action.entities, ctrl.entity)) { - action.title = action.title.replace('%1', ctrl.entityInfo.title_plural); - actions.push(action); - } - }, []); - ctrl.actions = _.sortBy(actions, 'title'); + ctrl.tasks = result.tasks; }); } diff --git a/ext/search/ang/crmSearchActions/crmSearchActions.html b/ext/search/ang/crmSearchActions/crmSearchActions.html index f00d293f25..54fbdb2034 100644 --- a/ext/search/ang/crmSearchActions/crmSearchActions.html +++ b/ext/search/ang/crmSearchActions/crmSearchActions.html @@ -4,10 +4,10 @@ {{:: ts('Action') }}