PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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