Skip to content
Snippets Groups Projects
Commit a323a20f authored by colemanw's avatar colemanw
Browse files

CRM-14093 - add CRM.api3 to rest.js

parent 42a11a93
No related branches found
No related tags found
No related merge requests found
......@@ -301,12 +301,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
......@@ -542,7 +540,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',
......@@ -577,13 +575,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
*/
......
......@@ -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 = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment