2013-04-19 09:59:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-01-26 00:40:22 +01:00
|
|
|
* @author Thomas Tanghus
|
|
|
|
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
|
|
|
|
*
|
2013-04-19 09:59:30 +02:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts;
|
2013-09-17 18:46:59 +02:00
|
|
|
use OCP\AppFramework\Http\JSONResponse as OriginalResponse,
|
2013-10-23 12:56:30 +02:00
|
|
|
OCP\AppFramework\Http;
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A renderer for JSON calls
|
|
|
|
*/
|
|
|
|
class JSONResponse extends OriginalResponse {
|
|
|
|
|
2014-02-05 22:46:26 +01:00
|
|
|
public function __construct($params = array(), $statusCode = Http::STATUS_OK) {
|
2013-09-10 07:16:32 +02:00
|
|
|
parent::__construct(array(), $statusCode);
|
2013-10-17 02:10:34 +02:00
|
|
|
$this->data = $params;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2013-09-17 18:46:59 +02:00
|
|
|
/**
|
|
|
|
* Sets values in the data json array
|
|
|
|
* @param array|object $params an array or object which will be transformed
|
|
|
|
* to JSON
|
|
|
|
*/
|
2013-09-27 16:38:22 +02:00
|
|
|
public function setParams(array $params) {
|
|
|
|
$this->setData($params);
|
|
|
|
return $this;
|
2013-09-17 18:46:59 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 02:10:34 +02:00
|
|
|
public function setData($data) {
|
2013-09-27 16:38:22 +02:00
|
|
|
$this->data = $data;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setStatus($status) {
|
|
|
|
parent::setStatus($status);
|
|
|
|
return $this;
|
|
|
|
}
|
2013-09-17 18:46:59 +02:00
|
|
|
|
2013-04-19 09:59:30 +02:00
|
|
|
/**
|
|
|
|
* in case we want to render an error message, also logs into the owncloud log
|
|
|
|
* @param string $message the error message
|
|
|
|
*/
|
|
|
|
public function setErrorMessage($message){
|
|
|
|
$this->error = true;
|
2014-03-18 23:51:32 +01:00
|
|
|
$this->data = array('status' => 'error', 'data' => array('message' => $message));
|
2013-09-27 16:38:22 +02:00
|
|
|
return $this;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2014-03-23 19:02:16 +01:00
|
|
|
public function bailOut($msg, $tracelevel = 1, $debuglevel = \OCP\Util::ERROR) {
|
2013-09-03 14:00:11 +02:00
|
|
|
if($msg instanceof \Exception) {
|
|
|
|
$this->setStatus($msg->getCode());
|
2014-03-23 19:02:16 +01:00
|
|
|
$msg = $msg->getMessage();
|
2013-09-03 14:00:11 +02:00
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
$this->setErrorMessage($msg);
|
2013-10-17 02:10:34 +02:00
|
|
|
return $this->debug($msg, $tracelevel, $debuglevel);
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2014-03-23 19:02:16 +01:00
|
|
|
public function debug($msg, $tracelevel = 0, $debuglevel = \OCP\Util::DEBUG) {
|
2013-04-19 09:59:30 +02:00
|
|
|
if(!is_numeric($tracelevel)) {
|
2013-09-27 16:38:22 +02:00
|
|
|
return $this;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(PHP_VERSION >= "5.4") {
|
2014-03-23 19:02:16 +01:00
|
|
|
$call = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $tracelevel + 1);
|
2013-04-19 09:59:30 +02:00
|
|
|
} else {
|
|
|
|
$call = debug_backtrace(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$call = $call[$tracelevel];
|
|
|
|
if($debuglevel !== false) {
|
|
|
|
\OCP\Util::writeLog('contacts',
|
|
|
|
$call['file'].'. Line: '.$call['line'].': '.$msg,
|
|
|
|
$debuglevel);
|
|
|
|
}
|
2013-09-27 16:38:22 +02:00
|
|
|
return $this;
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|