Rev 64 | Go to most recent revision | 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 | /** |
70 | PointedEar | 153 | * Adds a CSS resource (stylesheet) to the list of external |
154 | * stylesheets |
||
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. |
||
161 | */ |
||
162 | public function addStylesheet ($uri, $key = null) |
||
163 | { |
||
164 | $stylesheets =& $this->_stylesheets; |
||
165 | |||
166 | if ($key !== null) |
||
167 | { |
||
168 | $stylesheets[$key] = $uri; |
||
169 | } |
||
170 | else |
||
171 | { |
||
172 | $stylesheets[] = $uri; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Adds an ECMAScript resource (script) to the list of external |
||
178 | * scripts |
||
179 | * |
||
180 | * @param string $uri |
||
181 | * Script URI |
||
182 | * @param mixed $key (optional) |
||
183 | * Array key for the script. May be used later to exclude |
||
184 | * or include the code for a specific script. |
||
185 | */ |
||
186 | public function addScript ($uri, $key = null) |
||
187 | { |
||
188 | $scripts =& $this->_scripts; |
||
189 | |||
190 | if ($key !== null) |
||
191 | { |
||
192 | $scripts[$key] = $uri; |
||
193 | } |
||
194 | else |
||
195 | { |
||
196 | $scripts[] = $uri; |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** |
||
27 | PointedEar | 201 | * Renders the view by including a template |
202 | * |
||
203 | * @param string $template |
||
204 | * Optional alternative template resource path. |
||
205 | * If not provided, the default template ($template property) will be used. |
||
206 | * @throws Exception if no template has been defined before |
||
207 | */ |
||
70 | PointedEar | 208 | public function render ($template = null, $content = null) |
27 | PointedEar | 209 | { |
51 | PointedEar | 210 | if (!is_null($content)) |
27 | PointedEar | 211 | { |
212 | ob_start(); |
||
213 | require_once $content; |
||
214 | $this->_content = ob_get_contents(); |
||
215 | ob_end_clean(); |
||
216 | } |
||
51 | PointedEar | 217 | |
27 | PointedEar | 218 | if (!is_null($template)) |
219 | { |
||
220 | require $template; |
||
221 | } |
||
222 | elseif ($this->_template) |
||
223 | { |
||
224 | require $this->_template; |
||
225 | } |
||
226 | else |
||
227 | { |
||
64 | PointedEar | 228 | throw new \Exception('No template defined'); |
27 | PointedEar | 229 | } |
230 | } |
||
51 | PointedEar | 231 | |
27 | PointedEar | 232 | /** |
70 | PointedEar | 233 | * Returns the code for including all stylesheets, |
234 | * with exclusions. |
||
235 | * |
||
236 | * @param array $exclusions (optional, future) |
||
237 | * The keys of the stylesheets that should be excluded |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getStylesheets ($exclusions = array()) |
||
241 | { |
||
242 | return ( |
||
243 | implode(PHP_EOL, array_map(function ($uri) { |
||
244 | return '<link rel="stylesheet" href="' . $this->escape($uri) . '">'; |
||
245 | }, array_diff_key($this->_stylesheets, array_flip($exclusions)))) |
||
246 | . PHP_EOL); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Returns the code for including a specific stylesheet |
||
251 | * |
||
252 | * @param mixed $key |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getStylesheet ($key) |
||
256 | { |
||
257 | return '<link rel="stylesheet" href="' |
||
258 | . $this->escape($this->_stylesheets[$key]) |
||
259 | . '">' . PHP_EOL; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Returns the code for including all stylesheets, |
||
264 | * with exclusions. |
||
265 | * |
||
266 | * @param array $exclusions (optional, future) |
||
267 | * The keys of the scripts that should be excluded. |
||
268 | * Usually you would specify those scripts that you |
||
269 | * want to include with {@link #getScript()}. |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getScripts ($exclusions = array()) |
||
273 | { |
||
274 | return ( |
||
275 | implode(PHP_EOL, array_map(function ($uri) { |
||
276 | return '<script type="text/javascript" src="' |
||
277 | . $this->escape($uri) |
||
278 | . '"></script>'; |
||
279 | }, array_diff_key($this->_scripts, array_flip($exclusions)))) |
||
280 | . PHP_EOL); |
||
281 | } |
||
282 | |||
283 | public function getScript ($key) |
||
284 | { |
||
285 | return '<link rel="stylesheet" href="' |
||
286 | . $this->escape($this->_scripts[$key]) |
||
287 | . '">' . PHP_EOL; |
||
288 | } |
||
289 | |||
290 | /** |
||
27 | PointedEar | 291 | * Returns the content for insertion into the template |
292 | */ |
||
70 | PointedEar | 293 | public function getContent () |
27 | PointedEar | 294 | { |
295 | return $this->_content; |
||
296 | } |
||
51 | PointedEar | 297 | |
27 | PointedEar | 298 | /** |
299 | * @param string[optional] $controller |
||
300 | * @param string[optional] $action |
||
301 | * @param int[optional] $id |
||
302 | * @see Application::getURL() |
||
303 | */ |
||
70 | PointedEar | 304 | public function getURL ($controller = null, $action = null, $id = null) |
27 | PointedEar | 305 | { |
306 | return Application::getInstance()->getURL($controller, $action, $id); |
||
307 | } |
||
308 | } |
||
309 | |||
310 | ?> |