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