1 (function(angular
, $, _
) {
4 // Shared between router and searchMeta service
9 // Declare module and route/controller/services
10 angular
.module('crmSearchAdmin', CRM
.angRequires('crmSearchAdmin'))
12 .config(function($routeProvider
) {
13 $routeProvider
.when('/list', {
14 controller
: 'searchList',
15 templateUrl
: '~/crmSearchAdmin/searchList.html',
17 // Load data for lists
18 savedSearches: function(crmApi4
) {
19 return crmApi4('SavedSearch', 'get', {
26 'GROUP_CONCAT(display.name ORDER BY display.id) AS display_name',
27 'GROUP_CONCAT(display.label ORDER BY display.id) AS display_label',
28 'GROUP_CONCAT(display.type:icon ORDER BY display.id) AS display_icon',
29 'GROUP_CONCAT(DISTINCT group.title) AS groups'
31 join
: [['SearchDisplay AS display'], ['Group AS group']],
32 where
: [['api_entity', 'IS NOT NULL']],
38 $routeProvider
.when('/create/:entity', {
39 controller
: 'searchCreate',
40 reloadOnSearch
: false,
41 template
: '<crm-search-admin saved-search="$ctrl.savedSearch"></crm-search-admin>',
43 $routeProvider
.when('/edit/:id', {
44 controller
: 'searchEdit',
45 template
: '<crm-search-admin saved-search="$ctrl.savedSearch"></crm-search-admin>',
48 savedSearch: function($route
, crmApi4
) {
49 var params
= $route
.current
.params
;
50 return crmApi4('SavedSearch', 'get', {
51 where
: [['id', '=', params
.id
]],
53 groups
: ['Group', 'get', {select
: ['id', 'title', 'description', 'visibility', 'group_type'], where
: [['saved_search_id', '=', '$id']]}],
54 displays
: ['SearchDisplay', 'get', {where
: [['saved_search_id', '=', '$id']]}]
62 // Controller for creating a new search
63 .controller('searchCreate', function($scope
, $routeParams
, $location
) {
64 searchEntity
= $routeParams
.entity
;
67 api_entity
: searchEntity
,
69 // Changing entity will refresh the angular page
70 $scope
.$watch('$ctrl.savedSearch.api_entity', function(newEntity
, oldEntity
) {
71 if (newEntity
&& oldEntity
&& newEntity
!== oldEntity
) {
72 $location
.url('/create/' + newEntity
);
77 // Controller for editing a SavedSearch
78 .controller('searchEdit', function($scope
, savedSearch
) {
79 searchEntity
= savedSearch
.api_entity
;
80 this.savedSearch
= savedSearch
;
84 .factory('searchMeta', function() {
85 function getEntity(entityName
) {
87 return _
.find(CRM
.crmSearchAdmin
.schema
, {name
: entityName
});
90 // Get join metadata matching a given expression like "Email AS Contact_Email_contact_id_01"
91 function getJoin(fullNameOrAlias
) {
92 var alias
= _
.last(fullNameOrAlias
.split(' AS ')),
94 baseEntity
= searchEntity
,
100 join
= _
.find(CRM
.crmSearchAdmin
.joins
[baseEntity
], function(join
) {
101 return new RegExp('^' + join
.alias
+ '_\\d\\d').test(path
);
104 console
.warn( 'Join ' + fullNameOrAlias
+ ' not found.');
107 path
= path
.replace(join
.alias
+ '_', '');
108 var num
= parseInt(path
.substr(0, 2), 10);
109 label
.push(join
.label
+ (num
> 1 ? ' ' + num
: ''));
110 path
= path
.replace(/^\d\d_?/, '');
112 baseEntity
= join
.entity
;
115 result
= _
.assign(_
.cloneDeep(join
), {label
: label
.join(' - '), alias
: alias
, baseEntity
: baseEntity
});
116 // Add the numbered suffix to the join conditions
117 // If this is a deep join, also add the base entity prefix
118 var prefix
= alias
.replace(new RegExp('_?' + join
.alias
+ '_?\\d?\\d?$'), '');
119 _
.each(result
.conditions
, function(condition
) {
120 if (_
.isArray(condition
)) {
121 _
.each(condition
, function(ref
, side
) {
122 if (side
!== 1 && _
.includes(ref
, '.')) {
123 condition
[side
] = ref
.replace(join
.alias
+ '.', alias
+ '.');
124 } else if (side
!== 1 && prefix
.length
&& !_
.includes(ref
, '"') && !_
.includes(ref
, "'")) {
125 condition
[side
] = prefix
+ '.' + ref
;
132 function getFieldAndJoin(fieldName
, entityName
) {
133 var dotSplit
= fieldName
.split('.'),
134 joinEntity
= dotSplit
.length
> 1 ? dotSplit
[0] : null,
135 name
= _
.last(dotSplit
).split(':')[0],
138 // Custom fields contain a dot in their fieldname
139 // If 3 segments, the first is the joinEntity and the last 2 are the custom field
140 if (dotSplit
.length
=== 3) {
141 name
= dotSplit
[1] + '.' + name
;
143 // If 2 segments, it's ambiguous whether this is a custom field or joined field. Search the main entity first.
144 if (dotSplit
.length
=== 2) {
145 field
= _
.find(getEntity(entityName
).fields
, {name
: dotSplit
[0] + '.' + name
});
147 field
.entity
= entityName
;
148 return {field
: field
};
152 join
= getJoin(joinEntity
);
153 entityName
= getJoin(joinEntity
).entity
;
155 field
= _
.find(getEntity(entityName
).fields
, {name
: name
});
156 if (!field
&& join
&& join
.bridge
) {
157 field
= _
.find(getEntity(join
.bridge
).fields
, {name
: name
});
160 field
.entity
= entityName
;
161 return {field
: field
, join
: join
};
164 function parseExpr(expr
) {
165 var result
= {fn
: null, modifier
: ''},
167 bracketPos
= expr
.indexOf('(');
168 if (bracketPos
>= 0) {
169 var parsed
= expr
.substr(bracketPos
).match(/[ ]?([A-Z]+[ ]+)?([\w.:]+)/);
170 fieldName
= parsed
[2];
171 result
.fn
= _
.find(CRM
.crmSearchAdmin
.functions
, {name
: expr
.substring(0, bracketPos
)});
172 result
.modifier
= _
.trim(parsed
[1]);
174 var fieldAndJoin
= expr
? getFieldAndJoin(fieldName
, searchEntity
) : undefined;
175 if (fieldAndJoin
.field
) {
176 var split
= fieldName
.split(':'),
177 prefixPos
= split
[0].lastIndexOf(fieldAndJoin
.field
.name
);
178 result
.path
= split
[0];
179 result
.prefix
= prefixPos
> 0 ? result
.path
.substring(0, prefixPos
) : '';
180 result
.suffix
= !split
[1] ? '' : ':' + split
[1];
181 result
.field
= fieldAndJoin
.field
;
182 result
.join
= fieldAndJoin
.join
;
187 getEntity
: getEntity
,
188 getField: function(fieldName
, entityName
) {
189 return getFieldAndJoin(fieldName
, entityName
).field
;
192 parseExpr
: parseExpr
,
193 getDefaultLabel: function(col
) {
194 var info
= parseExpr(col
),
195 label
= info
.field
.label
;
197 label
= '(' + info
.fn
.title
+ ') ' + label
;
200 label
= info
.join
.label
+ ': ' + label
;
204 // Find all possible search columns that could serve as contact_id for a smart group
205 getSmartGroupColumns: function(api_entity
, api_params
) {
206 var joins
= _
.pluck((api_params
.join
|| []), 0);
207 return _
.transform([api_entity
].concat(joins
), function(columns
, joinExpr
) {
208 var joinName
= joinExpr
.split(' AS '),
209 joinInfo
= joinName
[1] ? getJoin(joinName
[1]) : {entity
: joinName
[0]},
210 entity
= getEntity(joinInfo
.entity
),
211 prefix
= joinInfo
.alias
? joinInfo
.alias
+ '.' : '';
212 _
.each(entity
.fields
, function(field
) {
213 if ((entity
.name
=== 'Contact' && field
.name
=== 'id') || (field
.fk_entity
=== 'Contact' && joinInfo
.baseEntity
!== 'Contact')) {
215 id
: prefix
+ field
.name
,
216 text
: (joinInfo
.label
? joinInfo
.label
+ ': ' : '') + field
.label
,
226 })(angular
, CRM
.$, CRM
._
);