From b0a24703f307433c29d73718ecd48422ce682d60 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 22 Feb 2014 11:01:15 +0700 Subject: [PATCH] `Table Row` allows `tblHeader` and `cantSplit` --- Classes/PHPWord/Section/Table.php | 27 +---- Classes/PHPWord/Section/Table/Row.php | 141 +++++++++++++++++++++++ Classes/PHPWord/Style/Row.php | 85 ++++++++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 28 +++-- 4 files changed, 252 insertions(+), 29 deletions(-) create mode 100644 Classes/PHPWord/Section/Table/Row.php create mode 100644 Classes/PHPWord/Style/Row.php diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index 02aeacb7..157bf070 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -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 * diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php new file mode 100644 index 00000000..9f8085a2 --- /dev/null +++ b/Classes/PHPWord/Section/Table/Row.php @@ -0,0 +1,141 @@ +_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; + } +} \ No newline at end of file diff --git a/Classes/PHPWord/Style/Row.php b/Classes/PHPWord/Style/Row.php new file mode 100644 index 00000000..0bd99144 --- /dev/null +++ b/Classes/PHPWord/Style/Row.php @@ -0,0 +1,85 @@ +_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; + } + +} \ No newline at end of file diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index cfaedc78..9ffbe59c 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -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();