1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\View; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait with view helpers, provided by the ThemeEngine to the views. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
trait THelpers |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Render a view with an optional data set of variables. |
13
|
|
|
* |
14
|
|
|
* @param string $template the template file, or array |
15
|
|
|
* @param array $data variables to make available to the |
16
|
|
|
* view, default is empty |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function renderView($template, $data = []) |
21
|
|
|
{ |
22
|
|
|
$template = $this->di->get("views")->getTemplateFile($template); |
|
|
|
|
23
|
|
|
$view = $this->di->get("view"); |
24
|
|
|
$view->setDI($this->di); |
25
|
|
|
$view->set($template, $data); |
26
|
|
|
$view->render(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a class attribute from a string or array. |
33
|
|
|
* |
34
|
|
|
* @param string|array $args variable amount of classlists. |
35
|
|
|
* |
36
|
|
|
* @return string as complete class attribute |
37
|
|
|
*/ |
38
|
5 |
|
public function classList(...$args) |
39
|
|
|
{ |
40
|
5 |
|
$classes = []; |
41
|
|
|
|
42
|
5 |
|
foreach ($args as $arg) { |
43
|
5 |
|
if (empty($arg)) { |
44
|
2 |
|
continue; |
45
|
4 |
|
} elseif (is_string($arg)) { |
46
|
4 |
|
$arg = explode(" ", $arg); |
47
|
4 |
|
} |
48
|
4 |
|
$classes = array_merge($classes, $arg); |
49
|
5 |
|
} |
50
|
|
|
|
51
|
5 |
|
return "class=\"" . implode(" ", $classes) . "\""; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create an url for a static asset. |
58
|
|
|
* |
59
|
|
|
* @param string $uri part of uri to use when creating an url. |
60
|
|
|
* |
61
|
|
|
* @return string as resulting url. |
62
|
|
|
*/ |
63
|
|
|
public function asset($uri = null) |
64
|
|
|
{ |
65
|
|
|
return $this->di->get("url")->asset($uri); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create an url and prepending the baseUrl. |
72
|
|
|
* |
73
|
|
|
* @param string $uri part of uri to use when creating an url. "" or null |
74
|
|
|
* means baseurl to current frontcontroller. |
75
|
|
|
* |
76
|
|
|
* @return string as resulting url. |
77
|
|
|
*/ |
78
|
|
|
public function url($uri = null) |
79
|
|
|
{ |
80
|
|
|
return $this->di->get("url")->create($uri); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get current url, without querystring. |
87
|
|
|
* |
88
|
|
|
* @return string as resulting url. |
89
|
|
|
*/ |
90
|
|
|
public function currentUrl() |
91
|
|
|
{ |
92
|
|
|
return $this->di->get("request")->getCurrentUrl(false); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if the region in the view container has views to render. |
99
|
|
|
* |
100
|
|
|
* @param string $region to check |
101
|
|
|
* |
102
|
|
|
* @return boolean true or false |
103
|
|
|
*/ |
104
|
|
|
public function regionHasContent($region) |
105
|
|
|
{ |
106
|
|
|
return $this->di->get("views")->hasContent($region); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Render views, from the view container, in the region. |
113
|
|
|
* |
114
|
|
|
* @param string $region to render in |
115
|
|
|
* |
116
|
|
|
* @return boolean true or false |
117
|
|
|
*/ |
118
|
|
|
public function renderRegion($region) |
119
|
|
|
{ |
120
|
|
|
$this->di->get("views")->render($region); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Load content from a route and return details to view. |
127
|
|
|
* |
128
|
|
|
* @param string $route to load content from. |
129
|
|
|
* |
130
|
|
|
* @return array with values to extract in view. |
131
|
|
|
*/ |
132
|
|
|
public function getContentForRoute($route) |
133
|
|
|
{ |
134
|
|
|
$content = $this->di->get("content")->contentForInternalRoute($route); |
135
|
|
|
return $content->views["main"]["data"]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Wrap a HTML element with start and end. |
142
|
|
|
* |
143
|
|
|
* @param string $text with content |
144
|
|
|
* @param string $tag HTML tag to search for |
145
|
|
|
* @param string $start wrap start part |
146
|
|
|
* @param string $end wrap end part |
147
|
|
|
* @param number $count hits to search for |
148
|
|
|
* |
149
|
|
|
* @return array with values to extract in view. |
150
|
|
|
*/ |
151
|
|
|
public function wrapElementWithStartEnd($text, $tag, $start, $end, $count) |
152
|
|
|
{ |
153
|
|
|
return $this->di->get("textFilter")->wrapElementWithStartEnd($text, $tag, $start, $end, $count); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Wrap content of a HTML element with start and end. |
160
|
|
|
* |
161
|
|
|
* @param string $text with content |
162
|
|
|
* @param string $tag HTML tag to search for |
163
|
|
|
* @param string $start wrap start part |
164
|
|
|
* @param string $end wrap end part |
165
|
|
|
* @param number $count hits to search for |
166
|
|
|
* |
167
|
|
|
* @return array with values to extract in view. |
168
|
|
|
*/ |
169
|
|
|
public function wrapElementContentWithStartEnd($text, $tag, $start, $end, $count) |
170
|
|
|
{ |
171
|
|
|
return $this->di->get("textFilter")->wrapElementContentWithStartEnd($text, $tag, $start, $end, $count); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Extrat the publish or update date for the article. |
178
|
|
|
* |
179
|
|
|
* @param array $dates a collection of possible date values. |
180
|
|
|
* |
181
|
|
|
* @return array with values for showing the date. |
182
|
|
|
*/ |
183
|
|
|
public function getPublishedDate($dates) |
184
|
|
|
{ |
185
|
|
|
$defaults = [ |
186
|
|
|
"revision" => [], |
187
|
|
|
"published" => null, |
188
|
|
|
"updated" => null, |
189
|
|
|
"created" => null, |
190
|
|
|
]; |
191
|
|
|
$dates = array_merge($defaults, $dates); |
192
|
|
|
|
193
|
|
|
if ($dates["revision"]) { |
194
|
|
|
return [t("Latest revision"), key($dates["revision"])]; |
195
|
|
|
} elseif ($dates["published"]) { |
196
|
|
|
return [t("Published"), $dates["published"]]; |
197
|
|
|
} elseif ($dates["updated"]) { |
198
|
|
|
return [t("Updated"), $dates["updated"]]; |
199
|
|
|
} elseif ($dates["created"]) { |
200
|
|
|
return [t("Created"), $dates["created"]]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return [t("Missing pubdate."), null]; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: