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