Table.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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
  5. * efficient.
  6. *
  7. * The PEAR::HTML_Table package provides methods for easy and efficient design
  8. * of HTML tables.
  9. * - Lots of customization options.
  10. * - Tables can be modified at any time.
  11. * - The logic is the same as standard HTML editors.
  12. * - Handles col and rowspans.
  13. * - PHP code is shorter, easier to read and to maintain.
  14. * - Tables options can be reused.
  15. *
  16. * For auto filling of data and such then check out
  17. * http://pear.php.net/package/HTML_Table_Matrix
  18. *
  19. * PHP versions 4 and 5
  20. *
  21. * LICENSE:
  22. *
  23. * Copyright (c) 2005-2007, Adam Daniel <adaniel1@eesus.jnj.com>,
  24. * Bertrand Mansion <bmansion@mamasam.com>,
  25. * Mark Wiesemann <wiesemann@php.net>
  26. * All rights reserved.
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. * * Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in the
  36. * documentation and/or other materials provided with the distribution.
  37. * * The names of the authors may not be used to endorse or promote products
  38. * derived from this software without specific prior written permission.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  41. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  42. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  44. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  45. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  46. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  47. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  48. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  49. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  50. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. * @category HTML
  53. * @package HTML_Table
  54. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  55. * @author Bertrand Mansion <bmansion@mamasam.com>
  56. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  57. * @version CVS: $Id: Table.php,v 1.39 2007/06/25 16:44:43 wiesemann Exp $
  58. * @link http://pear.php.net/package/HTML_Table
  59. */
  60. /**
  61. * PEAR::HTML_Table makes the design of HTML tables easy, flexible, reusable and efficient.
  62. *
  63. * The PEAR::HTML_Table package provides methods for easy and efficient design
  64. * of HTML tables.
  65. * - Lots of customization options.
  66. * - Tables can be modified at any time.
  67. * - The logic is the same as standard HTML editors.
  68. * - Handles col and rowspans.
  69. * - PHP code is shorter, easier to read and to maintain.
  70. * - Tables options can be reused.
  71. *
  72. * For auto filling of data and such then check out
  73. * http://pear.php.net/package/HTML_Table_Matrix
  74. *
  75. * @category HTML
  76. * @package HTML_Table
  77. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  78. * @author Bertrand Mansion <bmansion@mamasam.com>
  79. * @copyright 2005-2006 The PHP Group
  80. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  81. * @version Release: @package_version@
  82. * @link http://pear.php.net/package/HTML_Table
  83. */
  84. class HTML_Table extends HTML_Common
  85. {
  86. /**
  87. * Value to insert into empty cells. This is used as a default for
  88. * newly-created tbodies.
  89. * @var string
  90. * @access private
  91. */
  92. var $_autoFill = '&nbsp;';
  93. /**
  94. * Automatically adds a new row, column, or body if a given row, column, or
  95. * body index does not exist.
  96. * This is used as a default for newly-created tbodies.
  97. * @var bool
  98. * @access private
  99. */
  100. var $_autoGrow = true;
  101. /**
  102. * Array containing the table caption
  103. * @var array
  104. * @access private
  105. */
  106. var $_caption = array();
  107. /**
  108. * Array containing the table column group specifications
  109. *
  110. * @var array
  111. * @author Laurent Laville (pear at laurent-laville dot org)
  112. * @access private
  113. */
  114. var $_colgroup = array();
  115. /**
  116. * HTML_Table_Storage object for the (t)head of the table
  117. * @var object
  118. * @access private
  119. */
  120. var $_thead = null;
  121. /**
  122. * HTML_Table_Storage object for the (t)foot of the table
  123. * @var object
  124. * @access private
  125. */
  126. var $_tfoot = null;
  127. /**
  128. * HTML_Table_Storage object for the (t)body of the table
  129. * @var object
  130. * @access private
  131. */
  132. var $_tbodies = array();
  133. /**
  134. * Number of bodies in the table
  135. * @var int
  136. * @access private
  137. */
  138. var $_tbodyCount = 0;
  139. /**
  140. * Whether to use <thead>, <tfoot> and <tbody> or not
  141. * @var bool
  142. * @access private
  143. */
  144. var $_useTGroups = false;
  145. /**
  146. * Class constructor
  147. * @param array $attributes Associative array of table tag
  148. * attributes
  149. * @param int $tabOffset Tab offset of the table
  150. * @param bool $useTGroups Whether to use <thead>, <tfoot> and
  151. * <tbody> or not
  152. * @access public
  153. */
  154. public function __construct($attributes = null, $tabOffset = 0, $useTGroups = false)
  155. {
  156. parent::__construct($attributes, (int)$tabOffset);
  157. $this->_useTGroups = (boolean)$useTGroups;
  158. $this->addBody();
  159. if ($this->_useTGroups) {
  160. $this->_thead = new HTML_Table_Storage($tabOffset, $this->_useTGroups);
  161. $this->_tfoot = new HTML_Table_Storage($tabOffset, $this->_useTGroups);
  162. }
  163. }
  164. /**
  165. * Returns the API version
  166. * @access public
  167. * @return double
  168. * @deprecated
  169. */
  170. function apiVersion()
  171. {
  172. return 1.7;
  173. }
  174. /**
  175. * Returns the HTML_Table_Storage object for <thead>
  176. * @access public
  177. * @return object
  178. */
  179. function &getHeader()
  180. {
  181. if (is_null($this->_thead)) {
  182. $this->_useTGroups = true;
  183. $this->_thead = new HTML_Table_Storage($this->_tabOffset,
  184. $this->_useTGroups);
  185. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  186. $this->_tbodies[$i]->setUseTGroups(true);
  187. }
  188. }
  189. return $this->_thead;
  190. }
  191. /**
  192. * Returns the HTML_Table_Storage object for <tfoot>
  193. * @access public
  194. * @return object
  195. */
  196. function &getFooter()
  197. {
  198. if (is_null($this->_tfoot)) {
  199. $this->_useTGroups = true;
  200. $this->_tfoot = new HTML_Table_Storage($this->_tabOffset,
  201. $this->_useTGroups);
  202. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  203. $this->_tbodies[$i]->setUseTGroups(true);
  204. }
  205. }
  206. return $this->_tfoot;
  207. }
  208. /**
  209. * Returns the HTML_Table_Storage object for the specified <tbody>
  210. * (or the whole table if <t{head|foot|body}> is not used)
  211. * @param int $body (optional) The index of the body to
  212. * return.
  213. * @access public
  214. * @return object
  215. * @throws PEAR_Error
  216. */
  217. function &getBody($body = 0)
  218. {
  219. $ret = $this->_adjustTbodyCount($body, 'getBody');
  220. if (PEAR::isError($ret)) {
  221. return $ret;
  222. }
  223. return $this->_tbodies[$body];
  224. }
  225. /**
  226. * Adds a table body and returns the body identifier
  227. * @param mixed $attributes (optional) Associative array or
  228. * string of table body attributes
  229. * @access public
  230. * @return int
  231. */
  232. function addBody($attributes = null)
  233. {
  234. if (!$this->_useTGroups && $this->_tbodyCount > 0) {
  235. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  236. $this->_tbodies[$i]->setUseTGroups(true);
  237. }
  238. $this->_useTGroups = true;
  239. }
  240. $body = $this->_tbodyCount++;
  241. $this->_tbodies[$body] = new HTML_Table_Storage($this->_tabOffset,
  242. $this->_useTGroups);
  243. $this->_tbodies[$body]->setAutoFill($this->_autoFill);
  244. $this->_tbodies[$body]->setAttributes($attributes);
  245. return $body;
  246. }
  247. /**
  248. * Adjusts the number of bodies
  249. * @param int $body Body index
  250. * @param string $method Name of calling method
  251. * @access private
  252. * @throws PEAR_Error
  253. */
  254. function _adjustTbodyCount($body, $method)
  255. {
  256. if ($this->_autoGrow) {
  257. while ($this->_tbodyCount <= (int)$body) {
  258. $this->addBody();
  259. }
  260. } else {
  261. return PEAR::raiseError('Invalid body reference[' .
  262. $body . '] in HTML_Table::' . $method);
  263. }
  264. }
  265. /**
  266. * Sets the table caption
  267. * @param string $caption
  268. * @param mixed $attributes Associative array or string of
  269. * table row attributes
  270. * @access public
  271. */
  272. function setCaption($caption, $attributes = null)
  273. {
  274. $attributes = $this->_parseAttributes($attributes);
  275. $this->_caption = array('attr' => $attributes, 'contents' => $caption);
  276. }
  277. /**
  278. * Sets the table columns group specifications, or removes existing ones.
  279. *
  280. * @param mixed $colgroup (optional) Columns attributes
  281. * @param mixed $attributes (optional) Associative array or string
  282. * of table row attributes
  283. * @author Laurent Laville (pear at laurent-laville dot org)
  284. * @access public
  285. */
  286. function setColGroup($colgroup = null, $attributes = null)
  287. {
  288. if (isset($colgroup)) {
  289. $attributes = $this->_parseAttributes($attributes);
  290. $this->_colgroup[] = array('attr' => $attributes,
  291. 'contents' => $colgroup);
  292. } else {
  293. $this->_colgroup = array();
  294. }
  295. }
  296. /**
  297. * Sets the autoFill value
  298. * @param mixed $fill Whether autoFill should be enabled or not
  299. * @param int $body (optional) The index of the body to set.
  300. * Pass null to set for all bodies.
  301. * @access public
  302. * @throws PEAR_Error
  303. */
  304. function setAutoFill($fill, $body = null)
  305. {
  306. if (!is_null($body)) {
  307. $ret = $this->_adjustTbodyCount($body, 'setAutoFill');
  308. if (PEAR::isError($ret)) {
  309. return $ret;
  310. }
  311. $this->_tbodies[$body]->setAutoFill($fill);
  312. } else {
  313. $this->_autoFill = $fill;
  314. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  315. $this->_tbodies[$i]->setAutoFill($fill);
  316. }
  317. }
  318. }
  319. /**
  320. * Returns the autoFill value
  321. * @param int $body (optional) The index of the body to get.
  322. * Pass null to get the default for new bodies.
  323. * @access public
  324. * @return mixed
  325. * @throws PEAR_Error
  326. */
  327. function getAutoFill($body = null)
  328. {
  329. if (!is_null($body)) {
  330. $ret = $this->_adjustTbodyCount($body, 'getAutoFill');
  331. if (PEAR::isError($ret)) {
  332. return $ret;
  333. }
  334. return $this->_tbodies[$body]->getAutoFill();
  335. } else {
  336. return $this->_autoFill;
  337. }
  338. }
  339. /**
  340. * Sets the autoGrow value
  341. * @param bool $grow Whether autoGrow should be enabled or not
  342. * @param int $body (optional) The index of the body to set.
  343. * Pass null to set for all bodies.
  344. * @access public
  345. * @throws PEAR_Error
  346. */
  347. function setAutoGrow($grow, $body = null)
  348. {
  349. if (!is_null($body)) {
  350. $ret = $this->_adjustTbodyCount($body, 'setAutoGrow');
  351. if (PEAR::isError($ret)) {
  352. return $ret;
  353. }
  354. $this->_tbodies[$body]->setAutoGrow($grow);
  355. } else {
  356. $this->_autoGrow = $grow;
  357. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  358. $this->_tbodies[$i]->setAutoGrow($grow);
  359. }
  360. }
  361. }
  362. /**
  363. * Returns the autoGrow value
  364. * @param int $body (optional) The index of the body to get.
  365. * Pass null to get the default for new bodies.
  366. * @access public
  367. * @return mixed
  368. * @throws PEAR_Error
  369. */
  370. function getAutoGrow($body = null)
  371. {
  372. if (!is_null($body)) {
  373. $ret = $this->_adjustTbodyCount($body, 'getAutoGrow');
  374. if (PEAR::isError($ret)) {
  375. return $ret;
  376. }
  377. return $this->_tbodies[$body]->getAutoGrow();
  378. } else {
  379. return $this->_autoGrow;
  380. }
  381. }
  382. /**
  383. * Sets the number of rows in the table body
  384. * @param int $rows The number of rows
  385. * @param int $body (optional) The index of the body to set.
  386. * @access public
  387. * @throws PEAR_Error
  388. */
  389. function setRowCount($rows, $body = 0)
  390. {
  391. $ret = $this->_adjustTbodyCount($body, 'setRowCount');
  392. if (PEAR::isError($ret)) {
  393. return $ret;
  394. }
  395. $this->_tbodies[$body]->setRowCount($rows);
  396. }
  397. /**
  398. * Sets the number of columns in the table
  399. * @param int $cols The number of columns
  400. * @param int $body (optional) The index of the body to set.
  401. * @access public
  402. * @throws PEAR_Error
  403. */
  404. function setColCount($cols, $body = 0)
  405. {
  406. $ret = $this->_adjustTbodyCount($body, 'setColCount');
  407. if (PEAR::isError($ret)) {
  408. return $ret;
  409. }
  410. $this->_tbodies[$body]->setColCount($cols);
  411. }
  412. /**
  413. * Returns the number of rows in the table
  414. * @param int $body (optional) The index of the body to get.
  415. * Pass null to get the total number of
  416. * rows in all bodies.
  417. * @access public
  418. * @return int
  419. * @throws PEAR_Error
  420. */
  421. function getRowCount($body = null)
  422. {
  423. if (!is_null($body)) {
  424. $ret = $this->_adjustTbodyCount($body, 'getRowCount');
  425. if (PEAR::isError($ret)) {
  426. return $ret;
  427. }
  428. return $this->_tbodies[$body]->getRowCount();
  429. } else {
  430. $rowCount = 0;
  431. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  432. $rowCount += $this->_tbodies[$i]->getRowCount();
  433. }
  434. return $rowCount;
  435. }
  436. }
  437. /**
  438. * Gets the number of columns in the table
  439. *
  440. * If a row index is specified, the count will not take
  441. * the spanned cells into account in the return value.
  442. *
  443. * @param int $row Row index to serve for cols count
  444. * @param int $body (optional) The index of the body to get.
  445. * @access public
  446. * @return int
  447. * @throws PEAR_Error
  448. */
  449. function getColCount($row = null, $body = 0)
  450. {
  451. $ret = $this->_adjustTbodyCount($body, 'getColCount');
  452. if (PEAR::isError($ret)) {
  453. return $ret;
  454. }
  455. return $this->_tbodies[$body]->getColCount($row);
  456. }
  457. /**
  458. * Sets a rows type 'TH' or 'TD'
  459. * @param int $row Row index
  460. * @param string $type 'TH' or 'TD'
  461. * @param int $body (optional) The index of the body to set.
  462. * @access public
  463. * @throws PEAR_Error
  464. */
  465. function setRowType($row, $type, $body = 0)
  466. {
  467. $ret = $this->_adjustTbodyCount($body, 'setRowType');
  468. if (PEAR::isError($ret)) {
  469. return $ret;
  470. }
  471. $this->_tbodies[$body]->setRowType($row, $type);
  472. }
  473. /**
  474. * Sets a columns type 'TH' or 'TD'
  475. * @param int $col Column index
  476. * @param string $type 'TH' or 'TD'
  477. * @param int $body (optional) The index of the body to set.
  478. * Pass null to set for all bodies.
  479. * @access public
  480. * @throws PEAR_Error
  481. */
  482. function setColType($col, $type, $body = null)
  483. {
  484. if (!is_null($body)) {
  485. $ret = $this->_adjustTbodyCount($body, 'setColType');
  486. if (PEAR::isError($ret)) {
  487. return $ret;
  488. }
  489. $this->_tbodies[$body]->setColType($col, $type);
  490. } else {
  491. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  492. $this->_tbodies[$i]->setColType($col, $type);
  493. }
  494. }
  495. }
  496. /**
  497. * Sets the cell attributes for an existing cell.
  498. *
  499. * If the given indices do not exist and autoGrow is true then the given
  500. * row and/or col is automatically added. If autoGrow is false then an
  501. * error is returned.
  502. * @param int $row Row index
  503. * @param int $col Column index
  504. * @param mixed $attributes Associative array or string of
  505. * table row attributes
  506. * @param int $body (optional) The index of the body to set.
  507. * @access public
  508. * @throws PEAR_Error
  509. */
  510. function setCellAttributes($row, $col, $attributes, $body = 0)
  511. {
  512. $ret = $this->_adjustTbodyCount($body, 'setCellAttributes');
  513. if (PEAR::isError($ret)) {
  514. return $ret;
  515. }
  516. $ret = $this->_tbodies[$body]->setCellAttributes($row, $col, $attributes);
  517. if (PEAR::isError($ret)) {
  518. return $ret;
  519. }
  520. }
  521. /**
  522. * Updates the cell attributes passed but leaves other existing attributes
  523. * intact
  524. * @param int $row Row index
  525. * @param int $col Column index
  526. * @param mixed $attributes Associative array or string of table row
  527. * attributes
  528. * @param int $body (optional) The index of the body to set.
  529. * @access public
  530. * @throws PEAR_Error
  531. */
  532. function updateCellAttributes($row, $col, $attributes, $body = 0)
  533. {
  534. $ret = $this->_adjustTbodyCount($body, 'updateCellAttributes');
  535. if (PEAR::isError($ret)) {
  536. return $ret;
  537. }
  538. $ret = $this->_tbodies[$body]->updateCellAttributes($row, $col, $attributes);
  539. if (PEAR::isError($ret)) {
  540. return $ret;
  541. }
  542. }
  543. /**
  544. * Returns the attributes for a given cell
  545. * @param int $row Row index
  546. * @param int $col Column index
  547. * @param int $body (optional) The index of the body to get.
  548. * @return array
  549. * @access public
  550. * @throws PEAR_Error
  551. */
  552. function getCellAttributes($row, $col, $body = 0)
  553. {
  554. $ret = $this->_adjustTbodyCount($body, 'getCellAttributes');
  555. if (PEAR::isError($ret)) {
  556. return $ret;
  557. }
  558. return $this->_tbodies[$body]->getCellAttributes($row, $col);
  559. }
  560. /**
  561. * Sets the cell contents for an existing cell
  562. *
  563. * If the given indices do not exist and autoGrow is true then the given
  564. * row and/or col is automatically added. If autoGrow is false then an
  565. * error is returned.
  566. * @param int $row Row index
  567. * @param int $col Column index
  568. * @param mixed $contents May contain html or any object with a
  569. * toHTML() method; it is an array (with
  570. * strings and/or objects), $col will be
  571. * used as start offset and the array
  572. * elements will be set to this and the
  573. * following columns in $row
  574. * @param string $type (optional) Cell type either 'TH' or 'TD'
  575. * @param int $body (optional) The index of the body to set.
  576. * @access public
  577. * @throws PEAR_Error
  578. */
  579. function setCellContents($row, $col, $contents, $type = 'TD', $body = 0)
  580. {
  581. $ret = $this->_adjustTbodyCount($body, 'setCellContents');
  582. if (PEAR::isError($ret)) {
  583. return $ret;
  584. }
  585. $ret = $this->_tbodies[$body]->setCellContents($row, $col, $contents, $type);
  586. if (PEAR::isError($ret)) {
  587. return $ret;
  588. }
  589. }
  590. /**
  591. * Returns the cell contents for an existing cell
  592. * @param int $row Row index
  593. * @param int $col Column index
  594. * @param int $body (optional) The index of the body to get.
  595. * @access public
  596. * @return mixed
  597. * @throws PEAR_Error
  598. */
  599. function getCellContents($row, $col, $body = 0)
  600. {
  601. $ret = $this->_adjustTbodyCount($body, 'getCellContents');
  602. if (PEAR::isError($ret)) {
  603. return $ret;
  604. }
  605. return $this->_tbodies[$body]->getCellContents($row, $col);
  606. }
  607. /**
  608. * Sets the contents of a header cell
  609. * @param int $row
  610. * @param int $col
  611. * @param mixed $contents
  612. * @param mixed $attributes Associative array or string of
  613. * table row attributes
  614. * @param int $body (optional) The index of the body to set.
  615. * @access public
  616. * @throws PEAR_Error
  617. */
  618. function setHeaderContents($row, $col, $contents, $attributes = null,
  619. $body = 0)
  620. {
  621. $ret = $this->_adjustTbodyCount($body, 'setHeaderContents');
  622. if (PEAR::isError($ret)) {
  623. return $ret;
  624. }
  625. $this->_tbodies[$body]->setHeaderContents($row, $col, $contents, $attributes);
  626. }
  627. /**
  628. * Adds a table row and returns the row identifier
  629. * @param array $contents (optional) Must be a indexed array of
  630. * valid cell contents
  631. * @param mixed $attributes (optional) Associative array or string
  632. * of table row attributes. This can also
  633. * be an array of attributes, in which
  634. * case the attributes will be repeated
  635. * in a loop.
  636. * @param string $type (optional) Cell type either 'th' or 'td'
  637. * @param bool $inTR false if attributes are to be applied
  638. * in TD tags; true if attributes are to
  639. * �be applied in TR tag
  640. * @param int $body (optional) The index of the body to use.
  641. * @return int
  642. * @access public
  643. * @throws PEAR_Error
  644. */
  645. function addRow($contents = null, $attributes = null, $type = 'td',
  646. $inTR = false, $body = 0)
  647. {
  648. $ret = $this->_adjustTbodyCount($body, 'addRow');
  649. if (PEAR::isError($ret)) {
  650. return $ret;
  651. }
  652. $ret = $this->_tbodies[$body]->addRow($contents, $attributes, $type, $inTR);
  653. return $ret;
  654. }
  655. /**
  656. * Sets the row attributes for an existing row
  657. * @param int $row Row index
  658. * @param mixed $attributes Associative array or string of table row
  659. * attributes. This can also be an array of
  660. * attributes, in which case the attributes
  661. * will be repeated in a loop.
  662. * @param bool $inTR false if attributes are to be applied in
  663. * TD tags; true if attributes are to be
  664. * applied in TR tag
  665. * @param int $body (optional) The index of the body to set.
  666. * @access public
  667. * @throws PEAR_Error
  668. */
  669. function setRowAttributes($row, $attributes, $inTR = false, $body = 0)
  670. {
  671. $ret = $this->_adjustTbodyCount($body, 'setRowAttributes');
  672. if (PEAR::isError($ret)) {
  673. return $ret;
  674. }
  675. $ret = $this->_tbodies[$body]->setRowAttributes($row, $attributes, $inTR);
  676. if (PEAR::isError($ret)) {
  677. return $ret;
  678. }
  679. }
  680. /**
  681. * Updates the row attributes for an existing row
  682. * @param int $row Row index
  683. * @param mixed $attributes Associative array or string of table row
  684. * attributes
  685. * @param bool $inTR false if attributes are to be applied in
  686. * TD tags; true if attributes are to be
  687. * applied in TR tag
  688. * @param int $body (optional) The index of the body to set.
  689. * @access public
  690. * @throws PEAR_Error
  691. */
  692. function updateRowAttributes($row, $attributes = null, $inTR = false,
  693. $body = 0)
  694. {
  695. $ret = $this->_adjustTbodyCount($body, 'updateRowAttributes');
  696. if (PEAR::isError($ret)) {
  697. return $ret;
  698. }
  699. $ret = $this->_tbodies[$body]->updateRowAttributes($row, $attributes, $inTR);
  700. if (PEAR::isError($ret)) {
  701. return $ret;
  702. }
  703. }
  704. /**
  705. * Returns the attributes for a given row as contained in the TR tag
  706. * @param int $row Row index
  707. * @param int $body (optional) The index of the body to get.
  708. * @return array
  709. * @access public
  710. * @throws PEAR_Error
  711. */
  712. function getRowAttributes($row, $body = 0)
  713. {
  714. $ret = $this->_adjustTbodyCount($body, 'getRowAttributes');
  715. if (PEAR::isError($ret)) {
  716. return $ret;
  717. }
  718. return $this->_tbodies[$body]->getRowAttributes($row);
  719. }
  720. /**
  721. * Alternates the row attributes starting at $start
  722. * @param int $start Row index of row in which alternating
  723. * begins
  724. * @param mixed $attributes1 Associative array or string of table
  725. * row attributes
  726. * @param mixed $attributes2 Associative array or string of table
  727. * row attributes
  728. * @param bool $inTR false if attributes are to be applied
  729. * in TD tags; true if attributes are to
  730. * be applied in TR tag
  731. * @param int $firstAttributes (optional) Which attributes should be
  732. * applied to the first row, 1 or 2.
  733. * @param int $body (optional) The index of the body to set.
  734. * Pass null to set for all bodies.
  735. * @access public
  736. * @throws PEAR_Error
  737. */
  738. function altRowAttributes($start, $attributes1, $attributes2, $inTR = false,
  739. $firstAttributes = 1, $body = null)
  740. {
  741. if (!is_null($body)) {
  742. $ret = $this->_adjustTbodyCount($body, 'altRowAttributes');
  743. if (PEAR::isError($ret)) {
  744. return $ret;
  745. }
  746. $this->_tbodies[$body]->altRowAttributes($start, $attributes1,
  747. $attributes2, $inTR, $firstAttributes);
  748. } else {
  749. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  750. $this->_tbodies[$i]->altRowAttributes($start, $attributes1,
  751. $attributes2, $inTR, $firstAttributes);
  752. // if the tbody's row count is odd, toggle $firstAttributes to
  753. // prevent the next tbody's first row from having the same
  754. // attributes as this tbody's last row.
  755. if ($this->_tbodies[$i]->getRowCount() % 2) {
  756. $firstAttributes ^= 3;
  757. }
  758. }
  759. }
  760. }
  761. /**
  762. * Adds a table column and returns the column identifier
  763. * @param array $contents (optional) Must be a indexed array of
  764. * valid cell contents
  765. * @param mixed $attributes (optional) Associative array or string
  766. * of table row attributes
  767. * @param string $type (optional) Cell type either 'th' or 'td'
  768. * @param int $body (optional) The index of the body to use.
  769. * @return int
  770. * @access public
  771. * @throws PEAR_Error
  772. */
  773. function addCol($contents = null, $attributes = null, $type = 'td', $body = 0)
  774. {
  775. $ret = $this->_adjustTbodyCount($body, 'addCol');
  776. if (PEAR::isError($ret)) {
  777. return $ret;
  778. }
  779. return $this->_tbodies[$body]->addCol($contents, $attributes, $type);
  780. }
  781. /**
  782. * Sets the column attributes for an existing column
  783. * @param int $col Column index
  784. * @param mixed $attributes (optional) Associative array or string
  785. * of table row attributes
  786. * @param int $body (optional) The index of the body to set.
  787. * Pass null to set for all bodies.
  788. * @access public
  789. * @throws PEAR_Error
  790. */
  791. function setColAttributes($col, $attributes = null, $body = null)
  792. {
  793. if (!is_null($body)) {
  794. $ret = $this->_adjustTbodyCount($body, 'setColAttributes');
  795. if (PEAR::isError($ret)) {
  796. return $ret;
  797. }
  798. $this->_tbodies[$body]->setColAttributes($col, $attributes);
  799. } else {
  800. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  801. $this->_tbodies[$i]->setColAttributes($col, $attributes);
  802. }
  803. }
  804. }
  805. /**
  806. * Updates the column attributes for an existing column
  807. * @param int $col Column index
  808. * @param mixed $attributes (optional) Associative array or
  809. * string of table row attributes
  810. * @param int $body (optional) The index of the body to set.
  811. * Pass null to set for all bodies.
  812. * @access public
  813. * @throws PEAR_Error
  814. */
  815. function updateColAttributes($col, $attributes = null, $body = null)
  816. {
  817. if (!is_null($body)) {
  818. $ret = $this->_adjustTbodyCount($body, 'updateColAttributes');
  819. if (PEAR::isError($ret)) {
  820. return $ret;
  821. }
  822. $this->_tbodies[$body]->updateColAttributes($col, $attributes);
  823. } else {
  824. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  825. $this->_tbodies[$i]->updateColAttributes($col, $attributes);
  826. }
  827. }
  828. }
  829. /**
  830. * Sets the attributes for all cells
  831. * @param mixed $attributes (optional) Associative array or
  832. * string of table row attributes
  833. * @param int $body (optional) The index of the body to set.
  834. * Pass null to set for all bodies.
  835. * @access public
  836. * @throws PEAR_Error
  837. */
  838. function setAllAttributes($attributes = null, $body = null)
  839. {
  840. if (!is_null($body)) {
  841. $ret = $this->_adjustTbodyCount($body, 'setAllAttributes');
  842. if (PEAR::isError($ret)) {
  843. return $ret;
  844. }
  845. $this->_tbodies[$body]->setAllAttributes($attributes);
  846. } else {
  847. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  848. $this->_tbodies[$i]->setAllAttributes($attributes);
  849. }
  850. }
  851. }
  852. /**
  853. * Updates the attributes for all cells
  854. * @param mixed $attributes (optional) Associative array or string
  855. * of table row attributes
  856. * @param int $body (optional) The index of the body to set.
  857. * Pass null to set for all bodies.
  858. * @access public
  859. * @throws PEAR_Error
  860. */
  861. function updateAllAttributes($attributes = null, $body = null)
  862. {
  863. if (!is_null($body)) {
  864. $ret = $this->_adjustTbodyCount($body, 'updateAllAttributes');
  865. if (PEAR::isError($ret)) {
  866. return $ret;
  867. }
  868. $this->_tbodies[$body]->updateAllAttributes($attributes);
  869. } else {
  870. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  871. $this->_tbodies[$i]->updateAllAttributes($attributes);
  872. }
  873. }
  874. }
  875. /**
  876. * Returns the table structure as HTML
  877. * @access public
  878. * @return string
  879. */
  880. function toHtml()
  881. {
  882. $strHtml = '';
  883. $tabs = $this->_getTabs();
  884. $tab = $this->_getTab();
  885. $lnEnd = $this->_getLineEnd();
  886. $tBodyColCounts = array();
  887. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  888. $tBodyColCounts[] = $this->_tbodies[$i]->getColCount();
  889. }
  890. $tBodyMaxColCount = 0;
  891. if (count($tBodyColCounts) > 0) {
  892. $tBodyMaxColCount = max($tBodyColCounts);
  893. }
  894. if ($this->_comment) {
  895. $strHtml .= $tabs . "<!-- $this->_comment -->" . $lnEnd;
  896. }
  897. if ($this->getRowCount() > 0 && $tBodyMaxColCount > 0) {
  898. $strHtml .=
  899. $tabs . '<table' . $this->_getAttrString($this->_attributes) . '>' . $lnEnd;
  900. if (!empty($this->_caption)) {
  901. $attr = $this->_caption['attr'];
  902. $contents = $this->_caption['contents'];
  903. $strHtml .= $tabs . $tab . '<caption' . $this->_getAttrString($attr) . '>';
  904. if (is_array($contents)) {
  905. $contents = implode(', ', $contents);
  906. }
  907. $strHtml .= $contents;
  908. $strHtml .= '</caption>' . $lnEnd;
  909. }
  910. if (!empty($this->_colgroup)) {
  911. foreach ($this->_colgroup as $g => $col) {
  912. $attr = $this->_colgroup[$g]['attr'];
  913. $contents = $this->_colgroup[$g]['contents'];
  914. $strHtml .= $tabs . $tab . '<colgroup' . $this->_getAttrString($attr) . '>';
  915. if (!empty($contents)) {
  916. $strHtml .= $lnEnd;
  917. if (!is_array($contents)) {
  918. $contents = array($contents);
  919. }
  920. foreach ($contents as $a => $colAttr) {
  921. $attr = $this->_parseAttributes($colAttr);
  922. $strHtml .= $tabs . $tab . $tab . '<col' . $this->_getAttrString($attr) . ' />' . $lnEnd;
  923. }
  924. $strHtml .= $tabs . $tab;
  925. }
  926. $strHtml .= '</colgroup>' . $lnEnd;
  927. }
  928. }
  929. if ($this->_useTGroups) {
  930. $tHeadColCount = 0;
  931. if ($this->_thead !== null) {
  932. $tHeadColCount = $this->_thead->getColCount();
  933. }
  934. $tFootColCount = 0;
  935. if ($this->_tfoot !== null) {
  936. $tFootColCount = $this->_tfoot->getColCount();
  937. }
  938. $maxColCount = max($tHeadColCount, $tFootColCount, $tBodyMaxColCount);
  939. if ($this->_thead !== null) {
  940. $this->_thead->setColCount($maxColCount);
  941. if ($this->_thead->getRowCount() > 0) {
  942. $strHtml .= $tabs . $tab . '<thead' .
  943. $this->_getAttrString($this->_thead->_attributes) .
  944. '>' . $lnEnd;
  945. $strHtml .= $this->_thead->toHtml($tabs, $tab);
  946. $strHtml .= $tabs . $tab . '</thead>' . $lnEnd;
  947. }
  948. }
  949. if ($this->_tfoot !== null) {
  950. $this->_tfoot->setColCount($maxColCount);
  951. if ($this->_tfoot->getRowCount() > 0) {
  952. $strHtml .= $tabs . $tab . '<tfoot' .
  953. $this->_getAttrString($this->_tfoot->_attributes) .
  954. '>' . $lnEnd;
  955. $strHtml .= $this->_tfoot->toHtml($tabs, $tab);
  956. $strHtml .= $tabs . $tab . '</tfoot>' . $lnEnd;
  957. }
  958. }
  959. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  960. $this->_tbodies[$i]->setColCount($maxColCount);
  961. if ($this->_tbodies[$i]->getRowCount() > 0) {
  962. $strHtml .= $tabs . $tab . '<tbody' .
  963. $this->_getAttrString($this->_tbodies[$i]->_attributes) .
  964. '>' . $lnEnd;
  965. $strHtml .= $this->_tbodies[$i]->toHtml($tabs, $tab);
  966. $strHtml .= $tabs . $tab . '</tbody>' . $lnEnd;
  967. }
  968. }
  969. } else {
  970. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  971. $this->_tbodies[$i]->setAutoFill($this->_autoFill);
  972. $strHtml .= $this->_tbodies[$i]->toHtml($tabs, $tab);
  973. }
  974. }
  975. $strHtml .= $tabs . '</table>' . $lnEnd;
  976. }
  977. return $strHtml;
  978. }
  979. }
  980. ?>