Table.php 37 KB

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