SearchKit - Refactor tasks list to use API callback
authorColeman Watts <coleman@civicrm.org>
Thu, 11 Mar 2021 20:09:32 +0000 (15:09 -0500)
committerColeman Watts <coleman@civicrm.org>
Thu, 11 Mar 2021 21:01:15 +0000 (16:01 -0500)
ext/search/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php [new file with mode: 0644]
ext/search/Civi/Api4/SearchDisplay.php
ext/search/Civi/Search/Actions.php
ext/search/ang/crmSearchActions/crmSearchActions.component.js
ext/search/ang/crmSearchActions/crmSearchActions.html

diff --git a/ext/search/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php b/ext/search/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php
new file mode 100644 (file)
index 0000000..a65b4d5
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+namespace Civi\Api4\Action\SearchDisplay;
+
+use CRM_Search_ExtensionUtil as E;
+use Civi\Api4\Entity;
+
+/**
+ * Load the available tasks for a given entity.
+ *
+ * @package Civi\Api4\Action\SearchDisplay
+ */
+class GetSearchTasks extends \Civi\Api4\Generic\AbstractAction {
+
+  /**
+   * Name of entity
+   * @var string
+   * @required
+   */
+  protected $entity;
+
+  /**
+   * @param \Civi\Api4\Generic\Result $result
+   * @throws \API_Exception
+   */
+  public function _run(\Civi\Api4\Generic\Result $result) {
+    $entity = Entity::get($this->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);
+  }
+
+}
index 2ea595cbf114f2bb3f577126c1491cdc7c9d894f..85e65602f1167756ee825d7447b6203603df6ef5 100644 (file)
@@ -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);
+  }
+
 }
index f425b43e43b45660f418baf908d18f3649022121..3593141df32f19d25a7a9bdcf867384d62ca57f1 100644 (file)
@@ -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;
-  }
-
 }
index 46a9cdb2aec751d4f76aef06a804adf1eb7de69d..2f3570eeaa0889bf2a1fb1c2109598c98a5c423b 100644 (file)
       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;
         });
       }
 
index f00d293f25de480076566702ee8eb028936a4e91..54fbdb20347e89ea93acbe2f61620e3dc5d1600b 100644 (file)
@@ -4,10 +4,10 @@
     {{:: ts('Action') }} <span class="caret"></span>
   </button>
   <ul class="dropdown-menu">
-    <li ng-disabled="!$ctrl.isActionAllowed(action)" ng-repeat="action in $ctrl.actions">
+    <li ng-disabled="!$ctrl.isActionAllowed(action)" ng-repeat="action in $ctrl.tasks">
       <a href ng-click="$ctrl.doAction(action)"><i class="fa {{:: action.icon }}"></i> {{:: action.title }}</a>
     </li>
-    <li class="disabled" ng-if="!$ctrl.actions">
+    <li class="disabled" ng-if="!$ctrl.tasks">
       <a href><i class="fa fa-spinner fa-spin"></i></a>
     </li>
   </ul>