dolibarr 25.0.0-alpha
jsonResponse.class.php
1<?php
2/* Copyright (C) 2024 John BOTELLA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18
24{
28 const STATUS_SUCCESS = 1;
29
33 const STATUS_ERROR = 0;
34
38 const HTTP_OK = 200;
39
43 const HTTP_CREATED = 201;
44
48 const HTTP_ACCEPTED = 202;
49
53 const HTTP_BAD_REQUEST = 400;
54
58 const HTTP_UNAUTHORIZED = 401;
59
63 const HTTP_FORBIDDEN = 403;
64
68 const HTTP_NOT_FOUND = 404;
69
73 const HTTP_METHOD_NOT_ALLOWED = 405;
74
78 const HTTP_CONFLICT = 409;
79
83 const HTTP_INTERNAL_ERROR = 500;
84
88 const HTTP_NOT_IMPLEMENTED = 501;
89
93 const HTTP_SERVICE_UNAVAILABLE = 503;
94
101
107 private $httpResponseCode = null;
108
114 public $result = 0;
115
121 public $data;
122
129 public $debug;
130
136 public $msg = '';
137
143 public $newToken = '';
144
148 public function __construct()
149 {
150 $this->newToken = newToken();
151 }
152
158 public function getResponse()
159 {
160 global $dolibarr_main_prod;
161
162 if (!headers_sent()) {
163 header('Content-Type: application/json; charset=utf-8');
164 http_response_code($this->getHttpResponseCode());
165 }
166
167 $jsonResponse = new stdClass();
168 $jsonResponse->result = $this->result;
169 $jsonResponse->msg = $this->msg;
170 $jsonResponse->newToken = $this->newToken;
171 $jsonResponse->data = $this->data;
172
173 if (empty($dolibarr_main_prod) && defined('DEBUGJSONRESPONSE') && (int) constant('DEBUGJSONRESPONSE') > 0) {
174 $jsonResponse->debug = $this->debug;
175 }
176
177 return json_encode($jsonResponse, JSON_PRETTY_PRINT);
178 }
179
187 public function getHttpResponseCode()
188 {
189 $httpCode = $this->httpResponseCode;
190 if ($httpCode === null) {
191 $httpCode = $this->result ? self::HTTP_OK : self::HTTP_BAD_REQUEST;
192 }
193
194 if ($this->changeHeaderForErrors && !$this->result && $this->isValidHttpSuccessCode($httpCode)) {
195 $httpCode = self::HTTP_BAD_REQUEST;
196 }
197
198 return $httpCode;
199 }
200
211 public function setError($msg = '', $httpCode = self::HTTP_OK)
212 {
213 if (!$this->isValidHttpErrorCode($httpCode)) {
214 $httpCode = self::HTTP_OK;
215 }
216
217 $this->result = self::STATUS_ERROR;
218 $this->msg = $msg;
219 $this->setHttpResponseCode($httpCode);
220 }
221
229 public function setSuccess($msg = '', $httpCode = self::HTTP_OK)
230 {
231 if (!$this->isValidHttpSuccessCode($httpCode)) {
232 $httpCode = self::HTTP_OK;
233 }
234
235 $this->result = self::STATUS_SUCCESS;
236 $this->msg = $msg;
237 $this->setHttpResponseCode($httpCode);
238 }
239
247 public function setHttpResponseCode($httpCode)
248 {
249 if (!$this->isValidHttpErrorCode($httpCode) && !$this->isValidHttpSuccessCode($httpCode)) {
250 return false;
251 }
252
253 $this->httpResponseCode = $httpCode;
254 return true;
255 }
256
263 private function isValidHttpErrorCode($code)
264 {
265 return in_array($code, [
266 self::HTTP_BAD_REQUEST,
267 self::HTTP_UNAUTHORIZED,
268 self::HTTP_FORBIDDEN,
269 self::HTTP_NOT_FOUND,
270 self::HTTP_METHOD_NOT_ALLOWED,
271 self::HTTP_CONFLICT,
272 self::HTTP_INTERNAL_ERROR,
273 self::HTTP_NOT_IMPLEMENTED,
274 self::HTTP_SERVICE_UNAVAILABLE
275 ], true);
276 }
277
284 private function isValidHttpSuccessCode($code)
285 {
286 return in_array($code, [
287 self::HTTP_OK,
288 self::HTTP_CREATED,
289 self::HTTP_ACCEPTED
290 ], true);
291 }
292
299 public function output()
300 {
301 if (!headers_sent()) {
302 top_httphead('application/json');
303 }
304
305 print $this->getResponse();
306
307 exit;
308 }
309}
Class JsonResponse used for ajax responses in Dolibarr.
__construct()
JsonResponse constructor.
getHttpResponseCode()
Get the HTTP response code that will be sent.
output()
Send the JSON response to the client (JSON content-type header, HTTP response code and payload) and s...
isValidHttpSuccessCode($code)
Check if HTTP code is a valid success code (2xx).
setHttpResponseCode($httpCode)
Define the HTTP response code used when sending the JSON response.
$changeHeaderForErrors
When enabled, an error response sent with a 2xx HTTP code is upgraded to self\HTTP_BAD_REQUEST.
setError($msg='', $httpCode=self::HTTP_OK)
Set the current response as an error response.
$msg
returned message used usually as set event message
getResponse()
return json encoded of object and send the HTTP response code if headers are not already sent
setSuccess($msg='', $httpCode=self::HTTP_OK)
Set the response as a success.
isValidHttpErrorCode($code)
Check if HTTP code is a valid error code (4xx or 5xx).
newToken()
Return the value of token currently saved into session with name 'newtoken'.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.