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