html.class.php 4.2 KB

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