Merge pull request #18725 from civicrm/5.31
[civicrm-core.git] / ext / search / ang / search / crmSearchActions / crmSearchActionUpdate.ctrl.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('search').controller('crmSearchActionUpdate', function ($scope, $timeout, crmApi4, dialogService, searchMeta) {
5 var ts = $scope.ts = CRM.ts(),
6 model = $scope.model,
7 ctrl = $scope.$ctrl = this;
8
9 this.entity = searchMeta.getEntity(model.entity);
10 this.values = [];
11 this.add = null;
12
13 function fieldInUse(fieldName) {
14 return _.includes(_.collect(ctrl.values, 0), fieldName);
15 }
16
17 this.updateField = function(index) {
18 // Debounce the onchange event using timeout
19 $timeout(function() {
20 if (!ctrl.values[index][0]) {
21 ctrl.values.splice(index, 1);
22 }
23 });
24 };
25
26 this.addField = function() {
27 // Debounce the onchange event using timeout
28 $timeout(function() {
29 if (ctrl.add) {
30 ctrl.values.push([ctrl.add, '']);
31 }
32 ctrl.add = null;
33 });
34 };
35
36 this.availableFields = function() {
37 var results = _.transform(ctrl.entity.fields, function(result, item) {
38 var formatted = {id: item.name, text: item.label, description: item.description};
39 if (fieldInUse(item.name)) {
40 formatted.disabled = true;
41 }
42 if (item.name !== 'id') {
43 result.push(formatted);
44 }
45 }, []);
46 return {results: results};
47 };
48
49 this.cancel = function() {
50 dialogService.cancel('crmSearchAction');
51 };
52
53 this.save = function() {
54 crmApi4(model.entity, 'Update', {
55 where: [['id', 'IN', model.ids]],
56 values: _.zipObject(ctrl.values)
57 }).then(function() {
58 dialogService.close('crmSearchAction');
59 });
60 };
61
62 });
63 })(angular, CRM.$, CRM._);