2012-05-06 18:25:40 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
* word processing documents.
|
|
|
|
|
*
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2012-05-06 18:25:40 +02:00
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @see https://github.com/PHPOffice/PHPWord
|
2018-03-08 23:52:25 +01:00
|
|
|
* @copyright 2010-2018 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2012-05-06 18:25:40 +02:00
|
|
|
*/
|
|
|
|
|
|
2014-03-31 01:13:02 +07:00
|
|
|
namespace PhpOffice\PhpWord\Element;
|
2013-12-11 14:45:44 -05:00
|
|
|
|
2014-04-03 08:44:41 +07:00
|
|
|
use PhpOffice\PhpWord\Style\Table as TableStyle;
|
2014-04-03 09:20:21 +07:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Table element
|
|
|
|
|
*/
|
2014-04-08 00:23:49 +07:00
|
|
|
class Table extends AbstractElement
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2013-12-11 14:45:44 -05:00
|
|
|
/**
|
|
|
|
|
* Table style
|
|
|
|
|
*
|
2014-04-16 17:22:30 +04:00
|
|
|
* @var \PhpOffice\PhpWord\Style\Table
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-04-03 08:44:41 +07:00
|
|
|
private $style;
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Table rows
|
|
|
|
|
*
|
2014-05-19 00:14:14 +07:00
|
|
|
* @var \PhpOffice\PhpWord\Element\Row[]
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-04-03 08:44:41 +07:00
|
|
|
private $rows = array();
|
2013-12-11 14:45:44 -05:00
|
|
|
|
2014-01-13 23:14:05 +07:00
|
|
|
/**
|
|
|
|
|
* Table width
|
|
|
|
|
*
|
2014-05-11 22:54:51 +07:00
|
|
|
* @var int
|
2014-01-13 23:14:05 +07:00
|
|
|
*/
|
2014-04-03 08:44:41 +07:00
|
|
|
private $width = null;
|
2014-01-13 23:14:05 +07:00
|
|
|
|
2013-12-11 14:45:44 -05:00
|
|
|
/**
|
|
|
|
|
* Create a new table
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $style
|
|
|
|
|
*/
|
2014-05-10 21:38:44 +07:00
|
|
|
public function __construct($style = null)
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-06-08 16:44:46 +07:00
|
|
|
$this->style = $this->setNewStyle(new TableStyle(), $style);
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a row
|
|
|
|
|
*
|
2014-05-11 22:54:51 +07:00
|
|
|
* @param int $height
|
2014-03-17 07:26:25 +07:00
|
|
|
* @param mixed $style
|
2014-05-11 22:54:51 +07:00
|
|
|
* @return \PhpOffice\PhpWord\Element\Row
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-02-22 11:01:15 +07:00
|
|
|
public function addRow($height = null, $style = null)
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-05-11 22:54:51 +07:00
|
|
|
$row = new Row($height, $style);
|
2014-06-08 02:31:44 +07:00
|
|
|
$row->setParentContainer($this);
|
2014-04-03 08:44:41 +07:00
|
|
|
$this->rows[] = $row;
|
2014-05-11 22:54:51 +07:00
|
|
|
|
2014-02-22 11:01:15 +07:00
|
|
|
return $row;
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a cell
|
|
|
|
|
*
|
2014-05-11 22:54:51 +07:00
|
|
|
* @param int $width
|
2013-12-11 14:45:44 -05:00
|
|
|
* @param mixed $style
|
2014-05-11 22:54:51 +07:00
|
|
|
* @return \PhpOffice\PhpWord\Element\Cell
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-01-13 23:14:05 +07:00
|
|
|
public function addCell($width = null, $style = null)
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-04-22 17:25:10 +07:00
|
|
|
$index = count($this->rows) - 1;
|
2014-05-19 00:14:14 +07:00
|
|
|
$row = $this->rows[$index];
|
|
|
|
|
$cell = $row->addCell($width, $style);
|
2014-05-11 22:54:51 +07:00
|
|
|
|
2013-12-11 14:45:44 -05:00
|
|
|
return $cell;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all rows
|
|
|
|
|
*
|
2014-05-19 00:14:14 +07:00
|
|
|
* @return \PhpOffice\PhpWord\Element\Row[]
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public function getRows()
|
|
|
|
|
{
|
2014-04-03 08:44:41 +07:00
|
|
|
return $this->rows;
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get table style
|
|
|
|
|
*
|
2014-04-16 17:22:30 +04:00
|
|
|
* @return \PhpOffice\PhpWord\Style\Table
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public function getStyle()
|
|
|
|
|
{
|
2014-04-03 08:44:41 +07:00
|
|
|
return $this->style;
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
2014-01-13 23:14:05 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-05-31 09:29:09 +07:00
|
|
|
* Get table width
|
2014-01-13 23:14:05 +07:00
|
|
|
*
|
2014-05-31 09:29:09 +07:00
|
|
|
* @return int
|
2014-01-13 23:14:05 +07:00
|
|
|
*/
|
2014-05-31 09:29:09 +07:00
|
|
|
public function getWidth()
|
2014-01-13 23:14:05 +07:00
|
|
|
{
|
2014-05-31 09:29:09 +07:00
|
|
|
return $this->width;
|
2014-01-13 23:14:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Set table width.
|
2014-01-13 23:14:05 +07:00
|
|
|
*
|
2014-05-31 09:29:09 +07:00
|
|
|
* @param int $width
|
2014-01-13 23:14:05 +07:00
|
|
|
*/
|
2014-05-31 09:29:09 +07:00
|
|
|
public function setWidth($width)
|
2014-01-13 23:14:05 +07:00
|
|
|
{
|
2014-05-31 09:29:09 +07:00
|
|
|
$this->width = $width;
|
2014-01-13 23:14:05 +07:00
|
|
|
}
|
2014-04-11 23:02:05 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get column count
|
|
|
|
|
*
|
2014-05-11 22:54:51 +07:00
|
|
|
* @return int
|
2014-04-11 23:02:05 +07:00
|
|
|
*/
|
|
|
|
|
public function countColumns()
|
|
|
|
|
{
|
|
|
|
|
$columnCount = 0;
|
|
|
|
|
if (is_array($this->rows)) {
|
|
|
|
|
$rowCount = count($this->rows);
|
|
|
|
|
for ($i = 0; $i < $rowCount; $i++) {
|
2014-05-19 00:14:14 +07:00
|
|
|
/** @var \PhpOffice\PhpWord\Element\Row $row Type hint */
|
|
|
|
|
$row = $this->rows[$i];
|
|
|
|
|
$cellCount = count($row->getCells());
|
2014-04-11 23:02:05 +07:00
|
|
|
if ($columnCount < $cellCount) {
|
|
|
|
|
$columnCount = $cellCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $columnCount;
|
|
|
|
|
}
|
2018-04-18 22:34:53 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The first declared cell width for each column
|
|
|
|
|
*
|
|
|
|
|
* @return int[]
|
|
|
|
|
*/
|
|
|
|
|
public function findFirstDefinedCellWidths()
|
|
|
|
|
{
|
|
|
|
|
$cellWidths = array();
|
|
|
|
|
if (is_array($this->rows)) {
|
|
|
|
|
foreach ($this->rows as $row) {
|
|
|
|
|
$cells = $row->getCells();
|
|
|
|
|
if (count($cells) <= count($cellWidths)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$cellWidths = array();
|
|
|
|
|
foreach ($cells as $cell) {
|
|
|
|
|
$cellWidths[] = $cell->getWidth();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $cellWidths;
|
|
|
|
|
}
|
2012-05-06 18:25:40 +02:00
|
|
|
}
|