From 89ee60d5c6960c5758670ef0a318f3827d71e728 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 4 Feb 2015 15:14:55 -0500 Subject: [PATCH] Add Examples to api explorer --- CRM/Admin/Page/APIExplorer.php | 41 +++++- CRM/Core/xml/Menu/Misc.xml | 5 + templates/CRM/Admin/Page/APIExplorer.js | 49 ++++++- templates/CRM/Admin/Page/APIExplorer.tpl | 167 ++++++++++++++--------- 4 files changed, 198 insertions(+), 64 deletions(-) diff --git a/CRM/Admin/Page/APIExplorer.php b/CRM/Admin/Page/APIExplorer.php index 7b93156a49..79a7bacebe 100644 --- a/CRM/Admin/Page/APIExplorer.php +++ b/CRM/Admin/Page/APIExplorer.php @@ -42,12 +42,24 @@ class CRM_Admin_Page_APIExplorer extends CRM_Core_Page { * @return string */ public function run() { - CRM_Utils_System::setTitle(ts('API explorer and generator')); + CRM_Utils_System::setTitle('CiviCRM API'); 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()); + + // List example directories + global $civicrm_root; + $examples = array(); + foreach (scandir($civicrm_root . 'api/v3/examples') as $item) { + if ($item && strpos($item, '.') === FALSE) { + $examples[] = $item; + } + } + $this->assign('examples', $examples); + return parent::run(); } @@ -61,4 +73,31 @@ class CRM_Admin_Page_APIExplorer extends CRM_Core_Page { return 'civicrm/api/explorer'; } + /** + * AJAX callback to fetch examples + */ + public static function getExampleFile() { + global $civicrm_root; + if (!empty($_GET['entity']) && strpos($_GET['entity'], '.') === FALSE) { + $examples = array(); + foreach (scandir($civicrm_root . 'api/v3/examples/' . $_GET['entity']) as $item) { + $item = str_replace('.php', '', $item); + if ($item && strpos($item, '.') === FALSE) { + $examples[] = array('key' => $item, 'value' => $item); + } + } + CRM_Utils_JSON::output($examples); + } + if (!empty($_GET['file']) && strpos($_GET['file'], '.') === FALSE) { + $fileName = $civicrm_root . 'api/v3/examples/' . $_GET['file'] . '.php'; + if (file_exists($fileName)) { + echo file_get_contents($fileName); + } + else { + echo "Not found."; + } + CRM_Utils_System::civiExit(); + } + } + } diff --git a/CRM/Core/xml/Menu/Misc.xml b/CRM/Core/xml/Menu/Misc.xml index 0d023c655f..8abeb0894a 100644 --- a/CRM/Core/xml/Menu/Misc.xml +++ b/CRM/Core/xml/Menu/Misc.xml @@ -95,6 +95,11 @@ CRM_Admin_Page_APIExplorer access CiviCRM + + civicrm/ajax/apiexample + CRM_Admin_Page_APIExplorer::getExampleFile + access CiviCRM + civicrm/api/doc CRM_Utils_REST::APIDoc diff --git a/templates/CRM/Admin/Page/APIExplorer.js b/templates/CRM/Admin/Page/APIExplorer.js index 5723df8cac..e8c8119cf9 100644 --- a/templates/CRM/Admin/Page/APIExplorer.js +++ b/templates/CRM/Admin/Page/APIExplorer.js @@ -1,4 +1,6 @@ (function($, _, undefined) { + "use strict"; + /* jshint validthis: true */ var entity, action, @@ -523,8 +525,51 @@ }); } + /** + * Fetch list of example files for a given entity + */ + function getExamples() { + CRM.utils.setOptions($('#example-action').prop('disabled', true).addClass('loading'), []); + $.getJSON(CRM.url('civicrm/ajax/apiexample', {entity: $(this).val()})) + .done(function(result) { + CRM.utils.setOptions($('#example-action').prop('disabled', false).removeClass('loading'), result); + }); + } + + /** + * Fetch and display an example file + */ + function getExample() { + var + entity = $('#example-entity').val(), + action = $('#example-action').val(); + if (entity && action) { + $('#example-result').block(); + $.get(CRM.url('civicrm/ajax/apiexample', {file: entity + '/' + action})) + .done(function(result) { + $('#example-result').unblock().addClass('prettyprint').removeClass('prettyprinted').text(result); + prettyPrint(); + }); + } else { + $('#example-result').text($('#example-result').attr('title')); + } + } + $(document).ready(function() { - $('#api-entity').crmSelect2({ + // Set up tabs - bind active tab to document hash because... it's cool? + document.location.hash = document.location.hash || 'explorer'; + $('#mainTabContainer') + .tabs({ + active: $(document.location.hash + '-tab').index() - 1 + }) + .on('tabsactivate', function(e, ui) { + if (ui.newPanel) { + document.location.hash = ui.newPanel.attr('id').replace('-tab', ''); + } + }); + + // Initialize widgets + $('#api-entity, #example-entity').crmSelect2({ // Add strikethough class to selection to indicate deprecated apis formatSelection: function(option) { return $(option.element).hasClass('strikethrough') ? '' + option.text + '' : option.text; @@ -565,6 +610,8 @@ buildParams(); }) .on('change', 'select.api-chain-entity', getChainedAction); + $('#example-entity').on('change', getExamples); + $('#example-action').on('change', getExample); $('#api-params-add').on('click', function(e) { e.preventDefault(); addField(); diff --git a/templates/CRM/Admin/Page/APIExplorer.tpl b/templates/CRM/Admin/Page/APIExplorer.tpl index 8d157803b0..e85dc00a4a 100644 --- a/templates/CRM/Admin/Page/APIExplorer.tpl +++ b/templates/CRM/Admin/Page/APIExplorer.tpl @@ -25,16 +25,27 @@ *} -
- - -    - - -    +
+ - -  |  +
- + + + +    + + +    - - - - - - - - - -
{ts}Name{/ts} {help id='param-name'}{ts}Operator{/ts} {help id='param-op'}{ts}Value{/ts} {help id='param-value'}
- -
- - - - - - - {if $config->userSystem->is_drupal} - - {/if} - {if $config->userSystem->is_wordpress} - - {/if} -
{ts}Code{/ts}
Rest
Smarty
Php
Javascript
Drush
WP-CLI
-
-
- - - -
+ +  |  + + + + + + + + + + + + +
{ts}Name{/ts} {help id='param-name'}{ts}Operator{/ts} {help id='param-op'}{ts}Value{/ts} {help id='param-value'}
+ +
+ + + + + + + {if $config->userSystem->is_drupal} + + {/if} + {if $config->userSystem->is_wordpress} + + {/if} +
{ts}Code{/ts}
Rest
Smarty
Php
Javascript
Drush
WP-CLI
+
+
+ + + +
-{ts}The result of api calls are displayed in this area.{/ts}
+{ts}Results are displayed here.{/ts}
+
+ +
+ +
+
+ + +    + + +
+{ts}Results are displayed here.{/ts}
 
-
+ +
+
{strip}