18c63adbca60897278b98b5be1cdfc9d92984d4b
1 (function(angular
, $, _
) {
4 angular
.module('crmSearchAdmin').component('crmSearchAdminSegment', {
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'),
15 this.entitySelect
= searchMeta
.getPrimaryAndSecondaryEntitySelect();
18 ctrl
.segment
= {items
: []};
20 // Drag-n-drop settings for reordering items
21 this.sortableOptions
= {
22 containment
: 'fieldset',
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());
35 this.$onInit = function() {
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();
50 this.onChangeEntity = function() {
51 ctrl
.segment
.items
.length
= 0;
52 if (ctrl
.segment
.entity_name
) {
53 searchMeta
.loadFieldOptions([ctrl
.segment
.entity_name
]);
58 function getDefaultField() {
59 var item
= _
.findLast(ctrl
.segment
.items
, function(item
) {
60 return item
.when
&& item
.when
[0] && item
.when
[0][0];
62 return item
? item
.when
[0][0] : searchMeta
.getEntity(ctrl
.segment
.entity_name
).fields
[0].name
;
65 this.addItem = function(addCondition
) {
66 var item
= {label
: ''};
68 ctrl
.addCondition(item
);
70 ctrl
.segment
.items
.push(item
);
73 this.addCondition = function(item
) {
74 var defaultField
= getDefaultField();
75 item
.when
= item
.when
|| [];
76 item
.when
.push([defaultField
, '=']);
79 this.hasDefault = function() {
80 return !!_
.findLast(ctrl
.segment
.items
, function(item
) {
81 return !item
.when
|| !item
.when
[0].length
;
85 this.getField = function(fieldName
) {
86 return searchMeta
.getField(fieldName
, ctrl
.segment
.entity_name
);
89 this.selectFields = function() {
90 return {results
: formatForSelect2(searchMeta
.getEntity(ctrl
.segment
.entity_name
).fields
, 'name', 'label', ['description'])};
93 this.save = function() {
94 crmStatus({}, crmApi4('SearchSegment', 'save', {
95 records
: [ctrl
.segment
],
97 fields
: [ctrl
.segment
.entity_name
, 'getFields', {
98 loadOptions
: ['id', 'name', 'label', 'description', 'color', 'icon'],
99 where
: [['type', '=', 'Extra'], ['name', 'LIKE', 'segment_%']]
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
});
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;
112 _
.each(saved
.fields
, function(field
) {
113 field
.fieldName
= field
.name
;
114 entity
.fields
.push(field
);
116 entity
.fields
= _
.sortBy(entity
.fields
, 'label');
117 dialogService
.close('searchSegmentDialog');
124 })(angular
, CRM
.$, CRM
._
);