From 86caa4d78fd7ae5775842960a0ec1ae1dbd70a54 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 19 Jan 2014 16:12:43 -0800 Subject: [PATCH] CRM-14093 - add CRM.api3 to rest.js --- CRM/Utils/REST.php | 33 +++++++++++++++++++++++++++------ js/rest.js | 26 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/CRM/Utils/REST.php b/CRM/Utils/REST.php index a64a1a040b..6d501ddf46 100644 --- a/CRM/Utils/REST.php +++ b/CRM/Utils/REST.php @@ -305,12 +305,10 @@ class CRM_Utils_REST { return self::error("User API key invalid"); } - return self::process($args); + return self::process($args, self::buildParamList()); } - static function process(&$args, $restInterface = TRUE) { - $params = &self::buildParamList(); - + static function process(&$args, $params) { $params['check_permissions'] = TRUE; $fnName = $apiFile = NULL; // clean up all function / class names. they should be alphanumeric and _ only @@ -552,7 +550,7 @@ class CRM_Utils_REST { ) ) { require_once 'api/v3/utils.php'; - $error = civicrm_api3_create_error("SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api().", + $error = civicrm_api3_create_error("SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api3().", array( 'IP' => $_SERVER['REMOTE_ADDR'], 'level' => 'security', @@ -587,13 +585,36 @@ class CRM_Utils_REST { return self::error('Unknown function invocation.'); } - $result = self::process($args, FALSE); + // Support for multiple api calls + if (isset($entity) && $entity === 'api3') { + $result = self::processMultiple(); + } + else { + $result = self::process($args, self::buildParamList()); + } echo self::output($result); CRM_Utils_System::civiExit(); } + /** + * Callback for multiple ajax api calls from CRM.api3() + * @return array + */ + static function processMultiple() { + $output = array(); + foreach (json_decode($_REQUEST['json'], TRUE) as $key => $call) { + $args = array( + 'civicrm', + $call[0], + $call[1], + ); + $output[$key] = self::process($args, CRM_Utils_Array::value(2, $call, array())); + } + return $output; + } + /** * @return array|NULL NULL if execution should proceed; array if the response is already known */ diff --git a/js/rest.js b/js/rest.js index 434dcdf312..6a35914161 100644 --- a/js/rest.js +++ b/js/rest.js @@ -84,6 +84,32 @@ var CRM = CRM || {}; /** * AJAX api */ + CRM.api3 = function(entity, action, params) { + if (typeof(entity) === 'string') { + params = { + entity: entity, + action: action.toLowerCase(), + json: JSON.stringify(params || {}) + }; + } else { + params = { + entity: 'api3', + action: 'call', + json: JSON.stringify(entity) + } + } + return $.ajax({ + url: CRM.url('civicrm/ajax/rest'), + dataType: 'json', + data: params, + type: params.action.indexOf('get') < 0 ? 'POST' : 'GET' + }); + }; + + /** + * @deprecated + * AJAX api + */ CRM.api = function(entity, action, params, options) { // Default settings var settings = { -- 2.25.1