/
ViewHelperFunctions.php
326 lines (271 loc) · 7.03 KB
/
ViewHelperFunctions.php
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
namespace Anax\View;
/**
* Define helper functions to include before processing the view template.
* The functions here are exposed to the view and can be used in the view.
*/
/**
* Shortcut to create an url for a static asset.
*
* @param string $url url to use when creating the url.
*
* @return string as resulting url.
*/
function asset($url = "")
{
global $di;
return $di->get("url")->asset($url);
}
/**
* Shortcut to create an url for routing in the framework.
*
* @param null|string $url url to use when creating the url.
*
* @return string as resulting url.
*/
function url($url = "")
{
global $di;
return $di->get("url")->create($url);
}
/**
* Shortcut to create an url for routing in the framework,
* use current controller as the base for the url.
*
* @param null|string $url url, relative controller mount point,
* to use when creating the url.
*
* @return string as resulting url.
*/
function urlController($url = "")
{
global $di;
$mount = rtrim($di->get("router")->getLastRoute(), "/");
return $di->get("url")->create("$mount/$url");
}
/**
* Render a view with an optional data set of variables.
*
* @param string $template the template file, or array
* @param array $data variables to make available to the
* view, default is empty
*
* @return void
*/
function renderView($template, $data = [])
{
global $di;
$view = new View();
$template = $di->get("view")->getTemplateFile($template);
$view->set($template, $data);
$view->render($di);
}
/**
* Check if the region in the view container has views to render.
*
* @param string $region to check
*
* @return boolean true or false
*/
function regionHasContent($region)
{
global $di;
return $di->get("view")->hasContent($region);
}
/**
* Render views, from the view container, in the region.
*
* @param string $region to render in
*
* @return boolean true or false
*/
function renderRegion($region)
{
global $di;
return $di->get("view")->render($region);
}
/**
* Create a class attribute from a string or array.
*
* @param string|array $args variable amount of classlists.
*
* @return string as complete class attribute
*/
function classList(...$args)
{
$classes = [];
foreach ($args as $arg) {
if (empty($arg)) {
continue;
} elseif (is_string($arg)) {
$arg = explode(" ", $arg);
}
$classes = array_merge($classes, $arg);
}
return "class=\"" . e(implode(" ", $classes)) . "\"";
}
/**
* Get current url, without querystring.
*
* @return string as resulting url.
*/
function currentUrl()
{
global $di;
return $di->get("request")->getCurrentUrl(false);
}
/**
* Get current route.
*
* @return string as resulting route.
*/
function currentRoute()
{
global $di;
return $di->get("request")->getRoute();
}
/**
* Redirect to another url.
*
* @param string $url to be redirected to.
*
* @return void.
*
* @SuppressWarnings(PHPMD.ExitExpression)
*
*/
function redirect(string $url) : void
{
global $di;
$di->get("response")->redirect($url)->send();
exit;
}
/**
* Escape HTML entities.
*
* @param string $string to be escaped.
*
* @return string as resulting route.
*/
function e($string) : string
{
return htmlentities($string);
}
/**
* Show variables/functions that are currently defined and can
* be used within the view. Call the function with get_defined_vars()
* as the parameter.
*
* @param array $variables should be the returned value from
* get_defined_vars()
* @param array $functions should be the returned value from
* get_defined_functions()
*
* @return string showing variables and functions.
*/
function showEnvironment($variables, $functions)
{
$all = array_keys($variables);
sort($all);
$res = "<pre>\n"
. "VIEW DEVELOPMENT INFORMATION\n"
. "----------------------------\n"
. "Variables available:\n"
. " (var_dump each for more information):\n";
foreach ($all as $var) {
$variable = $variables[$var];
$res .= "* $var (" . gettype($variable) . ")";
if (is_integer($variable) || is_double($variable)) {
$res .= ": $variable";
} elseif (is_string($variable)) {
$res .= ": \"$variable\"";
} elseif (is_bool($variable)) {
$res .= ": " . ( $variable ? "true" : "false" );
}
$res .= "\n";
}
$res .= "\nView helper functions available:\n (see " . __FILE__ . ")\n";
$namespace = strtolower(__NAMESPACE__);
$matches = array_filter(
$functions["user"],
function ($function) use ($namespace) {
return substr($function, 0, strlen($namespace)) === $namespace;
}
);
sort($matches);
$res .= "* " . implode(",\n* ", $matches);
$res .= "</pre>";
return $res;
}
/**
* Extract the publish or update date for the article.
*
* @param array $dates a collection of possible date values.
*
* @return array with values for showing the date.
*/
function getPublishedDate($dates)
{
$defaults = [
"revision" => [],
"published" => null,
"updated" => null,
"created" => null,
];
$dates = array_merge($defaults, $dates);
if ($dates["revision"]) {
return [t("Latest revision"), key($dates["revision"])];
} elseif ($dates["published"]) {
return [t("Published"), $dates["published"]];
} elseif ($dates["updated"]) {
return [t("Updated"), $dates["updated"]];
} elseif ($dates["created"]) {
return [t("Created"), $dates["created"]];
}
return [t("Missing pubdate."), null];
}
/**
* Load content from a route and return details to view.
*
* @param string $route to load content from.
*
* @return array with values to extract in view.
*/
function getContentForRoute($route)
{
global $di;
$content = $di->get("content")->contentForInternalRoute($route);
return $content->views["main"]["data"];
}
/**
* Wrap a HTML element with start and end.
*
* @param string $text with content
* @param string $tag HTML tag to search for
* @param string $start wrap start part
* @param string $end wrap end part
* @param number $count hits to search for
*
* @return array with values to extract in view.
*/
function wrapElementWithStartEnd($text, $tag, $start, $end, $count)
{
global $di;
return $di->get("textfilter")->wrapElementWithStartEnd($text, $tag, $start, $end, $count);
}
/**
* Wrap content of a HTML element with start and end.
*
* @param string $text with content
* @param string $tag HTML tag to search for
* @param string $start wrap start part
* @param string $end wrap end part
* @param number $count hits to search for
*
* @return array with values to extract in view.
*/
function wrapElementContentWithStartEnd($text, $tag, $start, $end, $count)
{
global $di;
return $di->get("textfilter")->wrapElementContentWithStartEnd($text, $tag, $start, $end, $count);
}