1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
<?php
/**
* XHtmlSimpleElement
*
* Used to generate Xhtml-Code for simple xhtml elements
* (i.e. elements, that can't contain child elements)
*
*
* @author Felix Meinhold
*
*/
class XHtmlSimpleElement {
var $_element;
var $_siblings = array();
var $_htmlcode;
var $_attributes = array();
/**
* Constructor
*
* @param string The element's name. Defaults to name of the
* derived class
*
*/
function __construct($element = null) {
$this->_element = $this->is_element();
}
function set_style($style) {
$this->set_attribute('style', $style);
}
function set_class($class) {
$this->set_attribute('class', $class);
}
function is_element() {
return
str_replace('xhtml_', '', strtolower(get_class($this)));
}
/**
* Private function generates xhtml
* @access private
*/
function _html() {
$this->_htmlcode = "<";
foreach ($this->_attributeCollection as $attribute => $value) {
if (!empty($value)) $this->_htmlcode .= " {$attribute}=\"{$value}\"";
}
$this->_htmlcode .= "/>";
return $this->_htmlcode;
}
/**
* Returns xhtml code
*
*/
function fetch() {
return $this->_html();
}
/**
* Echoes xhtml
*
*/
function show() {
echo $this->fetch();
}
function set_attribute($attr, $value) {
$this->_attributes[$attr] = $value;
}
}
/**
* XHtmlElement
*
* Used to generate Xhtml-Code for xhtml elements
* that can contain child elements
*
*
*/
class XHtmlElement extends XHtmlSimpleElement {
var $_text = null;
var $_htmlcode = "";
var $_siblings = array();
function __construct($text = null) {
parent::__construct();
if ($text) $this->set_text($text);
}
/*
* Adds an xhtml child to element
*
* @param XHtmlElement The element to become a child of element
*/
function add(&$object) {
array_push($this->_siblings, $object);
}
/*
* The CDATA section of Element
*
* @param string Text
*/
function set_text($text) {
if ($text) $this->_text = htmlspecialchars($text);
}
function fetch() {
return $this->_html();
}
function _html() {
$this->_htmlcode = "<{$this->_element}";
foreach ($this->_attributes as $attribute =>$value) {
if (!empty($value)) $this->_htmlcode .= " {$attribute} =\"{$value}\"";
}
$this->_htmlcode .= ">";
if ($this->_text) {
$this->_htmlcode .= $this->_text;
}
foreach ($this->_siblings as $obj) {
$this->_htmlcode .= $obj->fetch();
}
$this->_htmlcode .= "</{$this->_element}>";
return $this->_htmlcode;
}
/*
* Returns siblings of Element
*
*/
function get_siblings() {
return $this->_siblings;
}
function has_siblings() {
return (count($this->_siblings) != 0);
}
}
class XHTML_Button extends XHtmlElement {
function __construct($name, $text = null) {
parent::__construct();
$this->set_attribute("name", $name);
if ($text) $this->set_text($text);
}
}
class XHTML_Option extends XHtmlElement {
function __construct($text, $value = null) {
parent::__construct(null);
$this->set_text($text);
}
}
class XHTML_Select extends XHTMLElement {
var $_data;
function __construct($name, $multiple = false, $size = null) {
parent::__construct();
$this->set_attribute("name", $name);
if ($multiple) $this->set_attribute("multiple","multiple");
if ($size) $this->set_attribute("size",$size);
}
function set_data(&$data, $delim = ",") {
switch (gettype($data)) {
case "string":
$this->_data = explode($delim, $data);
break;
case "array":
$this->_data = $data;
break;
default:
break;
}
}
function fetch() {
if (isset($this->_data) && $this->_data) {
foreach ($this->_data as $value) { $this->add(new XHTML_Option($value)); }
}
return parent::fetch();
}
}
?>
|