4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2019
36 class CRM_Api4_Page_AJAX
extends CRM_Core_Page
{
39 * Handler for api4 ajax requests
41 public function run() {
44 if (empty($this->urlPath
[3])) {
45 $calls = CRM_Utils_Request
::retrieve('calls', 'String', CRM_Core_DAO
::$_nullObject, TRUE, NULL, 'POST', TRUE);
46 $calls = json_decode($calls, TRUE);
48 foreach ($calls as $index => $call) {
49 $response[$index] = call_user_func_array([$this, 'execute'], $call);
54 $entity = $this->urlPath
[3];
55 $action = $this->urlPath
[4];
56 $params = CRM_Utils_Request
::retrieve('params', 'String');
57 $params = $params ?
json_decode($params, TRUE) : [];
58 $index = CRM_Utils_Request
::retrieve('index', 'String');
59 $response = $this->execute($entity, $action, $params, $index);
62 catch (Exception
$e) {
63 http_response_code(500);
65 'error_code' => $e->getCode(),
67 if (CRM_Core_Permission
::check('view debug output')) {
68 $response['error_message'] = $e->getMessage();
69 if (\Civi
::settings()->get('backtrace')) {
70 $response['backtrace'] = $e->getTrace();
74 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
75 echo json_encode($response);
76 CRM_Utils_System
::civiExit();
80 * Run api call & prepare result for json encoding
82 * @param string $entity
83 * @param string $action
84 * @param array $params
85 * @param string $index
88 protected function execute($entity, $action, $params = [], $index = NULL) {
89 $params['checkPermissions'] = TRUE;
91 // Handle numeric indexes later so we can get the count
92 $itemAt = CRM_Utils_Type
::validate($index, 'Integer', FALSE);
94 $result = civicrm_api4($entity, $action, $params, isset($itemAt) ?
NULL : $index);
96 // Convert arrayObject into something more suitable for json
97 $vals = ['values' => isset($itemAt) ?
$result->itemAt($itemAt) : (array) $result];
98 foreach (get_class_vars(get_class($result)) as $key => $val) {
99 $vals[$key] = $result->$key;
101 $vals['count'] = $result->count();