summaryrefslogtreecommitdiff
path: root/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/CRM/Api4/Page/AJAX.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/CRM/Api4/Page/AJAX.php')
-rw-r--r--www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/CRM/Api4/Page/AJAX.php66
1 files changed, 66 insertions, 0 deletions
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/CRM/Api4/Page/AJAX.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/CRM/Api4/Page/AJAX.php
new file mode 100644
index 00000000..6a1bc2de
--- /dev/null
+++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/CRM/Api4/Page/AJAX.php
@@ -0,0 +1,66 @@
+<?php
+
+class CRM_Api4_Page_AJAX extends CRM_Core_Page {
+
+ /**
+ * Handler for api4 ajax requests
+ */
+ public function run() {
+ try {
+ // Call multiple
+ if (empty($this->urlPath[3])) {
+ $calls = CRM_Utils_Request::retrieve('calls', 'String', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST', TRUE);
+ $calls = json_decode($calls, TRUE);
+ $response = [];
+ foreach ($calls as $index => $call) {
+ $response[$index] = call_user_func_array([$this, 'execute'], $call);
+ }
+ }
+ // Call single
+ else {
+ $entity = $this->urlPath[3];
+ $action = $this->urlPath[4];
+ $params = CRM_Utils_Request::retrieve('params', 'String');
+ $params = $params ? json_decode($params, TRUE) : [];
+ $index = CRM_Utils_Request::retrieve('index', 'String');
+ $response = $this->execute($entity, $action, $params, $index);
+ }
+ }
+ catch (Exception $e) {
+ http_response_code(500);
+ $response = [
+ 'error_code' => $e->getCode(),
+ ];
+ if (CRM_Core_Permission::check('view debug output')) {
+ $response['error_message'] = $e->getMessage();
+ if (CRM_Core_BAO_Setting::getItem(NULL, 'backtrace')) {
+ $response['backtrace'] = $e->getTrace();
+ }
+ }
+ }
+ CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
+ echo json_encode($response);
+ CRM_Utils_System::civiExit();
+ }
+
+ /**
+ * Run api call & prepare result for json encoding
+ *
+ * @param string $entity
+ * @param string $action
+ * @param array $params
+ * @param string $index
+ * @return array
+ */
+ protected function execute($entity, $action, $params = [], $index = NULL) {
+ $params['checkPermissions'] = TRUE;
+ $result = civicrm_api4($entity, $action, $params, $index);
+ // Convert arrayObject into something more suitable for json
+ $vals = ['values' => (array) $result];
+ foreach (get_class_vars(get_class($result)) as $key => $val) {
+ $vals[$key] = $result->$key;
+ }
+ return $vals;
+ }
+
+}