From 0c3a6e6447a649c77fdd3d88de328973a3f9ee79 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 20 Apr 2014 09:47:42 -0700 Subject: [PATCH] Api Explorer - code cleanup --- CRM/Admin/Page/APIExplorer.php | 9 +- templates/CRM/Admin/Page/APIExplorer.js | 152 +++++++++++++++ templates/CRM/Admin/Page/APIExplorer.tpl | 74 ++++++++ templates/CRM/Core/AjaxDoc.tpl | 230 ----------------------- 4 files changed, 229 insertions(+), 236 deletions(-) create mode 100644 templates/CRM/Admin/Page/APIExplorer.js create mode 100644 templates/CRM/Admin/Page/APIExplorer.tpl delete mode 100644 templates/CRM/Core/AjaxDoc.tpl diff --git a/CRM/Admin/Page/APIExplorer.php b/CRM/Admin/Page/APIExplorer.php index 15a29b2587..56f9bd540e 100644 --- a/CRM/Admin/Page/APIExplorer.php +++ b/CRM/Admin/Page/APIExplorer.php @@ -34,25 +34,22 @@ */ /** - * Page for displaying list of contact Subtypes + * Api Explorer */ 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'); return parent::run(); } - function getTemplateFileName() { - return 'CRM/Core/AjaxDoc.tpl'; - } - /** * Get user context. * * @return string user context. */ - function userContext($mode = NULL) { + function userContext() { return 'civicrm/api/explorer'; } } diff --git a/templates/CRM/Admin/Page/APIExplorer.js b/templates/CRM/Admin/Page/APIExplorer.js new file mode 100644 index 0000000000..88f890e2ab --- /dev/null +++ b/templates/CRM/Admin/Page/APIExplorer.js @@ -0,0 +1,152 @@ +CRM.$(function($) { + var restURL = CRM.url("civicrm/ajax/rest"); + + function toggleField (name, label, type) { + var h = '
\ + : \ + X\ +
'; + if ( $('#extra [name=' + name + ']').length > 0) { + $('#extra [name=' + name + ']').parent().remove(); + } + else { + $('#extra').append (h); + } + } + + function buildForm (params) { + var h = ''; + if (params.action == 'delete') { + $('#extra').html(h); + return; + } + + CRM.api(params.entity, 'getFields', {}, { + success:function (data) { + h = '' + ts('Available fields (click to add/remove):') + ''; + $.each(data.values, function(key, value) { + var required = value.required ? " required" : ""; + h += "" + value.title + ""; + }); + $('#selector').html(h); + } + }); + } + + function generateQuery () { + var params = {}; + $('#explorer input:checkbox:checked, #explorer select, #extra input').each(function() { + var val = $(this).val(); + if (val) { + params[$(this).data('id')] = val; + } + }); + query = CRM.url("civicrm/ajax/rest", params); + $('#query').val(query); + if (params.action == 'delete' && $('#selector a').length == 0) { + buildForm (params); + return; + } + if (params.action == 'create' && $('#selector a').length == 0) { + buildForm (params); + return; + } + } + + function runQuery(query) { + var vars = [], + hash, + smarty = '', + php = "$params = array(
  'version' => 3,", + json = "{", + link = "", + key, + value, + entity, + action, + query = $('#query').val(), + hashes = query.slice(query.indexOf('?') + 1).split('&'); + for(var i = 0; i < hashes.length; i++) { + hash = hashes[i].split('='); + key = hash[0]; + value = hash[1]; + + switch (key) { + case 'version': + case 'debug': + case 'json': + break; + case 'action': + action = value.toLowerCase(); + $('#action').val(action); + break; + case 'entity': + entity = value.charAt(0).toUpperCase() + value.substr(1); + $('#entity').val(entity); + break; + default: + if (typeof value == 'undefined') { + break; + } + value = isNaN(value) ? "'" + value + "'" : value; + smarty += ' ' + key + '=' + value; + php += "
  '" + key +"' => " + value + ","; + json += "'" + key + "': " + value + ", "; + } + } + + if (!entity) { + $('#query').val(ts('Choose an entity.{/ts}')); + $('#entity').val(''); + window.location.hash = 'explorer'; + return; + } + if (!action) { + $('#query').val(ts('Choose an action.')); + $('#action').val(''); + window.location.hash = 'explorer'; + return; + } + + window.location.hash = query; + $('#result').block(); + $.post(query,function(data) { + $('#result').unblock().text(data); + },'text'); + link="ajax query "; + var RESTquery = CRM.config.resourceBase + "extern/rest.php?"+ query.substring(restURL.length,query.length) + "&api_key={yoursitekey}&key={yourkey}"; + $("#link").html(link+"|REST query."); + + + json = (json.length > 1 ? json.slice (0,-2) : '{') + '}'; + php += "
);
"; + $('#php').html(php + "$result = civicrm_api('" + entity + "', '" + action + "', $params);"); + $('#jQuery').html ("CRM.api('"+entity+"', '"+action+"', "+json+",
  {success: function(data) {
      cj.each(data, function(key, value) {// do something });
    }
  }
);"); + + if (action.substring(0, 3) == "get") {//using smarty only make sense for get actions + $('#smarty').html("{crmAPI var='result' entity='" + entity + "' action='" + action + "' " + smarty + '}
{foreach from=$result.values item=' + entity + '}
  <li>{$' + entity +'.some_field}</li>
{/foreach}'); + } else { + $('#smarty').html("smarty uses only 'get' actions"); + } + $('#generated').show(); + } + + var query = window.location.hash; + var t = "#/civicrm/ajax/rest"; + if (query.substring(0, t.length) === t) { + $('#query').val (query.substring(1)).focus(); + } else { + window.location.hash="explorer"; //to be sure to display the result under the generated code in the viewport + } + $('#entity, #action').change (function() { $("#selector, #extra").empty(); generateQuery(); runQuery(); }); + $('#explorer input:checkbox').change(function() {generateQuery(); runQuery(); }); + $('#explorer').submit(function() {runQuery(); return false;}); + $('#extra').on('keyup', 'input', generateQuery); + $('#extra').on('click', 'a.remove-extra', function() { + $(this).parent().remove(); + generateQuery(); + }); + $('#selector').on('click', 'a', function() { + toggleField($(this).data('id'), this.innerHTML, this.class); + }); +}); diff --git a/templates/CRM/Admin/Page/APIExplorer.tpl b/templates/CRM/Admin/Page/APIExplorer.tpl new file mode 100644 index 0000000000..ab53cf0606 --- /dev/null +++ b/templates/CRM/Admin/Page/APIExplorer.tpl @@ -0,0 +1,74 @@ + + + + +
+ + + |  + + + + |  + + + |  + + + |  + + + +
+
+ + + + + + + + + +
+You can choose an entity and an action (eg Tag Get to retrieve a list of the tags)
+Or your can directly modify the url in the field above and press enter.
+
+When you use the create method, it displays the list of existing fields for this entity.
+click on the name of the fields you want to populate, fill the value(s) and press enter
+
+The result of the ajax calls are displayed in this grey area.
+
diff --git a/templates/CRM/Core/AjaxDoc.tpl b/templates/CRM/Core/AjaxDoc.tpl deleted file mode 100644 index 1110e5ece5..0000000000 --- a/templates/CRM/Core/AjaxDoc.tpl +++ /dev/null @@ -1,230 +0,0 @@ - - - - - - - - - |  - - - - |  - - - |  - - - |  - - - -
-
- - - - - - - - - -
-You can choose an entity and an action (eg Tag Get to retrieve a list of the tags)
-Or your can directly modify the url in the field above and press enter.
-
-When you use the create method, it displays the list of existing fields for this entity.
-click on the name of the fields you want to populate, fill the value(s) and press enter
-
-The result of the ajax calls are displayed in this grey area.
-
-- 2.25.1