2013-04-19 09:59:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Thomas Tanghus, Bart Visscher
|
|
|
|
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
|
|
|
|
* 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,
|
|
|
|
OCP\AppFramework\Http\Http;
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A renderer for JSON calls
|
|
|
|
*/
|
|
|
|
class JSONResponse extends OriginalResponse {
|
|
|
|
|
2013-09-10 07:16:32 +02:00
|
|
|
public function __construct($params = array(), $statusCode=Http::STATUS_OK) {
|
|
|
|
parent::__construct(array(), $statusCode);
|
2013-04-19 09:59:30 +02:00
|
|
|
$this->data['data'] = $params;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function setParams(array $params){
|
|
|
|
$this->data['data'] = $params;
|
|
|
|
$this->data['status'] = 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
$this->data['data']['message'] = $message;
|
|
|
|
$this->data['status'] = 'error';
|
|
|
|
}
|
|
|
|
|
|
|
|
function bailOut($msg, $tracelevel = 1, $debuglevel = \OCP\Util::ERROR) {
|
2013-09-03 14:00:11 +02:00
|
|
|
if($msg instanceof \Exception) {
|
|
|
|
$msg = $msg->getMessage();
|
|
|
|
$this->setStatus($msg->getCode());
|
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
$this->setErrorMessage($msg);
|
|
|
|
$this->debug($msg, $tracelevel, $debuglevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
function debug($msg, $tracelevel = 0, $debuglevel = \OCP\Util::DEBUG) {
|
|
|
|
if(!is_numeric($tracelevel)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(PHP_VERSION >= "5.4") {
|
|
|
|
$call = debug_backtrace(false, $tracelevel + 1);
|
|
|
|
} else {
|
|
|
|
$call = debug_backtrace(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$call = $call[$tracelevel];
|
|
|
|
if($debuglevel !== false) {
|
|
|
|
\OCP\Util::writeLog('contacts',
|
|
|
|
$call['file'].'. Line: '.$call['line'].': '.$msg,
|
|
|
|
$debuglevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|