html.class.php 3.9 KB

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