--- /dev/null
+<?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);
+ }
+
+}
->setCheckPermissions($checkPermissions);
}
+ /**
+ * @param bool $checkPermissions
+ * @return Action\SearchDisplay\GetSearchTasks
+ */
+ public static function getSearchTasks($checkPermissions = TRUE) {
+ return (new Action\SearchDisplay\GetSearchTasks(__CLASS__, __FUNCTION__))
+ ->setCheckPermissions($checkPermissions);
+ }
+
}
*/
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;
- }
-
}
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;
});
}
{{:: 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>