summaryrefslogtreecommitdiffstats
path: root/controllers/controller.php
blob: 43487f0ad26ef6a2cf488658cf55a24e75615c4c (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
<?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 $trans;
    private $safeParams;

    public function __construct(){
        $this->trans = \OC_L10N::get('news');
        $this->safeParams = array();
    }


    protected function addScript($name){
        \OCP\Util::addScript('news', $name);
    }


    protected function addStyle($name){
        \OCP\Util::addStyle('news', $name);
    }


    protected function add3rdPartyScript($name){
        \OCP\Util::addScript('news/3rdparty', $name);
    }


    protected function add3rdPartyStyle($name){
        \OCP\Util::addStyle('news/3rdparty', $name);
    }


    /**
     * Shortcut for setting a user defined value
     * @param $key the key under which the value is being stored
     * @param $value the value that you want to store
     */
    protected function setUserValue($key, $value){
        \OCP\Config::setUserValue($this->userId, 'news', $key, $value);
    }


    /**
     * Shortcut for getting a user defined value
     * @param $key the key under which the value is being stored
     */
    protected function getUserValue($key){
        return \OCP\Config::getUserValue($this->userId, 'news', $key);
    }


    /**
     * Renders a renderer and sets the csrf check and logged in check to true
     * @param Renderer $renderer: the render which should be used to render the page
     */
    protected function render(Renderer $renderer){
        $renderer->render();
    }


    /**
     * Binds variables to the template and prints it
     * @param $templateName the name of the template
     * @param $arguments an array with arguments in $templateVar => $content
     * @param $safeParams template parameters which should not be escaped
     * @param $fullPage if true, it will render a full page, otherwise only a part
     *                  defaults to true
     */
    protected function renderTemplate($templateName, $arguments=array(), 
                                      $safeParams=array(), $fullPage=true){
        $renderer = new TemplateRenderer($templateName, $fullPage);
        $renderer->bind($arguments);
        $renderer->bindSafe($safeParams);
        $this->render($renderer);
    }

    /**
     * Binds variables to a JSON array and prints it
     * @param $arguments an array with arguments in $key => $value
     * @param $error: Empty by default. If set, a log message written and the
     *                $error will be sent to the client
     */
    protected function renderJSON($arguments=array(), $error=""){
        $renderer = new JSONRenderer($error);
        $renderer->bind($arguments);
        $this->render($renderer);
    }


}


/**
 * Renderers
 */
interface Renderer {
    public function render();
    public function bind($params);
}


/**
 * Used to render a normal template
 */
class TemplateRenderer implements Renderer {

    private $safeParams = array();
    private $template;

    /**
     * @param string $name: the template which we want to render
     * @param $fullPage: if the page should be included into the standard page
     */
    public function __construct($name, $fullPage=true){
        if($fullPage){
            $this->template = new \OCP\Template('news', $name, 'user');
        } else {
            $this->template = new \OCP\Template('news', $name);
        }
    }


    /**
     * @brief binds parameters to the renderer which shouldnt be escaped
     * @param array $params: an array of the form $doNotEscape => true
     */
    public function bindSafe($params){
        $this->safeParams = $params;
    }


    /**
     * Bind parameters to the template
     * @param array $params: an array of the form $key => value which will be used
     *                       for access in templates
     */
    public function bind($params){
        foreach($params as $key => $value){
            if(array_key_exists($key, $this->safeParams)) {
                $this->template->assign($key, $value, false);
            } else {
                $this->template->assign($key, $value);
            }
        }
    }


    /**
     * Print the page
     */
    public function render(){
        $this->template->printPage();
    }


}



/**
 * Use this to render JSON calls
 */
class JSONRenderer implements Renderer {

    private $params;

    /**
     * @param string $error: if empty a success is sent, otherwise an error message
     *                       will be logged
     */
    public function __construct($error){
        $this->error = $error;
    }


    /**
     * Bind parameters to the template
     * @param array $params: an array which will be converted to JSON
     */
    public function bind($params){
        $this->params = $params;
    }


    /**
     * Print the json array
     */
    public function render(){
        if($this->error === ""){
            OCP\JSON::success($this->params);        
        } else {
            OCP\JSON::error(array(
                                'data' => array('message' => $l->t('An error occured: ') . $error)
                            )
            );
            OCP\Util::writeLog('news',$_SERVER['REQUEST_URI'] . 'Error: '. $error, OCP\Util::ERROR);
            exit();
        }
        
    }

    
}