1 <?php
2
3 4 5 6 7 8 9 10 11 12
13
14 namespace Slimore\Mvc;
15
16 use \Slimore\Debug as Debug;
17 use \Slimore\Mvc\Application as Application;
18
19 20 21 22 23 24
25
26 class Controller
27 {
28 29 30
31
32 protected $app;
33
34 35 36
37
38 protected $db;
39
40 41 42
43
44 protected $log;
45
46 47 48
49
50 protected $viewPath = '../app/views/';
51
52 53 54
55
56 protected $viewSuffix = '.php';
57
58 59 60
61
62 public function __construct()
63 {
64 $this->app = Application::getInstance();
65 $this->db = $this->app->db;
66 $this->log = $this->app->log;
67
68 $this->setViewPath($this->app->path . $this->moduleName . DIRECTORY_SEPARATOR . 'views');
69
70 $this->init();
71 }
72
73 74 75 76 77
78
79 protected function init()
80 {
81 }
82
83 84 85 86
87
88 protected function dbConnection($name = null)
89 {
90 return $this->db->getConnection($name);
91 }
92
93 94 95 96 97
98
99 public function json(array $array, $return = false)
100 {
101 $this->response->header('Content-Type', 'application/json');
102 $this->response->status(200);
103
104 $jsonpCallback = $this->app->request->get('callback', null);
105
106 $json = ($jsonpCallback !== null) ? $jsonpCallback . '('.json_encode($array).')' : json_encode($array);
107
108 if ($return) return $json;
109 else echo $json;
110 }
111
112 113 114 115 116 117 118
119
120 public function js($script, $wrap = false)
121 {
122 Debug::js($script, $wrap);
123 }
124
125 126 127 128 129 130 131
132
133 public function javascript($script, $wrap = false)
134 {
135 Debug::js($script, $wrap);
136 }
137
138 139 140 141 142 143
144
145 public function printr($array)
146 {
147 Debug::printr($array);
148 }
149
150 151 152 153 154 155 156
157
158 public function console($message, $type = "log")
159 {
160 Debug::console($message, $type);
161 }
162
163 164 165 166 167 168 169
170
171 public function gotoURL($url, $base = true)
172 {
173 if ($base) {
174 $url = $this->app->baseURL . $url;
175 }
176
177 $this->js('location.href="'.$url.'";');
178 }
179
180 181 182 183 184 185
186
187 protected function config($name)
188 {
189 return $this->app->config($name);
190 }
191
192 193 194 195 196 197
198
199 protected function redirect($path)
200 {
201 $path = str_replace('//', '/', $this->app->baseURL . $path);
202
203 $this->app->redirect($path);
204 }
205
206 207 208 209 210 211 212 213
214
215 protected function render($tpl, array $data = [])
216 {
217 $suffix = $this->app->config('template.suffix');
218 $suffix = (empty($suffix)) ? $this->viewSuffix : $suffix;
219 $tpl .= $suffix;
220
221 $this->app->render($tpl, $data);
222 }
223
224 225 226 227 228
229
230 protected function setViewPath($path)
231 {
232 $this->view->setTemplatesDirectory($path);
233 }
234
235 236 237 238 239
240
241 protected function getViewPath()
242 {
243 return $this->view->getTemplatesDirectory();
244 }
245
246 247 248 249 250 251
252
253 public function __get($name)
254 {
255 if ( property_exists ($this->app, $name ) || method_exists ($this->app, $name ))
256 {
257 return $this->app->$name;
258 }
259 }
260
261 262 263 264 265 266 267
268
269 public function __call($method, $args)
270 {
271 if ( method_exists ($this->app, $method ))
272 {
273 return $this->app->$method($args);
274 }
275 }
276
277 278 279 280 281 282 283
284
285 public static function __callStatic($method, $args)
286 {
287 $app = self::$app;
288
289 if ( method_exists ($app, $method ))
290 {
291 return $app::$method($args);
292 }
293 }
294 }