PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/pear/HTML/Table.php

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