PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/php-pear-HTML-Table-1.8.3/HTML_Table-1.8.3/Table.php

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