summaryrefslogtreecommitdiffstats
path: root/lib/controller.php
blob: 292e25a18d85bf54ee9c8ae71d23a66fe767f24d (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
<?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;

class Controller {

	protected $userId;
	protected $appName;
	protected $request;
	protected $api;
	protected $trans;

	public function __construct($request, $api){
		$this->api = $api;
		$this->userId = $api->getUserId();
		$this->appName = $api->getAppName();
		$this->request = $request;
		$this->trans = $api->getTrans();
	}


	/**
	 * @brief lets you access post and get parameters by the index
	 * @param string $key: the key which you want to access in the $_POST or
	 *                     $_GET array. If both arrays store things under the same
	 *                     key, return the value in $_POST
	 * @param $default: the value that is returned if the key does not exist
	 * @return: the content of the array
	 */
	protected function params($key, $default=null){
		$postValue = $this->request->getPOST($key);
		$getValue = $this->request->getGET($key);
		
		if($postValue !== null){
			return $postValue;
		}

		if($getValue !== null){
			return $getValue;
		}

		return $default;
	}

	/**
	 * Shortcut for accessing an uploaded file through the $_FILES array
	 * @param string $key: the key that will be taken from the $_FILES array
	 * @return the file in the $_FILES element
	 */
	protected function getUploadedFile($key){
		return $this->request->getFILES($key);
	}


	/**
	 * Binds variables to the template and prints it
	 * The following values are always assigned: userId, trans
	 * @param $templateName the name of the template
	 * @param $arguments an array with arguments in $templateVar => $content
	 * @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
	 */
	protected function render($templateName, $arguments=array(),
							  $renderAs='user'){
		$response = new TemplateResponse($this->appName, $templateName);
		$response->setParams($arguments);
		$response->renderAs($renderAs);
		return $response;
	}


	/**
	 * @brief renders a json success
	 * @param array $params an array which will be converted to JSON
	 */
	protected function renderJSON($params=array()){
		$response = new JSONResponse($this->appName);
		$response->setParams($params);
		return $response;
	}


	/**
	 * @brief renders a json error
	 * @param string $msg: the error message
	 * @param string $file: the file that it occured in
	 * @param array $params an array which will be converted to JSON
	 */
	protected function renderJSONError($msg, $file="", $params=array()){
		$response = new JSONResponse($this->appName);
		$response->setParams($params);
		$response->setErrorMessage($msg, $file);
		return $response;
	}


}