Html.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Html tag factory
  4. */
  5. class Html {
  6. function &_($tag_name, $content = '', $params = array()) {
  7. $tag_class_name = 'Html_' . $tag_name;
  8. if (class_exists($tag_class_name)) {
  9. $tag = new $tag_class_name($content, $params);
  10. } else {
  11. $tag = new Html_tag($tag_name, $content, $params);
  12. }
  13. return $tag;
  14. }
  15. }
  16. class Html_tag {
  17. var $name;
  18. var $attr;
  19. var $cnt;
  20. var $_empty;
  21. var $_childrens;
  22. function __construct($name, $cnt = null, $params = array()) {
  23. $this->name = $name;
  24. $this->cnt = $cnt;
  25. $this->_empty = false;// default false
  26. $this->_childrens = array();
  27. if (array_key_exists('empty', $params)) {
  28. $this->setEmpty();
  29. }
  30. $this->attr = $params;
  31. }
  32. function getAttr($name) {
  33. if (array_key_exists($name, $this->attr)) {
  34. $this->attr[$name];
  35. }
  36. return null;
  37. }
  38. function setAttr($name, $value = '') {
  39. $this->attr[$name] = $value;
  40. }
  41. function removeAttr($name, $value = '') {
  42. unset($this->attr[$name]);
  43. }
  44. function setEmpty($bool) {
  45. $this->_empty = ($bool)? true : false;
  46. }
  47. function isEmpty() {
  48. return $this->_empty;
  49. }
  50. function hasChildNodes() {
  51. return !empty($this->_childrens);
  52. }
  53. function &getChildNodes() {
  54. return $this->_childrens;
  55. }
  56. /**
  57. * String or object Html_tag
  58. */
  59. function appendChild($tag) {
  60. $this->_childrens []= $tag;
  61. }
  62. /**
  63. * overwrite in sub class
  64. */
  65. function _prepareHtml() {
  66. }
  67. function toHtml() {
  68. $this->_prepareHtml();
  69. $ret = '';
  70. $attr_out = '';
  71. if (!empty($this->attr)) {
  72. $attr_arr = array();
  73. foreach ($this->attr as $k_attr => $v_attr) {
  74. $attr_arr []= $k_attr . '="' . $v_attr . '"';
  75. }
  76. if (!empty($attr_arr)) {
  77. $attr_out = ' ' . implode(' ', $attr_arr);
  78. }
  79. }
  80. if ($this->_empty) {
  81. $ret .= '<' . $this->name . $attr_out . ' />';
  82. }
  83. else {
  84. $ret .= '<' . $this->name . $attr_out . '>';
  85. if (is_string($this->cnt)) {
  86. $ret .= $this->cnt;
  87. } else if (is_object($this->cnt)) {
  88. $ret .= $this->cnt->toHtml();
  89. }
  90. if (!empty($this->_childrens)) {
  91. foreach ($this->_childrens as $v_child) {
  92. if (is_string($v_child)) {
  93. $ret .= $v_child;
  94. } else if (is_object($v_child)) {
  95. $ret .= $v_child->toHtml();
  96. }
  97. }
  98. }
  99. $ret .= '</' . $this->name . '>';
  100. }
  101. return $ret;
  102. }
  103. function &wrap($tag_name, $params = array()) {
  104. $tag = Html::_($tag_name, $this, $params);
  105. return $tag;
  106. }
  107. }
  108. class Html_table extends Html_tag {
  109. var $thead;
  110. var $tfoot;
  111. var $tbody;
  112. function __construct($content, $params = array()) {
  113. parent::__construct('table', $content, $params);
  114. $this->thead = null;
  115. $this->tfoot = null;
  116. $this->tbody = null;
  117. }
  118. /**
  119. * add row to thead
  120. */
  121. function addHeadRow($html_row) {
  122. if (!$this->thead) $this->thead = Html::_('thead');
  123. $this->thead->appendChild($html_row);
  124. }
  125. /**
  126. * add row to tfoot
  127. */
  128. function addFootRow($html_row) {
  129. if (!$this->tfoot) $this->tfoot = Html::_('tfoot');
  130. $this->tfoot->appendChild($html_row);
  131. }
  132. /**
  133. * add row to tbody
  134. */
  135. function addBodyRow($html_row) {
  136. if (!$this->tbody) $this->tbody = Html::_('tbody');
  137. $this->tbody->appendChild($html_row);
  138. }
  139. function _prepareHtml() {
  140. $this->cnt = null;// remove all set cnt ?
  141. $this->_childrens = null;// remove all childrens ?
  142. if ($this->thead) $this->appendChild($this->thead);
  143. if ($this->tfoot) $this->appendChild($this->tfoot);
  144. if ($this->tbody) $this->appendChild($this->tbody);
  145. }
  146. }