Merge pull request #15365 from samuelsov/userdashboard
[civicrm-core.git] / CRM / Api4 / Page / AJAX.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36 class CRM_Api4_Page_AJAX extends CRM_Core_Page {
37
38 /**
39 * Handler for api4 ajax requests
40 */
41 public function run() {
42 try {
43 // Call multiple
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);
47 $response = [];
48 foreach ($calls as $index => $call) {
49 $response[$index] = call_user_func_array([$this, 'execute'], $call);
50 }
51 }
52 // Call single
53 else {
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);
60 }
61 }
62 catch (Exception $e) {
63 http_response_code(500);
64 $response = [
65 'error_code' => $e->getCode(),
66 ];
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();
71 }
72 }
73 }
74 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
75 echo json_encode($response);
76 CRM_Utils_System::civiExit();
77 }
78
79 /**
80 * Run api call & prepare result for json encoding
81 *
82 * @param string $entity
83 * @param string $action
84 * @param array $params
85 * @param string $index
86 * @return array
87 */
88 protected function execute($entity, $action, $params = [], $index = NULL) {
89 $params['checkPermissions'] = TRUE;
90
91 // Handle numeric indexes later so we can get the count
92 $itemAt = CRM_Utils_Type::validate($index, 'Integer', FALSE);
93
94 $result = civicrm_api4($entity, $action, $params, isset($itemAt) ? NULL : $index);
95
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;
100 }
101 $vals['count'] = $result->count();
102 return $vals;
103 }
104
105 }