/libraries/axiom-helper/axTableHelper.class.php

https://github.com/bdelespierre/php-axiom · PHP · 197 lines · 86 code · 17 blank · 94 comment · 7 complexity · 9aba0e47a0f7b89819a325a7556a559a MD5 · raw file

  1. <?php
  2. /**
  3. * @brief Table helper class file
  4. * @file axTableHelper.class.php
  5. */
  6. /**
  7. * @brief Table Helper Class
  8. *
  9. * @class axTableHelper
  10. * @author Delespierre
  11. * @ingroup Helper
  12. * @copyright Copyright 2010-2011, Benjamin Delespierre (http://bdelespierre.fr)
  13. * @license http://www.gnu.org/licenses/lgpl.html Lesser General Public Licence version 3
  14. */
  15. class axTableHelper extends axBaseHelper {
  16. /**
  17. * @brief thead element
  18. * @property axTableRowGroupHelper $head
  19. */
  20. public $head = null;
  21. /**
  22. * @brief tfoot element
  23. * @property axTableRowGroupHelper $foot
  24. */
  25. public $foot = null;
  26. /**
  27. * @brief tbody element
  28. * @property axTableRowGroupHelper $body
  29. */
  30. public $body = null;
  31. /**
  32. * @brief Constructor
  33. * @param string $caption @optional @default{false} The table caption value
  34. */
  35. public function __construct ($caption = false) {
  36. parent::__construct('table');
  37. if ($caption)
  38. $this->appendChild(axCaptionHelper::export($caption));
  39. $this->head = &$this->_children['head'];
  40. $this->foot = &$this->_children['foot'];
  41. $this->body = &$this->_children['body'];
  42. $this->setHead();
  43. $this->setFoot();
  44. $this->setBody();
  45. }
  46. /**
  47. * @brief Set the table column's name (and optionaly use them as a filter for any row added to the body)
  48. * @see axTableHelper::setHead()
  49. * @param array $columns
  50. * @param boolean $filter @optional @default{true} Use these columns as filter
  51. * @return axTableHelper
  52. */
  53. public function setColumnNames (array $columns, $filter = true) {
  54. if ($filter)
  55. $this->body->setFilter(array_keys($columns));
  56. return $this->setHead(array($columns));
  57. }
  58. /**
  59. * @brief Set the header
  60. *
  61. * If previous header was set, it will be discarded.
  62. *
  63. * @param Traversable|array $rows @optional @default{null} The rows to append
  64. * @return axTableHelper
  65. */
  66. public function setHead ($rows = null) {
  67. $this->_children['head'] = axTableRowGroupHelper::export('head');
  68. if (!empty($rows))
  69. $this->head->addRows($rows);
  70. return $this;
  71. }
  72. /**
  73. * @brief Set the footer
  74. *
  75. * If previous footer was set, it will be discarded.
  76. *
  77. * @param Traversable|array $rows @optional @default{null} The rows to append
  78. * @return axTableHelper
  79. */
  80. public function setFoot ($rows = null) {
  81. $this->_children['foot'] = axTableRowGroupHelper::export('foot');
  82. if (!empty($rows))
  83. $this->foot->addRows($rows);
  84. return $this;
  85. }
  86. /**
  87. * @brief Set the body.
  88. *
  89. * If previous body was set, it will be discarded.
  90. *
  91. * @param Traversable $rows @optional @default{null} The rows to append
  92. * @return axTableHelper
  93. */
  94. public function setBody ($rows = null) {
  95. $this->_children['body'] = axTableRowGroupHelper::export('body');
  96. if (!empty($rows))
  97. $this->body->addRows($rows);
  98. return $this;
  99. }
  100. /**
  101. * @brief Add a row to the given table row group.
  102. *
  103. * The @c $to parameter can be either:
  104. * @li head or thead
  105. * @li foot or tfoot
  106. * @li body or tbody
  107. * If the $to parameter is left to false, the row will be added to the body section.
  108. *
  109. * @param Traversable $row
  110. * @param string $to @optional @default{false} The section to append this row to
  111. * @return axTableHelper
  112. */
  113. public function addRow ($row, $to = false) {
  114. switch (strtolower($to)) {
  115. case 'head': case 'thead': $rowgroup = 'head'; break;
  116. case 'foot': case 'tfoot': $rowgroup = 'foot'; break;
  117. case 'body': case 'tbody': default: $rowgroup = 'body';
  118. }
  119. $this->$rowgroup->addRow($row);
  120. return $this;
  121. }
  122. /**
  123. * @brief Add multiple rows at once
  124. * @see axTableHelper::addRow()
  125. * @param Traversable|array $rows
  126. * @param string $to @optional @default{false} The section to append this row to
  127. * @return axTableHelper
  128. */
  129. public function addRows ($rows, $to = false) {
  130. foreach ($rows as $row)
  131. $this->addRow($row, $to);
  132. return $this;
  133. }
  134. /**
  135. * @brief Add a colgroup to the table and return it
  136. * @return axColGroupHelper
  137. */
  138. public function addColGroup () {
  139. return $this->appendChild(axColGroupHelper::export());
  140. }
  141. /**
  142. * @copydoc axBaseHelper::__toString()
  143. */
  144. public function __toString () {
  145. // Order the children elements according to xhtml DTD
  146. $children = array(
  147. 'caption' => array(),
  148. 'colgroup' => array(),
  149. 'col' => array(),
  150. 'thead' => array(),
  151. 'tfoot' => array(),
  152. 'tbody' => array(),
  153. );
  154. foreach ($this->_children as $node) {
  155. switch ($node) {
  156. case $node instanceof axCaptionHelper: $children['caption'][] = $node; break;
  157. case $node instanceof axColGroupHelper: $children['colgroup'][] = $node; break;
  158. case $node instanceof axColHelper: $children['col'][] = $node; break;
  159. case $node instanceof axTableRowGroupHelper:
  160. switch($node->getType()) {
  161. case 'head': $children['thead'][] = $node; break;
  162. case 'foot': $children['tfoot'][] = $node; break;
  163. case 'body': $children['tbody'][] = $node; break;
  164. }
  165. break;
  166. }
  167. }
  168. $this->_children = call_user_func_array('array_merge', $children);
  169. return parent::__toString();
  170. }
  171. /**
  172. * @copydoc axTableHelper::__construct()
  173. * @static
  174. * @brief Constructor static alias
  175. * @return axTableHelper
  176. */
  177. public static function export ($caption = false) {
  178. return new self ($caption);
  179. }
  180. }