Blame view

sources/apps/contacts/lib/jsonresponse.php 1.93 KB
923852aa1   Kload   Official Owncloud...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  <?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;
  use OCP\AppFramework\Http\JSONResponse as OriginalResponse,
  	OCP\AppFramework\Http;
  
  
  /**
   * A renderer for JSON calls
   */
  class JSONResponse extends OriginalResponse {
  
  	public function __construct($params = array(), $statusCode = Http::STATUS_OK) {
  		parent::__construct(array(), $statusCode);
  		$this->data = $params;
  	}
  
  	/**
  	 * 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->setData($params);
  		return $this;
  	}
  
  	public function setData($data) {
  		$this->data = $data;
  		return $this;
  	}
  
  	public function setStatus($status) {
  		parent::setStatus($status);
  		return $this;
  	}
  
  	/**
  	 * 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 = $message;
  		return $this;
  	}
  
  	function bailOut($msg, $tracelevel = 1, $debuglevel = \OCP\Util::ERROR) {
  		if($msg instanceof \Exception) {
  			$msg = $msg->getMessage();
  			$this->setStatus($msg->getCode());
  		}
  		$this->setErrorMessage($msg);
  		return $this->debug($msg, $tracelevel, $debuglevel);
  	}
  
  	function debug($msg, $tracelevel = 0, $debuglevel = \OCP\Util::DEBUG) {
  		if(!is_numeric($tracelevel)) {
  			return $this;
  		}
  
  		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);
  		}
  		return $this;
  	}
  
  }