Completed
Push — master ( 2e46fa...a517d4 )
by Mikael
03:41
created

ViewHelperFunctions.php ➔ redirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\View;
4
5
/**
6
 * Define helper functions to include before processing the view template.
7
 * The functions here are exposed to the view and can be used in the view.
8
 */
9
10
/**
11
 * Shortcut to create an url for a static asset.
12
 *
13
 * @param string $url url to use when creating the url.
14
 *
15
 * @return string as resulting url.
16
 */
17
function asset($url = "")
18
{
19
    global $di;
20
    return $di->get("url")->asset($url);
21
}
22
23
24
25
/**
26
 * Shortcut to create an url for routing in the framework.
27
 *
28
 * @param null|string $url url to use when creating the url.
29
 *
30
 * @return string as resulting url.
31
 */
32
function url($url = "")
33
{
34
    global $di;
35
    return $di->get("url")->create($url);
36
}
37
38
39
40
/**
41
 * Render a view with an optional data set of variables.
42
 *
43
 * @param string $template the template file, or array
44
 * @param array  $data     variables to make available to the
45
 *                         view, default is empty
46
 *
47
 * @return void
48
 */
49
function renderView($template, $data = [])
50
{
51
    global $di;
52
    $view = new View();
53
    $template = $di->get("view")->getTemplateFile($template);
54
    $view->set($template, $data);
55
    $view->render($di);
56
}
57
58
59
60
/**
61
 * Check if the region in the view container has views to render.
62
 *
63
 * @param string $region to check
64
 *
65
 * @return boolean true or false
66
 */
67
function regionHasContent($region)
68
{
69
    global $di;
70
    return $di->get("view")->hasContent($region);
71
}
72
73
74
75
/**
76
 * Render views, from the view container, in the region.
77
 *
78
 * @param string $region to render in
79
 *
80
 * @return boolean true or false
81
 */
82
function renderRegion($region)
83
{
84
    global $di;
85
    return $di->get("view")->render($region);
86
}
87
88
89
90
/**
91
 * Create a class attribute from a string or array.
92
 *
93
 * @param string|array $args variable amount of classlists.
94
 *
95
 * @return string as complete class attribute
96
 */
97
function classList(...$args)
98
{
99 5
    $classes = [];
100
101 5
    foreach ($args as $arg) {
102 5
        if (empty($arg)) {
103 2
            continue;
104 4
        } elseif (is_string($arg)) {
105 4
            $arg = explode(" ", $arg);
106
        }
107 4
        $classes = array_merge($classes, $arg);
108
    }
109
110 5
    return "class=\"" . implode(" ", $classes) . "\"";
111
}
112
113
114
115
/**
116
 * Get current url, without querystring.
117
 *
118
 * @return string as resulting url.
119
 */
120
function currentUrl()
121
{
122
    global $di;
123
    return $di->get("request")->getCurrentUrl(false);
124
}
125
126
127
128
/**
129
 * Get current route.
130
 *
131
 * @return string as resulting route.
132
 */
133
function currentRoute()
134
{
135
    global $di;
136
    return $di->get("request")->getRoute();
137
}
138
139
140
141
/**
142
 * Redirect to another url.
143
 *
144
 * @param string $url to be redirected to.
145
 *
146
 * @return void.
0 ignored issues
show
Documentation introduced by
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
147
 *
148
 * @SuppressWarnings(PHPMD.ExitExpression)
149
 *
150
 */
151
function redirect(string $url) : void
152
{
153
    global $di;
154
    $di->get("response")->redirect($url)->send();
155
    exit;
156
}
157
158
159
160
/**
161
 * Escape HTML entities.
162
 *
163
 * @param string $string to be escaped.
164
 *
165
 * @return string as resulting route.
166
 */
167
function e($string) : string
168
{
169
    return htmlentities($string);
170
}
171
172
173
174
/**
175
 * Show variables/functions that are currently defined and can
176
 * be used within the view. Call the function with get_defined_vars()
177
 * as the parameter.
178
 *
179
 * @param array $variables should be the returned value from
180
 *                         get_defined_vars()
181
 * @param array $functions should be the returned value from
182
 *                         get_defined_functions()
183
 *
184
 * @return string showing variables and functions.
185
 */
