1 (function(angular
, $, _
, undefined) {
4 var schema
= CRM
.vars
.api4
.schema
;
6 var links
= CRM
.vars
.api4
.links
;
7 // Cache list of entities
9 // Cache list of actions
12 var fieldOptions
= {};
15 angular
.module('api4Explorer').config(function($routeProvider
) {
16 $routeProvider
.when('/explorer/:api4entity?/:api4action?', {
17 controller
: 'Api4Explorer',
18 templateUrl
: '~/api4Explorer/Explorer.html',
23 angular
.module('api4Explorer').controller('Api4Explorer', function($scope
, $routeParams
, $location
, $timeout
, $http
, crmUiHelp
, crmApi4
) {
24 var ts
= $scope
.ts
= CRM
.ts();
25 $scope
.entities
= entities
;
26 $scope
.actions
= actions
;
28 $scope
.fieldsAndJoins
= [];
29 $scope
.selectFieldsAndJoins
= [];
30 $scope
.availableParams
= {};
33 $scope
.resultTab
= {selected
: 'result'};
35 accessDebugOutput
: CRM
.checkPerm('access debug output')
37 var getMetaParams
= {},
38 objectParams
= {orderBy
: 'ASC', values
: '', chain
: ['Entity', '', '{}']},
39 docs
= CRM
.vars
.api4
.docs
,
42 $scope
.helpTitle
= '';
43 $scope
.helpContent
= {};
44 $scope
.entity
= $routeParams
.api4entity
;
47 $scope
.status
= 'default';
48 $scope
.loading
= false;
51 oop
: ts('PHP (oop style)'),
52 php
: ts('PHP (traditional)'),
54 cli
: ts('Command Line')
56 $scope
.code
= codeDefaults();
58 function codeDefaults() {
59 return _
.mapValues($scope
.codeLabel
, function(val
, key
) {
60 return key
=== 'oop' ? ts('Select an entity and action') : '';
64 if (!entities
.length
) {
65 formatForSelect2(schema
, entities
, 'name', ['description']);
74 function ucfirst(str
) {
75 return str
[0].toUpperCase() + str
.slice(1);
78 function lcfirst(str
) {
79 return str
[0].toLowerCase() + str
.slice(1);
82 function pluralize(str
) {
83 switch (str
[str
.length
-1]) {
87 return str
.slice(0, -1) + 'ies';
93 // Turn a flat array into a select2 array
94 function arrayToSelect2(array
) {
96 _
.each(array
, function(item
) {
97 out
.push({id
: item
, text
: item
});
102 // Reformat an existing array of objects for compatibility with select2
103 function formatForSelect2(input
, container
, key
, extra
, prefix
) {
104 _
.each(input
, function(item
) {
105 var id
= (prefix
|| '') + item
[key
];
106 var formatted
= {id
: id
, text
: id
};
108 _
.merge(formatted
, _
.pick(item
, extra
));
110 container
.push(formatted
);
115 function getFieldList(action
) {
117 fieldInfo
= _
.findWhere(getEntity().actions
, {name
: action
}).fields
;
118 formatForSelect2(fieldInfo
, fields
, 'name', ['description', 'required', 'default_value']);
122 function addJoins(fieldList
, addWildcard
) {
123 var fields
= _
.cloneDeep(fieldList
),
124 fks
= _
.findWhere(links
, {entity
: $scope
.entity
}) || {};
125 _
.each(fks
.links
, function(link
) {
126 var linkFields
= _
.cloneDeep(entityFields(link
.entity
)),
127 wildCard
= addWildcard
? [{id
: link
.alias
+ '.*', text
: link
.alias
+ '.*', 'description': 'All core ' + link
.entity
+ ' fields'}] : [];
131 description
: 'Join to ' + link
.entity
,
132 children
: wildCard
.concat(formatForSelect2(linkFields
, [], 'name', ['description'], link
.alias
+ '.'))
139 $scope
.help = function(title
, param
) {
141 $scope
.helpTitle
= helpTitle
;
142 $scope
.helpContent
= helpContent
;
144 $scope
.helpTitle
= title
;
145 $scope
.helpContent
= param
;
149 // Format the href for a @see help annotation
150 $scope
.formatRef = function(see
) {
151 var match
= see
.match(/^\\Civi\\Api4\\([a-zA-Z]+)$/);
153 return '#/explorer/' + match
[1];
155 if (see
[0] === '\\') {
156 return 'https://github.com/civicrm/civicrm-core/blob/master' + see
.replace(/\\/i, '/') + '.php
';
161 $scope.fieldHelp = function(fieldName) {
162 var field = getField(fieldName, $scope.entity, $scope.action);
167 description: field.description,
168 type: field.data_type
170 if (field.default_value) {
171 info.default = field.default_value;
173 if (field.required_if) {
174 info.required_if = field.required_if;
175 } else if (field.required) {
176 info.required = 'true';
181 $scope.valuesFields = function() {
182 var fields = _.cloneDeep($scope.action === 'getFields
' ? getFieldList($scope.params.action || 'get') : $scope.fields);
183 // Disable fields that are already in use
184 _.each($scope.params.values || [], function(val) {
185 (_.findWhere(fields, {id: val[0]}) || {}).disabled = true;
187 return {results: fields};
190 $scope.formatSelect2Item = function(row) {
191 return _.escape(row.text) +
192 (row.required ? '<span
class="crm-marker"> *</span
>' : '') +
193 (row.description ? '<div
class="crm-select2-row-description"><p
>' + _.escape(row.description) + '</p></div>' : '');
196 $scope.clearParam = function(name) {
197 $scope.params[name] = $scope.availableParams[name].default;
200 $scope.isSpecial = function(name) {
201 var specialParams = ['select
', 'fields
', 'action
', 'where
', 'values
', 'orderBy
', 'chain
'];
202 return _.contains(specialParams, name);
205 $scope.selectRowCount = function() {
206 if ($scope.isSelectRowCount()) {
207 $scope.params.select = [];
209 $scope.params.select = ['row_count
'];
211 if ($scope.params.limit == 25) {
212 $scope.params.limit = 0;
217 $scope.isSelectRowCount = function() {
218 return $scope.params && $scope.params.select && $scope.params.select.length === 1 && $scope.params.select[0] === 'row_count
';
221 function getEntity(entityName) {
222 return _.findWhere(schema, {name: entityName || $scope.entity});
225 // Get all params that have been set
226 function getParams() {
228 _.each($scope.params, function(param, key) {
229 if (param != $scope.availableParams[key].default && !(typeof param === 'object
' && _.isEmpty(param))) {
230 if (_.contains($scope.availableParams[key].type, 'array
') && (typeof objectParams[key] === 'undefined')) {
231 params[key] = parseYaml(JSON.parse(angular.toJson(param)));
237 _.each(objectParams, function(defaultVal, key) {
240 _.each(params[key], function(item) {
241 var val = _.cloneDeep(item[1]);
242 // Remove blank items from "chain" array
243 if (_.isArray(val)) {
244 _.eachRight(item[1], function(v, k) {
251 newParam[item[0]] = parseYaml(val);
253 params[key] = newParam;
259 function parseYaml(input) {
260 if (typeof input === 'undefined') {
266 if (_.isObject(input) || _.isArray(input)) {
267 _.each(input, function(item, index) {
268 input[index] = parseYaml(item);
273 var output = (input === '>') ? '>' : jsyaml.safeLoad(input);
274 // We don't want dates parsed to js objects
275 return _
.isDate(output
) ? input
: output
;
281 function selectAction() {
282 $scope
.action
= $routeParams
.api4action
;
283 $scope
.fieldsAndJoins
.length
= 0;
284 $scope
.selectFieldsAndJoins
.length
= 0;
285 if (!actions
.length
) {
286 formatForSelect2(getEntity().actions
, actions
, 'name', ['description', 'params']);
289 var actionInfo
= _
.findWhere(actions
, {id
: $scope
.action
});
290 $scope
.fields
= getFieldList($scope
.action
);
291 if (_
.contains(['get', 'update', 'delete', 'replace'], $scope
.action
)) {
292 $scope
.fieldsAndJoins
= addJoins($scope
.fields
);
293 $scope
.selectFieldsAndJoins
= addJoins($scope
.fields
, true);
295 $scope
.fieldsAndJoins
= $scope
.fields
;
296 $scope
.selectFieldsAndJoins
= _
.cloneDeep($scope
.fields
);
298 $scope
.selectFieldsAndJoins
.unshift({id
: '*', text
: '*', 'description': 'All core ' + $scope
.entity
+ ' fields'});
299 _
.each(actionInfo
.params
, function (param
, name
) {
301 defaultVal
= _
.cloneDeep(param
.default);
303 switch (param
.type
[0]) {
306 format
= param
.type
[0];
317 if (name
== 'limit') {
320 if (name
=== 'values') {
321 defaultVal
= defaultValues(defaultVal
);
323 $scope
.$bindToRoute({
324 expr
: 'params["' + name
+ '"]',
328 deep
: format
=== 'json'
331 if (typeof objectParams
[name
] !== 'undefined') {
332 $scope
.$watch('params.' + name
, function(values
) {
333 // Remove empty values
334 _
.each(values
, function(clause
, index
) {
335 if (!clause
|| !clause
[0]) {
336 $scope
.params
[name
].splice(index
, 1);
340 $scope
.$watch('controls.' + name
, function(value
) {
342 $timeout(function() {
344 var defaultOp
= _
.cloneDeep(objectParams
[name
]);
345 if (name
=== 'chain') {
346 var num
= $scope
.params
.chain
.length
;
347 defaultOp
[0] = field
;
348 field
= 'name_me_' + num
;
350 $scope
.params
[name
].push([field
, defaultOp
]);
351 $scope
.controls
[name
] = null;
357 $scope
.availableParams
= actionInfo
.params
;
362 function defaultValues(defaultVal
) {
363 _
.each($scope
.fields
, function(field
) {
364 if (field
.required
) {
365 defaultVal
.push([field
.id
, '']);
371 function stringify(value
, trim
) {
372 if (typeof value
=== 'undefined') {
375 var str
= JSON
.stringify(value
).replace(/,/g
, ', ');
377 str
= str
.slice(1, -1);
382 function writeCode() {
383 var code
= codeDefaults(),
384 entity
= $scope
.entity
,
385 action
= $scope
.action
,
386 params
= getParams(),
387 index
= isInt($scope
.index
) ? +$scope
.index
: parseYaml($scope
.index
),
389 if ($scope
.entity
&& $scope
.action
) {
390 if (action
.slice(0, 3) === 'get') {
391 result
= entity
.substr(0, 7) === 'Custom_' ? _
.camelCase(entity
.substr(7)) : entity
;
392 result
= lcfirst(action
.replace(/s$/, '').slice(3) || result
);
394 var results
= lcfirst(_
.isNumber(index
) ? result
: pluralize(result
)),
395 paramCount
= _
.size(params
),
396 isSelectRowCount
= params
.select
&& params
.select
.length
=== 1 && params
.select
[0] === 'row_count',
399 if (isSelectRowCount
) {
400 results
= result
+ 'Count';
404 code
.js
= "CRM.api4('" + entity
+ "', '" + action
+ "', {";
405 _
.each(params
, function(param
, key
) {
406 code
.js
+= "\n " + key
+ ': ' + stringify(param
) +
407 (++i
< paramCount
? ',' : '');
408 if (key
=== 'checkPermissions') {
409 code
.js
+= ' // IGNORED: permissions are always enforced from client-side requests';
413 if (index
|| index
=== 0) {
414 code
.js
+= ', ' + JSON
.stringify(index
);
416 code
.js
+= ").then(function(" + results
+ ") {\n // do something with " + results
+ " array\n}, function(failure) {\n // handle failure\n});";
419 code
.php
= '$' + results
+ " = civicrm_api4('" + entity
+ "', '" + action
+ "', [";
420 _
.each(params
, function(param
, key
) {
421 code
.php
+= "\n '" + key
+ "' => " + phpFormat(param
, 4) + ',';
424 if (index
|| index
=== 0) {
425 code
.php
+= ', ' + phpFormat(index
);
430 if (entity
.substr(0, 7) !== 'Custom_') {
431 code
.oop
= '$' + results
+ " = \\Civi\\Api4\\" + entity
+ '::' + action
+ '()';
433 code
.oop
= '$' + results
+ " = \\Civi\\Api4\\CustomValue::" + action
+ "('" + entity
.substr(7) + "')";
435 _
.each(params
, function(param
, key
) {
437 if (typeof objectParams
[key
] !== 'undefined' && key
!== 'chain') {
438 _
.each(param
, function(item
, index
) {
439 val
= phpFormat(index
) + ', ' + phpFormat(item
, 4);
440 code
.oop
+= "\n ->add" + ucfirst(key
).replace(/s$/, '') + '(' + val
+ ')';
442 } else if (key
=== 'where') {
443 _
.each(param
, function (clause
) {
444 if (clause
[0] === 'AND' || clause
[0] === 'OR' || clause
[0] === 'NOT') {
445 code
.oop
+= "\n ->addClause(" + phpFormat(clause
[0]) + ", " + phpFormat(clause
[1]).slice(1, -1) + ')';
447 code
.oop
+= "\n ->addWhere(" + phpFormat(clause
).slice(1, -1) + ")";
450 } else if (key
=== 'select' && isSelectRowCount
) {
451 code
.oop
+= "\n ->selectRowCount()";
453 code
.oop
+= "\n ->set" + ucfirst(key
) + '(' + phpFormat(param
, 4) + ')';
456 code
.oop
+= "\n ->execute()";
457 if (isSelectRowCount
) {
458 code
.oop
+= "\n ->count()";
459 } else if (_
.isNumber(index
)) {
460 code
.oop
+= !index
? '\n ->first()' : (index
=== -1 ? '\n ->last()' : '\n ->itemAt(' + index
+ ')');
462 if (_
.isString(index
) || (_
.isPlainObject(index
) && !index
[0] && !index
['0'])) {
463 code
.oop
+= "\n ->indexBy('" + (_
.isPlainObject(index
) ? _
.keys(index
)[0] : index
) + "')";
465 if (_
.isArray(index
) || _
.isPlainObject(index
)) {
466 code
.oop
+= "\n ->column('" + (_
.isArray(index
) ? index
[0] : _
.values(index
)[0]) + "')";
470 if (!_
.isNumber(index
) && !isSelectRowCount
) {
471 code
.oop
+= "foreach ($" + results
+ ' as $' + ((_
.isString(index
) && index
) ? index
+ ' => $' : '') + result
+ ') {\n // do something\n}';
475 code
.cli
= 'cv api4 ' + entity
+ '.' + action
+ " '" + stringify(params
) + "'";
477 _
.each(code
, function(val
, type
) {
478 $scope
.code
[type
] = prettyPrintOne(_
.escape(val
));
482 function isInt(value
) {
483 if (_
.isFinite(value
)) {
486 if (!_
.isString(value
)) {
489 return /^-{0,1}\d+$/.test(value
);
492 function formatMeta(resp
) {
494 _
.each(resp
, function(val
, key
) {
495 if (key
!== 'values' && !_
.isPlainObject(val
) && !_
.isFunction(val
)) {
496 ret
+= (ret
.length
? ', ' : '') + key
+ ': ' + (_
.isArray(val
) ? '[' + val
+ ']' : val
);
499 return prettyPrintOne(_
.escape(ret
));
502 $scope
.execute = function() {
503 $scope
.status
= 'warning';
504 $scope
.loading
= true;
505 $http
.post(CRM
.url('civicrm/ajax/api4/' + $scope
.entity
+ '/' + $scope
.action
, {
506 params
: angular
.toJson(getParams()),
507 index
: isInt($scope
.index
) ? +$scope
.index
: parseYaml($scope
.index
)
510 'X-Requested-With': 'XMLHttpRequest'
512 }).then(function(resp
) {
513 $scope
.loading
= false;
514 $scope
.status
= 'success';
515 $scope
.debug
= debugFormat(resp
.data
);
516 $scope
.result
= [formatMeta(resp
.data
), prettyPrintOne(_
.escape(JSON
.stringify(resp
.data
.values
, null, 2)), 'js', 1)];
518 $scope
.loading
= false;
519 $scope
.status
= 'danger';
520 $scope
.debug
= debugFormat(resp
.data
);
521 $scope
.result
= [formatMeta(resp
), prettyPrintOne(_
.escape(JSON
.stringify(resp
.data
, null, 2)))];
525 function debugFormat(data
) {
526 var debug
= data
.debug
? prettyPrintOne(_
.escape(JSON
.stringify(data
.debug
, null, 2)).replace(/\\n/g, "\n")) : null;
532 * Format value to look like php code
534 function phpFormat(val
, indent
) {
535 if (typeof val
=== 'undefined') {
538 if (val
=== null || val
=== true || val
=== false) {
539 return JSON
.stringify(val
).toUpperCase();
541 indent
= (typeof indent
=== 'number') ? _
.repeat(' ', indent
) : (indent
|| '');
543 baseLine
= indent
? indent
.slice(0, -2) : '',
544 newLine
= indent
? '\n' : '',
545 trailingComma
= indent
? ',' : '';
546 if ($.isPlainObject(val
)) {
547 $.each(val
, function(k
, v
) {
548 ret
+= (ret
? ', ' : '') + newLine
+ indent
+ "'" + k
+ "' => " + phpFormat(v
);
550 return '[' + ret
+ trailingComma
+ newLine
+ baseLine
+ ']';
552 if ($.isArray(val
)) {
553 $.each(val
, function(k
, v
) {
554 ret
+= (ret
? ', ' : '') + newLine
+ indent
+ phpFormat(v
);
556 return '[' + ret
+ trailingComma
+ newLine
+ baseLine
+ ']';
558 if (_
.isString(val
) && !_
.contains(val
, "'")) {
559 return "'" + val
+ "'";
561 return JSON
.stringify(val
).replace(/\$/g, '\\$');
564 function fetchMeta() {
565 crmApi4(getMetaParams
)
566 .then(function(data
) {
568 getEntity().actions
= data
.actions
;
574 // Help for an entity with no action selected
575 function showEntityHelp(entityName
) {
576 var entityInfo
= getEntity(entityName
);
577 $scope
.helpTitle
= helpTitle
= $scope
.entity
;
578 $scope
.helpContent
= helpContent
= {
579 description
: entityInfo
.description
,
580 comment
: entityInfo
.comment
,
585 if (!$scope
.entity
) {
586 $scope
.helpTitle
= helpTitle
= ts('APIv4 Explorer');
587 $scope
.helpContent
= helpContent
= {description
: docs
.description
, comment
: docs
.comment
, see
: docs
.see
};
588 } else if (!actions
.length
&& !getEntity().actions
) {
589 getMetaParams
.actions
= [$scope
.entity
, 'getActions', {chain
: {fields
: [$scope
.entity
, 'getFields', {action
: '$name'}]}}];
596 showEntityHelp($scope
.entity
);
599 // Update route when changing entity
600 $scope
.$watch('entity', function(newVal
, oldVal
) {
601 if (oldVal
!== newVal
) {
602 // Flush actions cache to re-fetch for new entity
604 $location
.url('/explorer/' + newVal
);
608 // Update route when changing actions
609 $scope
.$watch('action', function(newVal
, oldVal
) {
610 if ($scope
.entity
&& $routeParams
.api4action
!== newVal
&& !_
.isUndefined(newVal
)) {
611 $location
.url('/explorer/' + $scope
.entity
+ '/' + newVal
);
613 $scope
.helpTitle
= helpTitle
= $scope
.entity
+ '::' + newVal
;
614 $scope
.helpContent
= helpContent
= _
.pick(_
.findWhere(getEntity().actions
, {name
: newVal
}), ['description', 'comment', 'see']);
618 $scope
.paramDoc = function(name
) {
619 return docs
.params
[name
];
622 $scope
.$watch('params', writeCode
, true);
623 $scope
.$watch('index', writeCode
);
628 angular
.module('api4Explorer').directive('crmApi4WhereClause', function($timeout
) {
631 data
: '=crmApi4WhereClause'
633 templateUrl
: '~/api4Explorer/WhereClause.html',
634 link: function (scope
, element
, attrs
) {
635 var ts
= scope
.ts
= CRM
.ts();
636 scope
.newClause
= '';
637 scope
.conjunctions
= ['AND', 'OR', 'NOT'];
638 scope
.operators
= CRM
.vars
.api4
.operators
;
640 scope
.addGroup = function(op
) {
641 scope
.data
.where
.push([op
, []]);
644 scope
.removeGroup = function() {
645 scope
.data
.groupParent
.splice(scope
.data
.groupIndex
, 1);
648 scope
.onSort = function(event
, ui
) {
649 $('.api4-where-fieldset').toggleClass('api4-sorting', event
.type
=== 'sortstart');
650 $('.api4-input.form-inline').css('margin-left', '');
653 // Indent clause while dragging between nested groups
654 scope
.onSortOver = function(event
, ui
) {
657 offset
= $(ui
.placeholder
).offset().left
- $(ui
.sender
).offset().left
;
659 $('.api4-input.form-inline.ui-sortable-helper').css('margin-left', '' + offset
+ 'px');
662 scope
.$watch('newClause', function(value
) {
664 $timeout(function() {
666 scope
.data
.where
.push([field
, '=', '']);
667 scope
.newClause
= null;
671 scope
.$watch('data.where', function(values
) {
672 // Remove empty values
673 _
.each(values
, function(clause
, index
) {
674 if (typeof clause
!== 'undefined' && !clause
[0]) {
675 values
.splice(index
, 1);
677 if (typeof clause
[1] === 'string' && _
.contains(clause
[1], 'NULL')) {
679 } else if (typeof clause
[1] === 'string' && clause
.length
== 2) {
688 angular
.module('api4Explorer').directive('api4ExpValue', function($routeParams
, crmApi4
) {
691 data
: '=api4ExpValue'
694 link: function (scope
, element
, attrs
, ctrl
) {
695 var ts
= scope
.ts
= CRM
.ts(),
696 multi
= _
.includes(['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'], scope
.data
.op
),
697 entity
= $routeParams
.api4entity
,
698 action
= scope
.data
.action
|| $routeParams
.api4action
;
700 function destroyWidget() {
701 var $el
= $(element
);
702 if ($el
.is('.crm-form-date-wrapper .crm-hidden-date')) {
703 $el
.crmDatepicker('destroy');
705 if ($el
.is('.select2-container + input')) {
706 $el
.crmEntityRef('destroy');
708 $(element
).removeData().removeAttr('type').removeAttr('placeholder').show();
711 function makeWidget(field
, op
) {
712 var $el
= $(element
),
713 inputType
= field
.input_type
,
714 dataType
= field
.data_type
;
716 op
= field
.serialize
|| dataType
=== 'Array' ? 'IN' : '=';
718 multi
= _
.includes(['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'], op
);
719 if (op
=== 'IS NULL' || op
=== 'IS NOT NULL') {
723 if (inputType
=== 'Date') {
724 if (_
.includes(['=', '!=', '<>', '<', '>=', '<', '<='], op
)) {
725 $el
.crmDatepicker({time
: (field
.input_attrs
&& field
.input_attrs
.time
) || false});
727 } else if (_
.includes(['=', '!=', '<>', 'IN', 'NOT IN'], op
) && (field
.fk_entity
|| field
.options
|| dataType
=== 'Boolean')) {
728 if (field
.fk_entity
) {
729 $el
.crmEntityRef({entity
: field
.fk_entity
, select
:{multiple
: multi
}});
730 } else if (field
.options
) {
731 $el
.addClass('loading').attr('placeholder', ts('- select -')).crmSelect2({multiple
: multi
, data
: [{id
: '', text
: ''}]});
732 loadFieldOptions(field
.entity
|| entity
).then(function(data
) {
734 _
.each(_
.findWhere(data
, {name
: field
.name
}).options
, function(val
, key
) {
735 options
.push({id
: key
, text
: val
});
737 $el
.removeClass('loading').select2({data
: options
, multiple
: multi
});
739 } else if (dataType
=== 'Boolean') {
740 $el
.attr('placeholder', ts('- select -')).crmSelect2({allowClear
: false, multiple
: multi
, placeholder
: ts('- select -'), data
: [
741 {id
: 'true', text
: ts('Yes')},
742 {id
: 'false', text
: ts('No')}
745 } else if (dataType
=== 'Integer' && !multi
) {
746 $el
.attr('type', 'number');
750 function loadFieldOptions(entity
) {
751 if (!fieldOptions
[entity
+ action
]) {
752 fieldOptions
[entity
+ action
] = crmApi4(entity
, 'getFields', {
755 where
: [["options", "!=", false]],
756 select
: ["name", "options"]
759 return fieldOptions
[entity
+ action
];
762 // Copied from ng-list but applied conditionally if field is multi-valued
763 var parseList = function(viewValue
) {
764 // If the viewValue is invalid (say required but empty) it will be `undefined`
765 if (_
.isUndefined(viewValue
)) return;
774 _
.each(viewValue
.split(','), function(value
) {
775 if (value
) list
.push(_
.trim(value
));
782 // Copied from ng-list
783 ctrl
.$parsers
.push(parseList
);
784 ctrl
.$formatters
.push(function(value
) {
785 return _
.isArray(value
) ? value
.join(', ') : value
;
788 // Copied from ng-list
789 ctrl
.$isEmpty = function(value
) {
790 return !value
|| !value
.length
;
793 scope
.$watchCollection('data', function(data
) {
795 var field
= getField(data
.field
, entity
, action
);
797 makeWidget(field
, data
.op
);
805 angular
.module('api4Explorer').directive('api4ExpChain', function(crmApi4
) {
808 chain
: '=api4ExpChain',
812 templateUrl
: '~/api4Explorer/Chain.html',
813 link: function (scope
, element
, attrs
) {
814 var ts
= scope
.ts
= CRM
.ts();
816 function changeEntity(newEntity
, oldEntity
) {
817 // When clearing entity remove this chain
822 // Reset action && index
823 if (newEntity
!== oldEntity
) {
824 scope
.chain
[1][1] = scope
.chain
[1][2] = '';
826 if (getEntity(newEntity
).actions
) {
829 crmApi4(newEntity
, 'getActions', {chain
: {fields
: [newEntity
, 'getFields', {action
: '$name'}]}})
830 .then(function(data
) {
831 getEntity(data
.entity
).actions
= data
;
832 if (data
.entity
=== scope
.chain
[1][0]) {
839 function setActions() {
840 scope
.actions
= [''].concat(_
.pluck(getEntity(scope
.chain
[1][0]).actions
, 'name'));
843 // Set default params when choosing action
844 function changeAction(newAction
, oldAction
) {
847 if (newAction
&& newAction
!== oldAction
) {
849 scope
.chain
[1][3] = '';
850 // Look for links back to main entity
851 _
.each(entityFields(scope
.chain
[1][0]), function(field
) {
852 if (field
.fk_entity
=== scope
.mainEntity
) {
853 link
= [field
.name
, '$id'];
856 // Look for links from main entity
857 if (!link
&& newAction
!== 'create') {
858 _
.each(entityFields(scope
.mainEntity
), function(field
) {
859 if (field
.fk_entity
=== scope
.chain
[1][0]) {
860 link
= ['id', '$' + field
.name
];
861 // Since we're specifying the id, set index to getsingle
862 scope
.chain
[1][3] = '0';
866 if (link
&& _
.contains(['get', 'update', 'replace', 'delete'], newAction
)) {
867 scope
.chain
[1][2] = '{where: [[' + link
[0] + ', =, ' + link
[1] + ']]}';
869 else if (link
&& _
.contains(['create'], newAction
)) {
870 scope
.chain
[1][2] = '{values: {' + link
[0] + ': ' + link
[1] + '}}';
872 else if (link
&& _
.contains(['save'], newAction
)) {
873 scope
.chain
[1][2] = '{records: [{' + link
[0] + ': ' + link
[1] + '}]}';
875 scope
.chain
[1][2] = '{}';
880 scope
.$watch("chain[1][0]", changeEntity
);
881 scope
.$watch("chain[1][1]", changeAction
);
886 function getEntity(entityName
) {
887 return _
.findWhere(schema
, {name
: entityName
});
890 function entityFields(entityName
, action
) {
891 var entity
= getEntity(entityName
);
892 if (entity
&& action
&& entity
.actions
) {
893 return _
.findWhere(entity
.actions
, {name
: action
}).fields
;
895 return _
.result(entity
, 'fields');
898 function getField(fieldName
, entity
, action
) {
899 var fieldNames
= fieldName
.split('.');
900 return get(entity
, fieldNames
);
902 function get(entity
, fieldNames
) {
903 if (fieldNames
.length
=== 1) {
904 return _
.findWhere(entityFields(entity
, action
), {name
: fieldNames
[0]});
906 var comboName
= _
.findWhere(entityFields(entity
, action
), {name
: fieldNames
[0] + '.' + fieldNames
[1]});
910 var linkName
= fieldNames
.shift(),
911 entityLinks
= _
.findWhere(links
, {entity
: entity
}).links
,
912 newEntity
= _
.findWhere(entityLinks
, {alias
: linkName
}).entity
;
913 return get(newEntity
, fieldNames
);
917 // Collapsible optgroups for select2
920 .on('select2-open', function(e
) {
921 if ($(e
.target
).hasClass('collapsible-optgroups')) {
923 .off('.collapseOptionGroup')
924 .addClass('collapsible-optgroups-enabled')
925 .on('click.collapseOptionGroup', '.select2-result-with-children > .select2-result-label', function() {
926 $(this).parent().toggleClass('optgroup-expanded');
930 .on('select2-close', function() {
931 $('#select2-drop').off('.collapseOptionGroup').removeClass('collapsible-optgroups-enabled');
934 })(angular
, CRM
.$, CRM
._
);