1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egzaminer; |
4
|
|
|
|
5
|
|
|
use Egzaminer\Roll\ExamsGroupModel; |
6
|
|
|
use Egzaminer\Themes\MaterialDesignLite; |
7
|
|
|
use Exception; |
8
|
|
|
use Twig_Environment; |
9
|
|
|
use Twig_Loader_Filesystem; |
10
|
|
|
|
11
|
|
|
class Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $container; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $data; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
* |
26
|
|
|
* @param array $container |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $container) |
29
|
|
|
{ |
30
|
|
|
$this->container = $container; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name Item name |
35
|
|
|
* |
36
|
|
|
* @return mixed Item from container |
37
|
|
|
*/ |
38
|
|
|
public function get($name) |
39
|
|
|
{ |
40
|
|
|
if (isset($this->container[$name])) { |
41
|
|
|
return $this->container[$name]; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $name Config name |
47
|
|
|
* |
48
|
|
|
* @return mixed Config value |
49
|
|
|
*/ |
50
|
|
|
public function config($name) |
51
|
|
|
{ |
52
|
|
|
if (isset($this->get('config')[$name])) { |
53
|
|
|
return $this->get('config')[$name]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get request variable. |
59
|
|
|
* |
60
|
|
|
* @param string $type Request type |
61
|
|
|
* @param string $name Index name. Null for all |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getFromRequest($type = 'get', $name = null) |
66
|
|
|
{ |
67
|
|
|
$request = $this->get('request'); |
68
|
|
|
|
69
|
|
|
// if unknow request type |
70
|
|
|
if (!isset($request[$type])) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// for get all indexes from type |
75
|
|
|
if (null === $name) { |
76
|
|
|
return $request[$type]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (isset($request[$type][$name])) { |
80
|
|
|
return $request[$type][$name]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function dir() |
88
|
|
|
{ |
89
|
|
|
return $this->get('dir'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check is user logged. |
94
|
|
|
* |
95
|
|
|
* @return bool Return true, if logged |
96
|
|
|
*/ |
97
|
|
|
public function isLogged() |
98
|
|
|
{ |
99
|
|
|
return $this->get('auth')->isLogged(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $type Message type |
104
|
|
|
* @param mixed $message Message content |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function setMessage($type = 'success', $message = 'Success') |
109
|
|
|
{ |
110
|
|
|
switch ($type) { |
111
|
|
|
case 'success': |
112
|
|
|
$this->get('flash')->success($message); |
113
|
|
|
break; |
114
|
|
|
case 'info': |
115
|
|
|
$this->get('flash')->info($message); |
116
|
|
|
break; |
117
|
|
|
case 'warning': |
118
|
|
|
$this->get('flash')->warning($message); |
119
|
|
|
break; |
120
|
|
|
case 'error': |
121
|
|
|
$this->get('flash')->error($message); |
122
|
|
|
break; |
123
|
|
|
|
124
|
|
|
default: |
125
|
|
|
$this->get('flash')->error($message); |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Redirect. |
132
|
|
|
* |
133
|
|
|
* @param string $path Path to redirect |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function redirect($path) |
138
|
|
|
{ |
139
|
|
|
header('Location: '.$this->dir().$path); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function terminate($code = 1) |
143
|
|
|
{ |
144
|
|
|
exit($code); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function selectMessagesTemplate() |
148
|
|
|
{ |
149
|
|
|
switch ($this->config('theme')) { |
150
|
|
|
case 'mdl': |
151
|
|
|
$this->get('flash')->setTemplate(new MaterialDesignLite()); |
152
|
|
|
break; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $template Template name |
158
|
|
|
* @param array $data Data to use in template |
159
|
|
|
*/ |
160
|
|
|
public function render($template, $data = []) |
161
|
|
|
{ |
162
|
|
|
$this->selectMessagesTemplate(); |
163
|
|
|
|
164
|
|
|
$data['version'] = $this->get('version'); |
165
|
|
|
$data['dir'] = $this->dir(); |
166
|
|
|
$data['flash'] = $this->get('flash')->display(); |
167
|
|
|
$data['headerTitle'] = isset($data['title']) ? $data['title'] : ''; |
168
|
|
|
$data['isLogged'] = $this->isLogged(); |
169
|
|
|
$data['siteTitle'] = $this->config('title'); |
170
|
|
|
$data['pageTitle'] = isset($data['title']) |
171
|
|
|
? $data['title'].' '.$this->config('title_divider').' '.$this->config('title') |
172
|
|
|
: $this->config('title'); |
173
|
|
|
|
174
|
|
|
$data['examsGroups'] = (new ExamsGroupModel($this->get('dbh')))->getExamsGroups(); |
175
|
|
|
|
176
|
|
|
$loader = new Twig_Loader_Filesystem( |
177
|
|
|
$this->get('rootDir').'/resources/themes/'.$this->config('theme').'/templates/' |
178
|
|
|
); |
179
|
|
|
$twig = new Twig_Environment($loader, [ |
180
|
|
|
'cache' => $this->config('cache') ? $this->get('rootDir').'/var/twig' : false, |
181
|
|
|
'debug' => $this->config('debug') ? true : false, |
182
|
|
|
]); |
183
|
|
|
|
184
|
|
|
try { |
185
|
|
|
echo $twig->render($template.'.twig', $data); |
186
|
|
|
} catch (Exception $e) { |
|
|
|
|
187
|
|
|
if ($this->config('debug')) { |
188
|
|
|
echo $e->getMessage(); |
189
|
|
|
} else { |
190
|
|
|
echo 'Error 500'; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.