Api Explorer - dynamically fetch actions
[civicrm-core.git] / templates / CRM / Admin / Page / APIExplorer.js
index 444453fb4a26d4123d60f7ee576b7bb828aa1e82..3a8da345e56d5182d382495cd611e2e3741a2b79 100644 (file)
@@ -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();
     $('.api-chain-entity', $row).crmSelect2({
       formatSelection: function(item) {
         return '<span class="icon ui-icon-link"></span> API ' + item.text;
-      }
+      },
+      placeholder: '<span class="icon ui-icon-link"></span> ' + ts('Entity'),
+      escapeMarkup: function(m) {return m}
     });
+
   }
 
   function getFields() {
     });
   }
 
+  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: '-',
    */
   function smartyFormat(js, key) {
     if (js.indexOf('[') > -1 || js.indexOf('{') > -1) {
+      smartyStub = true;
       return '$' + key.replace(/[. -]/g, '_');
     }
     return js;
     if (!$(el).hasClass('crm-error')) {
       $(el)
         .addClass('crm-error')
+        .css('width', '82%')
         .attr('title', ts('Syntax error'))
         .before('<div class="icon red-icon ui-icon-alert"/>');
     }
     $(el)
       .removeClass('crm-error')
       .attr('title', '')
+      .css('width', '85%')
       .siblings('.ui-icon-alert').remove();
   }
 
       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++) {
       }
       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) {
     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);
   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) {
       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();
     });
   }
     $('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('<tr><td colspan="4" class="crm-loading-element"></td></tr>');
           $('#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)