Commit | Line | Data |
---|---|---|
5145a3b5 CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
1e313889 | 4 | angular.module('crmSearchTasks').component('crmSearchBatchRunner', { |
5145a3b5 CW |
5 | bindings: { |
6 | entity: '<', | |
7 | action: '@', | |
8 | ids: '<', | |
8362288d | 9 | idField: '@', |
5145a3b5 CW |
10 | params: '<', |
11 | success: '&', | |
12 | error: '&' | |
13 | }, | |
1e313889 | 14 | templateUrl: '~/crmSearchTasks/crmSearchBatchRunner.html', |
5145a3b5 | 15 | controller: function($scope, $timeout, $interval, crmApi4) { |
33e81cf6 | 16 | var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), |
5145a3b5 CW |
17 | ctrl = this, |
18 | currentBatch = 0, | |
19 | totalBatches, | |
20 | incrementer; | |
21 | ||
22 | this.progress = 0; | |
23 | ||
24 | // Number of records to process in each batch | |
25 | var BATCH_SIZE = 500, | |
8362288d | 26 | // Estimated number of seconds each batch will take (for auto-incrementing the progress bar) |
5145a3b5 CW |
27 | EST_BATCH_TIME = 5; |
28 | ||
29 | this.$onInit = function() { | |
d2a39a85 CW |
30 | if (ctrl.action === 'create') { |
31 | ctrl.ids = [0]; | |
32 | } | |
5145a3b5 CW |
33 | totalBatches = Math.ceil(ctrl.ids.length / BATCH_SIZE); |
34 | runBatch(); | |
35 | }; | |
36 | ||
37 | this.$onDestroy = function() { | |
38 | stopIncrementer(); | |
39 | }; | |
40 | ||
41 | function runBatch() { | |
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; | |
46 | } | |
47 | var params = _.cloneDeep(ctrl.params); | |
8362288d CW |
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; | |
53 | records.push(record); | |
54 | }); | |
55 | }); | |
d2a39a85 | 56 | } else if (ctrl.action !== 'create') { |
8362288d CW |
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)]); | |
60 | } | |
5145a3b5 CW |
61 | crmApi4(ctrl.entity, ctrl.action, params).then( |
62 | function(result) { | |
63 | stopIncrementer(); | |
64 | ctrl.progress = Math.floor(100 * ++currentBatch / totalBatches); | |
65 | if (ctrl.last >= ctrl.ids.length) { | |
d2a39a85 CW |
66 | $timeout(function() { |
67 | ctrl.success({result: result}); | |
68 | }, 500); | |
5145a3b5 CW |
69 | } else { |
70 | runBatch(); | |
71 | } | |
72 | }, function(error) { | |
73 | ctrl.error(); | |
74 | }); | |
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); | |
80 | } | |
81 | ||
82 | function stopIncrementer() { | |
83 | if (angular.isDefined(incrementer)) { | |
84 | $interval.cancel(incrementer); | |
85 | incrementer = undefined; | |
86 | } | |
87 | } | |
88 | ||
89 | } | |
90 | }); | |
91 | ||
92 | })(angular, CRM.$, CRM._); |