b2a5f75757cba5fcd23478f4c4cf3bc48d737447
[squirrelmail.git] / class / html.class
1 <?php
2 /**
3 * html.class
4 *
5 * Copyright (c) 2002 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 *
9 * This contains functions needed to generate html output.
10 *
11 * $Id$
12 */
13
14
15 class html {
16 var $tag, $text, $style, $class,
17 $id, $html_el = array(), $javascript, $xtr_prop;
18
19 function html($tag='', $text='', $style ='', $class='', $id='',
20 $xtr_prop = '', $javascript = '') {
21 $this->tag = $tag;
22 $this->text = $text;
23 $this->style = $style;
24 $this->class = $class;
25 $this->id = $id;
26 $this->xtr_prop = $xtr_prop;
27 $this->javascript = $javascript;
28 }
29
30 function htmlAdd($el, $last=true) {
31 if ($last) {
32 $this->html_el[] = $el;
33 } else {
34 echo 'JOPPPEEE';
35 $new_html_el = array();
36 $new_html_el[] = $el;
37 foreach ($this->html_el as $html_el) {
38 $new_html_el[] = $html_el;
39 }
40 $this->html_el = $new_html_el;
41 }
42 }
43
44 function AddChild($tag='', $text='', $style ='', $class='', $id='',
45 $xtr_prop = '', $javascript = '') {
46 $el = new html ($tag, $text, $style, $class, $id, $xtr_prop, $javascript);
47 $this->htmlAdd($el);
48 }
49
50 function FindId($id) {
51 $cnt = count($this->html_el);
52 $el = false;
53 if ($cnt) {
54 for ($i = 0 ; $i < $cnt; $i++) {
55 if ($this->html_el[$i]->id == $id) {
56 $ret = $this->html_el[$i];
57 return $ret;
58 } else if (count($this->html_el[$i]->html_el)) {
59 $el = $this->html_el[$i]->FindId($id);
60 }
61 if ($el) return $el;
62 }
63 }
64 return $el;
65 }
66
67 function InsToId( $el, $id, $last=true) {
68 $html_el = &$this->FindId($id);
69 if ($html_el) {
70 $html_el->htmlAdd($el, $last);
71 }
72 }
73
74 function scriptAdd($script) {
75 $s = "\n".'<!--'."\n".
76 $script .
77 "\n".'// -->'."\n";
78 $el = new html ('script',$s,''.''.''.array('language' => 'JavaScript',
79 'type' => 'text/javascript'));
80 $this->htmlAdd($el);
81 }
82
83 function echoHtml( $usecss=false, $indent='x') {
84 if ($indent == 'x') {
85 $indent = ''; $indentmore = '';
86 } else {
87 $indentmore = $indent . ' ';
88 }
89 $tag = $this->tag;
90 $text = $this->text;
91 $class = $this->class;
92 $id = $this->id;
93 $style = $this->style;
94 $javascript = $this->javascript;
95 $xtr_prop = $this->xtr_prop;
96 if ($xtr_prop) {
97 $prop = '';
98 foreach ($xtr_prop as $k => $v) {
99 if (is_string($k)) {
100 $prop.=' '.$k.'="'.$v.'"';
101 } else {
102 $prop.=' '.$v;
103 }
104 }
105 }
106 if ($javascript) {
107 $js = '';
108 foreach ($javascript as $k => $v) { /* here we put the onclick, onmouseover etc entries */
109 $js.=' '.$k.'="'.$v.'";';
110 }
111 }
112 if ($tag) {
113 echo $indent . '<' . $tag;
114 } else {
115 echo $indent;
116 }
117 if ($class) {
118 echo ' class="'.$class.'"';
119 }
120 if ($id) {
121 echo ' id="'.$id.'"';
122 }
123 if ($xtr_prop) {
124 echo ' '.$prop;
125 }
126 if ($style && !$usecss && !is_array($style)) {
127 /* last premisse is to prevent 'style="Array"' in the output */
128 echo ' style="'.$style.'"';
129 }
130 if ($javascript) {
131 echo ' '.$js;
132 }
133 if ($tag) echo '>';
134 if ($text) {
135 if ($style && !$usecss) { /* if use css then fallback to stylesheet for layout */
136 foreach ($style as $k => $v) {
137 echo '<'.$k.'>';
138 }
139 echo $text;
140 foreach ($style as $k => $v) { /* if value of key value = true close the tag */
141 if ($v) {
142 echo '</'.$k.'>';
143 }
144 }
145 } else {
146 echo $text;
147 }
148 }
149 $cnt = count($this->html_el);
150 if ($cnt) {
151 if ($style && !$usecss) {
152 foreach ($style as $k => $v) {
153 echo '<'.$k.'>';
154 }
155 }
156 echo "\n";
157 for($i = 0;$i<$cnt;$i++) {
158 $el = $this->html_el[$i];
159 $el->echoHtml($usecss,$indentmore);
160 }
161 if ($style && !$usecss) {
162 foreach ($style as $k => $v) { /* if value of key value = true close the tag */
163 if ($v) {
164 echo '</'.$k.'>';
165 }
166 }
167 }
168 }
169 if ($tag) {
170 echo $indent . '</'.$tag.'>'."\n";
171 } else {
172 echo "\n";
173 }
174 }
175 }
176
177
178 ?>