templateUrl: '~/crmSearchAdmin/searchSegment/crmSearchAdminSegment.html',
controller: function ($scope, searchMeta, dialogService, crmApi4, crmStatus, formatForSelect2) {
var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'),
- ctrl = this;
+ ctrl = this,
+ originalEntity,
+ originalField;
this.entitySelect = searchMeta.getPrimaryAndSecondaryEntitySelect();
where: [['id', '=', ctrl.segmentId]]
}, 0).then(function(segment) {
ctrl.segment = segment;
+ originalEntity = segment.entity_name;
+ originalField = 'segment_' + segment.name;
searchMeta.loadFieldOptions([segment.entity_name]);
$('.ui-dialog:visible').unblock();
});
this.save = function() {
crmStatus({}, crmApi4('SearchSegment', 'save', {
- records: [ctrl.segment]
- })).then(function() {
+ records: [ctrl.segment],
+ chain: {
+ fields: [ctrl.segment.entity_name, 'getFields', {
+ loadOptions: ['id', 'name', 'label', 'description', 'color', 'icon'],
+ where: [['type', '=', 'Extra'], ['name', 'LIKE', 'segment_%']]
+ }]
+ }
+ }, 0)).then(function(saved) {
+ // If entity changed, remove field from orignal entity
+ if (originalEntity) {
+ _.remove(searchMeta.getEntity(originalEntity).fields, {name: originalField});
+ }
+ // Refresh all segment fields in this entity
+ var entity = searchMeta.getEntity(ctrl.segment.entity_name);
+ _.remove(entity.fields, function(field) {
+ return field.name.indexOf('segment_') === 0;
+ });
+ _.each(saved.fields, function(field) {
+ field.fieldName = field.name;
+ entity.fields.push(field);
+ });
+ entity.fields = _.sortBy(entity.fields, 'label');
dialogService.close('searchSegmentDialog');
});
};
api_params: {
version: 4,
select: [
+ 'id',
'label',
'description',
+ 'entity_name',
'entity_name:label',
+ 'CONCAT("segment_", name) AS field_name',
'items'
],
join: [],
{start: ts('Deleting...'), success: ts('Segment Deleted')},
row
);
+ // Delete field from metadata
+ var entity = searchMeta.getEntity(row.data.entity_name);
+ _.remove(entity.fields, {name: row.data.field_name});
};
function buildDisplaySettings() {
actions: false,
classes: ['table', 'table-striped'],
sort: [['label', 'ASC']],
+ // Do not make columns editable because it would require a metadata refresh
columns: [
{
key: 'label',
label: ts('Label'),
- title: ts('Edit Label'),
- type: 'field',
- editable: true
+ type: 'field'
},
{
key: 'description',
label: ts('Description'),
- type: 'field',
- editable: true
+ type: 'field'
},
{
key: 'entity_name:label',