1 (function(angular
, $, _
, undefined) {
4 var schema
= CRM
.vars
.api4
.schema
;
5 // Cache list of entities
7 // Cache list of actions
10 var fieldOptions
= {};
14 angular
.module('api4Explorer').config(function($routeProvider
) {
15 $routeProvider
.when('/explorer/:api4entity?/:api4action?', {
16 controller
: 'Api4Explorer',
17 templateUrl
: '~/api4Explorer/Explorer.html',
22 angular
.module('api4Explorer').controller('Api4Explorer', function($scope
, $routeParams
, $location
, $timeout
, $http
, crmUiHelp
, crmApi4
, dialogService
) {
23 var ts
= $scope
.ts
= CRM
.ts(),
24 ctrl
= $scope
.$ctrl
= this;
25 $scope
.entities
= entities
;
26 $scope
.actions
= actions
;
28 $scope
.havingOptions
= [];
29 $scope
.fieldsAndJoins
= [];
30 $scope
.fieldsAndJoinsAndFunctions
= [];
31 $scope
.fieldsAndJoinsAndFunctionsWithSuffixes
= [];
32 $scope
.fieldsAndJoinsAndFunctionsAndWildcards
= [];
33 $scope
.availableParams
= {};
34 params
= $scope
.params
= {};
36 $scope
.selectedTab
= {result
: 'result', code
: 'php'};
38 accessDebugOutput
: CRM
.checkPerm('access debug output'),
39 editGroups
: CRM
.checkPerm('edit groups')
41 marked
.setOptions({highlight
: prettyPrintOne
});
42 var getMetaParams
= {},
43 objectParams
= {orderBy
: 'ASC', values
: '', defaults
: '', chain
: ['Entity', '', '{}']},
44 docs
= CRM
.vars
.api4
.docs
,
47 $scope
.helpTitle
= '';
48 $scope
.helpContent
= {};
49 $scope
.entity
= $routeParams
.api4entity
;
52 $scope
.status
= 'default';
53 $scope
.loading
= false;
55 $scope
.langs
= ['php', 'js', 'ang', 'cli'];
56 $scope
.joinTypes
= [{k
: 'LEFT', v
: 'LEFT JOIN'}, {k
: 'INNER', v
: 'INNER JOIN'}, {k
: 'EXCLUDE', v
: 'EXCLUDE'}];
57 $scope
.bridgeEntities
= _
.filter(schema
, function(entity
) {return _
.includes(entity
.type
, 'EntityBridge');});
60 {name
: 'oop', label
: ts('OOP Style'), code
: ''},
61 {name
: 'php', label
: ts('Traditional'), code
: ''}
64 {name
: 'js', label
: ts('Single Call'), code
: ''},
65 {name
: 'js2', label
: ts('Batch Calls'), code
: ''}
68 {name
: 'ang', label
: ts('Single Call'), code
: ''},
69 {name
: 'ang2', label
: ts('Batch Calls'), code
: ''}
72 {name
: 'short', label
: ts('CV (short)'), code
: ''},
73 {name
: 'long', label
: ts('CV (long)'), code
: ''},
74 {name
: 'pipe', label
: ts('CV (pipe)'), code
: ''}
78 if (!entities
.length
) {
79 formatForSelect2(schema
, entities
, 'name', ['description', 'icon']);
88 function ucfirst(str
) {
89 return str
[0].toUpperCase() + str
.slice(1);
92 function lcfirst(str
) {
93 return str
[0].toLowerCase() + str
.slice(1);
96 function pluralize(str
) {
97 var lastLetter
= str
[str
.length
- 1],
98 lastTwo
= str
[str
.length
- 2] + lastLetter
;
99 if (lastLetter
=== 's' || lastLetter
=== 'x' || lastTwo
=== 'ch') {
102 if (lastLetter
=== 'y' && !_
.includes(['ay', 'ey', 'iy', 'oy', 'uy'], lastTwo
)) {
103 return str
.slice(0, -1) + 'ies';
108 // Reformat an existing array of objects for compatibility with select2
109 function formatForSelect2(input
, container
, key
, extra
, prefix
) {
110 _
.each(input
, function(item
) {
111 var id
= (prefix
|| '') + item
[key
];
112 var formatted
= {id
: id
, text
: id
};
114 _
.merge(formatted
, _
.pick(item
, extra
));
116 container
.push(formatted
);
121 // Replaces contents of fieldList array with current fields formatted for select2
122 function getFieldList(fieldList
, action
, addPseudoconstant
, addWriteJoins
) {
123 var fieldInfo
= _
.cloneDeep(_
.findWhere(getEntity().actions
, {name
: action
}).fields
);
124 fieldList
.length
= 0;
125 if (addPseudoconstant
) {
126 addPseudoconstants(fieldInfo
, addPseudoconstant
);
129 addWriteJoinFields(fieldInfo
);
131 formatForSelect2(fieldInfo
, fieldList
, 'name', ['description', 'required', 'default_value']);
134 // Note: this function expects fieldList to be select2-formatted already
135 function addJoins(fieldList
, addWildcard
, addPseudoconstant
) {
136 // Add entities specified by the join param
137 _
.each(getExplicitJoins(), function(join
) {
138 var wildCard
= addWildcard
? [{id
: join
.alias
+ '.*', text
: join
.alias
+ '.*', 'description': 'All core ' + join
.entity
+ ' fields'}] : [],
139 joinFields
= _
.cloneDeep(entityFields(join
.entity
));
141 // Add fields from bridge entity
143 var bridgeFields
= _
.cloneDeep(entityFields(join
.bridge
)),
144 bridgeEntity
= getEntity(join
.bridge
),
145 joinFieldNames
= _
.pluck(joinFields
, 'name'),
146 // Check if this is a symmetric bridge e.g. RelationshipCache joins Contact to Contact
147 bridgePair
= _
.keys(bridgeEntity
.bridge
),
148 symmetric
= getField(bridgePair
[0], join
.bridge
).entity
=== getField(bridgePair
[1], join
.bridge
).entity
;
149 _
.each(bridgeFields
, function(field
) {
151 // Only include bridge fields that link back to the original entity
152 (!bridgeEntity
.bridge
[field
.name
] || field
.fk_entity
!== join
.entity
|| symmetric
) &&
153 // Exclude fields with the same name as those in the original entity
154 !_
.includes(joinFieldNames
, field
.name
)
156 joinFields
.push(field
);
160 if (addPseudoconstant
) {
161 addPseudoconstants(joinFields
, addPseudoconstant
);
164 text
: join
.entity
+ ' AS ' + join
.alias
,
165 description
: 'Explicit join to ' + join
.entity
,
166 children
: wildCard
.concat(formatForSelect2(joinFields
, [], 'name', ['description'], join
.alias
+ '.'))
170 // Add implicit joins based on schema links
171 _
.each(entityFields($scope
.entity
, $scope
.action
), function(field
) {
172 if (field
.fk_entity
) {
173 var linkFields
= _
.cloneDeep(entityFields(field
.fk_entity
)),
174 wildCard
= addWildcard
? [{id
: field
.name
+ '.*', text
: field
.name
+ '.*', 'description': 'All core ' + field
.fk_entity
+ ' fields'}] : [];
175 if (addPseudoconstant
) {
176 addPseudoconstants(linkFields
, addPseudoconstant
);
180 description
: 'Implicit join to ' + field
.fk_entity
,
181 children
: wildCard
.concat(formatForSelect2(linkFields
, [], 'name', ['description'], field
.name
+ '.'))
187 // Note: this function transforms a raw list a-la getFields; not a select2-formatted list
188 function addPseudoconstants(fieldList
, toAdd
) {
189 var optionFields
= _
.filter(fieldList
, 'options');
190 _
.each(optionFields
, function(field
) {
191 var pos
= _
.findIndex(fieldList
, {name
: field
.name
}) + 1;
192 _
.each(toAdd
, function(suffix
) {
193 var newField
= _
.cloneDeep(field
);
194 newField
.name
+= ':' + suffix
;
195 fieldList
.splice(pos
, 0, newField
);
200 // Adds join fields for create actions
201 // Note: this function transforms a raw list a-la getFields; not a select2-formatted list
202 function addWriteJoinFields(fieldList
) {
203 _
.eachRight(fieldList
, function(field
, pos
) {
204 var fkNameField
= field
.fk_entity
&& getField('name', field
.fk_entity
, $scope
.action
);
206 var newField
= _
.cloneDeep(fkNameField
);
207 newField
.name
= field
.name
+ '.' + newField
.name
;
208 fieldList
.splice(pos
, 0, newField
);
213 $scope
.help = function(title
, content
) {
215 $scope
.helpTitle
= helpTitle
;
216 $scope
.helpContent
= helpContent
;
218 $scope
.helpTitle
= title
;
219 $scope
.helpContent
= formatHelp(content
);
223 // Sets the static help text (which gets overridden by mousing over other elements)
224 function setHelp(title
, content
) {
225 $scope
.helpTitle
= helpTitle
= title
;
226 $scope
.helpContent
= helpContent
= formatHelp(content
);
229 // Format help text with markdown; replace variables and format links
230 function formatHelp(rawContent
) {
231 function formatRefs(see
) {
232 _
.each(see
, function(ref
, idx
) {
233 var match
= ref
.match(/^(\\Civi\\Api4\\)?([a-zA-Z]+)$/);
235 ref
= '#/explorer/' + match
[2];
237 // Link to php classes on GitHub.
238 // Fixme: Only works for files in the core repo
239 if (ref
[0] === '\\' || ref
.indexOf('Civi\\') === 0 || ref
.indexOf('CRM_') === 0) {
240 var classFunction
= _
.trim(ref
, '\\').split('::'),
241 replacement
= new RegExp(classFunction
[0].indexOf('CRM_') === 0 ? '_' : '\\\\', 'g');
242 ref
= 'https://github.com/civicrm/civicrm-core/blob/master/' + classFunction
[0].replace(replacement
, '/') + '.php';
244 see
[idx
] = '<a target="' + (ref
[0] === '#' ? '_self' : '_blank') + '" href="' + ref
+ '">' + see
[idx
] + '</a>';
247 var formatted
= _
.cloneDeep(rawContent
);
248 if (formatted
.description
) {
249 formatted
.description
= marked(formatted
.description
);
251 if (formatted
.comment
) {
252 formatted
.comment
= marked(formatted
.comment
);
254 formatRefs(formatted
.see
);
258 $scope
.fieldHelp = function(fieldName
) {
259 var field
= getField(fieldName
, $scope
.entity
, $scope
.action
);
264 description
: field
.description
,
265 type
: field
.data_type
267 if (field
.default_value
) {
268 info
.default = field
.default_value
;
270 if (field
.required_if
) {
271 info
.required_if
= field
.required_if
;
272 } else if (field
.required
) {
273 info
.required
= 'true';
278 // Returns field list for write params (values, defaults)
279 $scope
.fieldList = function(param
) {
282 getFieldList(fields
, $scope
.action
=== 'getFields' ? ($scope
.params
.action
|| 'get') : $scope
.action
, ['name'], true);
283 // Disable fields that are already in use
284 _
.each($scope
.params
[param
] || [], function(val
) {
285 var usedField
= val
[0].replace(/[:.]name/, '');
286 (_
.findWhere(fields
, {id
: usedField
}) || {}).disabled
= true;
287 (_
.findWhere(fields
, {id
: usedField
+ ':name'}) || {}).disabled
= true;
288 (_
.findWhere(fields
, {id
: usedField
+ '.name'}) || {}).disabled
= true;
290 return {results
: fields
};
294 $scope
.formatSelect2Item = function(row
) {
295 return _
.escape(row
.text
) +
296 (row
.required
? '<span class="crm-marker"> *</span>' : '') +
297 (row
.description
? '<div class="crm-select2-row-description"><p>' + _
.escape(row
.description
) + '</p></div>' : '');
300 $scope
.clearParam = function(name
, idx
) {
301 if (typeof idx
=== 'undefined') {
302 $scope
.params
[name
] = $scope
.availableParams
[name
].default;
304 $scope
.params
[name
].splice(idx
, 1);
308 // Gets params that should be represented as generic input fields in the explorer
309 // This fn doesn't have to be particularly efficient as its output is cached in one-time bindings
310 $scope
.getGenericParams = function(paramType
, defaultNull
) {
311 // Returns undefined if params are not yet set; one-time bindings will stabilize when this function returns a value
312 if (_
.isEmpty($scope
.availableParams
)) {
315 var specialParams
= ['select', 'fields', 'action', 'where', 'values', 'defaults', 'orderBy', 'chain', 'groupBy', 'having', 'join'];
316 if ($scope
.availableParams
.limit
&& $scope
.availableParams
.offset
) {
317 specialParams
.push('limit', 'offset');
319 return _
.transform($scope
.availableParams
, function(genericParams
, param
, name
) {
320 if (!_
.contains(specialParams
, name
) && !param
.deprecated
&&
321 !(typeof paramType
!== 'undefined' && !_
.contains(paramType
, param
.type
[0])) &&
322 !(typeof defaultNull
!== 'undefined' && ((param
.default === null) !== defaultNull
))
324 genericParams
[name
] = param
;
329 $scope
.selectRowCount = function() {
330 var index
= params
.select
.indexOf('row_count');
332 $scope
.params
.select
.push('row_count');
334 $scope
.params
.select
.splice(index
, 1);
338 $scope
.isSelectRowCount = function() {
339 return isSelectRowCount($scope
.params
);
342 $scope
.selectLang = function(lang
) {
343 $scope
.selectedTab
.code
= lang
;
347 function isSelectRowCount(params
) {
348 return params
&& params
.select
&& params
.select
.indexOf('row_count') >= 0;
351 function getEntity(entityName
) {
352 return _
.findWhere(schema
, {name
: entityName
|| $scope
.entity
});
355 // Get name of entity given join alias
356 function entityNameFromAlias(alias
) {
357 var joins
= getExplicitJoins(),
358 entity
= $scope
.entity
,
359 path
= alias
.split('.');
360 // First check explicit joins
362 return joins
[alias
].entity
;
364 // Then lookup implicit joins
365 _
.each(path
, function(node
) {
366 var field
= getField(node
, entity
, $scope
.action
);
367 if (!field
|| !field
.fk_entity
) {
370 entity
= field
.fk_entity
;
375 // Get all params that have been set
376 function getParams() {
378 _
.each($scope
.params
, function(param
, key
) {
379 if (param
!= $scope
.availableParams
[key
].default && !(typeof param
=== 'object' && _
.isEmpty(param
))) {
380 if (_
.contains($scope
.availableParams
[key
].type
, 'array') && (typeof objectParams
[key
] === 'undefined')) {
381 params
[key
] = parseYaml(JSON
.parse(angular
.toJson(param
)));
387 _
.each(params
.join
, function(join
) {
388 // Add alias if not specified
389 if (!_
.contains(join
[0], 'AS')) {
390 join
[0] += ' AS ' + join
[0].toLowerCase();
392 // Remove EntityBridge from join if empty
397 _
.each(objectParams
, function(defaultVal
, key
) {
400 _
.each(params
[key
], function(item
) {
401 var val
= _
.cloneDeep(item
[1]);
402 // Remove blank items from "chain" array
403 if (_
.isArray(val
)) {
404 _
.eachRight(item
[1], function(v
) {
411 newParam
[item
[0]] = parseYaml(val
);
413 params
[key
] = newParam
;
419 function parseYaml(input
) {
420 if (typeof input
=== 'undefined' || input
=== '') {
423 // Return literal quoted string without removing quotes - for the sake of JOIN ON clauses
424 if (_
.isString(input
) && input
[0] === input
[input
.length
- 1] && _
.includes(["'", '"'], input
[0])) {
427 if (_
.isObject(input
) || _
.isArray(input
)) {
428 _
.each(input
, function(item
, index
) {
429 input
[index
] = parseYaml(item
);
434 var output
= (input
=== '>') ? '>' : jsyaml
.safeLoad(input
);
435 // We don't want dates parsed to js objects
436 return _
.isDate(output
) ? input
: output
;
442 this.buildFieldList = function() {
443 var actionInfo
= _
.findWhere(actions
, {id
: $scope
.action
});
444 getFieldList($scope
.fields
, $scope
.action
);
445 getFieldList($scope
.fieldsAndJoins
, $scope
.action
, ['name']);
446 getFieldList($scope
.fieldsAndJoinsAndFunctions
, $scope
.action
);
447 getFieldList($scope
.fieldsAndJoinsAndFunctionsWithSuffixes
, $scope
.action
, ['name', 'label']);
448 getFieldList($scope
.fieldsAndJoinsAndFunctionsAndWildcards
, $scope
.action
, ['name', 'label']);
449 if (_
.contains(['get', 'update', 'delete', 'replace'], $scope
.action
)) {
450 addJoins($scope
.fieldsAndJoins
);
451 // SQL functions are supported if HAVING is
452 if (actionInfo
.params
.having
) {
454 text
: ts('FUNCTION'),
455 description
: ts('Calculate result of a SQL function'),
456 children
: _
.transform(CRM
.vars
.api4
.functions
, function(result
, fn
) {
458 id
: fn
.name
+ '() AS ' + fn
.name
.toLowerCase(),
459 description
: fn
.description
,
460 text
: fn
.name
+ '(' + describeSqlFn(fn
.params
) + ')'
464 $scope
.fieldsAndJoinsAndFunctions
.push(functions
);
465 $scope
.fieldsAndJoinsAndFunctionsWithSuffixes
.push(functions
);
466 $scope
.fieldsAndJoinsAndFunctionsAndWildcards
.push(functions
);
468 addJoins($scope
.fieldsAndJoinsAndFunctions
, true);
469 addJoins($scope
.fieldsAndJoinsAndFunctionsWithSuffixes
, false, ['name', 'label']);
470 addJoins($scope
.fieldsAndJoinsAndFunctionsAndWildcards
, true, ['name', 'label']);
472 // Custom fields are supported if HAVING is
473 if (actionInfo
.params
.having
) {
474 $scope
.fieldsAndJoinsAndFunctionsAndWildcards
.unshift({id
: 'custom.*', text
: 'custom.*', 'description': 'All custom fields'});
476 $scope
.fieldsAndJoinsAndFunctionsAndWildcards
.unshift({id
: '*', text
: '*', 'description': 'All core ' + $scope
.entity
+ ' fields'});
479 // Select2 formatter: Add 'strikethrough' class to deprecated items
480 $scope
.formatResultCssClass = function(result
) {
481 return result
.deprecated
? 'strikethrough' : '';
484 function selectAction() {
485 $scope
.action
= $routeParams
.api4action
;
486 if (!actions
.length
) {
487 formatForSelect2(getEntity().actions
, actions
, 'name', ['description', 'params', 'deprecated']);
490 var actionInfo
= _
.findWhere(actions
, {id
: $scope
.action
});
491 _
.each(actionInfo
.params
, function (param
, name
) {
493 defaultVal
= _
.cloneDeep(param
.default);
495 switch (param
.type
[0]) {
498 format
= param
.type
[0];
509 if (name
=== 'limit' && $scope
.action
=== 'get') {
512 if (name
=== 'debug') {
515 if (name
=== 'values') {
516 defaultVal
= defaultValues(defaultVal
);
518 if (name
=== 'loadOptions' && $scope
.action
=== 'getFields') {
522 ['id', 'name', 'label'],
523 ['id', 'name', 'label', 'abbr', 'description', 'color', 'icon']
527 param
.type
= ['string'];
529 $scope
.$bindToRoute({
530 expr
: 'params["' + name
+ '"]',
534 deep
: format
=== 'json'
537 if (typeof objectParams
[name
] !== 'undefined' && name
!== 'orderBy') {
538 $scope
.$watch('params.' + name
, function (values
) {
539 // Remove empty values
540 _
.each(values
, function (clause
, index
) {
541 if (!clause
|| !clause
[0]) {
542 $scope
.clearParam(name
, index
);
547 if (name
=== 'select' && actionInfo
.params
.having
) {
548 $scope
.$watchCollection('params.select', function(newSelect
) {
549 // Ignore row_count, it can't be used in HAVING clause
550 var select
= _
.without(newSelect
, 'row_count');
551 $scope
.havingOptions
.length
= 0;
552 // An empty select is an implicit *
553 if (!select
.length
) {
556 _
.each(select
, function(item
) {
558 pieces
= item
.split(' AS '),
559 alias
= _
.trim(pieces
[pieces
.length
- 1]).replace(':label', ':name');
561 if (alias
[alias
.length
- 1] === '*') {
562 if (alias
.length
> 1) {
563 joinEntity
= entityNameFromAlias(alias
.slice(0, -2));
565 var fieldList
= _
.filter(getEntity(joinEntity
).fields
, {custom_field_id
: null});
566 formatForSelect2(fieldList
, $scope
.havingOptions
, 'name', ['description', 'required', 'default_value'], alias
.slice(0, -1));
569 $scope
.havingOptions
.push({id
: alias
, text
: alias
});
574 if (typeof objectParams
[name
] !== 'undefined' || name
=== 'groupBy' || name
=== 'select' || name
=== 'join') {
575 $scope
.$watch('controls.' + name
, function(value
) {
577 $timeout(function() {
579 if (name
=== 'join') {
580 $scope
.params
[name
].push([field
+ ' AS ' + _
.snakeCase(field
), 'LEFT']);
581 ctrl
.buildFieldList();
583 else if (typeof objectParams
[name
] === 'undefined') {
584 $scope
.params
[name
].push(field
);
586 var defaultOp
= _
.cloneDeep(objectParams
[name
]);
587 if (name
=== 'chain') {
588 var num
= $scope
.params
.chain
.length
;
589 defaultOp
[0] = field
;
590 field
= 'name_me_' + num
;
592 $scope
.params
[name
].push([field
, defaultOp
]);
594 $scope
.controls
[name
] = null;
600 ctrl
.buildFieldList();
601 $scope
.availableParams
= actionInfo
.params
;
606 function describeSqlFn(params
) {
608 _
.each(params
, function(param
) {
611 desc
+= param
.name
+ ' ';
613 if (!_
.isEmpty(param
.flag_before
)) {
614 desc
+= '[' + _
.filter(param
.name
? [param
.name
] : _
.keys(param
.flag_before
)).join('|') + '] ';
616 if (param
.max_expr
=== 1) {
618 } else if (param
.max_expr
> 1) {
619 desc
+= 'expr, ... ';
621 if (!_
.isEmpty(param
.flag_after
)) {
622 desc
+= ' [' + _
.filter(param
.flag_after
).join('|') + '] ';
625 return desc
.replace(/[ ]+/g, ' ');
628 function defaultValues(defaultVal
) {
629 _
.each($scope
.fields
, function(field
) {
630 if (field
.required
) {
631 defaultVal
.push([field
.id
, '']);
637 function stringify(value
, trim
) {
638 if (typeof value
=== 'undefined') {
641 var str
= JSON
.stringify(value
).replace(/,/g
, ', ');
643 str
= str
.slice(1, -1);
648 function writeCode() {
650 entity
= $scope
.entity
,
651 action
= $scope
.action
,
652 params
= getParams(),
653 index
= isInt($scope
.index
) ? +$scope
.index
: parseYaml($scope
.index
),
655 if ($scope
.entity
&& $scope
.action
) {
657 if (action
.slice(0, 3) === 'get') {
658 result
= entity
.substr(0, 7) === 'Custom_' ? _
.camelCase(entity
.substr(7)) : entity
;
659 result
= lcfirst(action
.replace(/s$/, '').slice(3) || result
);
661 var results
= lcfirst(_
.isNumber(index
) ? result
: pluralize(result
)),
662 paramCount
= _
.size(params
),
665 switch ($scope
.selectedTab
.code
) {
669 var js
= "'" + entity
+ "', '" + action
+ "', {";
670 _
.each(params
, function(param
, key
) {
671 js
+= "\n " + key
+ ': ' + stringify(param
) +
672 (++i
< paramCount
? ',' : '');
673 if (key
=== 'checkPermissions') {
674 js
+= ' // IGNORED: permissions are always enforced from client-side requests';
678 if (index
|| index
=== 0) {
679 js
+= ', ' + JSON
.stringify(index
);
681 code
.js
= "CRM.api4(" + js
+ ").then(function(" + results
+ ") {\n // do something with " + results
+ " array\n}, function(failure) {\n // handle failure\n});";
682 code
.js2
= "CRM.api4({" + results
+ ': [' + js
+ "]}).then(function(batch) {\n // do something with batch." + results
+ " array\n}, function(failure) {\n // handle failure\n});";
683 code
.ang
= "crmApi4(" + js
+ ").then(function(" + results
+ ") {\n // do something with " + results
+ " array\n}, function(failure) {\n // handle failure\n});";
684 code
.ang2
= "crmApi4({" + results
+ ': [' + js
+ "]}).then(function(batch) {\n // do something with batch." + results
+ " array\n}, function(failure) {\n // handle failure\n});";
689 code
.php
= '$' + results
+ " = civicrm_api4('" + entity
+ "', '" + action
+ "', [";
690 _
.each(params
, function(param
, key
) {
691 code
.php
+= "\n '" + key
+ "' => " + phpFormat(param
, 4) + ',';
694 if (index
|| index
=== 0) {
695 code
.php
+= ', ' + phpFormat(index
);
700 code
.oop
= '$' + results
+ " = " + formatOOP(entity
, action
, params
, 2) + "\n ->execute()";
701 if (_
.isNumber(index
)) {
702 code
.oop
+= !index
? '\n ->first()' : (index
=== -1 ? '\n ->last()' : '\n ->itemAt(' + index
+ ')');
704 if (_
.isString(index
) || (_
.isPlainObject(index
) && !index
[0] && !index
['0'])) {
705 code
.oop
+= "\n ->indexBy('" + (_
.isPlainObject(index
) ? _
.keys(index
)[0] : index
) + "')";
707 if (_
.isArray(index
) || _
.isPlainObject(index
)) {
708 code
.oop
+= "\n ->column('" + (_
.isArray(index
) ? index
[0] : _
.values(index
)[0]) + "')";
712 if (!_
.isNumber(index
)) {
713 code
.oop
+= "foreach ($" + results
+ ' as $' + ((_
.isString(index
) && index
) ? index
+ ' => $' : '') + result
+ ') {\n // do something\n}';
718 // Cli code using json input
719 code
.long = 'cv api4 ' + entity
+ '.' + action
+ ' ' + cliFormat(JSON
.stringify(params
));
720 code
.pipe
= 'echo ' + cliFormat(JSON
.stringify(params
)) + ' | cv api4 ' + entity
+ '.' + action
+ ' --in=json';
722 // Cli code using short syntax
723 code
.short = 'cv api4 ' + entity
+ '.' + action
;
724 var limitSet
= false;
725 _
.each(params
, function(param
, key
) {
727 case (key
=== 'select' && !_
.includes(param
.join(), ' ')):
728 code
.short += ' +s ' + cliFormat(param
.join(','));
730 case (key
=== 'where' && !_
.intersection(_
.map(param
, 0), ['AND', 'OR', 'NOT']).length
):
731 _
.each(param
, function(clause
) {
732 code
.short += ' +w ' + cliFormat(clause
[0] + ' ' + clause
[1] + (clause
.length
> 2 ? (' ' + JSON
.stringify(clause
[2])) : ''));
735 case (key
=== 'orderBy'):
736 _
.each(param
, function(dir
, field
) {
737 code
.short += ' +o ' + cliFormat(field
+ ' ' + dir
);
740 case (key
=== 'values'):
741 _
.each(param
, function(val
, field
) {
742 code
.short += ' +v ' + cliFormat(field
+ '=' + val
);
745 case (key
=== 'limit' || key
=== 'offset'):
746 // These 2 get combined
749 code
.short += ' +l ' + (params
.limit
|| '0') + (params
.offset
? ('@' + params
.offset
) : '');
753 code
.short += ' ' + key
+ '=' + (typeof param
=== 'string' ? cliFormat(param
) : cliFormat(JSON
.stringify(param
)));
758 _
.each($scope
.code
, function(vals
) {
759 _
.each(vals
, function(style
) {
760 style
.code
= code
[style
.name
] ? prettyPrintOne(_
.escape(code
[style
.name
])) : '';
766 function formatOOP(entity
, action
, params
, indent
) {
767 var info
= getEntity(entity
),
768 newLine
= "\n" + _
.repeat(' ', indent
),
769 code
= '\\' + info
.class + '::' + action
+ '(',
770 perm
= params
.checkPermissions
=== false ? 'FALSE' : '';
771 if (entity
.substr(0, 7) !== 'Custom_') {
774 code
+= "'" + entity
.substr(7) + "'" + (perm
? ', ' : '') + perm
+ ")";
776 _
.each(params
, function(param
, key
) {
778 if (typeof objectParams
[key
] !== 'undefined' && key
!== 'chain') {
779 _
.each(param
, function(item
, index
) {
780 val
= phpFormat(index
) + ', ' + phpFormat(item
, 2 + indent
);
781 code
+= newLine
+ "->add" + ucfirst(key
).replace(/s$/, '') + '(' + val
+ ')';
783 } else if (key
=== 'where') {
784 _
.each(param
, function (clause
) {
785 if (clause
[0] === 'AND' || clause
[0] === 'OR' || clause
[0] === 'NOT') {
786 code
+= newLine
+ "->addClause(" + phpFormat(clause
[0]) + ", " + phpFormat(clause
[1]).slice(1, -1) + ')';
788 code
+= newLine
+ "->addWhere(" + phpFormat(clause
).slice(1, -1) + ")";
791 } else if (key
=== 'select') {
792 // selectRowCount() is a shortcut for addSelect('row_count')
793 if (isSelectRowCount(params
)) {
794 code
+= newLine
+ '->selectRowCount()';
795 param
= _
.without(param
, 'row_count');
797 // addSelect() is a variadic function & can take multiple arguments
799 code
+= newLine
+ '->addSelect(' + phpFormat(param
).slice(1, -1) + ')';
801 } else if (key
=== 'chain') {
802 _
.each(param
, function(chain
, name
) {
803 code
+= newLine
+ "->addChain('" + name
+ "', " + formatOOP(chain
[0], chain
[1], chain
[2], 2 + indent
);
804 code
+= (chain
.length
> 3 ? ',' : '') + (!_
.isEmpty(chain
[2]) ? newLine
: ' ') + (chain
.length
> 3 ? phpFormat(chain
[3]) : '') + ')';
806 } else if (key
=== 'join') {
807 _
.each(param
, function(join
) {
808 code
+= newLine
+ "->addJoin(" + phpFormat(join
).slice(1, -1) + ')';
811 else if (key
!== 'checkPermissions') {
812 code
+= newLine
+ "->set" + ucfirst(key
) + '(' + phpFormat(param
, 2 + indent
) + ')';
818 function isInt(value
) {
819 if (_
.isFinite(value
)) {
822 if (!_
.isString(value
)) {
825 return /^-{0,1}\d+$/.test(value
);
828 function formatMeta(resp
) {
830 _
.each(resp
, function(val
, key
) {
831 if (key
!== 'values' && !_
.isPlainObject(val
) && !_
.isFunction(val
)) {
832 ret
+= (ret
.length
? ', ' : '') + key
+ ': ' + (_
.isArray(val
) ? '[' + val
+ ']' : val
);
835 return prettyPrintOne(_
.escape(ret
));
838 $scope
.execute = function() {
839 $scope
.status
= 'info';
840 $scope
.loading
= true;
841 $http
.post(CRM
.url('civicrm/ajax/api4/' + $scope
.entity
+ '/' + $scope
.action
, {
842 params
: angular
.toJson(getParams()),
843 index
: isInt($scope
.index
) ? +$scope
.index
: parseYaml($scope
.index
)
846 'X-Requested-With': 'XMLHttpRequest'
848 }).then(function(resp
) {
849 $scope
.loading
= false;
850 $scope
.status
= resp
.data
&& resp
.data
.debug
&& resp
.data
.debug
.log
? 'warning' : 'success';
851 $scope
.debug
= debugFormat(resp
.data
);
853 formatMeta(resp
.data
),
854 prettyPrintOne((_
.isArray(resp
.data
.values
) ? '(' + resp
.data
.values
.length
+ ') ' : '') + _
.escape(JSON
.stringify(resp
.data
.values
, null, 2)), 'js', 1)
857 $scope
.loading
= false;
858 $scope
.status
= 'danger';
859 $scope
.debug
= debugFormat(resp
.data
);
862 prettyPrintOne(_
.escape(JSON
.stringify(resp
.data
, null, 2)))
867 function debugFormat(data
) {
868 var debug
= data
.debug
? prettyPrintOne(_
.escape(JSON
.stringify(data
.debug
, null, 2)).replace(/\\n/g, "\n")) : null;
874 * Format value to look like php code
876 function phpFormat(val
, indent
) {
877 if (typeof val
=== 'undefined') {
880 if (val
=== null || val
=== true || val
=== false) {
881 return JSON
.stringify(val
).toUpperCase();
883 indent
= (typeof indent
=== 'number') ? _
.repeat(' ', indent
) : (indent
|| '');
885 baseLine
= indent
? indent
.slice(0, -2) : '',
886 newLine
= indent
? '\n' : '',
887 trailingComma
= indent
? ',' : '';
888 if ($.isPlainObject(val
)) {
889 $.each(val
, function(k
, v
) {
890 ret
+= (ret
? ', ' : '') + newLine
+ indent
+ "'" + k
+ "' => " + phpFormat(v
);
892 return '[' + ret
+ trailingComma
+ newLine
+ baseLine
+ ']';
894 if ($.isArray(val
)) {
895 $.each(val
, function(k
, v
) {
896 ret
+= (ret
? ', ' : '') + newLine
+ indent
+ phpFormat(v
);
898 return '[' + ret
+ trailingComma
+ newLine
+ baseLine
+ ']';
900 if (_
.isString(val
) && !_
.contains(val
, "'")) {
901 return "'" + val
+ "'";
903 return JSON
.stringify(val
).replace(/\$/g, '\\$');
906 // Format string to be cli-input-safe
907 function cliFormat(str
) {
908 if (!_
.includes(str
, ' ') && !_
.includes(str
, '"') && !_
.includes(str
, "'")) {
911 if (!_
.includes(str
, "'")) {
912 return "'" + str
+ "'";
914 if (!_
.includes(str
, '"')) {
915 return '"' + str
+ '"';
917 return "'" + str
.replace(/'/g, "\\'") + "'";
920 function fetchMeta() {
921 crmApi4(getMetaParams)
922 .then(function(data) {
924 getEntity().actions = data.actions;
930 // Help for an entity with no action selected
931 function showEntityHelp(entityName) {
932 var entityInfo = getEntity(entityName);
933 setHelp($scope.entity, {
934 description: entityInfo.description,
935 comment: entityInfo.comment,
936 type: entityInfo.type,
937 since: entityInfo.since,
942 if (!$scope.entity) {
943 setHelp(ts('APIv4 Explorer
'), {description: docs.description, comment: docs.comment, see: docs.see});
944 } else if (!actions.length && !getEntity().actions) {
945 getMetaParams.actions = [$scope.entity, 'getActions
', {chain: {fields: [$scope.entity, 'getFields
', {action: '$name
'}]}}];
952 showEntityHelp($scope.entity);
955 // Update route when changing entity
956 $scope.$watch('entity
', function(newVal, oldVal) {
957 if (oldVal !== newVal) {
958 // Flush actions cache to re-fetch for new entity
960 $location.url('/explorer/' + newVal);
964 // Update route when changing actions
965 $scope.$watch('action
', function(newVal, oldVal) {
966 if ($scope.entity && $routeParams.api4action !== newVal && !_.isUndefined(newVal)) {
967 $location.url('/explorer/' + $scope.entity + '/' + newVal);
969 setHelp($scope.entity + '::' + newVal, _.pick(_.findWhere(getEntity().actions, {name: newVal}), ['description
', 'comment
', 'see
', 'deprecated
']));
973 $scope.paramDoc = function(name) {
974 return docs.params[name];
977 $scope.executeDoc = function() {
979 description: ts('Runs API call on the CiviCRM database
.'),
980 comment: ts('Results and debugging info will be displayed below
.')
982 if ($scope.action === 'delete') {
983 doc.WARNING = ts('This API call will be executed on the real database
. Deleting data cannot be undone
.');
985 else if ($scope.action && $scope.action.slice(0, 3) !== 'get') {
986 doc.WARNING = ts('This API call will be executed on the real database
. It cannot be undone
.');
991 $scope.saveDoc = function() {
993 description: ts('Save API call as a smart group
.'),
994 comment: ts('Create a SavedSearch using these API params to populate a smart group
.') +
995 '\n\n' + ts('NOTE
: you must select contact id as the only field
.')
999 $scope.$watch('params
', writeCode, true);
1000 $scope.$watch('index
', writeCode);
1003 $scope.save = function() {
1004 $scope.params.limit = $scope.params.offset = 0;
1005 if ($scope.params.chain.length) {
1006 CRM.alert(ts('Smart groups are not compatible
with API chaining
.'), ts('Error
'), 'error
', {expires: 5000});
1009 if ($scope.params.select.length !== 1 || !_.includes($scope.params.select[0], 'id
')) {
1010 CRM.alert(ts('To create a smart group
, the API must select contact id and no other fields
.'), ts('Error
'), 'error
', {expires: 5000});
1016 visibility: 'User and User Admin Only
',
1019 entity: $scope.entity,
1020 params: JSON.parse(angular.toJson($scope.params))
1022 model.params.version = 4;
1023 delete model.params.chain;
1024 delete model.params.debug;
1025 delete model.params.limit;
1026 delete model.params.offset;
1027 delete model.params.orderBy;
1028 delete model.params.checkPermissions;
1029 var options = CRM.utils.adjustDialogDefaults({
1032 title: ts('Save smart group
')
1034 dialogService.open('saveSearchDialog
', '~/api4Explorer/SaveSearch
.html
', model, options);
1038 angular.module('api4Explorer
').controller('SaveSearchCtrl
', function($scope, crmApi4, dialogService) {
1039 var ts = $scope.ts = CRM.ts(),
1040 model = $scope.model;
1041 $scope.groupEntityRefParams = {
1044 params: {is_hidden: 0, is_active: 1, 'saved_search_id
.api_entity
': model.entity},
1045 extra: ['saved_search_id
', 'description
', 'visibility
', 'group_type
']
1049 minimumInputLength: 0,
1050 placeholder: ts('Select existing group
')
1053 if (!CRM.checkPerm('administer reserved groups
')) {
1054 $scope.groupEntityRefParams.api.params.is_reserved = 0;
1057 administerReservedGroups: CRM.checkPerm('administer reserved groups
')
1059 $scope.options = CRM.vars.api4.groupOptions;
1060 $scope.$watch('model
.id
', function(id) {
1062 _.assign(model, $('#api
-save
-search
-select
-group
').select2('data
').extra);
1065 $scope.cancel = function() {
1066 dialogService.cancel('saveSearchDialog
');
1068 $scope.save = function() {
1069 $('.ui
-dialog
:visible
').block();
1070 var group = model.id ? {id: model.id} : {title: model.title};
1071 group.description = model.description;
1072 group.visibility = model.visibility;
1073 group.group_type = model.group_type;
1074 group.saved_search_id = '$id
';
1076 api_entity: model.entity,
1077 api_params: model.params
1080 savedSearch.id = model.saved_search_id;
1082 crmApi4('SavedSearch
', 'save
', {records: [savedSearch], chain: {group: ['Group
', 'save
', {'records
': [group]}]}})
1083 .then(function(result) {
1084 dialogService.close('saveSearchDialog
', result[0]);
1089 angular.module('api4Explorer
').component('crmApi4Clause
', {
1100 templateUrl: '~/api4Explorer/Clause
.html
',
1101 controller: function ($scope, $element, $timeout) {
1102 var ts = $scope.ts = CRM.ts(),
1104 this.conjunctions = {AND: ts('And
'), OR: ts('Or
'), NOT: ts('Not
')};
1105 this.operators = CRM.vars.api4.operators;
1106 this.sortOptions = {
1108 connectWith: '.api4
-clause
-group
-sortable
',
1109 containment: $element.closest('.api4
-clause
-fieldset
'),
1115 this.$onInit = function() {
1116 ctrl.hasParent = !!$element.attr('delete-group
');
1119 this.addGroup = function(op) {
1120 ctrl.clauses.push([op, []]);
1123 function onSort(event, ui) {
1124 $($element).closest('.api4
-clause
-fieldset
').toggleClass('api4
-sorting
', event.type === 'sortstart
');
1125 $('.api4
-input
.form
-inline
').css('margin
-left
', '');
1128 // Indent clause while dragging between nested groups
1129 function onSortOver(event, ui) {
1132 offset = $(ui.placeholder).offset().left - $(ui.sender).offset().left;
1134 $('.api4
-input
.form
-inline
.ui
-sortable
-helper
').css('margin
-left
', '' + offset + 'px
');
1137 this.addClause = function() {
1138 $timeout(function() {
1139 if (ctrl.newClause) {
1140 if (ctrl.skip && ctrl.clauses.length < ctrl.skip) {
1141 ctrl.clauses.push(null);
1143 ctrl.clauses.push([ctrl.newClause, '=', '']);
1144 ctrl.newClause = null;
1149 this.deleteRow = function(index) {
1150 ctrl.clauses.splice(index, 1);
1153 // Remove empty values
1154 this.changeClauseField = function(clause, index) {
1155 if (clause[0] === '') {
1156 ctrl.deleteRow(index);
1160 // Add/remove value if operator allows for one
1161 this.changeClauseOperator = function(clause) {
1162 if (_.contains(clause[1], 'IS
')) {
1164 } else if (clause.length === 2) {
1171 angular.module('api4Explorer
').directive('api4ExpValue
', function($routeParams, crmApi4) {
1174 data: '=api4ExpValue
'
1177 link: function (scope, element, attrs, ctrl) {
1178 var ts = scope.ts = CRM.ts(),
1179 multi = _.includes(['IN
', 'NOT IN
', 'BETWEEN
', 'NOT BETWEEN
'], scope.data.op),
1180 entity = $routeParams.api4entity,
1181 action = scope.data.action || $routeParams.api4action;
1183 function destroyWidget() {
1184 var $el = $(element);
1185 if ($el.is('.crm
-form
-date
-wrapper
.crm
-hidden
-date
')) {
1186 $el.crmDatepicker('destroy
');
1188 if ($el.is('.select2
-container
+ input
')) {
1189 $el.crmEntityRef('destroy
');
1191 $(element).removeData().removeAttr('type
').removeAttr('placeholder
').show();
1194 function makeWidget(field, op) {
1195 var $el = $(element),
1196 inputType = field.input_type,
1197 dataType = field.data_type;
1199 op = field.serialize || dataType === 'Array
' ? 'IN
' : '=';
1201 multi = _.includes(['IN
', 'NOT IN
', 'BETWEEN
', 'NOT BETWEEN
'], op);
1202 // IS NULL, IS EMPTY, etc.
1203 if (_.contains(op, 'IS
')) {
1207 if (inputType === 'Date
') {
1208 if (_.includes(['=', '!=', '<>', '<', '>=', '<', '<='], op)) {
1209 $el.crmDatepicker({time: (field.input_attrs && field.input_attrs.time) || false});
1211 } else if (_.includes(['=', '!=', '<>', 'IN
', 'NOT IN
'], op) && (field.fk_entity || field.options || dataType === 'Boolean
')) {
1212 if (field.options) {
1213 var id = field.pseudoconstant || 'id
';
1214 $el.addClass('loading
').attr('placeholder
', ts('- select
-')).crmSelect2({multiple: multi, data: [{id: '', text: ''}]});
1215 loadFieldOptions(field.entity || entity).then(function(data) {
1216 var options = _.transform(data[field.name].options, function(options, opt) {
1217 options.push({id: opt[id], text: opt.label, description: opt.description, color: opt.color, icon: opt.icon});
1219 $el.removeClass('loading
').crmSelect2({data: options, multiple: multi});
1221 } else if (field.fk_entity) {
1222 var apiParams = field.id_field ? {id_field: field.id_field} : {};
1223 $el.crmEntityRef({entity: field.fk_entity, api: apiParams, select: {multiple: multi}, static: field.fk_entity === 'Contact
' ? ['user_contact_id
'] : []});
1224 } else if (dataType === 'Boolean
') {
1225 $el.attr('placeholder
', ts('- select
-')).crmSelect2({allowClear: false, multiple: multi, placeholder: ts('- select
-'), data: [
1226 {id: 'true', text: ts('Yes
')},
1227 {id: 'false', text: ts('No
')}
1230 } else if (dataType === 'Integer
' && !multi) {
1231 $el.attr('type
', 'number
');
1235 function loadFieldOptions(entity) {
1236 if (!fieldOptions[entity + action]) {
1237 fieldOptions[entity + action] = crmApi4(entity, 'getFields
', {
1238 loadOptions: ['id
', 'name
', 'label
', 'description
', 'color
', 'icon
'],
1240 where: [['options
', '!=', false]],
1244 return fieldOptions[entity + action];
1247 // Copied from ng-list but applied conditionally if field is multi-valued
1248 var parseList = function(viewValue) {
1249 // If the viewValue is invalid (say required but empty) it will be `undefined`
1250 if (_.isUndefined(viewValue)) return;
1259 _.each(viewValue.split(','), function(value) {
1260 if (value) list.push(_.trim(value));
1267 // Copied from ng-list
1268 ctrl.$parsers.push(parseList);
1269 ctrl.$formatters.push(function(value) {
1270 return _.isArray(value) ? value.join(', ') : value;
1273 // Copied from ng-list
1274 ctrl.$isEmpty = function(value) {
1275 return !value || !value.length;
1278 scope.$watchCollection('data
', function(data) {
1280 var field = getField(data.field, entity, action);
1281 if (field && data.format !== 'plain
') {
1282 makeWidget(field, data.op);
1290 angular.module('api4Explorer
').directive('api4ExpChain
', function(crmApi4) {
1293 chain: '=api4ExpChain
',
1297 templateUrl: '~/api4Explorer/Chain
.html
',
1298 link: function (scope, element, attrs) {
1299 var ts = scope.ts = CRM.ts();
1301 function changeEntity(newEntity, oldEntity) {
1302 // When clearing entity remove this chain
1304 scope.chain[0] = '';
1307 // Reset action && index
1308 if (newEntity !== oldEntity) {
1309 scope.chain[1][1] = scope.chain[1][2] = '';
1311 if (getEntity(newEntity).actions) {
1314 crmApi4(newEntity, 'getActions
', {chain: {fields: [newEntity, 'getFields
', {action: '$name
'}]}})
1315 .then(function(data) {
1316 getEntity(data.entity).actions = data;
1317 if (data.entity === scope.chain[1][0]) {
1324 function setActions() {
1325 scope.actions = [''].concat(_.pluck(getEntity(scope.chain[1][0]).actions, 'name
'));
1328 // Set default params when choosing action
1329 function changeAction(newAction, oldAction) {
1331 // Prepopulate links
1332 if (newAction && newAction !== oldAction) {
1334 scope.chain[1][3] = '';
1335 // Look for links back to main entity
1336 _.each(entityFields(scope.chain[1][0]), function(field) {
1337 if (field.fk_entity === scope.mainEntity) {
1338 link = [field.name, '$id
'];
1341 // Look for links from main entity
1342 if (!link && newAction !== 'create
') {
1343 _.each(entityFields(scope.mainEntity), function(field) {
1344 if (field.fk_entity === scope.chain[1][0]) {
1345 link = ['id
', '$' + field.name];
1346 // Since we're specifying the id
, set index to getsingle
1347 scope
.chain
[1][3] = '0';
1351 if (link
&& _
.contains(['get', 'update', 'replace', 'delete'], newAction
)) {
1352 scope
.chain
[1][2] = '{where: [[' + link
[0] + ', =, ' + link
[1] + ']]}';
1354 else if (link
&& _
.contains(['create'], newAction
)) {
1355 scope
.chain
[1][2] = '{values: {' + link
[0] + ': ' + link
[1] + '}}';
1357 else if (link
&& _
.contains(['save'], newAction
)) {
1358 scope
.chain
[1][2] = '{records: [{' + link
[0] + ': ' + link
[1] + '}]}';
1360 scope
.chain
[1][2] = '{}';
1365 scope
.$watch("chain[1][0]", changeEntity
);
1366 scope
.$watch("chain[1][1]", changeAction
);
1371 function getEntity(entityName
) {
1372 return _
.findWhere(schema
, {name
: entityName
});
1375 function entityFields(entityName
, action
) {
1376 var entity
= getEntity(entityName
);
1377 if (entity
&& action
&& entity
.actions
) {
1378 return _
.findWhere(entity
.actions
, {name
: action
}).fields
;
1380 return _
.result(entity
, 'fields');
1383 function getExplicitJoins() {
1384 return _
.transform(params
.join
, function(joins
, join
) {
1385 // Fix capitalization of AS
1386 join
[0] = join
[0].replace(/ as
/i
, ' AS ');
1387 var j
= join
[0].split(' AS '),
1388 joinEntity
= _
.trim(j
[0]),
1389 joinAlias
= _
.trim(j
[1]) || joinEntity
.toLowerCase();
1390 joins
[joinAlias
] = {
1393 side
: join
[1] || 'LEFT',
1394 bridge
: _
.isString(join
[2]) ? join
[2] : null
1399 function getField(fieldName
, entity
, action
) {
1400 var suffix
= fieldName
.split(':')[1];
1401 fieldName
= fieldName
.split(':')[0];
1402 var fieldNames
= fieldName
.split('.');
1403 var field
= _
.cloneDeep(get(entity
, fieldNames
));
1404 if (field
&& suffix
) {
1405 field
.pseudoconstant
= suffix
;
1407 // When joining to a 'name' field, value fields should render an appropriate entityRef
1408 if (field
&& field
.type
=== 'Field' && field
.name
=== 'name' && _
.includes(fieldName
, '.')) {
1409 field
.fk_entity
= field
.entity
;
1410 field
.id_field
= 'name';
1414 function get(entity
, fieldNames
) {
1415 if (fieldNames
.length
=== 1) {
1416 return _
.findWhere(entityFields(entity
, action
), {name
: fieldNames
[0]});
1418 var comboName
= _
.findWhere(entityFields(entity
, action
), {name
: fieldNames
[0] + '.' + fieldNames
[1]});
1422 var linkName
= fieldNames
.shift(),
1423 join
= getExplicitJoins()[linkName
],
1424 newEntity
= join
? join
.entity
: _
.findWhere(entityFields(entity
, action
), {name
: linkName
}).fk_entity
;
1425 return get(newEntity
, fieldNames
);
1429 // Collapsible optgroups for select2
1432 .on('select2-open', function(e
) {
1433 if ($(e
.target
).hasClass('collapsible-optgroups')) {
1435 .off('.collapseOptionGroup')
1436 .addClass('collapsible-optgroups-enabled')
1437 .on('click.collapseOptionGroup', '.select2-result-with-children > .select2-result-label', function() {
1438 $(this).parent().toggleClass('optgroup-expanded');
1442 .on('select2-close', function() {
1443 $('#select2-drop').off('.collapseOptionGroup').removeClass('collapsible-optgroups-enabled');
1446 })(angular
, CRM
.$, CRM
._
);