1 (function(angular
, $, _
) {
4 angular
.module('crmSearchTasks').component('crmSearchBatchRunner', {
14 templateUrl
: '~/crmSearchTasks/crmSearchBatchRunner.html',
15 controller: function($scope
, $timeout
, $interval
, crmApi4
) {
16 var ts
= $scope
.ts
= CRM
.ts('org.civicrm.search_kit'),
24 // Number of records to process in each batch
26 // Estimated number of seconds each batch will take (for auto-incrementing the progress bar)
29 this.$onInit = function() {
30 if (ctrl
.action
=== 'create') {
33 totalBatches
= Math
.ceil(ctrl
.ids
.length
/ BATCH_SIZE
);
37 this.$onDestroy = function() {
42 ctrl
.first
= currentBatch
* BATCH_SIZE
;
43 ctrl
.last
= (currentBatch
+ 1) * BATCH_SIZE
;
44 if (ctrl
.last
> ctrl
.ids
.length
) {
45 ctrl
.last
= ctrl
.ids
.length
;
47 var params
= _
.cloneDeep(ctrl
.params
);
48 if (ctrl
.action
=== 'save') {
49 // For the save action, take each record from params and copy it with each supplied id
50 params
.records
= _
.transform(ctrl
.ids
.slice(ctrl
.first
, ctrl
.last
), function(records
, id
) {
51 _
.each(_
.cloneDeep(ctrl
.params
.records
), function(record
) {
52 record
[ctrl
.idField
|| 'id'] = id
;
56 } else if (ctrl
.action
!== 'create') {
57 // For other batch actions (update, delete), add supplied ids to the where clause
58 params
.where
= params
.where
|| [];
59 params
.where
.push([ctrl
.idField
|| 'id', 'IN', ctrl
.ids
.slice(ctrl
.first
, ctrl
.last
)]);
61 crmApi4(ctrl
.entity
, ctrl
.action
, params
).then(
64 ctrl
.progress
= Math
.floor(100 * ++currentBatch
/ totalBatches
);
65 if (ctrl
.last
>= ctrl
.ids
.length
) {
67 ctrl
.success({result
: result
});
75 // Move the bar every second to simulate progress between batches
76 incrementer
= $interval(function(i
) {
77 var est
= Math
.floor(100 * (currentBatch
+ (i
/ EST_BATCH_TIME
)) / totalBatches
);
78 ctrl
.progress
= est
> 100 ? 100 : est
;
79 }, 1000, EST_BATCH_TIME
);
82 function stopIncrementer() {
83 if (angular
.isDefined(incrementer
)) {
84 $interval
.cancel(incrementer
);
85 incrementer
= undefined;
92 })(angular
, CRM
.$, CRM
._
);