2063c2a62f9ec655090d8d87ffc2ebd4feb1d220
1 (function($, _
, undefined) {
10 fieldTpl
= _
.template($('#api-param-tpl').html()),
11 optionsTpl
= _
.template($('#api-options-tpl').html()),
12 returnTpl
= _
.template($('#api-return-tpl').html()),
13 chainTpl
= _
.template($('#api-chain-tpl').html());
16 * Call prettyPrint function if it successfully loaded from the cdn
18 function prettyPrint() {
19 if (window
.prettyPrint
) {
28 function addField(name
) {
29 $('#api-params').append($(fieldTpl({name
: name
|| ''})));
30 var $row
= $('tr:last-child', '#api-params');
31 $('.api-param-name', $row
).crmSelect2({
32 data
: fields
.concat({id
: '-', text
: ts('Other') + '...'})
37 * Add a new "options" row
40 function addOptionField(name
) {
41 if ($('.api-options-row', '#api-params').length
) {
42 $('.api-options-row:last', '#api-params').after($(optionsTpl({})));
44 $('#api-params').append($(optionsTpl({})));
46 var $row
= $('.api-options-row:last', '#api-params');
47 $('.api-option-name', $row
).crmSelect2({data
: [
48 {id
: 'limit', text
: 'limit'},
49 {id
: 'offset', text
: 'offset'},
50 {id
: 'sort', text
: 'sort'},
51 {id
: 'metadata', text
: 'metadata'},
52 {id
: '-', text
: ts('Other') + '...'}
57 * Add an "api chain" row
59 function addChainField() {
60 $('#api-params').append($(chainTpl({})));
61 var $row
= $('tr:last-child', '#api-params');
62 $('.api-chain-entity', $row
).crmSelect2({
63 formatSelection: function(item
) {
64 return '<span class="icon ui-icon-link"></span> API ' + item
.text
;
66 placeholder
: '<span class="icon ui-icon-link"></span> ' + ts('Entity'),
67 escapeMarkup: function(m
) {return m
}
72 * Fetch fields for entity+action
74 function getFields() {
78 // Special case for getfields
79 if (action
=== 'getfields') {
84 options
.api_action
= [];
85 $('option', '#api-action').each(function() {
87 options
.api_action
.push({key
: this.value
, value
: $(this).text()});
90 showFields(['api_action']);
93 CRM
.api3(entity
, 'getFields', {'api_action': action
, sequential
: 1, options
: {get_options
: 'all'}}).done(function(data
) {
94 _
.each(data
.values
, function(field
) {
98 text
: field
.title
|| field
.name
,
99 required
: field
['api.required'] || false
101 if (field
['api.required']) {
102 required
.push(field
.name
);
105 options
[field
.name
] = field
.options
;
109 showFields(required
);
110 if (action
=== 'get' || action
=== 'getsingle') {
117 * For "get" actions show the "return" options
119 function showReturn() {
120 $('#api-params').prepend($(returnTpl({})));
121 $('#api-return-value').crmSelect2({data
: fields
, multiple
: true});
125 * Fetch actions for entity
127 function getActions() {
129 $('#api-action').addClass('loading');
130 CRM
.api3(entity
, 'getactions').done(function(data
) {
131 actions
= data
.values
|| ['get'];
141 * Called after getActions to populate action list
144 function populateActions(el
) {
145 var val
= $('#api-action').val();
146 $('#api-action').removeClass('loading').select2({
147 data
: _
.transform(actions
, function(ret
, item
) {ret
.push({text
: item
, id
: item
})})
149 // If previously selected action is not available, set it to 'get' if possible
150 if (_
.indexOf(actions
, val
) < 0) {
151 $('#api-action').select2('val', _
.indexOf(actions
, 'get') < 0 ? actions
[0] : 'get', true);
156 * Called after getfields to show buttons and required fields
159 function showFields(required
) {
160 $('#api-params').empty();
161 $('#api-param-buttons').show();
162 if (required
.length
) {
163 _
.each(required
, addField
);
170 * Add/remove option list for selected field's pseudoconstant
172 function toggleOptions() {
173 var name
= $(this).val(),
174 $valField
= $(this).closest('tr').find('.api-param-value');
176 $valField
.val('').select2({
178 data
: _
.transform(options
[name
], function(result
, option
) {
179 result
.push({id
: option
.key
, text
: option
.value
});
183 else if ($valField
.data('select2')) {
184 $valField
.select2('destroy');
189 * Attempt to parse a string into a value of the intended type
191 * @param makeArray bool
193 function evaluate(val
, makeArray
) {
198 var first
= val
.charAt(0),
199 last
= val
.slice(-1);
201 if (val
=== 'true' || val
=== 'false' || val
=== 'null') {
204 // Integers - quote any number that starts with 0 to avoid oddities
205 if (!isNaN(val
) && val
.search(/[^\d]/) < 0 && (val
.length
=== 1 || first
!== '0')) {
206 return parseInt(val
, 10);
209 if ((first
=== '"' || first
=== "'") && last
=== first
) {
210 return val
.slice(1, -1);
213 if ((first
=== '[' && last
=== ']') || (first
=== '{' && last
=== '}')) {
214 return eval('(' + val
+ ')');
216 // Transform csv to array
217 if (makeArray
&& val
.indexOf(',') > 0) {
218 return val
.split(',');
220 // Ok ok it's really a string
223 // If eval crashed return undefined
229 * Format value to look like php code
232 function phpFormat(val
) {
234 if ($.isPlainObject(val
)) {
235 $.each(val
, function(k
, v
) {
236 ret
+= (ret
? ', ' : '') + "'" + k
+ "' => " + phpFormat(v
);
238 return 'array(' + ret
+ ')';
240 if ($.isArray(val
)) {
241 $.each(val
, function(k
, v
) {
242 ret
+= (ret
? ', ' : '') + phpFormat(v
);
244 return 'array(' + ret
+ ')';
246 return JSON
.stringify(val
).replace(/\$/g, '\\$');
250 * Smarty doesn't support array literals so we provide a stub
253 function smartyFormat(js
, key
) {
254 if (js
.indexOf('[') > -1 || js
.indexOf('{') > -1) {
256 return '$' + key
.replace(/[. -]/g, '_');
262 * Create the params array from user input
265 function buildParams(e
) {
267 $('.api-param-checkbox:checked').each(function() {
268 params
[this.name
] = 1;
270 $('input.api-param-value, input.api-option-value').each(function() {
271 var $row
= $(this).closest('tr'),
272 val
= evaluate($(this).val(), $(this).is('.select2-offscreen')),
273 name
= $('input.api-param-name', $row
).val(),
274 op
= $('select.api-param-op', $row
).val() || '=';
276 // Ignore blank values for the return field
277 if ($(this).is('#api-return-value') && !val
) {
280 // Special syntax for api chaining
281 if (!name
&& $('select.api-chain-entity', $row
).val()) {
282 name
= 'api.' + $('select.api-chain-entity', $row
).val() + '.' + $('select.api-chain-action', $row
).val();
284 // Special handling for options
285 if ($(this).is('.api-option-value')) {
286 op
= $('input.api-option-name', $row
).val();
291 if (name
&& val
!== undefined) {
292 params
[name
] = op
=== '=' ? val
: (params
[name
] || {});
294 params
[name
][op
] = val
;
296 if ($(this).hasClass('crm-error')) {
300 else if (name
&& (!e
|| e
.type
!== 'keyup')) {
304 if (entity
&& action
) {
309 function setError(el
) {
310 if (!$(el
).hasClass('crm-error')) {
311 var msg
= ts('Syntax error: input should be valid JSON or a quoted string.');
313 .addClass('crm-error')
316 .before('<div class="icon red-icon ui-icon-alert" title="'+msg
+'"/>')
321 function clearError(el
) {
323 .removeClass('crm-error')
327 .siblings('.ui-icon-alert').remove();
330 function formatQuery() {
332 smarty
: "{crmAPI var='result' entity='" + entity
+ "' action='" + action
+ "'",
333 php
: "$result = civicrm_api3('" + entity
+ "', '" + action
+ "'",
334 json
: "CRM.api3('" + entity
+ "', '" + action
+ "'",
335 drush
: "drush cvapi " + entity
+ '.' + action
+ ' ',
336 wpcli
: "wp cv api " + entity
+ '.' + action
+ ' ',
337 rest
: CRM
.config
.resourceBase
+ "extern/rest.php?entity=" + entity
+ "&action=" + action
+ "&json=" + JSON
.stringify(params
) + "&api_key=yoursitekey&key=yourkey"
340 $.each(params
, function(key
, value
) {
341 var js
= JSON
.stringify(value
);
343 q
.php
+= ", array(\n";
348 q
.php
+= " '" + key
+ "' => " + phpFormat(value
) + ",\n";
349 q
.json
+= " \"" + key
+ '": ' + js
;
350 q
.smarty
+= ' ' + key
+ '=' + smartyFormat(js
, key
);
351 q
.drush
+= key
+ '=' + js
+ ' ';
352 q
.wpcli
+= key
+ '=' + js
+ ' ';
359 q
.json
+= ").done(function(result) {\n // do something\n});";
360 q
.smarty
+= "}\n{foreach from=$result.values item=" + entity
.toLowerCase() + "}\n {$" + entity
.toLowerCase() + ".some_field}\n{/foreach}";
361 if (action
.indexOf('get') < 0) {
362 q
.smarty
= '{* Smarty API only works with get actions *}';
363 } else if (smartyStub
) {
364 q
.smarty
= "{* Smarty does not have a syntax for array literals; assign complex variables from php *}\n" + q
.smarty
;
366 $.each(q
, function(type
, val
) {
367 $('#api-' + type
).removeClass('prettyprinted').text(val
);
374 if (!entity
|| !action
) {
375 alert(ts('Select an entity.'));
378 if (action
.indexOf('get') < 0 && action
!= 'check') {
379 var msg
= action
=== 'delete' ? ts('This will delete data from CiviCRM. Are you sure?') : ts('This will write to the database. Continue?');
380 CRM
.confirm({title
: ts('Confirm %1', {1: action
}), message
: msg
}).on('crmConfirm:yes', execute
);
387 $('#api-result').html('<div class="crm-loading-element"></div>');
389 url
: CRM
.url('civicrm/ajax/rest'),
394 json
: JSON
.stringify(params
)
396 type
: action
.indexOf('get') < 0 ? 'POST' : 'GET',
398 }).done(function(text
) {
399 $('#api-result').addClass('prettyprint').removeClass('prettyprinted').text(text
);
404 $(document
).ready(function() {
405 $('form#api-explorer')
406 .on('change', '#api-entity, #api-action', function() {
407 entity
= $('#api-entity').val();
408 if ($(this).is('#api-entity')) {
411 action
= $('#api-action').val();
412 if (entity
&& action
) {
413 $('#api-params').html('<tr><td colspan="4" class="crm-loading-element"></td></tr>');
414 $('#api-params-table thead').show();
418 $('#api-params, #api-generated pre').empty();
419 $('#api-param-buttons, #api-params-table thead').hide();
422 .on('change keyup', 'input.api-input, #api-params select', buildParams
)
423 .on('submit', submit
);
425 .on('change', '.api-param-name', toggleOptions
)
426 .on('change', '.api-param-name, .api-option-name', function() {
427 if ($(this).val() === '-') {
428 $(this).select2('destroy');
429 $(this).val('').focus();
432 .on('click', '.api-param-remove', function(e
) {
434 $(this).closest('tr').remove();
437 $('#api-params-add').on('click', function(e
) {
441 $('#api-option-add').on('click', function(e
) {
445 $('#api-chain-add').on('click', function(e
) {
449 $('#api-entity').change();