|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class GenericViewHelper |
|
4
|
|
|
* |
|
5
|
|
|
* @package Faulancer\View |
|
6
|
|
|
* @author Florian Knapp <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Faulancer\View; |
|
9
|
|
|
|
|
10
|
|
|
use Faulancer\Security\Csrf; |
|
11
|
|
|
use Faulancer\Service\Config; |
|
12
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
|
13
|
|
|
use Faulancer\Session\SessionManager; |
|
14
|
|
|
use Faulancer\Translate\Translator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class GenericViewHelper |
|
18
|
|
|
*/ |
|
19
|
|
|
class GenericViewHelper |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Instance of view controller |
|
24
|
|
|
* @var ViewController $view |
|
25
|
|
|
*/ |
|
26
|
|
|
private $view; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The block name |
|
30
|
|
|
* @var string $blockName |
|
31
|
|
|
*/ |
|
32
|
|
|
private $blockName; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The block content |
|
36
|
|
|
* @var string $blockContent |
|
37
|
|
|
*/ |
|
38
|
|
|
private $blockContent; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* ViewFunctions constructor. |
|
42
|
|
|
* @param ViewController $view |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(ViewController $view) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->view = $view; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Render view |
|
51
|
|
|
* @param string $template |
|
52
|
|
|
* @param array $variables |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function render(string $template = "", array $variables = []) |
|
56
|
|
|
{ |
|
57
|
|
|
$view = new ViewController(); |
|
58
|
|
|
$view->setTemplate( $template ); |
|
59
|
|
|
$view->setVariables( $variables ); |
|
60
|
|
|
return $view->render(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Set parent template |
|
65
|
|
|
* @param string $template |
|
66
|
|
|
*/ |
|
67
|
|
|
public function extendsTemplate(string $template) |
|
68
|
|
|
{ |
|
69
|
|
|
$view = new ViewController(); |
|
70
|
|
|
$view->setTemplate($template); |
|
71
|
|
|
$this->view->setExtendedTemplate($view); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Set block |
|
76
|
|
|
* @param string $name The block name |
|
77
|
|
|
*/ |
|
78
|
|
|
public function block(string $name = "") |
|
79
|
|
|
{ |
|
80
|
|
|
|
|
81
|
|
|
if (!empty($name)) { |
|
82
|
|
|
|
|
83
|
|
|
$this->blockName = $name; |
|
84
|
|
|
ob_start(); |
|
85
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
|
|
88
|
|
|
$this->blockContent = ob_get_flush(); |
|
89
|
|
|
$this->view->getExtendedTemplate()->setVariable($this->blockName, $this->blockContent); |
|
90
|
|
|
unset($this->blockName); |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return block contents |
|
98
|
|
|
* @param string $name The block name |
|
99
|
|
|
* @param string $default The default value |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function renderBlock(string $name, string $default = "") |
|
103
|
|
|
{ |
|
104
|
|
|
if($this->view->getVariable($name) == null) { |
|
105
|
|
|
return $default; |
|
106
|
|
|
} |
|
107
|
|
|
return trim($this->view->getVariable( $name )); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Strip slashes from value |
|
112
|
|
|
* @param string $string |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function escape(string $string) |
|
116
|
|
|
{ |
|
117
|
|
|
return stripslashes( strip_tags( $string ) ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get assets by type |
|
122
|
|
|
* @param $type |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getAssets($type) |
|
126
|
|
|
{ |
|
127
|
|
|
$result = ''; |
|
128
|
|
|
$pattern = ''; |
|
129
|
|
|
|
|
130
|
|
|
switch ($type) { |
|
131
|
|
|
|
|
132
|
|
|
case 'js': |
|
133
|
|
|
$pattern = '<script src="%s"></script>'; |
|
134
|
|
|
break; |
|
135
|
|
|
|
|
136
|
|
|
case 'css': |
|
137
|
|
|
$pattern = '<link rel="stylesheet" type="text/css" href="%s">'; |
|
138
|
|
|
break; |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$files = $this->view->getVariable('assets'.ucfirst($type)); |
|
143
|
|
|
|
|
144
|
|
|
foreach ($files AS $file) { |
|
|
|
|
|
|
145
|
|
|
$result .= sprintf($pattern, $file). "\n"; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $result; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Generate a CSRF token |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function generateCsrfToken() |
|
156
|
|
|
{ |
|
157
|
|
|
return Csrf::getToken(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get form error by input field |
|
162
|
|
|
* @param $field |
|
163
|
|
|
* @return bool|string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getFormError($field) |
|
166
|
|
|
{ |
|
167
|
|
|
$error = SessionManager::instance()->getFlashbagError($field); |
|
168
|
|
|
|
|
169
|
|
|
if (!empty($error)) { |
|
170
|
|
|
|
|
171
|
|
|
$result = '<div class="form-error ' . $field . '">'; |
|
172
|
|
|
|
|
173
|
|
|
foreach ($error as $err) { |
|
|
|
|
|
|
174
|
|
|
$result .= '<span>' . $this->translate($err['message']) . '</span>'; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$result .= '</div>'; |
|
178
|
|
|
|
|
179
|
|
|
return $result; |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Check if form error exists |
|
188
|
|
|
* @param $field |
|
189
|
|
|
* @return boolean |
|
190
|
|
|
*/ |
|
191
|
|
|
public function hasFormError($field) |
|
192
|
|
|
{ |
|
193
|
|
|
return SessionManager::instance()->hasFlashbagErrorsKey($field); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Get form data for specific input field |
|
198
|
|
|
* @param $key |
|
199
|
|
|
* @return array|null|string |
|
200
|
|
|
*/ |
|
201
|
|
|
public function getFormData($key) |
|
202
|
|
|
{ |
|
203
|
|
|
return SessionManager::instance()->getFlashbagFormData($key); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param string $name |
|
208
|
|
|
* @return string |
|
209
|
|
|
* @throws \Exception |
|
210
|
|
|
*/ |
|
211
|
|
|
public function route($name) |
|
212
|
|
|
{ |
|
213
|
|
|
/** @var Config $config */ |
|
214
|
|
|
$config = ServiceLocator::instance()->get(Config::class); |
|
215
|
|
|
$routes = require $config->get('routeFile'); |
|
216
|
|
|
|
|
217
|
|
|
foreach ($routes as $routeName => $data) { |
|
218
|
|
|
|
|
219
|
|
|
if ($routeName === $name) { |
|
220
|
|
|
return $data['path']; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
throw new \Exception('No route for name "' . $name . '" found'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Translate key |
|
230
|
|
|
* @param string $string The key which holds the translated value |
|
231
|
|
|
* @param array $value The variable content for the placeholder |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
public function translate($string, $value = []) |
|
235
|
|
|
{ |
|
236
|
|
|
$translator = new Translator(); |
|
237
|
|
|
return $translator->translate($string, $value); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.