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