444453fb4a26d4123d60f7ee576b7bb828aa1e82
1 (function($, _
, undefined) {
8 fieldTpl
= _
.template($('#api-param-tpl').html()),
9 chainTpl
= _
.template($('#api-chain-tpl').html());
11 function prettyPrint() {
12 if (window
.prettyPrint
) {
17 function addField(name
) {
18 $('#api-params').append($(fieldTpl({name
: name
|| ''})));
19 var $row
= $('tr:last-child', '#api-params');
20 $('.api-param-name', $row
).crmSelect2({data
: fields
}).change();
23 function addChainField() {
24 $('#api-params').append($(chainTpl({})));
25 var $row
= $('tr:last-child', '#api-params');
26 $('.api-chain-entity', $row
).crmSelect2({
27 formatSelection: function(item
) {
28 return '<span class="icon ui-icon-link"></span> API ' + item
.text
;
33 function getFields() {
37 // Special case for getfields
38 if (action
=== 'getfields') {
43 options
.api_action
= [];
44 $('option', '#api-action').each(function() {
46 options
.api_action
.push({key
: this.value
, value
: $(this).text()});
49 showFields(['api_action']);
52 CRM
.api3(entity
, 'getFields', {'api_action': action
, sequential
: 1, options
: {get_options
: 'all'}}).done(function(data
) {
53 _
.each(data
.values
, function(field
) {
57 text
: field
.title
|| field
.name
,
58 required
: field
['api.required'] || false
60 if (field
['api.required']) {
61 required
.push(field
.name
);
64 options
[field
.name
] = field
.options
;
72 function showFields(required
) {
75 text
: ts('Other') + '...'
77 $('#api-params').empty();
78 $('#api-param-buttons').show();
79 if (required
.length
) {
80 _
.each(required
, addField
);
86 function toggleOptions() {
87 var name
= $(this).val(),
88 $valField
= $(this).closest('tr').find('.api-param-value');
90 $valField
.val('').select2({
92 data
: _
.transform(options
[name
], function(result
, option
) {
93 result
.push({id
: option
.key
, text
: option
.value
});
97 else if ($valField
.data('select2')) {
98 $valField
.select2('destroy');
101 $(this).select2('destroy');
102 $(this).val('').focus();
107 * Attempt to parse a string into a value of the intended type
110 function evaluate(val
, makeArray
) {
115 var first
= val
.charAt(0),
116 last
= val
.slice(-1);
118 if (val
=== 'true' || val
=== 'false' || val
=== 'null' || !isNaN(val
)) {
122 if ((first
=== '"' || first
=== "'") && last
=== first
) {
123 return val
.slice(1, -1);
126 if ((first
=== '[' && last
=== ']') || (first
=== '{' && last
=== '}')) {
127 return eval('(' + val
+ ')');
129 // Transform csv to array
130 if (makeArray
&& val
.indexOf(',') > 0) {
131 return val
.split(',');
133 // Ok ok it's really a string
136 // If eval crashed return undefined
142 * Format value to look like php code
145 function phpFormat(val
) {
147 if ($.isPlainObject(val
)) {
148 $.each(val
, function(k
, v
) {
149 ret
+= (ret
? ',' : '') + "'" + k
+ "' => " + phpFormat(v
);
151 return 'array(' + ret
+ ')';
153 if ($.isArray(val
)) {
154 $.each(val
, function(k
, v
) {
155 ret
+= (ret
? ', ' : '') + phpFormat(v
);
157 return 'array(' + ret
+ ')';
159 return JSON
.stringify(val
);
163 * Smarty doesn't support array literals so we provide a stub
166 function smartyFormat(js
, key
) {
167 if (js
.indexOf('[') > -1 || js
.indexOf('{') > -1) {
168 return '$' + key
.replace(/[. -]/g, '_');
173 function buildParams(e
) {
175 $('.api-param-checkbox:checked').each(function() {
176 params
[this.name
] = 1;
178 $('input.api-param-value').each(function() {
179 var $row
= $(this).closest('tr'),
180 val
= evaluate($(this).val(), $(this).is('.select2-offscreen')),
181 name
= $('input.api-param-name', $row
).val(),
182 op
= $('select.api-param-op', $row
).val() || '=';
183 // Special syntax for api chaining
184 if (!name
&& $('select.api-chain-entity', $row
).val()) {
185 name
= 'api.' + $('select.api-chain-entity', $row
).val() + '.' + $('select.api-chain-action', $row
).val();
187 if (name
&& val
!== undefined) {
188 params
[name
] = op
=== '=' ? val
: {};
190 params
[name
][op
] = val
;
194 else if (name
&& (!e
|| e
.type
!== 'keyup')) {
198 if (entity
&& action
) {
203 function setError(el
) {
204 if (!$(el
).hasClass('crm-error')) {
206 .addClass('crm-error')
207 .attr('title', ts('Syntax error'))
208 .before('<div class="icon red-icon ui-icon-alert"/>');
212 function clearError(el
) {
214 .removeClass('crm-error')
216 .siblings('.ui-icon-alert').remove();
219 function formatQuery() {
221 smarty
: "{crmAPI var='result' entity='" + entity
+ "' action='" + action
+ "'",
222 php
: "$result = civicrm_api3('" + entity
+ "', '" + action
+ "'",
223 json
: "CRM.api3('" + entity
+ "', '" + action
+ "'",
224 rest
: CRM
.config
.resourceBase
+ "extern/rest.php?entity=" + entity
+ "&action=" + action
+ "&json=" + JSON
.stringify(params
) + "&api_key=yoursitekey&key=yourkey"
226 $.each(params
, function(key
, value
) {
227 var js
= JSON
.stringify(value
);
229 q
.php
+= ", array(\n";
234 q
.php
+= " '" + key
+ "' => " + phpFormat(value
) + ",\n";
235 q
.json
+= " \"" + key
+ '": ' + js
;
236 // FIXME: How to deal with complex values in smarty?
237 q
.smarty
+= ' ' + key
+ '=' + smartyFormat(js
, key
);
244 q
.json
+= ").done(function(result) {\n // do something\n});";
245 q
.smarty
+= "}\n{foreach from=$result.values item=" + entity
.toLowerCase() + "}\n {$" + entity
.toLowerCase() + ".some_field}\n{/foreach}";
246 if (action
.indexOf('get') < 0) {
247 q
.smarty
= '{* Smarty API only works with get actions *}';
249 $.each(q
, function(type
, val
) {
250 $('#api-' + type
).removeClass('prettyprinted').text(val
);
257 if (!entity
|| !action
) {
258 alert(ts('Select an entity & action.'));
261 if (action
.indexOf('get') < 0) {
262 var msg
= action
=== 'delete' ? ts('This will delete data from CiviCRM. Are you sure?') : ts('This will write to the database. Continue?');
263 CRM
.confirm({title
: ts('Confirm %1', {1: action
}), message
: msg
}).on('crmConfirm:yes', execute
);
270 $('#api-result').html('<div class="crm-loading-element"></div>');
272 url
: CRM
.url('civicrm/ajax/rest'),
277 json
: JSON
.stringify(params
)
279 type
: action
.indexOf('get') < 0 ? 'POST' : 'GET',
281 }).done(function(text
) {
282 $('#api-result').addClass('prettyprint linenums').removeClass('prettyprinted').text(text
);
287 $(document
).ready(function() {
288 $('form#api-explorer')
289 .on('change', '#api-entity, #api-action', function() {
290 entity
= $('#api-entity').val();
291 action
= $('#api-action').val();
292 if (entity
&& action
) {
293 $('#api-params').html('<tr><td colspan="4" class="crm-loading-element"></td></tr>');
294 $('#api-params-table thead').show();
298 $('#api-params, #api-generated pre').empty();
299 $('#api-param-buttons, #api-params-table thead').hide();
302 .on('change keyup', 'input.api-param-checkbox, input.api-param-value, input.api-param-name, #api-params select', buildParams
)
303 .on('submit', submit
);
305 .on('change', '.api-param-name', toggleOptions
)
306 .on('click', '.api-param-remove', function(e
) {
308 $(this).closest('tr').remove();
311 $('#api-params-add').on('click', function(e
) {
315 $('#api-chain-add').on('click', function(e
) {
319 $('#api-entity').change();