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