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

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

https://bitbucket.org/chamilo/chamilo/
PHP | 899 lines | 444 code | 54 blank | 401 comment | 115 complexity | aa3f15305551f659171b2fea620b6893 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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. * Value to insert into empty cells
  71. * @var string
  72. * @access private
  73. */
  74. var $_autoFill = '&nbsp;';
  75. /**
  76. * Automatically adds a new row or column if a given row or column index
  77. * does not exist
  78. * @var bool
  79. * @access private
  80. */
  81. var $_autoGrow = true;
  82. /**
  83. * Array containing the table structure
  84. * @var array
  85. * @access private
  86. */
  87. var $_structure = array();
  88. /**
  89. * Number of rows composing in the table
  90. * @var int
  91. * @access private
  92. */
  93. var $_rows = 0;
  94. /**
  95. * Number of column composing the table
  96. * @var int
  97. * @access private
  98. */
  99. var $_cols = 0;
  100. /**
  101. * Tracks the level of nested tables
  102. * @var int
  103. * @access private
  104. */
  105. var $_nestLevel = 0;
  106. /**
  107. * Whether to use <thead>, <tfoot> and <tbody> or not
  108. * @var bool
  109. * @access private
  110. */
  111. var $_useTGroups = false;
  112. /**
  113. * Class constructor
  114. * @param int $tabOffset
  115. * @param bool $useTGroups Whether to use <thead>, <tfoot> and
  116. * <tbody> or not
  117. * @access public
  118. */
  119. function __construct($tabOffset = 0, $useTGroups = false)
  120. {
  121. parent::__construct(null, (int)$tabOffset);
  122. $this->_useTGroups = (boolean)$useTGroups;
  123. }
  124. /**
  125. * Sets the useTGroups value
  126. * @param boolean $useTGroups
  127. * @access public
  128. */
  129. function setUseTGroups($useTGroups)
  130. {
  131. $this->_useTGroups = $useTGroups;
  132. }
  133. /**
  134. * Returns the useTGroups value
  135. * @access public
  136. * @return boolean
  137. */
  138. function getUseTGroups()
  139. {
  140. return $this->_useTGroups;
  141. }
  142. /**
  143. * Sets the autoFill value
  144. * @param mixed $fill
  145. * @access public
  146. */
  147. function setAutoFill($fill)
  148. {
  149. $this->_autoFill = $fill;
  150. }
  151. /**
  152. * Returns the autoFill value
  153. * @access public
  154. * @return mixed
  155. */
  156. function getAutoFill()
  157. {
  158. return $this->_autoFill;
  159. }
  160. /**
  161. * Sets the autoGrow value
  162. * @param bool $fill
  163. * @access public
  164. */
  165. function setAutoGrow($grow)
  166. {
  167. $this->_autoGrow = $grow;
  168. }
  169. /**
  170. * Returns the autoGrow value
  171. * @access public
  172. * @return mixed
  173. */
  174. function getAutoGrow()
  175. {
  176. return $this->_autoGrow;
  177. }
  178. /**
  179. * Sets the number of rows in the table
  180. * @param int $rows
  181. * @access public
  182. */
  183. function setRowCount($rows)
  184. {
  185. $this->_rows = $rows;
  186. }
  187. /**
  188. * Sets the number of columns in the table
  189. * @param int $cols
  190. * @access public
  191. */
  192. function setColCount($cols)
  193. {
  194. $this->_cols = $cols;
  195. }
  196. /**
  197. * Returns the number of rows in the table
  198. * @access public
  199. * @return int
  200. */
  201. function getRowCount()
  202. {
  203. return $this->_rows;
  204. }
  205. /**
  206. * Gets the number of columns in the table
  207. *
  208. * If a row index is specified, the count will not take
  209. * the spanned cells into account in the return value.
  210. *
  211. * @param int Row index to serve for cols count
  212. * @access public
  213. * @return int
  214. */
  215. function getColCount($row = null)
  216. {
  217. if (!is_null($row)) {
  218. $count = 0;
  219. foreach ($this->_structure[$row] as $cell) {
  220. if (is_array($cell)) {
  221. $count++;
  222. }
  223. }
  224. return $count;
  225. }
  226. return $this->_cols;
  227. }
  228. /**
  229. * Sets a rows type 'TH' or 'TD'
  230. * @param int $row Row index
  231. * @param string $type 'TH' or 'TD'
  232. * @access public
  233. */
  234. function setRowType($row, $type)
  235. {
  236. for ($counter = 0; $counter < $this->_cols; $counter++) {
  237. $this->_structure[$row][$counter]['type'] = $type;
  238. }
  239. }
  240. /**
  241. * Sets a columns type 'TH' or 'TD'
  242. * @param int $col Column index
  243. * @param string $type 'TH' or 'TD'
  244. * @access public
  245. */
  246. function setColType($col, $type)
  247. {
  248. for ($counter = 0; $counter < $this->_rows; $counter++) {
  249. $this->_structure[$counter][$col]['type'] = $type;
  250. }
  251. }
  252. /**
  253. * Sets the cell attributes for an existing cell.
  254. *
  255. * If the given indices do not exist and autoGrow is true then the given
  256. * row and/or col is automatically added. If autoGrow is false then an
  257. * error is returned.
  258. * @param int $row Row index
  259. * @param int $col Column index
  260. * @param mixed $attributes Associative array or string of table
  261. * row attributes
  262. * @access public
  263. * @throws PEAR_Error
  264. */
  265. function setCellAttributes($row, $col, $attributes)
  266. {
  267. if ( isset($this->_structure[$row][$col])
  268. && $this->_structure[$row][$col] == '__SPANNED__'
  269. ) {
  270. return;
  271. }
  272. $attributes = $this->_parseAttributes($attributes);
  273. $err = $this->_adjustEnds($row, $col, 'setCellAttributes', $attributes);
  274. if (PEAR::isError($err)) {
  275. return $err;
  276. }
  277. $this->_structure[$row][$col]['attr'] = $attributes;
  278. $this->_updateSpanGrid($row, $col);
  279. }
  280. /**
  281. * Updates the cell attributes passed but leaves other existing attributes
  282. * intact
  283. * @param int $row Row index
  284. * @param int $col Column index
  285. * @param mixed $attributes Associative array or string of table row
  286. * attributes
  287. * @access public
  288. */
  289. function updateCellAttributes($row, $col, $attributes)
  290. {
  291. if ( isset($this->_structure[$row][$col])
  292. && $this->_structure[$row][$col] == '__SPANNED__'
  293. ) {
  294. return;
  295. }
  296. $attributes = $this->_parseAttributes($attributes);
  297. $err = $this->_adjustEnds($row, $col, 'updateCellAttributes', $attributes);
  298. if (PEAR::isError($err)) {
  299. return $err;
  300. }
  301. $this->_updateAttrArray($this->_structure[$row][$col]['attr'], $attributes);
  302. $this->_updateSpanGrid($row, $col);
  303. }
  304. /**
  305. * Returns the attributes for a given cell
  306. * @param int $row Row index
  307. * @param int $col Column index
  308. * @return array
  309. * @access public
  310. */
  311. function getCellAttributes($row, $col)
  312. {
  313. if ( isset($this->_structure[$row][$col])
  314. && $this->_structure[$row][$col] != '__SPANNED__'
  315. ) {
  316. return $this->_structure[$row][$col]['attr'];
  317. } elseif (!isset($this->_structure[$row][$col])) {
  318. return PEAR::raiseError('Invalid table cell reference[' .
  319. $row . '][' . $col . '] in HTML_Table::getCellAttributes');
  320. }
  321. return;
  322. }
  323. /**
  324. * Sets the cell contents for an existing cell
  325. *
  326. * If the given indices do not exist and autoGrow is true then the given
  327. * row and/or col is automatically added. If autoGrow is false then an
  328. * error is returned.
  329. * @param int $row Row index
  330. * @param int $col Column index
  331. * @param mixed $contents May contain html or any object with a
  332. * toHTML() method; if it is an array (with
  333. * strings and/or objects), $col will be used
  334. * as start offset and the array elements will
  335. * be set to this and the following columns
  336. * in $row
  337. * @param string $type (optional) Cell type either 'TH' or 'TD'
  338. * @access public
  339. * @throws PEAR_Error
  340. */
  341. function setCellContents($row, $col, $contents, $type = 'TD')
  342. {
  343. if (is_array($contents)) {
  344. foreach ($contents as $singleContent) {
  345. $ret = $this->_setSingleCellContents($row, $col, $singleContent,
  346. $type);
  347. if (PEAR::isError($ret)) {
  348. return $ret;
  349. }
  350. $col++;
  351. }
  352. } else {
  353. $ret = $this->_setSingleCellContents($row, $col, $contents, $type);
  354. if (PEAR::isError($ret)) {
  355. return $ret;
  356. }
  357. }
  358. }
  359. /**
  360. * Sets the cell contents for a single existing cell
  361. *
  362. * If the given indices do not exist and autoGrow is true then the given
  363. * row and/or col is automatically added. If autoGrow is false then an
  364. * error is returned.
  365. * @param int $row Row index
  366. * @param int $col Column index
  367. * @param mixed $contents May contain html or any object with a
  368. * toHTML() method; if it is an array (with
  369. * strings and/or objects), $col will be used
  370. * as start offset and the array elements will
  371. * be set to this and the following columns
  372. * in $row
  373. * @param string $type (optional) Cell type either 'TH' or 'TD'
  374. * @access private
  375. * @throws PEAR_Error
  376. */
  377. function _setSingleCellContents($row, $col, $contents, $type = 'TD')
  378. {
  379. if ( isset($this->_structure[$row][$col])
  380. && $this->_structure[$row][$col] == '__SPANNED__'
  381. ) {
  382. return;
  383. }
  384. $err = $this->_adjustEnds($row, $col, 'setCellContents');
  385. if (PEAR::isError($err)) {
  386. return $err;
  387. }
  388. $this->_structure[$row][$col]['contents'] = $contents;
  389. $this->_structure[$row][$col]['type'] = $type;
  390. }
  391. /**
  392. * Returns the cell contents for an existing cell
  393. * @param int $row Row index
  394. * @param int $col Column index
  395. * @access public
  396. * @return mixed
  397. */
  398. function getCellContents($row, $col)
  399. {
  400. if ( isset($this->_structure[$row][$col])
  401. && $this->_structure[$row][$col] == '__SPANNED__'
  402. ) {
  403. return;
  404. }
  405. if (!isset($this->_structure[$row][$col])) {
  406. return null;
  407. //return PEAR::raiseError('Invalid table cell reference[' .
  408. // $row . '][' . $col . '] in HTML_Table::getCellContents');
  409. }
  410. return $this->_structure[$row][$col]['contents'];
  411. }
  412. /**
  413. * Sets the contents of a header cell
  414. * @param int $row
  415. * @param int $col
  416. * @param mixed $contents
  417. * @param mixed $attributes Associative array or string of table row
  418. * attributes
  419. * @access public
  420. */
  421. function setHeaderContents($row, $col, $contents, $attributes = null)
  422. {
  423. $this->setCellContents($row, $col, $contents, 'TH');
  424. if (!is_null($attributes)) {
  425. $this->updateCellAttributes($row, $col, $attributes);
  426. }
  427. }
  428. /**
  429. * Adds a table row and returns the row identifier
  430. * @param array $contents (optional) Must be a indexed array of valid
  431. * cell contents
  432. * @param mixed $attributes (optional) Associative array or string of
  433. * table row attributes. This can
  434. * also be an array of attributes,
  435. * in which case the attributes
  436. * will be repeated in a loop.
  437. * @param string $type (optional) Cell type either 'th' or 'td'
  438. * @param bool $inTR false if attributes are to be
  439. * applied in TD tags; true if
  440. * attributes are to be applied in
  441. * TR tag
  442. * @return int
  443. * @access public
  444. */
  445. function addRow($contents = null, $attributes = null, $type = 'td',
  446. $inTR = false)
  447. {
  448. if (isset($contents) && !is_array($contents)) {
  449. return PEAR::raiseError('First parameter to HTML_Table::addRow ' .
  450. 'must be an array');
  451. }
  452. if (is_null($contents)) {
  453. $contents = array();
  454. }
  455. $type = strtolower($type);
  456. $row = $this->_rows++;
  457. foreach ($contents as $col => $content) {
  458. if ($type == 'td') {
  459. $this->setCellContents($row, $col, $content);
  460. } elseif ($type == 'th') {
  461. $this->setHeaderContents($row, $col, $content);
  462. }
  463. }
  464. $this->setRowAttributes($row, $attributes, $inTR);
  465. return $row;
  466. }
  467. /**
  468. * Sets the row attributes for an existing row
  469. * @param int $row Row index
  470. * @param mixed $attributes Associative array or string of table
  471. * row attributes. This can also be an
  472. * array of attributes, in which case the
  473. * attributes will be repeated in a loop.
  474. * @param bool $inTR false if attributes are to be applied
  475. * in TD tags; true if attributes are to
  476. * be applied in TR tag
  477. * @access public
  478. * @throws PEAR_Error
  479. */
  480. function setRowAttributes($row, $attributes, $inTR = false)
  481. {
  482. if (!$inTR) {
  483. $multiAttr = $this->_isAttributesArray($attributes);
  484. for ($i = 0; $i < $this->_cols; $i++) {
  485. if ($multiAttr) {
  486. $this->setCellAttributes($row, $i,
  487. $attributes[$i - ((ceil(($i + 1) / count($attributes))) - 1) * count($attributes)]);
  488. } else {
  489. $this->setCellAttributes($row, $i, $attributes);
  490. }
  491. }
  492. } else {
  493. $attributes = $this->_parseAttributes($attributes);
  494. $err = $this->_adjustEnds($row, 0, 'setRowAttributes', $attributes);
  495. if (PEAR::isError($err)) {
  496. return $err;
  497. }
  498. $this->_structure[$row]['attr'] = $attributes;
  499. }
  500. }
  501. /**
  502. * Updates the row attributes for an existing row
  503. * @param int $row Row index
  504. * @param mixed $attributes Associative array or string of table
  505. * row attributes
  506. * @param bool $inTR false if attributes are to be applied
  507. * in TD tags; true if attributes are to
  508. * be applied in TR tag
  509. * @access public
  510. * @throws PEAR_Error
  511. */
  512. function updateRowAttributes($row, $attributes = null, $inTR = false)
  513. {
  514. if (!$inTR) {
  515. $multiAttr = $this->_isAttributesArray($attributes);
  516. for ($i = 0; $i < $this->_cols; $i++) {
  517. if ($multiAttr) {
  518. $this->updateCellAttributes($row, $i,
  519. $attributes[$i - ((ceil(($i + 1) / count($attributes))) - 1) * count($attributes)]);
  520. } else {
  521. $this->updateCellAttributes($row, $i, $attributes);
  522. }
  523. }
  524. } else {
  525. $attributes = $this->_parseAttributes($attributes);
  526. $err = $this->_adjustEnds($row, 0, 'updateRowAttributes', $attributes);
  527. if (PEAR::isError($err)) {
  528. return $err;
  529. }
  530. $this->_updateAttrArray($this->_structure[$row]['attr'], $attributes);
  531. }
  532. }
  533. /**
  534. * Returns the attributes for a given row as contained in the TR tag
  535. * @param int $row Row index
  536. * @return array
  537. * @access public
  538. */
  539. function getRowAttributes($row)
  540. {
  541. if (isset($this->_structure[$row]['attr'])) {
  542. return $this->_structure[$row]['attr'];
  543. }
  544. return;
  545. }
  546. /**
  547. * Alternates the row attributes starting at $start
  548. * @param int $start Row index of row in which alternating
  549. * begins
  550. * @param mixed $attributes1 Associative array or string of table
  551. * row attributes
  552. * @param mixed $attributes2 Associative array or string of table
  553. * row attributes
  554. * @param bool $inTR false if attributes are to be applied
  555. * in TD tags; true if attributes are to
  556. * be applied in TR tag
  557. * @param int $firstAttributes (optional) Which attributes should be
  558. * applied to the first row, 1 or 2.
  559. * @access public
  560. */
  561. function altRowAttributes($start, $attributes1, $attributes2, $inTR = false,
  562. $firstAttributes = 1)
  563. {
  564. for ($row = $start; $row < $this->_rows; $row++) {
  565. if (($row + $start + ($firstAttributes - 1)) % 2 == 0) {
  566. $attributes = $attributes1;
  567. } else {
  568. $attributes = $attributes2;
  569. }
  570. $this->updateRowAttributes($row, $attributes, $inTR);
  571. }
  572. }
  573. /**
  574. * Alternates the col attributes starting at $start
  575. * @param int $start Col index of col in which alternating
  576. * begins
  577. * @param mixed $attributes1 Associative array or string of table
  578. * col attributes
  579. * @param mixed $attributes2 Associative array or string of table
  580. * col attributes
  581. * @param bool $inTR false if attributes are to be applied
  582. * in TD tags; true if attributes are to
  583. * be applied in TR tag
  584. * @param int $firstAttributes (optional) Which attributes should be
  585. * applied to the first row, 1 or 2.
  586. * @access public
  587. */
  588. function altColAttributes($start, $attributes1, $attributes2, $inTR = false,
  589. $firstAttributes = 1)
  590. {
  591. for ($col = $start; $col < $this->_cols; $col++) {
  592. if (($col + $start + ($firstAttributes - 1)) % 2 == 0) {
  593. $attributes = $attributes1;
  594. } else {
  595. $attributes = $attributes2;
  596. }
  597. $this->updateColAttributes($col, $attributes, $inTR);
  598. }
  599. }
  600. /**
  601. * Adds a table column and returns the column identifier
  602. * @param array $contents (optional) Must be a indexed array of valid
  603. * cell contents
  604. * @param mixed $attributes (optional) Associative array or string of
  605. * table row attributes
  606. * @param string $type (optional) Cell type either 'th' or 'td'
  607. * @return int
  608. * @access public
  609. */
  610. function addCol($contents = null, $attributes = null, $type = 'td')
  611. {
  612. if (isset($contents) && !is_array($contents)) {
  613. return PEAR::raiseError('First parameter to HTML_Table::addCol ' .
  614. 'must be an array');
  615. }
  616. if (is_null($contents)) {
  617. $contents = array();
  618. }
  619. $type = strtolower($type);
  620. $col = $this->_cols++;
  621. foreach ($contents as $row => $content) {
  622. if ($type == 'td') {
  623. $this->setCellContents($row, $col, $content);
  624. } elseif ($type == 'th') {
  625. $this->setHeaderContents($row, $col, $content);
  626. }
  627. }
  628. $this->setColAttributes($col, $attributes);
  629. return $col;
  630. }
  631. /**
  632. * Sets the column attributes for an existing column
  633. * @param int $col Column index
  634. * @param mixed $attributes (optional) Associative array or string
  635. * of table row attributes
  636. * @access public
  637. */
  638. function setColAttributes($col, $attributes = null)
  639. {
  640. $multiAttr = $this->_isAttributesArray($attributes);
  641. for ($i = 0; $i < $this->_rows; $i++) {
  642. if ($multiAttr) {
  643. $this->setCellAttributes($i, $col,
  644. $attributes[$i - ((ceil(($i + 1) / count($attributes))) - 1) * count($attributes)]);
  645. } else {
  646. $this->setCellAttributes($i, $col, $attributes);
  647. }
  648. }
  649. }
  650. /**
  651. * Updates the column attributes for an existing column
  652. * @param int $col Column index
  653. * @param mixed $attributes (optional) Associative array or string
  654. * of table row attributes
  655. * @access public
  656. */
  657. function updateColAttributes($col, $attributes = null)
  658. {
  659. $multiAttr = $this->_isAttributesArray($attributes);
  660. for ($i = 0; $i < $this->_rows; $i++) {
  661. if ($multiAttr) {
  662. $this->updateCellAttributes($i, $col,
  663. $attributes[$i - ((ceil(($i + 1) / count($attributes))) - 1) * count($attributes)]);
  664. } else {
  665. $this->updateCellAttributes($i, $col, $attributes);
  666. }
  667. }
  668. }
  669. /**
  670. * Sets the attributes for all cells
  671. * @param mixed $attributes (optional) Associative array or
  672. * string of table row attributes
  673. * @access public
  674. */
  675. function setAllAttributes($attributes = null)
  676. {
  677. for ($i = 0; $i < $this->_rows; $i++) {
  678. $this->setRowAttributes($i, $attributes);
  679. }
  680. }
  681. /**
  682. * Updates the attributes for all cells
  683. * @param mixed $attributes (optional) Associative array or
  684. * string of table row attributes
  685. * @access public
  686. */
  687. function updateAllAttributes($attributes = null)
  688. {
  689. for ($i = 0; $i < $this->_rows; $i++) {
  690. $this->updateRowAttributes($i, $attributes);
  691. }
  692. }
  693. /**
  694. * Returns the table rows as HTML
  695. * @access public
  696. * @return string
  697. */
  698. function toHtml($tabs = null, $tab = null)
  699. {
  700. $strHtml = '';
  701. if (is_null($tabs)) {
  702. $tabs = $this->_getTabs();
  703. }
  704. if (is_null($tab)) {
  705. $tab = $this->_getTab();
  706. }
  707. $lnEnd = $this->_getLineEnd();
  708. if ($this->_useTGroups) {
  709. $extraTab = $tab;
  710. } else {
  711. $extraTab = '';
  712. }
  713. if ($this->_cols > 0) {
  714. for ($i = 0 ; $i < $this->_rows ; $i++) {
  715. $attr = '';
  716. if (isset($this->_structure[$i]['attr'])) {
  717. $attr = $this->_getAttrString($this->_structure[$i]['attr']);
  718. }
  719. $strHtml .= $tabs .$tab . $extraTab . '<tr'.$attr.'>' . $lnEnd;
  720. for ($j = 0 ; $j < $this->_cols ; $j++) {
  721. $attr = '';
  722. $contents = '';
  723. $type = 'td';
  724. if (isset($this->_structure[$i][$j]) && $this->_structure[$i][$j] == '__SPANNED__') {
  725. continue;
  726. }
  727. if (isset($this->_structure[$i][$j]['type'])) {
  728. $type = (strtolower($this->_structure[$i][$j]['type']) == 'th' ? 'th' : 'td');
  729. }
  730. if (isset($this->_structure[$i][$j]['attr'])) {
  731. $attr = $this->_structure[$i][$j]['attr'];
  732. }
  733. if (isset($this->_structure[$i][$j]['contents'])) {
  734. $contents = $this->_structure[$i][$j]['contents'];
  735. }
  736. $strHtml .= $tabs . $tab . $tab . $extraTab . "<$type" . $this->_getAttrString($attr) . '>';
  737. if (is_object($contents)) {
  738. // changes indent and line end settings on nested tables
  739. if (is_subclass_of($contents, 'html_common')) {
  740. $contents->setTab($tab . $extraTab);
  741. $contents->setTabOffset($this->_tabOffset + 3);
  742. $contents->_nestLevel = $this->_nestLevel + 1;
  743. $contents->setLineEnd($this->_getLineEnd());
  744. }
  745. if (method_exists($contents, 'toHtml')) {
  746. $contents = $contents->toHtml();
  747. } elseif (method_exists($contents, 'toString')) {
  748. $contents = $contents->toString();
  749. }
  750. }
  751. if (is_array($contents)) {
  752. $contents = implode(', ', $contents);
  753. }
  754. if (isset($this->_autoFill) && $contents === '') {
  755. $contents = $this->_autoFill;
  756. }
  757. $strHtml .= $contents;
  758. $strHtml .= "</$type>" . $lnEnd;
  759. }
  760. $strHtml .= $tabs . $tab . $extraTab . '</tr>' . $lnEnd;
  761. }
  762. }
  763. return $strHtml;
  764. }
  765. /**
  766. * Checks if rows or columns are spanned
  767. * @param int $row Row index
  768. * @param int $col Column index
  769. * @access private
  770. */
  771. function _updateSpanGrid($row, $col)
  772. {
  773. if (isset($this->_structure[$row][$col]['attr']['colspan'])) {
  774. $colspan = $this->_structure[$row][$col]['attr']['colspan'];
  775. }
  776. if (isset($this->_structure[$row][$col]['attr']['rowspan'])) {
  777. $rowspan = $this->_structure[$row][$col]['attr']['rowspan'];
  778. }
  779. if (isset($colspan)) {
  780. for ($j = $col + 1; (($j < $this->_cols) && ($j <= ($col + $colspan - 1))); $j++) {
  781. $this->_structure[$row][$j] = '__SPANNED__';
  782. }
  783. }
  784. if (isset($rowspan)) {
  785. for ($i = $row + 1; (($i < $this->_rows) && ($i <= ($row + $rowspan - 1))); $i++) {
  786. $this->_structure[$i][$col] = '__SPANNED__';
  787. }
  788. }
  789. if (isset($colspan) && isset($rowspan)) {
  790. for ($i = $row + 1; (($i < $this->_rows) && ($i <= ($row + $rowspan - 1))); $i++) {
  791. for ($j = $col + 1; (($j <= $this->_cols) && ($j <= ($col + $colspan - 1))); $j++) {
  792. $this->_structure[$i][$j] = '__SPANNED__';
  793. }
  794. }
  795. }
  796. }
  797. /**
  798. * Adjusts ends (total number of rows and columns)
  799. * @param int $row Row index
  800. * @param int $col Column index
  801. * @param string $method Method name of caller
  802. * Used to populate PEAR_Error if thrown.
  803. * @param array $attributes Assoc array of attributes
  804. * Default is an empty array.
  805. * @access private
  806. * @throws PEAR_Error
  807. */
  808. function _adjustEnds($row, $col, $method, $attributes = array())
  809. {
  810. $colspan = isset($attributes['colspan']) ? $attributes['colspan'] : 1;
  811. $rowspan = isset($attributes['rowspan']) ? $attributes['rowspan'] : 1;
  812. if (($row + $rowspan - 1) >= $this->_rows) {
  813. if ($this->_autoGrow) {
  814. $this->_rows = $row + $rowspan;
  815. } else {
  816. return PEAR::raiseError('Invalid table row reference[' .
  817. $row . '] in HTML_Table::' . $method);
  818. }
  819. }
  820. if (($col + $colspan - 1) >= $this->_cols) {
  821. if ($this->_autoGrow) {
  822. $this->_cols = $col + $colspan;
  823. } else {
  824. return PEAR::raiseError('Invalid table column reference[' .
  825. $col . '] in HTML_Table::' . $method);
  826. }
  827. }
  828. }
  829. /**
  830. * Tells if the parameter is an array of attribute arrays/strings
  831. * @param mixed $attributes Variable to test
  832. * @access private
  833. * @return bool
  834. */
  835. function _isAttributesArray($attributes)
  836. {
  837. if (is_array($attributes) && isset($attributes[0])) {
  838. if (is_array($attributes[0]) || (is_string($attributes[0]) && count($attributes) > 1)) {
  839. return true;
  840. }
  841. }
  842. return false;
  843. }
  844. }
  845. ?>