Skip to content

Commit b0a2470

Browse files
committed
Table Row allows tblHeader and cantSplit
1 parent 194940d commit b0a2470

File tree

4 files changed

+252
-29
lines changed

4 files changed

+252
-29
lines changed

Classes/PHPWord/Section/Table.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ class PHPWord_Section_Table
4545
*/
4646
private $_rows = array();
4747

48-
/**
49-
* Row heights
50-
*
51-
* @var array
52-
*/
53-
private $_rowHeights = array();
54-
5548
/**
5649
* Table holder
5750
*
@@ -107,10 +100,11 @@ public function __construct($insideOf, $pCount, $style = null)
107100
*
108101
* @param int $height
109102
*/
110-
public function addRow($height = null)
103+
public function addRow($height = null, $style = null)
111104
{
112-
$this->_rows[] = array();
113-
$this->_rowHeights[] = $height;
105+
$row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style);
106+
$this->_rows[] = $row;
107+
return $row;
114108
}
115109

116110
/**
@@ -122,9 +116,8 @@ public function addRow($height = null)
122116
*/
123117
public function addCell($width = null, $style = null)
124118
{
125-
$cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style);
126119
$i = count($this->_rows) - 1;
127-
$this->_rows[$i][] = $cell;
120+
$cell = $this->_rows[$i]->addCell($width, $style);
128121
return $cell;
129122
}
130123

@@ -138,16 +131,6 @@ public function getRows()
138131
return $this->_rows;
139132
}
140133

141-
/**
142-
* Get all row heights
143-
*
144-
* @return array
145-
*/
146-
public function getRowHeights()
147-
{
148-
return $this->_rowHeights;
149-
}
150-
151134
/**
152135
* Get table style
153136
*

Classes/PHPWord/Section/Table/Row.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2013 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 2013 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version 0.7.0
26+
*/
27+
28+
/**
29+
* PHPWord_Section_Table_Row
30+
*/
31+
class PHPWord_Section_Table_Row
32+
{
33+
34+
/**
35+
* Row height
36+
*
37+
* @var int
38+
*/
39+
private $_height = null;
40+
41+
/**
42+
* Row style
43+
*
44+
* @var PHPWord_Style_Row
45+
*/
46+
private $_style;
47+
48+
/**
49+
* Row cells
50+
*
51+
* @var array
52+
*/
53+
private $_cells = array();
54+
55+
/**
56+
* Table holder
57+
*
58+
* @var string
59+
*/
60+
private $_insideOf;
61+
62+
/**
63+
* Section/Header/Footer count
64+
*
65+
* @var int
66+
*/
67+
private $_pCount;
68+
69+
70+
/**
71+
* Create a new table row
72+
*
73+
* @param string $insideOf
74+
* @param int $pCount
75+
* @param int $height
76+
* @param mixed $style
77+
*/
78+
public function __construct($insideOf, $pCount, $height = null, $style = null)
79+
{
80+
$this->_insideOf = $insideOf;
81+
$this->_pCount = $pCount;
82+
$this->_height = $height;
83+
$this->_style = new PHPWord_Style_Row();
84+
85+
if (!is_null($style)) {
86+
if (is_array($style)) {
87+
88+
foreach ($style as $key => $value) {
89+
if (substr($key, 0, 1) != '_') {
90+
$key = '_' . $key;
91+
}
92+
$this->_style->setStyleValue($key, $value);
93+
}
94+
}
95+
}
96+
}
97+
98+
/**
99+
* Add a cell
100+
*
101+
* @param int $width
102+
* @param mixed $style
103+
* @return PHPWord_Section_Table_Cell
104+
*/
105+
public function addCell($width = null, $style = null)
106+
{
107+
$cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style);
108+
$this->_cells[] = $cell;
109+
return $cell;
110+
}
111+
112+
/**
113+
* Get all cells
114+
*
115+
* @return array
116+
*/
117+
public function getCells()
118+
{
119+
return $this->_cells;
120+
}
121+
122+
/**
123+
* Get row style
124+
*
125+
* @return PHPWord_Style_Row
126+
*/
127+
public function getStyle()
128+
{
129+
return $this->_style;
130+
}
131+
132+
/**
133+
* Get row height
134+
*
135+
* @return int
136+
*/
137+
public function getHeight()
138+
{
139+
return $this->_height;
140+
}
141+
}

Classes/PHPWord/Style/Row.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2013 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 2013 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version 0.7.0
26+
*/
27+
28+
/**
29+
* PHPWord_Style_Row
30+
*/
31+
class PHPWord_Style_Row
32+
{
33+
34+
/**
35+
* Repeat table row on every new page
36+
*
37+
* @var bool
38+
*/
39+
private $_tblHeader;
40+
41+
/**
42+
* Table row cannot break across pages
43+
*
44+
* @var bool
45+
*/
46+
private $_cantSplit;
47+
48+
/**
49+
* Create a new row style
50+
*/
51+
public function __construct()
52+
{
53+
$this->_tblHeader = null;
54+
$this->_cantSplit = null;
55+
}
56+
57+
/**
58+
* Set style value
59+
*/
60+
public function setStyleValue($key, $value)
61+
{
62+
$this->$key = $value;
63+
}
64+
65+
public function setTblHeader($pValue = null)
66+
{
67+
$this->_tblHeader = $pValue;
68+
}
69+
70+
public function getTblHeader()
71+
{
72+
return $this->_tblHeader ? 1 : 0;
73+
}
74+
75+
public function setCantSplit($pValue = null)
76+
{
77+
$this->_cantSplit = $pValue;
78+
}
79+
80+
public function getCantSplit()
81+
{
82+
return $this->_cantSplit ? 1 : 0;
83+
}
84+
85+
}

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,22 +431,36 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
431431
}
432432
}
433433

434-
$_heights = $table->getRowHeights();
435434
for ($i = 0; $i < $_cRows; $i++) {
436435
$row = $_rows[$i];
437-
$height = $_heights[$i];
436+
$height = $row->getHeight();
437+
$rowStyle = $row->getStyle();
438+
$tblHeader = $rowStyle->getTblHeader();
439+
$cantSplit = $rowStyle->getCantSplit();
438440

439441
$objWriter->startElement('w:tr');
440442

441-
if (!is_null($height)) {
443+
if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) {
442444
$objWriter->startElement('w:trPr');
443-
$objWriter->startElement('w:trHeight');
444-
$objWriter->writeAttribute('w:val', $height);
445-
$objWriter->endElement();
445+
if (!is_null($height)) {
446+
$objWriter->startElement('w:trHeight');
447+
$objWriter->writeAttribute('w:val', $height);
448+
$objWriter->endElement();
449+
}
450+
if (!is_null($tblHeader)) {
451+
$objWriter->startElement('w:tblHeader');
452+
$objWriter->writeAttribute('w:val', $tblHeader);
453+
$objWriter->endElement();
454+
}
455+
if (!is_null($cantSplit)) {
456+
$objWriter->startElement('w:cantSplit');
457+
$objWriter->writeAttribute('w:val', $cantSplit);
458+
$objWriter->endElement();
459+
}
446460
$objWriter->endElement();
447461
}
448462

449-
foreach ($row as $cell) {
463+
foreach ($row->getCells() as $cell) {
450464
$objWriter->startElement('w:tc');
451465

452466
$cellStyle = $cell->getStyle();

0 commit comments

Comments
 (0)