18c63adbca60897278b98b5be1cdfc9d92984d4b
[civicrm-core.git] / ext / search_kit / ang / crmSearchAdmin / searchSegment / crmSearchAdminSegment.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('crmSearchAdmin').component('crmSearchAdminSegment', {
5 bindings: {
6 segmentId: '<',
7 },
8 templateUrl: '~/crmSearchAdmin/searchSegment/crmSearchAdminSegment.html',
9 controller: function ($scope, searchMeta, dialogService, crmApi4, crmStatus, formatForSelect2) {
10 var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'),
11 ctrl = this,
12 originalEntity,
13 originalField;
14
15 this.entitySelect = searchMeta.getPrimaryAndSecondaryEntitySelect();
16
17 ctrl.saving = false;
18 ctrl.segment = {items: []};
19
20 // Drag-n-drop settings for reordering items
21 this.sortableOptions = {
22 containment: 'fieldset',
23 axis: 'y',
24 handle: '.crm-draggable',
25 forcePlaceholderSize: true,
26 helper: function(e, ui) {
27 // Prevent table row width from changing during drag
28 ui.children().each(function() {
29 $(this).width($(this).width());
30 });
31 return ui;
32 }
33 };
34
35 this.$onInit = function() {
36 if (ctrl.segmentId) {
37 $('.ui-dialog:visible').block();
38 crmApi4('SearchSegment', 'get', {
39 where: [['id', '=', ctrl.segmentId]]
40 }, 0).then(function(segment) {
41 ctrl.segment = segment;
42 originalEntity = segment.entity_name;
43 originalField = 'segment_' + segment.name;
44 searchMeta.loadFieldOptions([segment.entity_name]);
45 $('.ui-dialog:visible').unblock();
46 });
47 }
48 };
49
50 this.onChangeEntity = function() {
51 ctrl.segment.items.length = 0;
52 if (ctrl.segment.entity_name) {
53 searchMeta.loadFieldOptions([ctrl.segment.entity_name]);
54 ctrl.addItem(true);
55 }
56 };
57
58 function getDefaultField() {
59 var item = _.findLast(ctrl.segment.items, function(item) {
60 return item.when && item.when[0] && item.when[0][0];
61 });
62 return item ? item.when[0][0] : searchMeta.getEntity(ctrl.segment.entity_name).fields[0].name;
63 }
64
65 this.addItem = function(addCondition) {
66 var item = {label: ''};
67 if (addCondition) {
68 ctrl.addCondition(item);
69 }
70 ctrl.segment.items.push(item);
71 };
72
73 this.addCondition = function(item) {
74 var defaultField = getDefaultField();
75 item.when = item.when || [];
76 item.when.push([defaultField, '=']);
77 };
78
79 this.hasDefault = function() {
80 return !!_.findLast(ctrl.segment.items, function(item) {
81 return !item.when || !item.when[0].length;
82 });
83 };
84
85 this.getField = function(fieldName) {
86 return searchMeta.getField(fieldName, ctrl.segment.entity_name);
87 };
88
89 this.selectFields = function() {
90 return {results: formatForSelect2(searchMeta.getEntity(ctrl.segment.entity_name).fields, 'name', 'label', ['description'])};
91 };
92
93 this.save = function() {
94 crmStatus({}, crmApi4('SearchSegment', 'save', {
95 records: [ctrl.segment],
96 chain: {
97 fields: [ctrl.segment.entity_name, 'getFields', {
98 loadOptions: ['id', 'name', 'label', 'description', 'color', 'icon'],
99 where: [['type', '=', 'Extra'], ['name', 'LIKE', 'segment_%']]
100 }]
101 }
102 }, 0)).then(function(saved) {
103 // If entity changed, remove field from orignal entity
104 if (originalEntity) {
105 _.remove(searchMeta.getEntity(originalEntity).fields, {name: originalField});
106 }
107 // Refresh all segment fields in this entity
108 var entity = searchMeta.getEntity(ctrl.segment.entity_name);
109 _.remove(entity.fields, function(field) {
110 return field.name.indexOf('segment_') === 0;
111 });
112 _.each(saved.fields, function(field) {
113 field.fieldName = field.name;
114 entity.fields.push(field);
115 });
116 entity.fields = _.sortBy(entity.fields, 'label');
117 dialogService.close('searchSegmentDialog');
118 });
119 };
120
121 }
122 });
123
124 })(angular, CRM.$, CRM._);