2 /* ***** BEGIN LICENSE BLOCK *****
4 * The contents of this file are subject to Austrian copyright reegulations
5 * ("Urheberrecht"); you may not use this file except in compliance with
7 * This contents and any derived work, if it gets distributed in any way,
8 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
9 * either express or implied.
11 * The Original Code is KaiRo's extended DOM document classes.
13 * The Initial Developer of the Original Code is
14 * KaiRo - Robert Kaiser.
15 * Portions created by the Initial Developer are Copyright (C) 2010
16 * the Initial Developer. All Rights Reserved.
18 * Contributor(s): Robert Kaiser <kairo@kairo.at>
20 * ***** END LICENSE BLOCK ***** */
22 class ExtendedDocument extends DOMDocument {
23 // ExtendedDocument PHP class
24 // this extends the general PHP DOM Document class to simplify some usual constructs
26 // function __construct([$version], [$encoding])
28 // construct a new DOM Document that uses our element definitions
31 // the default XML header
33 // private $xhtmldtype
34 // the XHTML doctype to use by default
36 // function appendElement($name, [$value])
37 // appends a DOMDocument::createElement() as a child of this document (see there for params)
38 // returns the new child
40 // function appendElementXML($name, $xmldata)
41 // appends a DOMDocument::createElement() with the given name as a child of this document,
42 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
43 // returns the new child
45 // function appendLink($target, [$value])
46 // appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
47 // returns the new child
49 // function appendFormDiv($action, $method, $name)
50 // appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
51 // returns an HTML <div> that is a child of the new child
53 // function appendInputHidden($name, $value)
54 // appends an ExtendedDocument::createElementInputHidden() as a child of this document (see there for params)
55 // returns the new child
57 // function appendInputText($name, $maxlength, $size, [$id], [$value])
58 // appends an ExtendedDocument::createElementInputText() as a child of this document (see there for params)
59 // returns the new child
61 // function appendInputRadio($name, $id, $value, $checked)
62 // appends an ExtendedDocument::createElementInputRadio() as a child of this document (see there for params)
63 // returns the new child
65 // function appendInputCheckbox($name, $id, $value, $checked)
66 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this document (see there for params)
67 // returns the new child
69 // function appendInputSubmit($value)
70 // appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
71 // returns the new child
73 // function appendLabel($for_id, $value)
74 // appends an ExtendedDocument::createElementLabel() as a child of this document (see there for params)
75 // returns the new child
77 // function appendText($text)
78 // appends a DOMDocument::createTextNode() as a child of this document (see there for params)
79 // returns the new child
81 // function appendHTMLMarkup($htmldata, [$parentNode])
82 // appends a representation of the HTML data as children of the given parent node, by default this document
85 // function appendXMLMarkup($xmldata, [$parentNode])
86 // appends a representation of the XML data as children of the given parent node, by default this document
89 // function appendJSElement($jsdata)
90 // appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
93 // function createElementLink($target, [$value])
94 // returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
96 // function createElementForm($action, $method, $name)
97 // returns an ExtendedElement that is an HTML <div> that is a child of an HTML <form>
98 // with the given action, method, and name
100 // function createElementInputHidden($name, $value)
101 // returns an ExtendedElement that is an HTML <input> of type 'hidden' with the given name and value
103 // function createElementInputText($name, $maxlength, $size, [$id], [$value])
104 // returns an ExtendedElement that is an HTML <input> of type 'text' with the given name, maxlength, size,
105 // and optionally id and value
107 // function createElementInputRadio($name, $id, $value, $checked)
108 // returns an ExtendedElement that is an HTML <input> of type 'radio' with the given name, id, value and
111 // function createElementInputCheckbox($name, $id, $value, $checked)
112 // returns an ExtendedElement that is an HTML <input> of type 'checkbox' with the given name, id, value and
115 // function createElementInputSubmit($value)
116 // returns an ExtendedElement that is an HTML <input> of type 'submit' with the given name and value
118 // function createElementLabel($for_id, $value)
119 // returns an ExtendedElement that is an HTML <label> with the given 'for' and value
121 // function createElementJS($jsdata)
122 // returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
124 function __construct($version = '1.0', $encoding = 'utf-8') {
125 // make sure the default DOMDocument constructor runs
126 parent::__construct($version, $encoding);
127 $this->registerNodeClass('DOMElement', 'ExtendedElement');
130 function appendElement($name, $value = '') {
131 return $this->appendChild($this->createElement($name, $value));
133 function appendElementXML($name, $xmldata) {
134 $aelem = $this->appendChild($this->createElement($name));
135 $aelem->appendXMLMarkup($xmldata);
136 //$aelem->appendChild($this->createXMLFragment($xmldata));
139 function appendLink($target, $value = '') {
140 return $this->appendChild($this->createElementLink($target, $value));
142 function appendFormDiv($action, $method, $name) {
143 $formelem = $this->appendChild($this->createElementForm($action, $method, $name));
144 return $formelem->appendElement('div');
146 function appendInputHidden($name, $value) {
147 return $this->appendChild($this->createElementInputHidden($name, $value));
149 function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
150 return $this->appendChild($this->createElementInputText($name, $maxlength, $size, $id, $value));
152 function appendInputRadio($name, $id, $value, $checked) {
153 return $this->appendChild($this->createElementInputRadio($name, $id, $value, $checked));
155 function appendInputCheckbox($name, $id, $value, $checked) {
156 return $this->appendChild($this->createElementInputCheckbox($name, $id, $value, $checked));
158 function appendInputSubmit($value) {
159 return $this->appendChild($this->createElementInputSubmit($value));
161 function appendLabel($for_id, $value) {
162 return $this->appendChild($this->createElementLabel($for_id, $value));
164 function appendText($text) {
165 return $this->appendChild($this->createTextNode($text));
167 function appendJSElement($jsdata) {
168 $this->appendChild($this->createElementJS($jsdata));
171 function appendHTMLMarkup($htmldata, $parentNode = null) {
172 if (is_null($parentNode)) { $parentNode =& $this; }
173 // XXX: just a workaround for now!
174 $parentNode->appendChild($this->createCDATASection($htmldata));
177 function appendXMLMarkup($xmldata, $parentNode = null) {
178 if (is_null($parentNode)) { $parentNode =& $this; }
179 $tmpdoc = new ExtendedDocument;
180 $tmpxml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
181 $tmpxml .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
182 $tmpxml .= '<root>'.$xmldata.'</root>';
183 $tmpdoc->loadXML($tmpxml);
184 foreach ($tmpdoc->getElementsByTagName('root')->item(0)->childNodes as $child) {
185 $parentNode->appendChild($this->importNode($child, true));
189 function createElementLink($target, $value = '') {
190 $link = $this->createElement('a', $value);
191 $link->setAttribute('href', $target);
195 function createElementForm($action, $method, $name) {
196 $formelem = $this->createElement('form');
197 $formelem->setAttribute('action', $action);
198 $formelem->setAttribute('method', $method);
199 $formelem->setAttribute('name', $name);
203 function createElementInputHidden($name, $value) {
204 $hidden = $this->createElement('input');
205 $hidden->setAttribute('type', 'hidden');
206 $hidden->setAttribute('name', $name);
207 $hidden->setAttribute('value', $value);
211 function createElementInputText($name, $maxlength, $size, $id = null, $value = null) {
212 $txfield = $this->createElement('input');
213 $txfield->setAttribute('type', 'text');
214 if (!is_null($id)) { $txfield->setAttribute('id', $id); }
215 $txfield->setAttribute('name', $name);
216 $txfield->setAttribute('maxlength', $maxlength);
217 $txfield->setAttribute('size', $size);
218 if (!is_null($value)) { $txfield->setAttribute('value', $value); }
222 function createElementInputRadio($name, $id, $value, $checked) {
223 $radio = $this->createElement('input');
224 $radio->setAttribute('type', 'radio');
225 $radio->setAttribute('name', $name);
226 $radio->setAttribute('id', $id);
227 $radio->setAttribute('value', $value);
228 if ($checked) { $radio->setAttribute('checked', ''); }
232 function createElementInputCheckbox($name, $id, $value, $checked) {
233 $cbox = $this->createElement('input');
234 $cbox->setAttribute('type', 'checkbox');
235 $cbox->setAttribute('name', $name);
236 $cbox->setAttribute('id', $id);
237 $cbox->setAttribute('value', $value);
238 if ($checked) { $cbox->setAttribute('checked', ''); }
242 function createElementInputSubmit($value) {
243 $submitbtn = $this->createElement('input');
244 $submitbtn->setAttribute('type', 'submit');
245 $submitbtn->setAttribute('value', $value);
249 function createElementLabel($for_id, $value) {
250 $label = $this->createElement('label', $value);
251 $label->setAttribute('for', $for_id);
255 function createElementJS($jsdata) {
256 $jselem = $this->createElement('script');
257 $jselem->setAttribute('type', 'text/javascript');
258 $jselem->appendChild($this->createCDATASection($jsdata));
263 class ExtendedElement extends DOMElement {
264 // ExtendedElement PHP class
265 // this extends the general PHP DOM Element class to simplify some usual constructs
267 // function appendElement($name, [$value])
268 // appends a DOMDocument::createElement() as a child of this element (see there for params)
269 // returns the new child
271 // function appendElementXML($name, $xmldata)
272 // appends a DOMDocument::createElement() with the given name as a child of this element,
273 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
274 // returns the new child
276 // function appendLink($target, [$value])
277 // appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
278 // returns the new child
280 // function appendFormDiv($action, $method, $name)
281 // appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
282 // returns an HTML <div> that is a child of the new child
284 // function appendInputHidden($name, $value)
285 // appends an ExtendedDocument::createElementInputHidden() as a child of this element (see there for params)
286 // returns the new child
288 // function appendInputText($name, $maxlength, $size, [$id], [$value])
289 // appends an ExtendedDocument::createElementInputText() as a child of this element (see there for params)
290 // returns the new child
292 // function appendInputRadio($name, $id, $value, $checked)
293 // appends an ExtendedDocument::createElementInputRadio() as a child of this element (see there for params)
294 // returns the new child
296 // function appendInputCheckbox($name, $id, $value, $checked)
297 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this element (see there for params)
298 // returns the new child
300 // function appendInputSubmit($value)
301 // appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
302 // returns the new child
304 // function appendLabel($for_id, $value)
305 // appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
306 // returns the new child
308 // function appendText($text)
309 // appends a DOMDocument::createTextNode() as a child of this element (see there for params)
310 // returns the new child
312 // function appendHTMLMarkup($htmldata)
313 // appends a representation of the HTML data as children of this element
316 // function appendXMLMarkup($xmldata)
317 // appends a representation of the XML data as children of this element
320 // function appendJSElement($jsdata)
321 // appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
324 function appendElement($name, $value = '') {
325 return $this->appendChild($this->ownerDocument->createElement($name, $value));
327 function appendElementXML($name, $xmldata) {
328 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
329 $aelem->appendXMLMarkup($xmldata);
332 function appendLink($target, $value = '') {
333 return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
335 function appendFormDiv($action, $method, $name) {
336 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
337 return $formelem->appendElement('div');
339 function appendInputHidden($name, $value) {
340 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
342 function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
343 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
345 function appendInputRadio($name, $id, $value, $checked) {
346 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
348 function appendInputCheckbox($name, $id, $value, $checked) {
349 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
351 function appendInputSubmit($value) {
352 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
354 function appendLabel($for_id, $value) {
355 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
357 function appendText($text) {
358 return $this->appendChild($this->ownerDocument->createTextNode($text));
360 function appendHTMLMarkup($htmldata) {
361 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
363 function appendXMLMarkup($xmldata) {
364 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
366 function appendJSElement($jsdata) {
367 $this->appendChild($this->ownerDocument->createElementJS($jsdata));