Table.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * PEAR::HTML_Table makes the design of HTML tables easy, flexible, reusable and efficient.
  5. *
  6. * The PEAR::HTML_Table package provides methods for easy and efficient design of HTML tables.
  7. * - Lots of customization options.
  8. * - Tables can be modified at any time.
  9. * - The logic is the same as standard HTML editors.
  10. * - Handles col and rowspans.
  11. * - PHP code is shorter, easier to read and to maintain.
  12. * - Tables options can be reused.
  13. *
  14. * For auto filling of data and such then check out http://pear.php.net/package/HTML_Table_Matrix
  15. *
  16. * PHP versions 4
  17. *
  18. * LICENSE: This source file is subject to version 3.0 of the PHP license
  19. * that is available through the world-wide-web at the following URI:
  20. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  21. * the PHP License and are unable to obtain it through the web, please
  22. * send a note to license@php.net so we can mail you a copy immediately.
  23. *
  24. * @category HTML
  25. * @package HTML_Table
  26. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  27. * @author Bertrand Mansion <bmansion@mamasam.com>
  28. * @copyright 2005 The PHP Group
  29. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  30. * @version CVS: $Id: Table.php 9611 2006-10-20 11:47:56Z bmol $
  31. * @link http://pear.php.net/package/HTML_Table
  32. */
  33. /**
  34. * Requires PEAR, HTML_Common and HTML_Table_Storage
  35. */
  36. require_once 'PEAR.php';
  37. require_once 'HTML/Common.php';
  38. require_once 'HTML/Table/Storage.php';
  39. /**
  40. * PEAR::HTML_Table makes the design of HTML tables easy, flexible, reusable and efficient.
  41. *
  42. * The PEAR::HTML_Table package provides methods for easy and efficient design of HTML tables.
  43. * - Lots of customization options.
  44. * - Tables can be modified at any time.
  45. * - The logic is the same as standard HTML editors.
  46. * - Handles col and rowspans.
  47. * - PHP code is shorter, easier to read and to maintain.
  48. * - Tables options can be reused.
  49. *
  50. * For auto filling of data and such then check out http://pear.php.net/package/HTML_Table_Matrix
  51. *
  52. * @category HTML
  53. * @package HTML_Table
  54. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  55. * @author Bertrand Mansion <bmansion@mamasam.com>
  56. * @copyright 2005 The PHP Group
  57. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  58. * @version Release: @package_version@
  59. * @link http://pear.php.net/package/HTML_Table
  60. */
  61. class HTML_Table extends HTML_Common {
  62. /**
  63. * Value to insert into empty cells
  64. * @var string
  65. * @access private
  66. */
  67. var $_autoFill = '&nbsp;';
  68. /**
  69. * Array containing the table caption
  70. * @var array
  71. * @access private
  72. */
  73. var $_caption = array();
  74. /**
  75. * Array containing the table column group specifications
  76. *
  77. * @var array
  78. * @author Laurent Laville (pear at laurent-laville dot org)
  79. * @access private
  80. */
  81. var $_colgroup = array();
  82. /**
  83. * HTML_Table_Storage object for the (t)head of the table
  84. * @var object
  85. * @access private
  86. */
  87. var $_thead = null;
  88. /**
  89. * HTML_Table_Storage object for the (t)foot of the table
  90. * @var object
  91. * @access private
  92. */
  93. var $_tfoot = null;
  94. /**
  95. * HTML_Table_Storage object for the (t)body of the table
  96. * @var object
  97. * @access private
  98. */
  99. var $_tbody = null;
  100. /**
  101. * Whether to use <thead>, <tfoot> and <tbody> or not
  102. * @var bool
  103. * @access private
  104. */
  105. var $_useTGroups = false;
  106. /**
  107. * Class constructor
  108. * @param array $attributes Associative array of table tag attributes
  109. * @param int $tabOffset Tab offset of the table
  110. * @param bool $useTGroups Whether to use <thead>, <tfoot> and
  111. * <tbody> or not
  112. * @access public
  113. */
  114. function HTML_Table($attributes = null, $tabOffset = 0, $useTGroups = false)
  115. {
  116. $commonVersion = 1.7;
  117. if (HTML_Common::apiVersion() < $commonVersion) {
  118. return PEAR::raiseError('HTML_Table version ' . $this->apiVersion() . ' requires ' .
  119. "HTML_Common version $commonVersion or greater.", 0, PEAR_ERROR_TRIGGER);
  120. }
  121. HTML_Common::HTML_Common($attributes, (int)$tabOffset);
  122. $this->_useTGroups = (boolean)$useTGroups;
  123. $this->_tbody =& new HTML_Table_Storage($attributes, $tabOffset, $this->_useTGroups);
  124. if ($this->_useTGroups) {
  125. $this->_thead =& new HTML_Table_Storage($attributes, $tabOffset, $this->_useTGroups);
  126. $this->_tfoot =& new HTML_Table_Storage($attributes, $tabOffset, $this->_useTGroups);
  127. }
  128. }
  129. /**
  130. * Returns the API version
  131. * @access public
  132. * @return double
  133. */
  134. function apiVersion()
  135. {
  136. return 1.7;
  137. }
  138. /**
  139. * Returns the HTML_Table_Storage object for <thead>
  140. * @access public
  141. * @return object
  142. */
  143. function &getHeader()
  144. {
  145. if (is_null($this->_thead)) {
  146. $this->_useTGroups = true;
  147. $this->_thead =& new HTML_Table_Storage($this->_attributes,
  148. $this->_tabOffset, $this->_useTGroups);
  149. $this->_tbody->setUseTGroups(true);
  150. }
  151. return $this->_thead;
  152. }
  153. /**
  154. * Returns the HTML_Table_Storage object for <tfoot>
  155. * @access public
  156. * @return object
  157. */
  158. function &getFooter()
  159. {
  160. if (is_null($this->_tfoot)) {
  161. $this->_useTGroups = true;
  162. $this->_tfoot =& new HTML_Table_Storage($this->_attributes,
  163. $this->_tabOffset, $this->_useTGroups);
  164. $this->_tbody->setUseTGroups(true);
  165. }
  166. return $this->_tfoot;
  167. }
  168. /**
  169. * Returns the HTML_Table_Storage object for <tbody>
  170. * (or the whole table if <t{head|foot|body> is not used)
  171. * @access public
  172. * @return object
  173. */
  174. function &getBody()
  175. {
  176. return $this->_tbody;
  177. }
  178. /**
  179. * Sets the table caption
  180. * @param string $caption
  181. * @param mixed $attributes Associative array or string of table row attributes
  182. * @access public
  183. */
  184. function setCaption($caption, $attributes = null)
  185. {
  186. $attributes = $this->_parseAttributes($attributes);
  187. $this->_caption = array('attr' => $attributes, 'contents' => $caption);
  188. }
  189. /**
  190. * Sets the table columns group specifications, or removes existing ones.
  191. *
  192. * @param mixed $colgroup (optional) Columns attributes
  193. * @param mixed $attributes (optional) Associative array or string
  194. * of table row attributes
  195. * @author Laurent Laville (pear at laurent-laville dot org)
  196. * @access public
  197. */
  198. function setColGroup($colgroup = null, $attributes = null)
  199. {
  200. if (isset($colgroup)) {
  201. $attributes = $this->_parseAttributes($attributes);
  202. $this->_colgroup[] = array('attr' => $attributes,
  203. 'contents' => $colgroup);
  204. } else {
  205. $this->_colgroup = array();
  206. }
  207. }
  208. /**
  209. * Sets the autoFill value
  210. * @param mixed $fill
  211. * @access public
  212. */
  213. function setAutoFill($fill)
  214. {
  215. $this->_tbody->setAutoFill($fill);
  216. }
  217. /**
  218. * Returns the autoFill value
  219. * @access public
  220. * @return mixed
  221. */
  222. function getAutoFill()
  223. {
  224. return $this->_tbody->getAutoFill();
  225. }
  226. /**
  227. * Sets the autoGrow value
  228. * @param bool $fill
  229. * @access public
  230. */
  231. function setAutoGrow($grow)
  232. {
  233. $this->_tbody->setAutoGrow($grow);
  234. }
  235. /**
  236. * Returns the autoGrow value
  237. * @access public
  238. * @return mixed
  239. */
  240. function getAutoGrow()
  241. {
  242. return $this->_tbody->getAutoGrow();
  243. }
  244. /**
  245. * Sets the number of rows in the table
  246. * @param int $rows
  247. * @access public
  248. */
  249. function setRowCount($rows)
  250. {
  251. $this->_tbody->setRowCount($rows);
  252. }
  253. /**
  254. * Sets the number of columns in the table
  255. * @param int $cols
  256. * @access public
  257. */
  258. function setColCount($cols)
  259. {
  260. $this->_tbody->setColCount($cols);
  261. }
  262. /**
  263. * Returns the number of rows in the table
  264. * @access public
  265. * @return int
  266. */
  267. function getRowCount()
  268. {
  269. return $this->_tbody->getRowCount();
  270. }
  271. /**
  272. * Gets the number of columns in the table
  273. *
  274. * If a row index is specified, the count will not take
  275. * the spanned cells into account in the return value.
  276. *
  277. * @param int Row index to serve for cols count
  278. * @access public
  279. * @return int
  280. */
  281. function getColCount($row = null)
  282. {
  283. return $this->_tbody->getColCount($row);
  284. }
  285. /**
  286. * Sets a rows type 'TH' or 'TD'
  287. * @param int $row Row index
  288. * @param string $type 'TH' or 'TD'
  289. * @access public
  290. */
  291. function setRowType($row, $type)
  292. {
  293. $this->_tbody->setRowType($row, $type);
  294. }
  295. /**
  296. * Sets a columns type 'TH' or 'TD'
  297. * @param int $col Column index
  298. * @param string $type 'TH' or 'TD'
  299. * @access public
  300. */
  301. function setColType($col, $type)
  302. {
  303. $this->_tbody->setColType($col, $type);
  304. }
  305. /**
  306. * Sets the cell attributes for an existing cell.
  307. *
  308. * If the given indices do not exist and autoGrow is true then the given
  309. * row and/or col is automatically added. If autoGrow is false then an
  310. * error is returned.
  311. * @param int $row Row index
  312. * @param int $col Column index
  313. * @param mixed $attributes Associative array or string of table row attributes
  314. * @access public
  315. * @throws PEAR_Error
  316. */
  317. function setCellAttributes($row, $col, $attributes)
  318. {
  319. $ret = $this->_tbody->setCellAttributes($row, $col, $attributes);
  320. if (PEAR::isError($ret)) {
  321. return $ret;
  322. }
  323. }
  324. /**
  325. * Updates the cell attributes passed but leaves other existing attributes in tact
  326. * @param int $row Row index
  327. * @param int $col Column index
  328. * @param mixed $attributes Associative array or string of table row attributes
  329. * @access public
  330. */
  331. function updateCellAttributes($row, $col, $attributes)
  332. {
  333. $ret = $this->_tbody->updateCellAttributes($row, $col, $attributes);
  334. if (PEAR::isError($ret)) {
  335. return $ret;
  336. }
  337. }
  338. /**
  339. * Returns the attributes for a given cell
  340. * @param int $row Row index
  341. * @param int $col Column index
  342. * @return array
  343. * @access public
  344. */
  345. function getCellAttributes($row, $col)
  346. {
  347. return $this->_tbody->getCellAttributes($row, $col);
  348. }
  349. /**
  350. * Sets the cell contents for an existing cell
  351. *
  352. * If the given indices do not exist and autoGrow is true then the given
  353. * row and/or col is automatically added. If autoGrow is false then an
  354. * error is returned.
  355. * @param int $row Row index
  356. * @param int $col Column index
  357. * @param mixed $contents May contain html or any object with a toHTML method;
  358. * if it is an array (with strings and/or objects), $col
  359. * will be used as start offset and the array elements
  360. * will be set to this and the following columns in $row
  361. * @param string $type (optional) Cell type either 'TH' or 'TD'
  362. * @access public
  363. * @throws PEAR_Error
  364. */
  365. function setCellContents($row, $col, $contents, $type = 'TD')
  366. {
  367. $ret = $this->_tbody->setCellContents($row, $col, $contents, $type);
  368. if (PEAR::isError($ret)) {
  369. return $ret;
  370. }
  371. }
  372. /**
  373. * Returns the cell contents for an existing cell
  374. * @param int $row Row index
  375. * @param int $col Column index
  376. * @access public
  377. * @return mixed
  378. */
  379. function getCellContents($row, $col)
  380. {
  381. return $this->_tbody->getCellContents($row, $col);
  382. }
  383. /**
  384. * Sets the contents of a header cell
  385. * @param int $row
  386. * @param int $col
  387. * @param mixed $contents
  388. * @param mixed $attributes Associative array or string of table row attributes
  389. * @access public
  390. */
  391. function setHeaderContents($row, $col, $contents, $attributes = null)
  392. {
  393. $this->_tbody->setHeaderContents($row, $col, $contents, $attributes);
  394. }
  395. /**
  396. * Adds a table row and returns the row identifier
  397. * @param array $contents (optional) Must be a indexed array of valid cell contents
  398. * @param mixed $attributes (optional) Associative array or string of table row attributes
  399. * This can also be an array of attributes, in which case the attributes
  400. * will be repeated in a loop.
  401. * @param string $type (optional) Cell type either 'th' or 'td'
  402. * @param bool $inTR false if attributes are to be applied in TD tags
  403. * true if attributes are to be applied in TR tag
  404. * @return int
  405. * @access public
  406. */
  407. function addRow($contents = null, $attributes = null, $type = 'td', $inTR = false)
  408. {
  409. $ret = $this->_tbody->addRow($contents, $attributes, $type, $inTR);
  410. return $ret;
  411. }
  412. /**
  413. * Sets the row attributes for an existing row
  414. * @param int $row Row index
  415. * @param mixed $attributes Associative array or string of table row attributes
  416. * This can also be an array of attributes, in which case the attributes
  417. * will be repeated in a loop.
  418. * @param bool $inTR false if attributes are to be applied in TD tags
  419. * true if attributes are to be applied in TR tag
  420. * @access public
  421. * @throws PEAR_Error
  422. */
  423. function setRowAttributes($row, $attributes, $inTR = false)
  424. {
  425. $ret = $this->_tbody->setRowAttributes($row, $attributes, $inTR);
  426. if (PEAR::isError($ret)) {
  427. return $ret;
  428. }
  429. }
  430. /**
  431. * Updates the row attributes for an existing row
  432. * @param int $row Row index
  433. * @param mixed $attributes Associative array or string of table row attributes
  434. * @param bool $inTR false if attributes are to be applied in TD tags
  435. * true if attributes are to be applied in TR tag
  436. * @access public
  437. * @throws PEAR_Error
  438. */
  439. function updateRowAttributes($row, $attributes = null, $inTR = false)
  440. {
  441. $ret = $this->_tbody->updateRowAttributes($row, $attributes, $inTR);
  442. if (PEAR::isError($ret)) {
  443. return $ret;
  444. }
  445. }
  446. /**
  447. * Returns the attributes for a given row as contained in the TR tag
  448. * @param int $row Row index
  449. * @return array
  450. * @access public
  451. */
  452. function getRowAttributes($row)
  453. {
  454. return $this->_tbody->getRowAttributes($row);
  455. }
  456. /**
  457. * Alternates the row attributes starting at $start
  458. * @param int $start Row index of row in which alternating begins
  459. * @param mixed $attributes1 Associative array or string of table row attributes
  460. * @param mixed $attributes2 Associative array or string of table row attributes
  461. * @param bool $inTR false if attributes are to be applied in TD tags
  462. * true if attributes are to be applied in TR tag
  463. * @access public
  464. */
  465. function altRowAttributes($start, $attributes1, $attributes2, $inTR = false)
  466. {
  467. $this->_tbody->altRowAttributes($start, $attributes1, $attributes2, $inTR);
  468. }
  469. /**
  470. * Adds a table column and returns the column identifier
  471. * @param array $contents (optional) Must be a indexed array of valid cell contents
  472. * @param mixed $attributes (optional) Associative array or string of table row attributes
  473. * @param string $type (optional) Cell type either 'th' or 'td'
  474. * @return int
  475. * @access public
  476. */
  477. function addCol($contents = null, $attributes = null, $type = 'td')
  478. {
  479. return $this->_tbody->addCol($contents, $attributes, $type);
  480. }
  481. /**
  482. * Sets the column attributes for an existing column
  483. * @param int $col Column index
  484. * @param mixed $attributes (optional) Associative array or string of table row attributes
  485. * @access public
  486. */
  487. function setColAttributes($col, $attributes = null)
  488. {
  489. $this->_tbody->setColAttributes($col, $attributes);
  490. }
  491. /**
  492. * Updates the column attributes for an existing column
  493. * @param int $col Column index
  494. * @param mixed $attributes (optional) Associative array or string of table row attributes
  495. * @access public
  496. */
  497. function updateColAttributes($col, $attributes = null)
  498. {
  499. $this->_tbody->updateColAttributes($col, $attributes);
  500. }
  501. /**
  502. * Sets the attributes for all cells
  503. * @param mixed $attributes (optional) Associative array or string of table row attributes
  504. * @access public
  505. */
  506. function setAllAttributes($attributes = null)
  507. {
  508. $this->_tbody->setAllAttributes($attributes);
  509. }
  510. /**
  511. * Updates the attributes for all cells
  512. * @param mixed $attributes (optional) Associative array or string of table row attributes
  513. * @access public
  514. */
  515. function updateAllAttributes($attributes = null)
  516. {
  517. $this->_tbody->updateAllAttributes($attributes);
  518. }
  519. /**
  520. * Returns the table structure as HTML
  521. * @access public
  522. * @return string
  523. */
  524. function toHtml()
  525. {
  526. $strHtml = '';
  527. $tabs = $this->_getTabs();
  528. $tab = $this->_getTab();
  529. $lnEnd = $this->_getLineEnd();
  530. if ($this->_comment) {
  531. $strHtml .= $tabs . "<!-- $this->_comment -->" . $lnEnd;
  532. }
  533. $strHtml .=
  534. $tabs . '<table' . $this->_getAttrString($this->_attributes) . '>' . $lnEnd;
  535. if (!empty($this->_caption)) {
  536. $attr = $this->_caption['attr'];
  537. $contents = $this->_caption['contents'];
  538. $strHtml .= $tabs . $tab . '<caption' . $this->_getAttrString($attr) . '>';
  539. if (is_array($contents)) {
  540. $contents = implode(', ', $contents);
  541. }
  542. $strHtml .= $contents;
  543. $strHtml .= '</caption>' . $lnEnd;
  544. }
  545. if (!empty($this->_colgroup)) {
  546. foreach ($this->_colgroup as $g => $col) {
  547. $attr = $this->_colgroup[$g]['attr'];
  548. $contents = $this->_colgroup[$g]['contents'];
  549. $strHtml .= $tabs . $tab . '<colgroup' . $this->_getAttrString($attr) . '>';
  550. if (!empty($contents)) {
  551. $strHtml .= $lnEnd;
  552. if (!is_array($contents)) {
  553. $contents = array($contents);
  554. }
  555. foreach ($contents as $a => $colAttr) {
  556. $attr = $this->_parseAttributes($colAttr);
  557. $strHtml .= $tabs . $tab . $tab . '<col' . $this->_getAttrString($attr) . '>' . $lnEnd;
  558. }
  559. $strHtml .= $tabs . $tab;
  560. }
  561. $strHtml .= '</colgroup>' . $lnEnd;
  562. }
  563. }
  564. if ($this->_useTGroups) {
  565. $tHeadColCount = 0;
  566. if ($this->_thead !== null) {
  567. $tHeadColCount = $this->_thead->getColCount();
  568. }
  569. $tFootColCount = 0;
  570. if ($this->_tfoot !== null) {
  571. $tFootColCount = $this->_tfoot->getColCount();
  572. }
  573. $tBodyColCount = 0;
  574. if ($this->_tbody !== null) {
  575. $tBodyColCount = $this->_tbody->getColCount();
  576. }
  577. $maxColCount = max($tHeadColCount, $tFootColCount, $tBodyColCount);
  578. if ($this->_thead !== null) {
  579. $this->_thead->setColCount($maxColCount);
  580. if ($this->_thead->getRowCount() > 0) {
  581. $strHtml .= $tabs . $tab . '<thead>' . $lnEnd;
  582. $strHtml .= $this->_thead->toHtml($tabs, $tab);
  583. $strHtml .= $tabs . $tab . '</thead>' . $lnEnd;
  584. }
  585. }
  586. if ($this->_tfoot !== null) {
  587. $this->_tfoot->setColCount($maxColCount);
  588. if ($this->_tfoot->getRowCount() > 0) {
  589. $strHtml .= $tabs . $tab . '<tfoot>' . $lnEnd;
  590. $strHtml .= $this->_tfoot->toHtml($tabs, $tab);
  591. $strHtml .= $tabs . $tab . '</tfoot>' . $lnEnd;
  592. }
  593. }
  594. if ($this->_tbody !== null) {
  595. $this->_tbody->setColCount($maxColCount);
  596. if ($this->_tbody->getRowCount() > 0) {
  597. $strHtml .= $tabs . $tab . '<tbody>' . $lnEnd;
  598. $strHtml .= $this->_tbody->toHtml($tabs, $tab);
  599. $strHtml .= $tabs . $tab . '</tbody>' . $lnEnd;
  600. }
  601. }
  602. } else {
  603. $strHtml .= $this->_tbody->toHtml($tabs, $tab);
  604. }
  605. $strHtml .= $tabs . '</table>' . $lnEnd;
  606. return $strHtml;
  607. }
  608. }
  609. ?>