Skip to content

Commit

Permalink
Port search kit fix
Browse files Browse the repository at this point in the history
Port of civicrm/civicrm-core#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
  • Loading branch information
eileenmcnaughton committed Oct 26, 2022
1 parent 5aba850 commit cb1a0e7
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.'),
],
];
}

Expand Down Expand Up @@ -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',
],
];
}

}
Original file line number Diff line number Diff line change
@@ -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._);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div id="bootstrap-theme" crm-dialog="crmSearchTask">
<form ng-controller="crmSearchTaskApiBatch as $ctrl">
<p><strong ng-if="model.apiBatch.confirmMsg">{{:: ts(model.apiBatch.confirmMsg, {1: model.ids.length, 2: $ctrl.entityTitle}) }}</strong></p>
<hr />
<div ng-if="$ctrl.run" class="crm-search-task-progress">
<h5>{{:: ts(model.apiBatch.runMsg, {1: model.ids.length, 2: $ctrl.entityTitle}) }}</h5>
<crm-search-batch-runner entity="model.entity" action="{{:: model.apiBatch.action }}" params="$ctrl.run" ids="model.ids" success="$ctrl.onSuccess()" error="$ctrl.onError()" id-field="{{:: $ctrl.entityInfo.primary_key[0] }}"></crm-search-batch-runner>
</div>

<crm-dialog-button text="ts('Cancel')" icons="{primary: 'fa-times'}" on-click="$ctrl.cancel()" disabled="$ctrl.run" ></crm-dialog-button>
<crm-dialog-button ng-if="model.apiBatch.confirmMsg" text="model.taskTitle" icons="{primary: $ctrl.run ? 'fa-spin fa-spinner' : 'fa-check'}" on-click="$ctrl.start(model.apiBatch.params)" disabled="$ctrl.run" ></crm-dialog-button>
</form>
</div>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit cb1a0e7

Please sign in to comment.