convert everything to UTF-8, part 1: large groundwork
[php-utility-classes.git] / include / cbsm / util / document.php-class
1 <?php
2 /* ***** BEGIN LICENSE BLOCK *****
3  *
4  * The contents of this file are subject to Austrian copyright reegulations
5  * ("Urheberrecht"); you may not use this file except in compliance with
6  * those laws.
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.
10  *
11  * The Original Code is KaiRo's extended DOM document classes.
12  *
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.
17  *
18  * Contributor(s): Robert Kaiser <kairo@kairo.at>
19  *
20  * ***** END LICENSE BLOCK ***** */
21
22 class ExtendedDocument extends DOMDocument {
23   // ExtendedDocument PHP class
24   // this extends the general PHP DOM Document class to simplify some usual constructs
25   //
26   // function __construct([$version], [$encoding])
27   //   CONSTRUCTOR
28   //   construct a new DOM Document that uses our element definitions
29   //
30   // private $xmheader
31   //   the default XML header
32   //
33   // private $xhtmldtype
34   //   the XHTML doctype to use by default
35   //
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
39   //
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
44   //
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
48   //
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
52   //
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
56   //
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
60   //
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
64   //
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
68   //
69   // function appendInputSubmit($value)
70   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
71   //     returns the new child
72   //
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
76   //
77   // function appendText($text)
78   //   appends a DOMDocument::createTextNode() as a child of this document (see there for params)
79   //     returns the new child
80   //
81   // function appendHTMLMarkup($htmldata, [$parentNode])
82   //   appends a representation of the HTML data as children of the given parent node, by default this document
83   //     NO return value!
84   //
85   // function appendXMLMarkup($xmldata, [$parentNode])
86   //   appends a representation of the XML data as children of the given parent node, by default this document
87   //     NO return value!
88   //
89   // function appendJSElement($jsdata)
90   //   appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
91   //     NO return value!
92   //
93   // function createElementLink($target, [$value])
94   //   returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
95   //
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
99   //
100   // function createElementInputHidden($name, $value)
101   //   returns an ExtendedElement that is an HTML <input> of type 'hidden' with the given name and value
102   //
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
106   //
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
109   //   checked state
110   //
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
113   //   checked state
114   //
115   // function createElementInputSubmit($value)
116   //   returns an ExtendedElement that is an HTML <input> of type 'submit' with the given name and value
117   //
118   // function createElementLabel($for_id, $value)
119   //   returns an ExtendedElement that is an HTML <label> with the given 'for' and value
120   //
121   // function createElementJS($jsdata)
122   //   returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
123
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');
128   }
129
130   function appendElement($name, $value = '') {
131     return $this->appendChild($this->createElement($name, $value));
132   }
133   function appendElementXML($name, $xmldata) {
134     $aelem = $this->appendChild($this->createElement($name));
135     $aelem->appendXMLMarkup($xmldata);
136     //$aelem->appendChild($this->createXMLFragment($xmldata));
137     return $aelem;
138   }
139   function appendLink($target, $value = '') {
140     return $this->appendChild($this->createElementLink($target, $value));
141   }
142   function appendFormDiv($action, $method, $name) {
143     $formelem = $this->appendChild($this->createElementForm($action, $method, $name));
144     return $formelem->appendElement('div');
145   }
146   function appendInputHidden($name, $value) {
147     return $this->appendChild($this->createElementInputHidden($name, $value));
148   }
149   function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
150     return $this->appendChild($this->createElementInputText($name, $maxlength, $size, $id, $value));
151   }
152   function appendInputRadio($name, $id, $value, $checked) {
153     return $this->appendChild($this->createElementInputRadio($name, $id, $value, $checked));
154   }
155   function appendInputCheckbox($name, $id, $value, $checked) {
156     return $this->appendChild($this->createElementInputCheckbox($name, $id, $value, $checked));
157   }
158   function appendInputSubmit($value) {
159     return $this->appendChild($this->createElementInputSubmit($value));
160   }
161   function appendLabel($for_id, $value) {
162     return $this->appendChild($this->createElementLabel($for_id, $value));
163   }
164   function appendText($text) {
165     return $this->appendChild($this->createTextNode($text));
166   }
167   function appendJSElement($jsdata) {
168     $this->appendChild($this->createElementJS($jsdata));
169   }
170
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));
175   }
176
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));
186     }
187   }
188
189   function createElementLink($target, $value = '') {
190     $link = $this->createElement('a', $value);
191     $link->setAttribute('href', $target);
192     return $link;
193   }
194
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);
200     return $formelem;
201   }
202
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);
208     return $hidden;
209   }
210
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); }
219     return $txfield;
220   }
221
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', ''); }
229     return $radio;
230   }
231
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', ''); }
239     return $cbox;
240   }
241
242   function createElementInputSubmit($value) {
243     $submitbtn = $this->createElement('input');
244     $submitbtn->setAttribute('type', 'submit');
245     $submitbtn->setAttribute('value', $value);
246     return $submitbtn;
247   }
248
249   function createElementLabel($for_id, $value) {
250     $label = $this->createElement('label', $value);
251     $label->setAttribute('for', $for_id);
252     return $label;
253   }
254
255   function createElementJS($jsdata) {
256     $jselem = $this->createElement('script');
257     $jselem->setAttribute('type', 'text/javascript');
258     $jselem->appendChild($this->createCDATASection($jsdata));
259     return $jselem;
260   }
261 }
262
263 class ExtendedElement extends DOMElement {
264   // ExtendedElement PHP class
265   // this extends the general PHP DOM Element class to simplify some usual constructs
266   //
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
270   //
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
275   //
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
279   //
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
283   //
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
287   //
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
291   //
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
295   //
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
299   //
300   // function appendInputSubmit($value)
301   //   appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
302   //     returns the new child
303   //
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
307   //
308   // function appendText($text)
309   //   appends a DOMDocument::createTextNode() as a child of this element (see there for params)
310   //     returns the new child
311   //
312   // function appendHTMLMarkup($htmldata)
313   //   appends a representation of the HTML data as children of this element
314   //     NO return value!
315   //
316   // function appendXMLMarkup($xmldata)
317   //   appends a representation of the XML data as children of this element
318   //     NO return value!
319   //
320   // function appendJSElement($jsdata)
321   //   appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
322   //     NO return value!
323
324   function appendElement($name, $value = '') {
325     return $this->appendChild($this->ownerDocument->createElement($name, $value));
326   }
327   function appendElementXML($name, $xmldata) {
328     $aelem = $this->appendChild($this->ownerDocument->createElement($name));
329     $aelem->appendXMLMarkup($xmldata);
330     return $aelem;
331   }
332   function appendLink($target, $value = '') {
333     return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
334   }
335   function appendFormDiv($action, $method, $name) {
336     $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
337     return $formelem->appendElement('div');
338   }
339   function appendInputHidden($name, $value) {
340     return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
341   }
342   function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
343     return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
344   }
345   function appendInputRadio($name, $id, $value, $checked) {
346     return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
347   }
348   function appendInputCheckbox($name, $id, $value, $checked) {
349     return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
350   }
351   function appendInputSubmit($value) {
352     return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
353   }
354   function appendLabel($for_id, $value) {
355     return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
356   }
357   function appendText($text) {
358     return $this->appendChild($this->ownerDocument->createTextNode($text));
359   }
360   function appendHTMLMarkup($htmldata) {
361     $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
362   }
363   function appendXMLMarkup($xmldata) {
364     $this->ownerDocument->appendXMLMarkup($xmldata, $this);
365   }
366   function appendJSElement($jsdata) {
367     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
368   }
369 }
370 ?>