1 (function($, _
, undefined) {
3 /* jshint validthis: true */
7 actions
= {values
: ['get']},
13 fieldTpl
= _
.template($('#api-param-tpl').html()),
14 optionsTpl
= _
.template($('#api-options-tpl').html()),
15 returnTpl
= _
.template($('#api-return-tpl').html()),
16 chainTpl
= _
.template($('#api-chain-tpl').html()),
17 docCodeTpl
= _
.template($('#doc-code-tpl').html()),
19 // These types of entityRef don't require any input to open
20 // FIXME: ought to be in getfields metadata
21 OPEN_IMMEDIATELY
= ['RelationshipType', 'Event', 'Group', 'Tag'],
23 // Actions that don't support fancy operators
24 NO_OPERATORS
= ['create', 'update', 'delete', 'setvalue', 'getoptions', 'getactions', 'getfields'],
26 // Actions that don't support multiple values
27 NO_MULTI
= ['delete', 'getoptions', 'getactions', 'getfields', 'getfield', 'setvalue'],
29 // Operators with special properties
30 BOOL
= ['IS NULL', 'IS NOT NULL'],
31 TEXT
= ['LIKE', 'NOT LIKE'],
32 MULTI
= ['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'];
35 * Call prettyPrint function and perform additional formatting
38 function prettyPrint(ele
) {
39 if (typeof window
.prettyPrint
=== 'function') {
40 $(ele
).removeClass('prettyprinted').addClass('prettyprint');
44 // Highlight errors in api result
45 $('span:contains("error_code"),span:contains("error_message")', '#api-result')
46 .siblings('span.str').css('color', '#B00');
54 function addField(name
) {
55 $('#api-params').append($(fieldTpl({name
: name
|| '', noOps
: _
.includes(NO_OPERATORS
, action
)})));
56 var $row
= $('tr:last-child', '#api-params');
57 $('input.api-param-name', $row
).crmSelect2({
58 data
: fields
.concat({id
: '-', text
: ts('Other') + '...', description
: ts('Choose a field not in this list')}),
59 formatSelection: function(field
) {
61 (field
.required
? ' <span class="crm-marker">*</span>' : '');
63 formatResult: function(field
) {
65 (field
.required
? ' <span class="crm-marker">*</span>' : '') +
66 '<div class="api-field-desc">' + field
.description
+ '</div>';
72 * Add a new "options" row
74 function addOptionField() {
75 if ($('.api-options-row', '#api-params').length
) {
76 $('.api-options-row:last', '#api-params').after($(optionsTpl({})));
78 $('#api-params').append($(optionsTpl({})));
80 var $row
= $('.api-options-row:last', '#api-params');
81 $('.api-option-name', $row
).crmSelect2({data
: [
82 {id
: 'limit', text
: 'limit'},
83 {id
: 'offset', text
: 'offset'},
84 {id
: 'sort', text
: 'sort'},
85 {id
: 'metadata', text
: 'metadata'},
86 {id
: '-', text
: ts('Other') + '...'}
91 * Add an "api chain" row
93 function addChainField() {
94 $('#api-params').append($(chainTpl({})));
95 var $row
= $('tr:last-child', '#api-params');
96 $('.api-chain-entity', $row
).crmSelect2({
97 formatSelection: function(item
) {
98 return '<i class="crm-i fa-link"></i> API ' +
99 ($(item
.element
).hasClass('strikethrough') ? '<span class="strikethrough">' + item
.text
+ '</span>' : item
.text
);
101 placeholder
: '<i class="crm-i fa-link"></i> ' + ts('Entity'),
102 escapeMarkup: function(m
) {return m
;}
107 * Fetch available actions for selected chained entity
109 function getChainedAction() {
112 entity
= $selector
.val(),
113 $row
= $selector
.closest('tr');
115 $selector
.prop('disabled', true);
116 CRM
.api3(entity
, 'getactions')
117 .done(function(actions
) {
118 $selector
.prop('disabled', false);
119 CRM
.utils
.setOptions($('.api-chain-action', $row
), _
.transform(actions
.values
, function(ret
, item
) {ret
.push({value
: item
, key
: item
});}));
125 * Fetch fields for entity+action
127 function getFields(changedElement
) {
131 // Special case for getfields
132 if (action
=== 'getfields') {
137 getFieldData
.api_action
= {
138 options
: _
.reduce(actions
.values
, function(ret
, item
) {
143 showFields(['api_action']);
146 CRM
.api3(entity
, 'getfields', {'api_action': action
, options
: {get_options
: 'all', get_options_context
: 'match'}}).done(function(data
) {
147 _
.each(data
.values
, function(field
) {
149 getFieldData
[field
.name
] = field
;
152 text
: field
.title
|| field
.name
,
153 multi
: !!field
['api.multiple'],
154 description
: field
.description
|| '',
155 required
: !(!field
['api.required'] || field
['api.required'] === '0')
157 if (field
['api.required'] && field
['api.required'] !== '0') {
158 required
.push(field
.name
);
162 if ($(changedElement
).is('#api-entity') && data
.deprecated
) {
163 CRM
.alert(data
.deprecated
, entity
+ ' Deprecated');
165 showFields(required
);
166 if (action
=== 'get' || action
=== 'getsingle' || action
== 'getvalue' || action
=== 'getstat') {
173 * For "get" actions show the "return" options
175 * TODO: Too many hard-coded actions here. Need a way to fetch this from metadata
177 function showReturn() {
178 var title
= ts('Fields to return'),
182 placeholder
: ts('Leave blank for default')
184 if (action
== 'getstat') {
185 title
= ts('Group by');
187 if (action
== 'getvalue') {
188 title
= ts('Return Value');
189 params
.placeholder
= ts('Select field');
190 params
.multiple
= false;
192 $('#api-params').prepend($(returnTpl({title
: title
, required
: action
== 'getvalue'})));
193 $('#api-return-value').crmSelect2(params
);
197 * Fetch actions for entity
199 function getActions() {
201 $('#api-action').addClass('loading');
202 CRM
.api3(entity
, 'getactions').done(function(data
) {
207 actions
= {values
: ['get']};
213 * Test whether an action is deprecated
217 function isActionDeprecated(action
) {
218 return !!(typeof actions
.deprecated
=== 'object' && actions
.deprecated
[action
]);
222 * Render action text depending on deprecation status
226 function renderAction(option
) {
227 return isActionDeprecated(option
.id
) ? '<span class="strikethrough">' + option
.text
+ '</span>' : option
.text
;
231 * Called after getActions to populate action list
233 function populateActions() {
234 var val
= $('#api-action').val();
235 $('#api-action').removeClass('loading').select2({
236 data
: _
.transform(actions
.values
, function(ret
, item
) {ret
.push({text
: item
, id
: item
});}),
237 formatSelection
: renderAction
,
238 formatResult
: renderAction
240 // If previously selected action is not available, set it to 'get' if possible
241 if (!_
.includes(actions
.values
, val
)) {
242 $('#api-action').select2('val', !_
.includes(actions
.values
, 'get') ? actions
.values
[0] : 'get', true);
247 * Check for and display action-specific deprecation notices
250 function onChangeAction(action
) {
251 if (isActionDeprecated(action
)) {
252 CRM
.alert(actions
.deprecated
[action
], action
+ ' deprecated');
257 * Called after getfields to show buttons and required fields
260 function showFields(required
) {
261 $('#api-params').empty();
262 $('#api-param-buttons').show();
263 if (required
.length
) {
264 _
.each(required
, addField
);
270 function isYesNo(fieldName
) {
271 return getFieldData
[fieldName
] && getFieldData
[fieldName
].type
=== 16;
275 * Should we render a select or textfield?
281 function isSelect(fieldName
, operator
) {
282 var fieldSpec
= getFieldData
[fieldName
] || {};
283 return (isYesNo(fieldName
) || fieldSpec
.options
|| fieldSpec
.FKApiName
) && !_
.includes(TEXT
, operator
);
287 * Should we render a select as single or multi?
293 function isMultiSelect(fieldName
, operator
) {
294 if (isYesNo(fieldName
) || _
.includes(NO_MULTI
, action
)) {
297 if (_
.includes(MULTI
, operator
)) {
300 // The = operator is ambiguous but all others can be safely assumed to be single
301 if (operator
!== '=') {
306 * Attempt to resolve the ambiguity of the = operator using metadata
307 * commented out because there is not enough metadata in the api at this time
308 * to accurately figure it out.
310 // var field = fieldName && _.find(fields, 'id', fieldName);
311 // return field && field.multi;
315 * Render value input as a textfield, option list, entityRef, or hidden,
316 * Depending on selected param name and operator
318 function renderValueField() {
319 var $row
= $(this).closest('tr'),
320 name
= $('input.api-param-name', $row
).val(),
321 operator
= $('.api-param-op', $row
).val(),
322 $valField
= $('input.api-param-value', $row
),
323 multiSelect
= isMultiSelect(name
, operator
),
324 currentVal
= $valField
.val(),
325 wasSelect
= $valField
.data('select2');
327 $valField
.crmEntityRef('destroy');
329 $valField
.attr('placeholder', ts('Value'));
330 // Boolean fields only have 1 possible value
331 if (_
.includes(BOOL
, operator
)) {
332 $valField
.css('visibility', 'hidden').val('1');
335 $valField
.css('visibility', '');
336 // Option list or entityRef input
337 if (isSelect(name
, operator
)) {
338 $valField
.attr('placeholder', ts('- select -'));
339 // Reset value before switching to a select from something else
340 if ($(this).is('.api-param-name') || !wasSelect
) {
343 // When switching from multi-select to single select
344 else if (!multiSelect
&& _
.includes(currentVal
, ',')) {
345 $valField
.val(currentVal
.split(',')[0]);
350 data
: [{id
: 1, text
: ts('Yes')}, {id
: 0, text
: ts('No')}]
354 else if (getFieldData
[name
].options
) {
356 multiple
: multiSelect
,
357 data
: _
.map(getFieldData
[name
].options
, function (value
, key
) {
358 return {id
: key
, text
: value
};
364 var entity
= getFieldData
[name
].FKApiName
;
365 $valField
.attr('placeholder', entity
== 'Contact' ? '[' + ts('Auto-Select Current User') + ']' : ts('- select -'));
366 $valField
.crmEntityRef({
369 multiple
: multiSelect
,
370 minimumInputLength
: _
.includes(OPEN_IMMEDIATELY
, entity
) ? 0 : 1,
371 // If user types a numeric id, allow it as a choice
372 createSearchChoice: function(input
) {
373 var match
= /[1-9][0-9]*/.exec(input
);
374 if (match
&& match
[0] === input
) {
375 return {id
: input
, label
: input
};
385 * Attempt to parse a string into a value of the intended type
387 * @param makeArray bool
389 function evaluate(val
, makeArray
) {
392 return makeArray
? [] : '';
394 var first
= val
.charAt(0),
395 last
= val
.slice(-1);
397 if (val
=== 'true' || val
=== 'false' || val
=== 'null') {
398 /* jshint evil: true */
402 if ((first
=== '"' || first
=== "'") && last
=== first
) {
403 return val
.slice(1, -1);
405 // Parse json - use eval rather than $.parseJSON because it's less strict about formatting
406 if ((first
=== '[' && last
=== ']') || (first
=== '{' && last
=== '}')) {
407 return eval('(' + val
+ ')');
409 // Transform csv to array
412 $.each(val
.split(','), function(k
, v
) {
413 result
.push(evaluate($.trim(v
)) || v
);
417 // Integers - skip any multidigit number that starts with 0 to avoid oddities (it will be treated as a string below)
418 if (!isNaN(val
) && val
.search(/[^\d]/) < 0 && (val
.length
=== 1 || first
!== '0')) {
419 return parseInt(val
, 10);
421 // Ok ok it's really a string
424 // If eval crashed return undefined
430 * Format value to look like php code
431 * TODO: Use short array syntax when we drop support for php 5.3
434 function phpFormat(val
) {
436 if ($.isPlainObject(val
)) {
437 $.each(val
, function(k
, v
) {
438 ret
+= (ret
? ', ' : '') + "'" + k
+ "' => " + phpFormat(v
);
440 return 'array(' + ret
+ ')';
442 if ($.isArray(val
)) {
443 $.each(val
, function(k
, v
) {
444 ret
+= (ret
? ', ' : '') + phpFormat(v
);
446 return 'array(' + ret
+ ')';
448 return JSON
.stringify(val
).replace(/\$/g, '\\$');
452 * @param value string
456 function smartyFormat(value
, js
, key
) {
457 var varName
= 'param_' + key
.replace(/[. -]/g, '_').toLowerCase();
458 // Can't pass array literals directly into smarty so we add a php snippet
459 if (_
.includes(js
, '[') || _
.includes(js
, '{')) {
460 smartyPhp
.push('$this->assign("'+ varName
+ '", '+ phpFormat(value
) +');');
461 return '$' + varName
;
467 * Create the params array from user input
470 function buildParams(e
) {
472 $('.api-param-checkbox:checked').each(function() {
473 params
[this.name
] = 1;
475 $('input.api-param-value, input.api-option-value').each(function() {
476 var $row
= $(this).closest('tr'),
477 input
= $(this).val(),
478 op
= $('select.api-param-op', $row
).val() || '=',
479 name
= $('input.api-param-name', $row
).val(),
480 // Workaround for ambiguity of the = operator
481 makeArray
= (op
=== '=' && isSelect(name
, op
)) ? _
.includes(input
, ',') : op
!== '=' && isMultiSelect(name
, op
),
482 val
= evaluate(input
, makeArray
);
484 // Ignore blank values for the return field
485 if ($(this).is('#api-return-value') && !val
) {
488 // Special syntax for api chaining
489 if (!name
&& $('select.api-chain-entity', $row
).val()) {
490 name
= 'api.' + $('select.api-chain-entity', $row
).val() + '.' + $('select.api-chain-action', $row
).val();
492 // Special handling for options
493 if ($(this).is('.api-option-value')) {
494 op
= $('input.api-option-name', $row
).val();
499 // Default for contact ref fields
500 if ($(this).is('.crm-contact-ref') && input
=== '') {
501 val
= evaluate('user_contact_id', makeArray
);
503 if (name
&& val
!== undefined) {
504 params
[name
] = op
=== '=' ? val
: (params
[name
] || {});
506 params
[name
][op
] = val
;
508 if ($(this).hasClass('crm-error')) {
512 else if (name
&& (!e
|| e
.type
!== 'keyup')) {
516 if (entity
&& action
) {
522 * Display error message on incorrectly-formatted params
525 function setError(el
) {
526 if (!$(el
).hasClass('crm-error')) {
527 var msg
= ts('Syntax error: input should be valid JSON or a quoted string.');
529 .addClass('crm-error')
532 .before('<i class="crm-i fa-exclamation-triangle crm-i-red" title="'+msg
+'"></i> ')
538 * Remove error message
541 function clearError(el
) {
543 .removeClass('crm-error')
547 .siblings('.fa-exclamation-triangle').remove();
551 * Render the api request in various formats
553 function formatQuery() {
555 smarty
: "{crmAPI var='result' entity='" + entity
+ "' action='" + action
+ "'" + (params
.sequential
? '' : ' sequential=0'),
556 php
: "$result = civicrm_api3('" + entity
+ "', '" + action
+ "'",
557 json
: "CRM.api3('" + entity
+ "', '" + action
+ "'",
558 drush
: "drush cvapi " + entity
+ '.' + action
+ ' ',
559 wpcli
: "wp cv api " + entity
+ '.' + action
+ ' ',
560 rest
: CRM
.config
.resourceBase
+ "extern/rest.php?entity=" + entity
+ "&action=" + action
+ "&api_key=userkey&key=sitekey&json=" + JSON
.stringify(params
)
563 $.each(params
, function(key
, value
) {
564 var json
= JSON
.stringify(value
),
565 // Encourage 'return' to be an array - at least in php & js
566 js
= key
=== 'return' ? JSON
.stringify(evaluate(value
, true)) : json
,
567 php
= key
=== 'return' ? phpFormat(evaluate(value
, true)) : phpFormat(value
);
569 q
.php
+= ", array(\n";
574 q
.php
+= " '" + key
+ "' => " + php
+ ",\n";
575 q
.json
+= " \"" + key
+ '": ' + js
;
576 // smarty already defaults to sequential
577 if (key
!== 'sequential') {
578 q
.smarty
+= ' ' + key
+ '=' + smartyFormat(value
, json
, key
);
580 // FIXME: This is not totally correct cli syntax
581 q
.drush
+= key
+ '=' + json
+ ' ';
582 q
.wpcli
+= key
+ '=' + json
+ ' ';
589 q
.json
+= ").done(function(result) {\n // do something\n});";
590 q
.smarty
+= "}\n{foreach from=$result.values item=" + entity
.toLowerCase() + "}\n {$" + entity
.toLowerCase() + ".some_field}\n{/foreach}";
591 if (!_
.includes(action
, 'get')) {
592 q
.smarty
= '{* Smarty API only works with get actions *}';
593 } else if (smartyPhp
.length
) {
594 q
.smarty
= "{php}\n " + smartyPhp
.join("\n ") + "\n{/php}\n" + q
.smarty
;
596 $.each(q
, function(type
, val
) {
597 $('#api-' + type
).text(val
);
599 prettyPrint('#api-generated pre');
603 * Submit button handler
608 if (!entity
|| !action
) {
609 alert(ts('Select an entity.'));
612 if (!_
.includes(action
, 'get') && action
!= 'check') {
613 var msg
= action
=== 'delete' ? ts('This will delete data from CiviCRM. Are you sure?') : ts('This will write to the database. Continue?');
614 CRM
.confirm({title
: ts('Confirm %1', {1: action
}), message
: msg
}).on('crmConfirm:yes', execute
);
621 * Execute api call and display the results
622 * Note: We have to manually execute the ajax in order to add the secret extra "prettyprint" param
626 $('#api-result').html('<div class="crm-loading-element"></div>');
628 url
: CRM
.url('civicrm/ajax/rest'),
633 json
: JSON
.stringify(params
)
635 type
: _
.includes(action
, 'get') ? 'GET' : 'POST',
637 }).done(function(text
) {
638 // There may be debug information appended to the end of the json string
639 var footerPos
= text
.indexOf("\n}<");
641 footer
= text
.substr(footerPos
+ 2);
642 text
= text
.substr(0, footerPos
+ 2);
644 $('#api-result').text(text
);
645 prettyPrint('#api-result');
647 $('#api-result').append(footer
);
653 * Fetch list of example files for a given entity
655 function getExamples() {
656 CRM
.utils
.setOptions($('#example-action').prop('disabled', true).addClass('loading'), []);
657 $.getJSON(CRM
.url('civicrm/ajax/apiexample', {entity
: $(this).val()}))
658 .done(function(result
) {
659 CRM
.utils
.setOptions($('#example-action').prop('disabled', false).removeClass('loading'), result
);
664 * Fetch and display an example file
666 function getExample() {
668 entity
= $('#example-entity').val(),
669 action
= $('#example-action').val();
670 if (entity
&& action
) {
671 $('#example-result').block();
672 $.get(CRM
.url('civicrm/ajax/apiexample', {file
: entity
+ '/' + action
}))
673 .done(function(result
) {
674 $('#example-result').unblock().text(result
);
675 prettyPrint('#example-result');
678 $('#example-result').text($('#example-result').attr('placeholder'));
683 * Fetch entity docs & actions
685 function getDocEntity() {
686 CRM
.utils
.setOptions($('#doc-action').prop('disabled', true).addClass('loading'), []);
687 $.getJSON(CRM
.url('civicrm/ajax/apidoc', {entity
: $(this).val()}))
688 .done(function(result
) {
689 entityDoc
= result
.doc
;
690 CRM
.utils
.setOptions($('#doc-action').prop('disabled', false).removeClass('loading'), result
.actions
);
691 $('#doc-result').html(result
.doc
);
692 prettyPrint('#doc-result pre');
697 * Fetch entity+action docs & code
699 function getDocAction() {
701 entity
= $('#doc-entity').val(),
702 action
= $('#doc-action').val();
703 if (entity
&& action
) {
704 $('#doc-result').block();
705 $.get(CRM
.url('civicrm/ajax/apidoc', {entity
: entity
, action
: action
}))
706 .done(function(result
) {
707 $('#doc-result').unblock().html(result
.doc
);
709 $('#doc-result').append(docCodeTpl(result
));
711 prettyPrint('#doc-result pre');
714 $('#doc-result').html(entityDoc
);
715 prettyPrint('#doc-result pre');
717 checkBookKeepingEntity(entity
, action
);
721 * Check if entity is Financial Trxn and Entity Financial Trxn
722 * and Action is Create, delete, update etc then display warning
724 function checkBookKeepingEntity(entity
, action
) {
725 if ($.inArray(entity
, ['EntityFinancialTrxn', 'FinancialTrxn']) > -1 && $.inArray(action
, ['delete', 'setvalue', 'replace', 'create']) > -1) {
726 var msg
= ts('Given the importance of auditability, extension developers are strongly discouraged from writing code to add, update or delete entries in the civicrm_financial_item, civicrm_entity_financial_trxn, and civicrm_financial_trxn tables. Before publishing an extension on civicrm.org that does any of this, please ask for a special bookkeeping code review for the extension.');
727 CRM
.alert(msg
, 'warning');
731 $(document
).ready(function() {
732 // Set up tabs - bind active tab to document hash because... it's cool?
733 document
.location
.hash
= document
.location
.hash
|| 'explorer';
734 $('#mainTabContainer')
736 active
: $(document
.location
.hash
+ '-tab').index() - 1
738 .on('tabsactivate', function(e
, ui
) {
740 document
.location
.hash
= ui
.newPanel
.attr('id').replace('-tab', '');
743 $(window
).on('hashchange', function() {
744 $('#mainTabContainer').tabs('option', 'active', $(document
.location
.hash
+ '-tab').index() - 1);
747 // Initialize widgets
748 $('#api-entity, #example-entity, #doc-entity').crmSelect2({
749 // Add strikethough class to selection to indicate deprecated apis
750 formatSelection: function(option
) {
751 return $(option
.element
).hasClass('strikethrough') ? '<span class="strikethrough">' + option
.text
+ '</span>' : option
.text
;
754 $('form#api-explorer')
755 .on('change', '#api-entity, #api-action', function() {
756 entity
= $('#api-entity').val();
757 action
= $('#api-action').val();
758 if ($(this).is('#api-entity')) {
761 onChangeAction(action
);
763 if (entity
&& action
) {
764 $('#api-params').html('<tr><td colspan="4" class="crm-loading-element"></td></tr>');
765 $('#api-params-table thead').show();
768 checkBookKeepingEntity(entity
, action
);
770 $('#api-params, #api-generated pre').empty();
771 $('#api-param-buttons, #api-params-table thead').hide();
774 .on('change keyup', 'input.api-input, #api-params select', buildParams
)
775 .on('submit', submit
);
777 .on('change', 'input.api-param-name, select.api-param-op', renderValueField
)
778 .on('change', 'input.api-param-name, .api-option-name', function() {
779 if ($(this).val() === '-' && $(this).data('select2')) {
781 .crmSelect2('destroy')
786 .on('click', '.api-param-remove', function(e
) {
788 $(this).closest('tr').remove();
791 .on('change', 'select.api-chain-entity', getChainedAction
);
792 $('#example-entity').on('change', getExamples
);
793 $('#example-action').on('change', getExample
);
794 $('#doc-entity').on('change', getDocEntity
);
795 $('#doc-action').on('change', getDocAction
);
796 $('#api-params-add').on('click', function(e
) {
800 $('#api-option-add').on('click', function(e
) {
804 $('#api-chain-add').on('click', function(e
) {
808 $('#api-entity').change();