Rev 70 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
51 | PointedEar | 3 | namespace PointedEars\PHPX; |
4 | |||
27 | PointedEar | 5 | /** |
6 | * A general view handled by a controller according to the MVC pattern |
||
7 | * |
||
64 | PointedEar | 8 | * @author Thomas 'PointedEars' Lahn <php@PointedEars.de> |
27 | PointedEar | 9 | */ |
10 | class View |
||
11 | { |
||
12 | /** |
||
13 | * Default template resource path |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $_template = ''; |
||
51 | PointedEar | 18 | |
27 | PointedEar | 19 | /** |
20 | * Content that can be inserted in the template |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $_content = ''; |
||
51 | PointedEar | 25 | |
27 | PointedEar | 26 | /** |
27 | * Template variables. The variable name serves as item key, the item's value |
||
28 | * is the variable value. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $_template_vars = array(); |
||
51 | PointedEar | 33 | |
27 | PointedEar | 34 | /** |
70 | PointedEar | 35 | * Stylesheets to be inserted into the <code>head</code> element |
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $_stylesheets = array(); |
||
40 | |||
41 | /** |
||
42 | * Scripts to be inserted into the <code>head</code> or |
||
43 | * <code>body</code> element |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $_scripts = array(); |
||
48 | |||
49 | /** |
||
27 | PointedEar | 50 | * Creates a new view |
51 | * |
||
52 | * @param string $template |
||
53 | * Template resource path |
||
54 | */ |
||
70 | PointedEar | 55 | public function __construct ($template) |
27 | PointedEar | 56 | { |
57 | $this->_template = $template; |
||
58 | } |
||
51 | PointedEar | 59 | |
27 | PointedEar | 60 | /** |
61 | * Magic setter method used for defining template variables |
||
62 | * |
||
63 | * @param string $name |
||
64 | * Variable name |
||
65 | * @param mixed $value |
||
66 | * Variable value |
||
67 | */ |
||
70 | PointedEar | 68 | public function __set ($name, $value) |
27 | PointedEar | 69 | { |
70 | $this->_template_vars[$name] = $value; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Magic getter method used for retrieving values of template variables |
||
75 | * |
||
76 | * @param string $name |
||
77 | * Variable name |
||
78 | */ |
||
70 | PointedEar | 79 | public function __get ($name) |
27 | PointedEar | 80 | { |
81 | return $this->_template_vars[$name]; |
||
82 | } |
||
51 | PointedEar | 83 | |
27 | PointedEar | 84 | /** |
85 | * Returns <var>$v</var> with occurences of '&' (ampersand), '"' (double quote), |
||
86 | * "'" (single quote), '<' (less than), and '>' (greater than) replaced by their |
||
87 | * HTML character entity references, if any, or their numeric HTML character |
||
88 | * reference (as required primarily in HTML for attribute values and element |
||
89 | * content). |
||
90 | * |
||
91 | * @param mixed $value |
||
92 | */ |
||
70 | PointedEar | 93 | public function escape ($value) |
27 | PointedEar | 94 | { |
95 | if (is_array($value)) |
||
96 | { |
||
97 | return array_map(array('self', 'escape'), $value); |
||
98 | } |
||
99 | else if (is_object($value)) |
||
100 | { |
||
101 | if ($value instanceof AbstractModel) |
||
102 | { |
||
103 | foreach ($value->getPropertyVars() as $varName) |
||
104 | { |
||
105 | $value->$varName = self::escape($value->$varName); |
||
106 | } |
||
107 | } |
||
51 | PointedEar | 108 | |
27 | PointedEar | 109 | return $value; |
110 | } |
||
111 | else |
||
112 | { |
||
113 | if (is_string($value)) |
||
114 | { |
||
115 | $encoding = mb_detect_encoding($value); |
||
116 | if ($encoding === 'ASCII') |
||
117 | { |
||
118 | $encoding = 'ISO-8859-1'; |
||
119 | } |
||
120 | return htmlspecialchars($value, ENT_QUOTES, $encoding); |
||
121 | } |
||
51 | PointedEar | 122 | |
27 | PointedEar | 123 | return $value; |
124 | } |
||
125 | } |
||
51 | PointedEar | 126 | |
27 | PointedEar | 127 | /** |
128 | * Assigns a value to a template variable |
||
129 | * |
||
130 | * @param string $name |
||
131 | * Variable name |
||
132 | * @param mixed $value |
||
133 | * Variable value |
||
134 | * @param bool $escape |
||
135 | * If <code>true</code>, replace all potentially conflicting characters |
||
136 | * in <var>$value</var> with their HTML entity references. The default is |
||
137 | * <code>false</code>. |
||
138 | * @return mixed The assigned value (after possible HTML encoding) |
||
139 | * @see View::escape() |
||
140 | */ |
||
70 | PointedEar | 141 | public function assign ($name, $value, $escape = false) |
27 | PointedEar | 142 | { |
143 | if ($escape) |
||
144 | { |
||
145 | $value = $this->escape($value); |
||
146 | } |
||
147 | |||
148 | $this->$name = $value; |
||
149 | return $value; |
||
150 | } |
||
51 | PointedEar | 151 | |
27 | PointedEar | 152 | /** |
71 | PointedEar | 153 | * Adds an CSS resource (stylesheet) |
154 | * to the list of external stylesheets |
||
70 | PointedEar | 155 | * |
156 | * @param string $uri |
||
157 | * Stylesheet URI |
||
158 | * @param mixed $key (optional) |
||
159 | * Array key for the script. May be used later to exclude |
||
160 | * or include the code for a specific stylesheet. |
||
71 | PointedEar | 161 | * @return array |
162 | * The list of stylesheets |
||
70 | PointedEar | 163 | */ |
164 | public function addStylesheet ($uri, $key = null) |
||
165 | { |
||
166 | $stylesheets =& $this->_stylesheets; |
||
167 | |||
168 | if ($key !== null) |
||
169 | { |
||
170 | $stylesheets[$key] = $uri; |
||
171 | } |
||
172 | else |
||
173 | { |
||
174 | $stylesheets[] = $uri; |
||
175 | } |
||
71 | PointedEar | 176 | |
177 | return $stylesheets; |
||
70 | PointedEar | 178 | } |
179 | |||
180 | /** |
||
71 | PointedEar | 181 | * Adds several CSS resources (stylesheets) |
182 | * to the list of external stylesheets |
||
70 | PointedEar | 183 | * |
71 | PointedEar | 184 | * @param array $uris |
185 | * Stylesheet URIs |
||
186 | * @return array |
||
187 | * The list of stylesheets |
||
188 | */ |
||
189 | public function addStylesheets (array $uris) |
||
190 | { |
||
191 | $stylesheets = $this->_stylesheets; |
||
192 | |||
193 | foreach ($uris as $uri) |
||
194 | { |
||
195 | $stylesheets = $this->addStylesheet($uri); |
||
196 | } |
||
197 | |||
198 | return $stylesheets; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Adds an ECMAScript resource (script) |
||
203 | * to the list of external scripts |
||
204 | * |
||
70 | PointedEar | 205 | * @param string $uri |
206 | * Script URI |
||
207 | * @param mixed $key (optional) |
||
208 | * Array key for the script. May be used later to exclude |
||
209 | * or include the code for a specific script. |
||
210 | */ |
||
211 | public function addScript ($uri, $key = null) |
||
212 | { |
||
213 | $scripts =& $this->_scripts; |
||
214 | |||
215 | if ($key !== null) |
||
216 | { |
||
217 | $scripts[$key] = $uri; |
||
218 | } |
||
219 | else |
||
220 | { |
||
221 | $scripts[] = $uri; |
||
222 | } |
||
71 | PointedEar | 223 | |
224 | return $scripts; |
||
70 | PointedEar | 225 | } |
226 | |||
227 | /** |
||
71 | PointedEar | 228 | * Adds several ECMAScript resources (scripts) |
229 | * to the list of external scripts |
||
230 | * |
||
231 | * @param array $uris |
||
232 | * Script URIs |
||
233 | * @return array |
||
234 | * The list of scripts |
||
235 | */ |
||
236 | public function addScripts (array $uris) |
||
237 | { |
||
238 | $scripts = $this->_scripts; |
||
239 | |||
240 | foreach ($uris as $uri) |
||
241 | { |
||
242 | $scripts = $this->addScript($uri); |
||
243 | } |
||
244 | |||
245 | return $scripts; |
||
246 | } |
||
247 | |||
248 | /** |
||
27 | PointedEar | 249 | * Renders the view by including a template |
250 | * |
||
251 | * @param string $template |
||
252 | * Optional alternative template resource path. |
||
253 | * If not provided, the default template ($template property) will be used. |
||
254 | * @throws Exception if no template has been defined before |
||
255 | */ |
||
70 | PointedEar | 256 | public function render ($template = null, $content = null) |
27 | PointedEar | 257 | { |
51 | PointedEar | 258 | if (!is_null($content)) |
27 | PointedEar | 259 | { |
260 | ob_start(); |
||
261 | require_once $content; |
||
262 | $this->_content = ob_get_contents(); |
||
263 | ob_end_clean(); |
||
264 | } |
||
51 | PointedEar | 265 | |
27 | PointedEar | 266 | if (!is_null($template)) |
267 | { |
||
268 | require $template; |
||
269 | } |
||
270 | elseif ($this->_template) |
||
271 | { |
||
272 | require $this->_template; |
||
273 | } |
||
274 | else |
||
275 | { |
||
64 | PointedEar | 276 | throw new \Exception('No template defined'); |
27 | PointedEar | 277 | } |
278 | } |
||
51 | PointedEar | 279 | |
27 | PointedEar | 280 | /** |
70 | PointedEar | 281 | * Returns the code for including all stylesheets, |
282 | * with exclusions. |
||
283 | * |
||
284 | * @param array $exclusions (optional, future) |
||
285 | * The keys of the stylesheets that should be excluded |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getStylesheets ($exclusions = array()) |
||
289 | { |
||
290 | return ( |
||
291 | implode(PHP_EOL, array_map(function ($uri) { |
||
292 | return '<link rel="stylesheet" href="' . $this->escape($uri) . '">'; |
||
293 | }, array_diff_key($this->_stylesheets, array_flip($exclusions)))) |
||
294 | . PHP_EOL); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Returns the code for including a specific stylesheet |
||
299 | * |
||
300 | * @param mixed $key |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getStylesheet ($key) |
||
304 | { |
||
305 | return '<link rel="stylesheet" href="' |
||
306 | . $this->escape($this->_stylesheets[$key]) |
||
307 | . '">' . PHP_EOL; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Returns the code for including all stylesheets, |
||
312 | * with exclusions. |
||
313 | * |
||
314 | * @param array $exclusions (optional, future) |
||
315 | * The keys of the scripts that should be excluded. |
||
316 | * Usually you would specify those scripts that you |
||
317 | * want to include with {@link #getScript()}. |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getScripts ($exclusions = array()) |
||
321 | { |
||
322 | return ( |
||
323 | implode(PHP_EOL, array_map(function ($uri) { |
||
324 | return '<script type="text/javascript" src="' |
||
325 | . $this->escape($uri) |
||
326 | . '"></script>'; |
||
327 | }, array_diff_key($this->_scripts, array_flip($exclusions)))) |
||
328 | . PHP_EOL); |
||
329 | } |
||
330 | |||
331 | public function getScript ($key) |
||
332 | { |
||
333 | return '<link rel="stylesheet" href="' |
||
334 | . $this->escape($this->_scripts[$key]) |
||
335 | . '">' . PHP_EOL; |
||
336 | } |
||
337 | |||
338 | /** |
||
27 | PointedEar | 339 | * Returns the content for insertion into the template |
340 | */ |
||
70 | PointedEar | 341 | public function getContent () |
27 | PointedEar | 342 | { |
343 | return $this->_content; |
||
344 | } |
||
51 | PointedEar | 345 | |
27 | PointedEar | 346 | /** |
347 | * @param string[optional] $controller |
||
348 | * @param string[optional] $action |
||
349 | * @param int[optional] $id |
||
350 | * @see Application::getURL() |
||
351 | */ |
||
70 | PointedEar | 352 | public function getURL ($controller = null, $action = null, $id = null) |
27 | PointedEar | 353 | { |
354 | return Application::getInstance()->getURL($controller, $action, $id); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | ?> |