Table.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * PEAR::HTML_Table makes the design of HTML tables easy, flexible, reusable and
  5. * efficient.
  6. *
  7. * The PEAR::HTML_Table package provides methods for easy and efficient design
  8. * of HTML tables.
  9. * - Lots of customization options.
  10. * - Tables can be modified at any time.
  11. * - The logic is the same as standard HTML editors.
  12. * - Handles col and rowspans.
  13. * - PHP code is shorter, easier to read and to maintain.
  14. * - Tables options can be reused.
  15. *
  16. * For auto filling of data and such then check out
  17. * http://pear.php.net/package/HTML_Table_Matrix
  18. *
  19. * PHP versions 4 and 5
  20. *
  21. * LICENSE:
  22. *
  23. * Copyright (c) 2005-2007, Adam Daniel <adaniel1@eesus.jnj.com>,
  24. * Bertrand Mansion <bmansion@mamasam.com>,
  25. * Mark Wiesemann <wiesemann@php.net>
  26. * All rights reserved.
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. * * Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in the
  36. * documentation and/or other materials provided with the distribution.
  37. * * The names of the authors may not be used to endorse or promote products
  38. * derived from this software without specific prior written permission.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  41. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  42. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  44. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  45. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  46. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  47. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  48. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  49. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  50. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. * @category HTML
  53. * @package HTML_Table
  54. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  55. * @author Bertrand Mansion <bmansion@mamasam.com>
  56. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  57. * @version CVS: $Id: Table.php,v 1.39 2007/06/25 16:44:43 wiesemann Exp $
  58. * @link http://pear.php.net/package/HTML_Table
  59. */
  60. /**
  61. * PEAR::HTML_Table makes the design of HTML tables easy, flexible, reusable and efficient.
  62. *
  63. * The PEAR::HTML_Table package provides methods for easy and efficient design
  64. * of HTML tables.
  65. * - Lots of customization options.
  66. * - Tables can be modified at any time.
  67. * - The logic is the same as standard HTML editors.
  68. * - Handles col and rowspans.
  69. * - PHP code is shorter, easier to read and to maintain.
  70. * - Tables options can be reused.
  71. *
  72. * For auto filling of data and such then check out
  73. * http://pear.php.net/package/HTML_Table_Matrix
  74. *
  75. * @category HTML
  76. * @package HTML_Table
  77. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  78. * @author Bertrand Mansion <bmansion@mamasam.com>
  79. * @copyright 2005-2006 The PHP Group
  80. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  81. * @version Release: @package_version@
  82. * @link http://pear.php.net/package/HTML_Table
  83. */
  84. class HTML_Table extends HTML_Common
  85. {
  86. /**
  87. * Value to insert into empty cells. This is used as a default for
  88. * newly-created tbodies.
  89. * @var string
  90. * @access private
  91. */
  92. var $_autoFill = '&nbsp;';
  93. /**
  94. * Automatically adds a new row, column, or body if a given row, column, or
  95. * body index does not exist.
  96. * This is used as a default for newly-created tbodies.
  97. * @var bool
  98. * @access private
  99. */
  100. var $_autoGrow = true;
  101. /**
  102. * Array containing the table caption
  103. * @var array
  104. * @access private
  105. */
  106. var $_caption = array();
  107. /**
  108. * Array containing the table column group specifications
  109. *
  110. * @var array
  111. * @author Laurent Laville (pear at laurent-laville dot org)
  112. * @access private
  113. */
  114. var $_colgroup = array();
  115. /**
  116. * HTML_Table_Storage object for the (t)head of the table
  117. * @var object
  118. * @access private
  119. */
  120. var $_thead = null;
  121. /**
  122. * HTML_Table_Storage object for the (t)foot of the table
  123. * @var object
  124. * @access private
  125. */
  126. var $_tfoot = null;
  127. /**
  128. * HTML_Table_Storage object for the (t)body of the table
  129. * @var object
  130. * @access private
  131. */
  132. var $_tbodies = array();
  133. /**
  134. * Number of bodies in the table
  135. * @var int
  136. * @access private
  137. */
  138. var $_tbodyCount = 0;
  139. /**
  140. * Whether to use <thead>, <tfoot> and <tbody> or not
  141. * @var bool
  142. * @access private
  143. */
  144. var $_useTGroups = false;
  145. /**
  146. * Class constructor
  147. * @param array $attributes Associative array of table tag
  148. * attributes
  149. * @param int $tabOffset Tab offset of the table
  150. * @param bool $useTGroups Whether to use <thead>, <tfoot> and
  151. * <tbody> or not
  152. * @access public
  153. */
  154. public function __construct($attributes = null, $tabOffset = 0, $useTGroups = false)
  155. {
  156. parent::__construct($attributes, (int)$tabOffset);
  157. $this->_useTGroups = (boolean)$useTGroups;
  158. $this->addBody();
  159. if ($this->_useTGroups) {
  160. $this->_thead = new HTML_Table_Storage($tabOffset, $this->_useTGroups);
  161. $this->_tfoot = new HTML_Table_Storage($tabOffset, $this->_useTGroups);
  162. }
  163. }
  164. /**
  165. * Returns the API version
  166. * @access public
  167. * @return double
  168. * @deprecated
  169. */
  170. function apiVersion()
  171. {
  172. return 1.7;
  173. }
  174. /**
  175. * Returns the HTML_Table_Storage object for <thead>
  176. * @access public
  177. * @return object
  178. */
  179. function &getHeader()
  180. {
  181. if (is_null($this->_thead)) {
  182. $this->_useTGroups = true;
  183. $this->_thead = new HTML_Table_Storage($this->_tabOffset,
  184. $this->_useTGroups);
  185. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  186. $this->_tbodies[$i]->setUseTGroups(true);
  187. }
  188. }
  189. return $this->_thead;
  190. }
  191. /**
  192. * Returns the HTML_Table_Storage object for <tfoot>
  193. * @access public
  194. * @return object
  195. */
  196. function &getFooter()
  197. {
  198. if (is_null($this->_tfoot)) {
  199. $this->_useTGroups = true;
  200. $this->_tfoot = new HTML_Table_Storage($this->_tabOffset,
  201. $this->_useTGroups);
  202. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  203. $this->_tbodies[$i]->setUseTGroups(true);
  204. }
  205. }
  206. return $this->_tfoot;
  207. }
  208. /**
  209. * Returns the HTML_Table_Storage object for the specified <tbody>
  210. * (or the whole table if <t{head|foot|body}> is not used)
  211. * @param int $body (optional) The index of the body to
  212. * return.
  213. * @access public
  214. * @return object
  215. * @throws PEAR_Error
  216. */
  217. function &getBody($body = 0)
  218. {
  219. $ret = $this->_adjustTbodyCount($body, 'getBody');
  220. if (PEAR::isError($ret)) {
  221. return $ret;
  222. }
  223. return $this->_tbodies[$body];
  224. }
  225. /**
  226. * Adds a table body and returns the body identifier
  227. * @param mixed $attributes (optional) Associative array or
  228. * string of table body attributes
  229. * @access public
  230. * @return int
  231. */
  232. function addBody($attributes = null)
  233. {
  234. if (!$this->_useTGroups && $this->_tbodyCount > 0) {
  235. for ($i = 0; $i < $this->_tbodyCount; $i++) {
  236. $this->_tbodies[$i]->setUseTGroups(true);
  237. }
  238. $this->_useTGroups = true;
  239. }
  240. $body = $this->_tbodyCount++;
  241. $this->_tbodies[$body] = new HTML_Table_Storage($this->_tabOffset,
  242. $this->_useTGroups);
  243. $this->_tbodies[$body]->setAutoFill($this->_autoFill);
  244. $this->_tbodies[$body]->setAttributes($attributes);
  245. return $body;
  246. }
  247. /**
  248. * Adjusts the number of bodies
  249. * @param int $body Body index
  250. * @param string $method Name of calling method
  251. * @access private
  252. * @throws PEAR_Error
  253. */
  254. function _adjustTbodyCount($body, $method)
  255. {
  256. if ($this->_autoGrow) {
  257. while ($this->_tbodyCount <= (int)$body) {
  258. $this->addBody();
  259. }
  260. } else {
  261. throw new \Exception('Invalid body reference[' .$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. ?>