1 | <?php |
||
2 | /** |
||
3 | * @package toolkit |
||
4 | */ |
||
5 | /** |
||
6 | * HTMLPage extends the Page class to provide an object representation |
||
7 | * of a Symphony backend page. |
||
8 | */ |
||
9 | |||
10 | class HTMLPage extends Page |
||
11 | { |
||
12 | /** |
||
13 | * An XMLElement object for the `<html>` element. This is the parent |
||
14 | * DOM element for all other elements on the output page. |
||
15 | * @var XMLElement |
||
16 | */ |
||
17 | public $Html = null; |
||
18 | |||
19 | /** |
||
20 | * An XMLElement object for the `<head>` |
||
21 | * @var XMLElement |
||
22 | */ |
||
23 | public $Head = null; |
||
24 | |||
25 | /** |
||
26 | * An XMLElement object for the `<body>` |
||
27 | * @var XMLElement |
||
28 | */ |
||
29 | public $Body = null; |
||
30 | |||
31 | /** |
||
32 | * An XMLElement object for the `<form>`. Most Symphony backend pages |
||
33 | * are contained within a main form |
||
34 | * @var XMLElement |
||
35 | */ |
||
36 | public $Form = null; |
||
37 | |||
38 | /** |
||
39 | * This holds all the elements that will eventually be in the `$Head`. |
||
40 | * This allows extensions to add elements at certain indexes so |
||
41 | * resource dependancies can be met, and duplicates can be removed. |
||
42 | * Defaults to an empty array. |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $_head = array(); |
||
46 | |||
47 | /** |
||
48 | * Accessor function for `$this->_head`. Returns all the XMLElements that are |
||
49 | * about to be added to `$this->Head`. |
||
50 | * |
||
51 | * @since Symphony 2.3.3 |
||
52 | * @return array |
||
53 | */ |
||
54 | public function Head() |
||
55 | { |
||
56 | return $this->_head; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Constructor for the HTMLPage. Intialises the class variables with |
||
61 | * empty instances of XMLElement |
||
62 | */ |
||
63 | public function __construct() |
||
64 | { |
||
65 | parent::__construct(); |
||
66 | |||
67 | $this->Html = new XMLElement('html'); |
||
68 | $this->Html->setIncludeHeader(false); |
||
69 | |||
70 | $this->Head = new XMLElement('head'); |
||
71 | |||
72 | $this->Body = new XMLElement('body'); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Setter function for the `<title>` of a backend page. Uses the |
||
77 | * `addElementToHead()` function to place into the `$this->_head` array. |
||
78 | * Makes sure that only one title can be set. |
||
79 | * |
||
80 | * @see addElementToHead() |
||
81 | * @param string $title |
||
82 | * @return int |
||
83 | * Returns the position that the title has been set in the $_head |
||
84 | */ |
||
85 | public function setTitle($title) |
||
86 | { |
||
87 | return $this->addElementToHead( |
||
88 | new XMLElement('title', $title), |
||
89 | null, |
||
90 | false |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * The generate function calls the `__build()` function before appending |
||
96 | * all the current page's headers and then finally calling the `$Html's` |
||
97 | * generate function which generates a HTML DOM from all the |
||
98 | * XMLElement children. |
||
99 | * |
||
100 | * @param null $page |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
101 | * @return string |
||
102 | */ |
||
103 | public function generate($page = null) |
||
0 ignored issues
–
show
|
|||
104 | { |
||
105 | $this->__build(); |
||
106 | parent::generate($page); |
||
107 | return $this->Html->generate(true); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Called when page is generated, this function appends the `$Head`, |
||
112 | * `$Form` and `$Body` elements to the `$Html`. |
||
113 | * |
||
114 | * @see __generateHead() |
||
115 | */ |
||
116 | protected function __build() |
||
117 | { |
||
118 | $this->__generateHead(); |
||
119 | $this->Html->appendChild($this->Head); |
||
120 | $this->Html->appendChild($this->Body); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Sorts the `$this->_head` elements by key, then appends them to the |
||
125 | * `$Head` XMLElement in order. |
||
126 | */ |
||
127 | protected function __generateHead() |
||
128 | { |
||
129 | ksort($this->_head); |
||
130 | |||
131 | foreach ($this->_head as $position => $obj) { |
||
132 | if (is_object($obj)) { |
||
133 | $this->Head->appendChild($obj); |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Adds an XMLElement to the `$this->_head` array at a desired position. |
||
140 | * If no position is given, the object will be added to the end |
||
141 | * of the `$this->_head` array. If that position is already taken, it will |
||
142 | * add the object at the next available position. |
||
143 | * |
||
144 | * @see toolkit.General#array_find_available_index() |
||
145 | * @param XMLElement $object |
||
146 | * @param integer $position |
||
147 | * Defaults to null which will put the `$object` at the end of the |
||
148 | * `$this->_head`. |
||
149 | * @param boolean $allowDuplicate |
||
150 | * If set to false, make this function check if there is already an XMLElement that as the same name in the head. |
||
151 | * Defaults to true. @since Symphony 2.3.2 |
||
152 | * @return integer |
||
153 | * Returns the position that the `$object` has been set in the `$this->_head` |
||
154 | */ |
||
155 | public function addElementToHead(XMLElement $object, $position = null, $allowDuplicate = true) |
||
0 ignored issues
–
show
|
|||
156 | { |
||
157 | // find the right position |
||
158 | if (($position && isset($this->_head[$position]))) { |
||
0 ignored issues
–
show
The expression
$position of type integer|null is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||
159 | $position = General::array_find_available_index($this->_head, $position); |
||
160 | } elseif (is_null($position)) { |
||
161 | if (count($this->_head) > 0) { |
||
162 | $position = max(array_keys($this->_head))+1; |
||
163 | } else { |
||
164 | $position = 0; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // check if we allow duplicate |
||
169 | if (!$allowDuplicate && !empty($this->_head)) { |
||
170 | $this->removeFromHead($object->getName()); |
||
171 | } |
||
172 | |||
173 | // append new element |
||
174 | $this->_head[$position] = $object; |
||
175 | |||
176 | return $position; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Given an elementName, this function will remove the corresponding |
||
181 | * XMLElement from the `$this->_head` |
||
182 | * |
||
183 | * @param string $elementName |
||
184 | */ |
||
185 | public function removeFromHead($elementName) |
||
186 | { |
||
187 | foreach ($this->_head as $position => $element) { |
||
188 | if ($element->getName() !== $elementName) { |
||
189 | continue; |
||
190 | } |
||
191 | |||
192 | $this->removeFromHeadByPosition($position); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Removes an item from `$this->_head` by it's index. |
||
198 | * |
||
199 | * @since Symphony 2.3.3 |
||
200 | * @param integer $position |
||
201 | */ |
||
202 | public function removeFromHeadByPosition($position) |
||
203 | { |
||
204 | if (isset($position, $this->_head[$position])) { |
||
205 | unset($this->_head[$position]); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Determines if two elements are duplicates based on an attribute and value |
||
211 | * |
||
212 | * @param string $path |
||
213 | * The value of the attribute |
||
214 | * @param string $attribute |
||
215 | * The attribute to check |
||
216 | * @return boolean |
||
217 | */ |
||
218 | public function checkElementsInHead($path, $attribute) |
||
219 | { |
||
220 | foreach ($this->_head as $element) { |
||
221 | if (basename($element->getAttribute($attribute)) == basename($path)) { |
||
222 | return true; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | return false; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Convenience function to add a `<script>` element to the `$this->_head`. By default |
||
231 | * the function will allow duplicates to be added to the `$this->_head`. A duplicate |
||
232 | * is determined by if the `$path` is unique. |
||
233 | * |
||
234 | * @param string $path |
||
235 | * The path to the script file |
||
236 | * @param integer $position |
||
237 | * The desired position that the resulting XMLElement will be placed |
||
238 | * in the `$this->_head`. Defaults to null which will append to the end. |
||
239 | * @param boolean $duplicate |
||
240 | * When set to false the function will only add the script if it doesn't |
||
241 | * already exist. Defaults to true which allows duplicates. |
||
242 | * @return integer |
||
243 | * Returns the position that the script has been set in the `$this->_head` |
||
244 | */ |
||
245 | public function addScriptToHead($path, $position = null, $duplicate = true) |
||
0 ignored issues
–
show
|
|||
246 | { |
||
247 | if ($duplicate === true || ($duplicate === false && $this->checkElementsInHead($path, 'src') === false)) { |
||
248 | $script = new XMLElement('script'); |
||
249 | $script->setSelfClosingTag(false); |
||
250 | $script->setAttributeArray(array('type' => 'text/javascript', 'src' => $path)); |
||
251 | |||
252 | return $this->addElementToHead($script, $position); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Convenience function to add a stylesheet to the `$this->_head` in a `<link>` element. |
||
258 | * By default the function will allow duplicates to be added to the `$this->_head`. |
||
259 | * A duplicate is determined by if the `$path` is unique. |
||
260 | * |
||
261 | * @param string $path |
||
262 | * The path to the stylesheet file |
||
263 | * @param string $type |
||
264 | * The media attribute for this stylesheet, defaults to 'screen' |
||
265 | * @param integer $position |
||
266 | * The desired position that the resulting XMLElement will be placed |
||
267 | * in the `$this->_head`. Defaults to null which will append to the end. |
||
268 | * @param boolean $duplicate |
||
269 | * When set to false the function will only add the script if it doesn't |
||
270 | * already exist. Defaults to true which allows duplicates. |
||
271 | * @return integer |
||
272 | * Returns the position that the stylesheet has been set in the `$this->_head` |
||
273 | */ |
||
274 | public function addStylesheetToHead($path, $type = 'screen', $position = null, $duplicate = true) |
||
0 ignored issues
–
show
|
|||
275 | { |
||
276 | if ($duplicate === true || ($duplicate === false && $this->checkElementsInHead($path, 'href') === false)) { |
||
277 | $link = new XMLElement('link'); |
||
278 | $link->setAttributeArray(array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => $type, 'href' => $path)); |
||
279 | |||
280 | return $this->addElementToHead($link, $position); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * This function builds a HTTP query string from `$_GET` parameters with |
||
286 | * the option to remove parameters with an `$exclude` array. Since Symphony 2.6.0 |
||
287 | * it is also possible to override the default filters on the resulting string. |
||
288 | * |
||
289 | * @link http://php.net/manual/en/filter.filters.php |
||
290 | * @param array $exclude |
||
291 | * A simple array with the keys that should be omitted in the resulting |
||
292 | * query string. |
||
293 | * @param integer $filters |
||
294 | * The resulting query string is parsed through `filter_var`. By default |
||
295 | * the options are FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH and |
||
296 | * FILTER_SANITIZE_STRING, but these can be overridden as desired. |
||
297 | * @return string |
||
298 | */ |
||
299 | public function __buildQueryString(array $exclude = array(), $filters = null) |
||
0 ignored issues
–
show
|
|||
300 | { |
||
301 | $exclude[] = 'page'; |
||
302 | if (is_null($filters)) { |
||
303 | $filters = FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_SANITIZE_STRING; |
||
304 | } |
||
305 | |||
306 | // Generate the full query string and then parse it back to an array |
||
307 | $pre_exclusion = http_build_query($_GET, null, '&'); |
||
308 | parse_str($pre_exclusion, $query); |
||
309 | |||
310 | // Remove the excluded keys from query string and then build |
||
311 | // the query string again |
||
312 | $post_exclusion = array_diff_key($query, array_fill_keys($exclude, true)); |
||
313 | |||
314 | $query = http_build_query($post_exclusion, null, '&'); |
||
315 | |||
316 | return filter_var(urldecode($query), $filters); |
||
317 | } |
||
318 | } |
||
319 |