`Table Row` allows `tblHeader` and `cantSplit`

This commit is contained in:
Ivan Lanin 2014-02-22 11:01:15 +07:00
parent 194940d478
commit b0a24703f3
4 changed files with 252 additions and 29 deletions

View File

@ -45,13 +45,6 @@ class PHPWord_Section_Table
*/
private $_rows = array();
/**
* Row heights
*
* @var array
*/
private $_rowHeights = array();
/**
* Table holder
*
@ -107,10 +100,11 @@ class PHPWord_Section_Table
*
* @param int $height
*/
public function addRow($height = null)
public function addRow($height = null, $style = null)
{
$this->_rows[] = array();
$this->_rowHeights[] = $height;
$row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style);
$this->_rows[] = $row;
return $row;
}
/**
@ -122,9 +116,8 @@ class PHPWord_Section_Table
*/
public function addCell($width = null, $style = null)
{
$cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style);
$i = count($this->_rows) - 1;
$this->_rows[$i][] = $cell;
$cell = $this->_rows[$i]->addCell($width, $style);
return $cell;
}
@ -138,16 +131,6 @@ class PHPWord_Section_Table
return $this->_rows;
}
/**
* Get all row heights
*
* @return array
*/
public function getRowHeights()
{
return $this->_rowHeights;
}
/**
* Get table style
*

View File

@ -0,0 +1,141 @@
<?php
/**
* PHPWord
*
* Copyright (c) 2013 PHPWord
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPWord
* @package PHPWord
* @copyright Copyright (c) 2013 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 0.7.0
*/
/**
* PHPWord_Section_Table_Row
*/
class PHPWord_Section_Table_Row
{
/**
* Row height
*
* @var int
*/
private $_height = null;
/**
* Row style
*
* @var PHPWord_Style_Row
*/
private $_style;
/**
* Row cells
*
* @var array
*/
private $_cells = array();
/**
* Table holder
*
* @var string
*/
private $_insideOf;
/**
* Section/Header/Footer count
*
* @var int
*/
private $_pCount;
/**
* Create a new table row
*
* @param string $insideOf
* @param int $pCount
* @param int $height
* @param mixed $style
*/
public function __construct($insideOf, $pCount, $height = null, $style = null)
{
$this->_insideOf = $insideOf;
$this->_pCount = $pCount;
$this->_height = $height;
$this->_style = new PHPWord_Style_Row();
if (!is_null($style)) {
if (is_array($style)) {
foreach ($style as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_style->setStyleValue($key, $value);
}
}
}
}
/**
* Add a cell
*
* @param int $width
* @param mixed $style
* @return PHPWord_Section_Table_Cell
*/
public function addCell($width = null, $style = null)
{
$cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style);
$this->_cells[] = $cell;
return $cell;
}
/**
* Get all cells
*
* @return array
*/
public function getCells()
{
return $this->_cells;
}
/**
* Get row style
*
* @return PHPWord_Style_Row
*/
public function getStyle()
{
return $this->_style;
}
/**
* Get row height
*
* @return int
*/
public function getHeight()
{
return $this->_height;
}
}

View File

@ -0,0 +1,85 @@
<?php
/**
* PHPWord
*
* Copyright (c) 2013 PHPWord
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPWord
* @package PHPWord
* @copyright Copyright (c) 2013 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 0.7.0
*/
/**
* PHPWord_Style_Row
*/
class PHPWord_Style_Row
{
/**
* Repeat table row on every new page
*
* @var bool
*/
private $_tblHeader;
/**
* Table row cannot break across pages
*
* @var bool
*/
private $_cantSplit;
/**
* Create a new row style
*/
public function __construct()
{
$this->_tblHeader = null;
$this->_cantSplit = null;
}
/**
* Set style value
*/
public function setStyleValue($key, $value)
{
$this->$key = $value;
}
public function setTblHeader($pValue = null)
{
$this->_tblHeader = $pValue;
}
public function getTblHeader()
{
return $this->_tblHeader ? 1 : 0;
}
public function setCantSplit($pValue = null)
{
$this->_cantSplit = $pValue;
}
public function getCantSplit()
{
return $this->_cantSplit ? 1 : 0;
}
}

View File

@ -431,22 +431,36 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
$_heights = $table->getRowHeights();
for ($i = 0; $i < $_cRows; $i++) {
$row = $_rows[$i];
$height = $_heights[$i];
$height = $row->getHeight();
$rowStyle = $row->getStyle();
$tblHeader = $rowStyle->getTblHeader();
$cantSplit = $rowStyle->getCantSplit();
$objWriter->startElement('w:tr');
if (!is_null($height)) {
if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) {
$objWriter->startElement('w:trPr');
$objWriter->startElement('w:trHeight');
$objWriter->writeAttribute('w:val', $height);
$objWriter->endElement();
if (!is_null($height)) {
$objWriter->startElement('w:trHeight');
$objWriter->writeAttribute('w:val', $height);
$objWriter->endElement();
}
if (!is_null($tblHeader)) {
$objWriter->startElement('w:tblHeader');
$objWriter->writeAttribute('w:val', $tblHeader);
$objWriter->endElement();
}
if (!is_null($cantSplit)) {
$objWriter->startElement('w:cantSplit');
$objWriter->writeAttribute('w:val', $cantSplit);
$objWriter->endElement();
}
$objWriter->endElement();
}
foreach ($row as $cell) {
foreach ($row->getCells() as $cell) {
$objWriter->startElement('w:tc');
$cellStyle = $cell->getStyle();