From 2b6e11740c5c7bd9e0e63aeb32cd0d0aead7054b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 21 Apr 2014 19:16:31 -0700 Subject: [PATCH] Api Explorer - dynamically fetch actions --- CRM/Admin/Page/APIExplorer.php | 17 ++----- api/v3/Generic.php | 2 + templates/CRM/Admin/Page/APIExplorer.js | 46 ++++++++++++++++--- templates/CRM/Admin/Page/APIExplorer.tpl | 56 ++++++++++++++---------- 4 files changed, 80 insertions(+), 41 deletions(-) diff --git a/CRM/Admin/Page/APIExplorer.php b/CRM/Admin/Page/APIExplorer.php index 3ea21f5d65..a1902553f3 100644 --- a/CRM/Admin/Page/APIExplorer.php +++ b/CRM/Admin/Page/APIExplorer.php @@ -40,20 +40,11 @@ class CRM_Admin_Page_APIExplorer extends CRM_Core_Page { function run() { CRM_Utils_System::setTitle(ts('API explorer and generator')); - CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Admin/Page/APIExplorer.js'); + CRM_Core_Resources::singleton() + ->addScriptFile('civicrm', 'templates/CRM/Admin/Page/APIExplorer.js') + ->addScriptUrl('//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js', 99) + ->addStyleUrl('//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css', 99); $this->assign('operators', CRM_Core_DAO::acceptedSQLOperators()); - $this->assign('actions', array( - 'get', - 'create', - 'delete', - 'getfields', - 'getactions', - 'getcount', - 'getsingle', - 'getvalue', - 'getoptions', - 'getlist', - )); return parent::run(); } diff --git a/api/v3/Generic.php b/api/v3/Generic.php index 13bcdfa789..06804ad4b3 100644 --- a/api/v3/Generic.php +++ b/api/v3/Generic.php @@ -59,6 +59,8 @@ function civicrm_api3_generic_getfields($apiRequest) { case 'replace': $unique = FALSE; case 'get': + case 'getsingle': + case 'getcount': $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']); if (empty($metadata['id'])){ // if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias diff --git a/templates/CRM/Admin/Page/APIExplorer.js b/templates/CRM/Admin/Page/APIExplorer.js index 444453fb4a..3a8da345e5 100644 --- a/templates/CRM/Admin/Page/APIExplorer.js +++ b/templates/CRM/Admin/Page/APIExplorer.js @@ -2,12 +2,17 @@ var entity, action, + actions = ['get'], fields = [], options = {}, params = {}, + smartyStub, fieldTpl = _.template($('#api-param-tpl').html()), chainTpl = _.template($('#api-chain-tpl').html()); + /** + * Call prettyPrint function if it successfully loaded from the cdn + */ function prettyPrint() { if (window.prettyPrint) { window.prettyPrint(); @@ -26,8 +31,11 @@ $('.api-chain-entity', $row).crmSelect2({ formatSelection: function(item) { return ' API ' + item.text; - } + }, + placeholder: ' ' + ts('Entity'), + escapeMarkup: function(m) {return m} }); + } function getFields() { @@ -69,6 +77,25 @@ }); } + function getActions() { + if (entity) { + CRM.api3(entity, 'getactions').done(function(data) { + // Ensure 'get' is always an action + actions = _.union(['get'], data.values); + populateActions(); + }); + } else { + actions = ['get']; + populateActions(); + } + } + + function populateActions(el) { + $('#api-action').select2({ + data: _.transform(actions, function(ret, item) {ret.push({text: item, id: item})}) + }); + } + function showFields(required) { fields.push({ id: '-', @@ -165,6 +192,7 @@ */ function smartyFormat(js, key) { if (js.indexOf('[') > -1 || js.indexOf('{') > -1) { + smartyStub = true; return '$' + key.replace(/[. -]/g, '_'); } return js; @@ -204,6 +232,7 @@ if (!$(el).hasClass('crm-error')) { $(el) .addClass('crm-error') + .css('width', '82%') .attr('title', ts('Syntax error')) .before('
'); } @@ -213,6 +242,7 @@ $(el) .removeClass('crm-error') .attr('title', '') + .css('width', '85%') .siblings('.ui-icon-alert').remove(); } @@ -223,6 +253,7 @@ json: "CRM.api3('" + entity + "', '" + action + "'", rest: CRM.config.resourceBase + "extern/rest.php?entity=" + entity + "&action=" + action + "&json=" + JSON.stringify(params) + "&api_key=yoursitekey&key=yourkey" }; + smartyStub = false; $.each(params, function(key, value) { var js = JSON.stringify(value); if (!i++) { @@ -233,7 +264,6 @@ } q.php += " '" + key + "' => " + phpFormat(value) + ",\n"; q.json += " \"" + key + '": ' + js; - // FIXME: How to deal with complex values in smarty? q.smarty += ' ' + key + '=' + smartyFormat(js, key); }); if (i) { @@ -245,6 +275,8 @@ q.smarty += "}\n{foreach from=$result.values item=" + entity.toLowerCase() + "}\n {$" + entity.toLowerCase() + ".some_field}\n{/foreach}"; if (action.indexOf('get') < 0) { q.smarty = '{* Smarty API only works with get actions *}'; + } else if (smartyStub) { + q.smarty = "{* Smarty does not have a syntax for array literals; assign complex variables on the server *}\n" + q.smarty; } $.each(q, function(type, val) { $('#api-' + type).removeClass('prettyprinted').text(val); @@ -255,7 +287,7 @@ function submit(e) { e.preventDefault(); if (!entity || !action) { - alert(ts('Select an entity & action.')); + alert(ts('Select an entity.')); return; } if (action.indexOf('get') < 0) { @@ -279,7 +311,7 @@ type: action.indexOf('get') < 0 ? 'POST' : 'GET', dataType: 'text' }).done(function(text) { - $('#api-result').addClass('prettyprint linenums').removeClass('prettyprinted').text(text); + $('#api-result').addClass('prettyprint').removeClass('prettyprinted').text(text); prettyPrint(); }); } @@ -288,6 +320,10 @@ $('form#api-explorer') .on('change', '#api-entity, #api-action', function() { entity = $('#api-entity').val(); + if ($(this).is('#api-entity')) { + $('#api-action').select2('val', 'get'); + getActions(); + } action = $('#api-action').val(); if (entity && action) { $('#api-params').html(''); @@ -299,7 +335,7 @@ $('#api-param-buttons, #api-params-table thead').hide(); } }) - .on('change keyup', 'input.api-param-checkbox, input.api-param-value, input.api-param-name, #api-params select', buildParams) + .on('change keyup', 'input.api-input, #api-params select', buildParams) .on('submit', submit); $('#api-params') .on('change', '.api-param-name', toggleOptions) diff --git a/templates/CRM/Admin/Page/APIExplorer.tpl b/templates/CRM/Admin/Page/APIExplorer.tpl index 1ecbaf983d..c3a2aa0ad2 100644 --- a/templates/CRM/Admin/Page/APIExplorer.tpl +++ b/templates/CRM/Admin/Page/APIExplorer.tpl @@ -25,9 +25,16 @@ *} - -
@@ -79,20 +90,16 @@    - +     |  @@ -119,14 +126,15 @@
-
+
 {ts}The result of api calls are displayed in this area.{/ts}
 
+ {strip}