summaryrefslogtreecommitdiffstats
path: root/lib/response.php
blob: 993d061d3030d0cf1d5d28232c0c9da33f35ed45 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
* ownCloud - News app
*
* @author Bernhard Posselt
* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
*
* This file is licensed under the Affero General Public License version 3 or later.
* See the COPYING-README file
*
*/

namespace OCA\News;


abstract class Response {

	private $headers;

	protected function __construct(){
		$this->headers = array();
	}

	/**
	 * Adds a new header to the response that will be called before the render
	 * function
	 * @param string header: the string that will be used in the header() function
	 */
	public function addHeader($header){
		array_push($this->headers, $header);
	}


	/**
	 * Renders all headers
	 */
	public function render(){
		foreach($this->headers as $value) {
			header($value);
		}
	}


}


/**
 * Prompts the user to download the a textfile
 */
class TextDownloadResponse extends Response {
	
	private $content;
	private $filename;
	private $contentType;

	/**
	 * Creates a response that prompts the user to download the file
	 * @param string $content: the content that should be written into the file
	 * @param string $filename: the name that the downloaded file should have
	 * @param string $contentType: the mimetype that the downloaded file should have
	 */
	public function __construct($content, $filename, $contentType){
		parent::__construct();
		$this->content = $content;
		$this->filename = $filename;
		$this->contentType = $contentType;

		$this->addHeader('Content-Disposition: attachment; filename="' . $filename . '"');
		$this->addHeader('Content-Type: ' . $contentType);
	}


	/**
	 * Simply sets the headers and returns the file contents
	 * @return the file contents
	 */
	public function render(){
		parent::render();
		return $this->content;
	}


}


/**
 * Response for a normal template
 */
class TemplateResponse extends Response {

	private $templateName;
	private $params;
	private $appName;
	private $renderAs;

	/**
	 * @param string $appName: the name of your app
	 * @param string $templateName: the name of the template
	 */
	public function __construct($appName, $templateName) {
		parent::__construct();
		$this->templateName = $templateName;
		$this->appName = $appName;
		$this->params = array();
		$this->renderAs = 'user';
	}


	/**
	 * @brief sets template parameters
	 * @param array $params: an array with key => value structure which sets template
	 *                       variables
	 */
	public function setParams($params){
		$this->params = $params;
	}


	/**
	 * @brief sets the template page
	 * @param string $renderAs: admin, user or blank: admin renders the page on
	 *                          the admin settings page, user renders a normal
	 *                          owncloud page, blank renders the template alone
	 */
	public function renderAs($renderAs='user'){
		$this->renderAs = $renderAs;
	}


	/**
	 * Returns the rendered html
	 * @return the rendered html
	 */
	public function render(){
		parent::render();

		if($this->renderAs === 'blank'){
			$template = new \OCP\Template($this->appName, $this->templateName);
		} else {
			$template = new \OCP\Template($this->appName, $this->templateName,
											$this->renderAs);
		}

		foreach($this->params as $key => $value){
			$template->assign($key, $value, false);
		}

		return $template->fetchPage();
	}

}


/**
 * A renderer for JSON calls
 */
class JSONResponse extends Response {

	private $name;
	private $data;
	private $appName;

	/**
	 * @param string $appName: the name of your app
	 */
	public function __construct($appName) {
		parent::__construct();
		$this->appName = $appName;
		$this->data = array();
		$this->error = false;
	}

	/**
	 * @brief sets values in the data json array
	 * @param array $params: an array with key => value structure which will be
	 *                       transformed to JSON
	 */
	public function setParams($params){
		$this->data['data'] = $params;
	}


	/**
	 * @brief in case we want to render an error message, also logs into the
	 *        owncloud log
	 * @param string $message: the error message
	 * @param string $file: the file where the error occured, use __FILE__ in
	 *                      the file where you call it
	 */
	public function setErrorMessage($msg, $file){
		$this->error = true;
		$this->data['msg'] = $msg;
		\OCP\Util::writeLog($this->appName, $file . ': ' . $msg, \OCP\Util::ERROR);
	}


	/**
	 * Returns the rendered json
	 * @return the rendered json
	 */
	public function render(){
		parent::render();

		ob_start();

		if($this->error){
		\OCP\JSON::error($this->data);
		} else {
		\OCP\JSON::success($this->data);
		}

		$result = ob_get_contents();
		ob_end_clean();

		return $result;
	}

}