Rev 64 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 64 | Rev 70 | ||
|---|---|---|---|
| 1 | <?php
|
1 | <?php
|
| 2 | 2 | ||
| 3 | namespace PointedEars\PHPX; |
3 | namespace PointedEars\PHPX; |
| 4 | 4 | ||
| 5 | /**
|
5 | /**
|
| 6 | * A general view handled by a controller according to the MVC pattern
|
6 | * A general view handled by a controller according to the MVC pattern
|
| 7 | *
|
7 | *
|
| 8 | * @author Thomas 'PointedEars' Lahn <php@PointedEars.de>
|
8 | * @author Thomas 'PointedEars' Lahn <php@PointedEars.de>
|
| 9 | */
|
9 | */
|
| 10 | class View
|
10 | class View
|
| 11 | {
|
11 | {
|
| 12 | /**
|
12 | /**
|
| 13 | * Default template resource path
|
13 | * Default template resource path
|
| 14 | *
|
14 | *
|
| 15 | * @var string
|
15 | * @var string
|
| 16 | */
|
16 | */
|
| 17 | protected $_template = ''; |
17 | protected $_template = ''; |
| 18 | 18 | ||
| 19 | /**
|
19 | /**
|
| 20 | * Content that can be inserted in the template
|
20 | * Content that can be inserted in the template
|
| 21 | *
|
21 | *
|
| 22 | * @var string
|
22 | * @var string
|
| 23 | */
|
23 | */
|
| 24 | protected $_content = ''; |
24 | protected $_content = ''; |
| 25 | 25 | ||
| 26 | /**
|
26 | /**
|
| 27 | * Template variables. The variable name serves as item key, the item's value
|
27 | * Template variables. The variable name serves as item key, the item's value
|
| 28 | * is the variable value.
|
28 | * is the variable value.
|
| 29 | *
|
29 | *
|
| 30 | * @var array
|
30 | * @var array
|
| 31 | */
|
31 | */
|
| 32 | protected $_template_vars = array(); |
32 | protected $_template_vars = array(); |
| 33 | 33 | ||
| 34 | /**
|
34 | /**
|
| - | 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 | /**
|
|
| 35 | * Creates a new view
|
50 | * Creates a new view
|
| 36 | *
|
51 | *
|
| 37 | * @param string $template
|
52 | * @param string $template
|
| 38 | * Template resource path
|
53 | * Template resource path
|
| 39 | */
|
54 | */
|
| 40 | public function __construct($template) |
55 | public function __construct ($template) |
| 41 | {
|
56 | {
|
| 42 | $this->_template = $template; |
57 | $this->_template = $template; |
| 43 | }
|
58 | }
|
| 44 | 59 | ||
| 45 | /**
|
60 | /**
|
| 46 | * Magic setter method used for defining template variables
|
61 | * Magic setter method used for defining template variables
|
| 47 | *
|
62 | *
|
| 48 | * @param string $name
|
63 | * @param string $name
|
| 49 | * Variable name
|
64 | * Variable name
|
| 50 | * @param mixed $value
|
65 | * @param mixed $value
|
| 51 | * Variable value
|
66 | * Variable value
|
| 52 | */
|
67 | */
|
| 53 | public function __set($name, $value) |
68 | public function __set ($name, $value) |
| 54 | {
|
69 | {
|
| 55 | $this->_template_vars[$name] = $value; |
70 | $this->_template_vars[$name] = $value; |
| 56 | }
|
71 | }
|
| 57 | 72 | ||
| 58 | /**
|
73 | /**
|
| 59 | * Magic getter method used for retrieving values of template variables
|
74 | * Magic getter method used for retrieving values of template variables
|
| 60 | *
|
75 | *
|
| 61 | * @param string $name
|
76 | * @param string $name
|
| 62 | * Variable name
|
77 | * Variable name
|
| 63 | */
|
78 | */
|
| 64 | public function __get($name) |
79 | public function __get ($name) |
| 65 | {
|
80 | {
|
| 66 | return $this->_template_vars[$name]; |
81 | return $this->_template_vars[$name]; |
| 67 | }
|
82 | }
|
| 68 | 83 | ||
| 69 | /**
|
84 | /**
|
| 70 | * Returns <var>$v</var> with occurences of '&' (ampersand), '"' (double quote),
|
85 | * Returns <var>$v</var> with occurences of '&' (ampersand), '"' (double quote),
|
| 71 | * "'" (single quote), '<' (less than), and '>' (greater than) replaced by their
|
86 | * "'" (single quote), '<' (less than), and '>' (greater than) replaced by their
|
| 72 | * HTML character entity references, if any, or their numeric HTML character
|
87 | * HTML character entity references, if any, or their numeric HTML character
|
| 73 | * reference (as required primarily in HTML for attribute values and element
|
88 | * reference (as required primarily in HTML for attribute values and element
|
| 74 | * content).
|
89 | * content).
|
| 75 | *
|
90 | *
|
| 76 | * @param mixed $value
|
91 | * @param mixed $value
|
| 77 | */
|
92 | */
|
| 78 | public function escape($value) |
93 | public function escape ($value) |
| 79 | {
|
94 | {
|
| 80 | if (is_array($value)) |
95 | if (is_array($value)) |
| 81 | {
|
96 | {
|
| 82 | return array_map(array('self', 'escape'), $value); |
97 | return array_map(array('self', 'escape'), $value); |
| 83 | }
|
98 | }
|
| 84 | else if (is_object($value)) |
99 | else if (is_object($value)) |
| 85 | {
|
100 | {
|
| 86 | if ($value instanceof AbstractModel) |
101 | if ($value instanceof AbstractModel) |
| 87 | {
|
102 | {
|
| 88 | foreach ($value->getPropertyVars() as $varName) |
103 | foreach ($value->getPropertyVars() as $varName) |
| 89 | {
|
104 | {
|
| 90 | $value->$varName = self::escape($value->$varName); |
105 | $value->$varName = self::escape($value->$varName); |
| 91 | }
|
106 | }
|
| 92 | }
|
107 | }
|
| 93 | 108 | ||
| 94 | return $value; |
109 | return $value; |
| 95 | }
|
110 | }
|
| 96 | else
|
111 | else
|
| 97 | {
|
112 | {
|
| 98 | if (is_string($value)) |
113 | if (is_string($value)) |
| 99 | {
|
114 | {
|
| 100 | $encoding = mb_detect_encoding($value); |
115 | $encoding = mb_detect_encoding($value); |
| 101 | if ($encoding === 'ASCII') |
116 | if ($encoding === 'ASCII') |
| 102 | {
|
117 | {
|
| 103 | $encoding = 'ISO-8859-1'; |
118 | $encoding = 'ISO-8859-1'; |
| 104 | }
|
119 | }
|
| 105 | return htmlspecialchars($value, ENT_QUOTES, $encoding); |
120 | return htmlspecialchars($value, ENT_QUOTES, $encoding); |
| 106 | }
|
121 | }
|
| 107 | 122 | ||
| 108 | return $value; |
123 | return $value; |
| 109 | }
|
124 | }
|
| 110 | }
|
125 | }
|
| 111 | 126 | ||
| 112 | /**
|
127 | /**
|
| 113 | * Assigns a value to a template variable
|
128 | * Assigns a value to a template variable
|
| 114 | *
|
129 | *
|
| 115 | * @param string $name
|
130 | * @param string $name
|
| 116 | * Variable name
|
131 | * Variable name
|
| 117 | * @param mixed $value
|
132 | * @param mixed $value
|
| 118 | * Variable value
|
133 | * Variable value
|
| 119 | * @param bool $escape
|
134 | * @param bool $escape
|
| 120 | * If <code>true</code>, replace all potentially conflicting characters
|
135 | * If <code>true</code>, replace all potentially conflicting characters
|
| 121 | * in <var>$value</var> with their HTML entity references. The default is
|
136 | * in <var>$value</var> with their HTML entity references. The default is
|
| 122 | * <code>false</code>.
|
137 | * <code>false</code>.
|
| 123 | * @return mixed The assigned value (after possible HTML encoding)
|
138 | * @return mixed The assigned value (after possible HTML encoding)
|
| 124 | * @see View::escape()
|
139 | * @see View::escape()
|
| 125 | */
|
140 | */
|
| 126 | public function assign($name, $value, $escape = false) |
141 | public function assign ($name, $value, $escape = false) |
| 127 | {
|
142 | {
|
| 128 | if ($escape) |
143 | if ($escape) |
| 129 | {
|
144 | {
|
| 130 | $value = $this->escape($value); |
145 | $value = $this->escape($value); |
| 131 | }
|
146 | }
|
| 132 | 147 | ||
| 133 | $this->$name = $value; |
148 | $this->$name = $value; |
| 134 | return $value; |
149 | return $value; |
| 135 | }
|
150 | }
|
| 136 | 151 | ||
| 137 | /**
|
152 | /**
|
| - | 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 | /**
|
|
| 138 | * Renders the view by including a template
|
201 | * Renders the view by including a template
|
| 139 | *
|
202 | *
|
| 140 | * @param string $template
|
203 | * @param string $template
|
| 141 | * Optional alternative template resource path.
|
204 | * Optional alternative template resource path.
|
| 142 | * If not provided, the default template ($template property) will be used.
|
205 | * If not provided, the default template ($template property) will be used.
|
| 143 | * @throws Exception if no template has been defined before
|
206 | * @throws Exception if no template has been defined before
|
| 144 | */
|
207 | */
|
| 145 | public function render($template = null, $content = null) |
208 | public function render ($template = null, $content = null) |
| 146 | {
|
209 | {
|
| 147 | if (!is_null($content)) |
210 | if (!is_null($content)) |
| 148 | {
|
211 | {
|
| 149 | ob_start(); |
212 | ob_start(); |
| 150 | require_once $content; |
213 | require_once $content; |
| 151 | $this->_content = ob_get_contents(); |
214 | $this->_content = ob_get_contents(); |
| 152 | ob_end_clean(); |
215 | ob_end_clean(); |
| 153 | }
|
216 | }
|
| 154 | 217 | ||
| 155 | if (!is_null($template)) |
218 | if (!is_null($template)) |
| 156 | {
|
219 | {
|
| 157 | require $template; |
220 | require $template; |
| 158 | }
|
221 | }
|
| 159 | elseif ($this->_template) |
222 | elseif ($this->_template) |
| 160 | {
|
223 | {
|
| 161 | require $this->_template; |
224 | require $this->_template; |
| 162 | }
|
225 | }
|
| 163 | else
|
226 | else
|
| 164 | {
|
227 | {
|
| 165 | throw new \Exception('No template defined'); |
228 | throw new \Exception('No template defined'); |
| 166 | }
|
229 | }
|
| 167 | }
|
230 | }
|
| 168 | 231 | ||
| 169 | /**
|
232 | /**
|
| - | 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 | /**
|
|
| 170 | * Returns the content for insertion into the template
|
291 | * Returns the content for insertion into the template
|
| 171 | */
|
292 | */
|
| 172 | public function getContent() |
293 | public function getContent () |
| 173 | {
|
294 | {
|
| 174 | return $this->_content; |
295 | return $this->_content; |
| 175 | }
|
296 | }
|
| 176 | 297 | ||
| 177 | /**
|
298 | /**
|
| 178 | * @param string[optional] $controller
|
299 | * @param string[optional] $controller
|
| 179 | * @param string[optional] $action
|
300 | * @param string[optional] $action
|
| 180 | * @param int[optional] $id
|
301 | * @param int[optional] $id
|
| 181 | * @see Application::getURL()
|
302 | * @see Application::getURL()
|
| 182 | */
|
303 | */
|
| 183 | public function getURL($controller = null, $action = null, $id = null) |
304 | public function getURL ($controller = null, $action = null, $id = null) |
| 184 | {
|
305 | {
|
| 185 | return Application::getInstance()->getURL($controller, $action, $id); |
306 | return Application::getInstance()->getURL($controller, $action, $id); |
| 186 | }
|
307 | }
|
| 187 | }
|
308 | }
|
| 188 | 309 | ||
| 189 | ?>
|
310 | ?>
|