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