186
function showEnvironment($variables, $functions)
187
{
188
    $all = array_keys($variables);
189
    sort($all);
190
    $res = "<pre>\n"
191
        . "VIEW DEVELOPMENT INFORMATION\n"
192
        . "----------------------------\n"
193
        . "Variables available:\n"
194
        . " (var_dump each for more information):\n";
195
    foreach ($all as $var) {
196
        $variable = $variables[$var];
197
        $res .= "* $var (" . gettype($variable) . ")";
198
        if (is_integer($variable) || is_double($variable)) {
199
            $res .= ": $variable";
200
        } elseif (is_string($variable)) {
201
            $res .= ": \"$variable\"";
202
        } elseif (is_bool($variable)) {
203
            $res .= ": " . ( $variable ? "true" : "false" );
204
        }
205
        $res .= "\n";
206
    }
207
208
    $res .= "\nView helper functions available:\n (see " . __FILE__ . ")\n";
209
    $namespace = strtolower(__NAMESPACE__);
210
    $matches = array_filter(
211
        $functions["user"],
212
        function ($function) use ($namespace) {
213
            return substr($function, 0, strlen($namespace)) === $namespace;
214
        }
215
    );
216
    sort($matches);
217
    $res .= "* " . implode(",\n* ", $matches);
218
    $res .= "</pre>";
219
220
    return $res;
221
}
222
223
224
225
/**
226
 * Extract the publish or update date for the article.
227
 *
228
 * @param array $dates a collection of possible date values.
229
 *
230
 * @return array with values for showing the date.
231
 */
232
function getPublishedDate($dates)
233
{
234
    $defaults = [
235
        "revision" => [],
236
        "published" => null,
237
        "updated" => null,
238
        "created" => null,
239
    ];
240
    $dates = array_merge($defaults, $dates);
241
242
    if ($dates["revision"]) {
243
        return [t("Latest revision"), key($dates["revision"])];
244
    } elseif ($dates["published"]) {
245
        return [t("Published"), $dates["published"]];
246
    } elseif ($dates["updated"]) {
247
        return [t("Updated"), $dates["updated"]];
248
    } elseif ($dates["created"]) {
249
        return [t("Created"), $dates["created"]];
250
    }
251
252
    return [t("Missing pubdate."), null];
253
}
254
255
256
257
/**
258
 * Load content from a route and return details to view.
259
 *
260
 * @param string $route to load content from.
261
 *
262
 * @return array with values to extract in view.
263
 */
264
function getContentForRoute($route)
265
{
266
    global $di;
267
    $content = $di->get("content")->contentForInternalRoute($route);
268
    return $content->views["main"]["data"];
269
}
270
271
272
273
/* OLDER VERSIONS MIGHT NEED IMPLEMENTING */
274
275
/**
276
 * Wrap a HTML element with start and end.
277
 *
278
 * @param string  $text  with content
279
 * @param string  $tag   HTML tag to search for
280
 * @param string  $start wrap start part
281
 * @param string  $end   wrap end part
282
 * @param number  $count hits to search for
283
 *
284
 * @return array with values to extract in view.
285
 */
286
// public function wrapElementWithStartEnd($text, $tag, $start, $end, $count)
287
// {
288
//     return $this->di->get("textFilter")->wrapElementWithStartEnd($text, $tag, $start, $end, $count);
289
// }
290
291
292
293
/**
294
 * Wrap content of a HTML element with start and end.
295
 *
296
 * @param string  $text  with content
297
 * @param string  $tag   HTML tag to search for
298
 * @param string  $start wrap start part
299
 * @param string  $end   wrap end part
300
 * @param number  $count hits to search for
301
 *
302
 * @return array with values to extract in view.
303
 */
304
// public function wrapElementContentWithStartEnd($text, $tag, $start, $end, $count)
305
// {
306
//     return $this->di->get("textFilter")->wrapElementContentWithStartEnd($text, $tag, $start, $end, $count);
307
// }
308