Skip to content

Commit a5c815d

Browse files
committed
ODT: Basic table writing support
1 parent a3a9af5 commit a5c815d

File tree

4 files changed

+316
-95
lines changed

4 files changed

+316
-95
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This release marked heavy refactorings on internal code structure with the creat
3030
- Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table, and list - @ivanlanin
3131
- Endnote: Ability to add endnotes - @ivanlanin
3232
- ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198
33+
- ODT Writer: Basic table writing support - @ivanlanin
3334

3435
### Bugfixes
3536

src/PhpWord/Element/AbstractElement.php

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ abstract class AbstractElement
8181
*/
8282
protected $elements = array();
8383

84+
/**
85+
* Index of element in the elements collection (start with 1)
86+
*
87+
* @var integer
88+
*/
89+
protected $elementIndex = 1;
90+
91+
/**
92+
* Unique Id for element
93+
*
94+
* @var integer
95+
*/
96+
protected $elementId;
97+
8498
/**
8599
* Relation Id
86100
*
@@ -108,7 +122,7 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null)
108122
$text = String::toUTF8($text);
109123
$textObject = new Text($text, $fontStyle, $paragraphStyle);
110124
$textObject->setDocPart($this->getDocPart(), $this->getDocPartId());
111-
$this->elements[] = $textObject;
125+
$this->addElement($textObject);
112126

113127
return $textObject;
114128
}
@@ -125,7 +139,7 @@ public function addTextRun($paragraphStyle = null)
125139

126140
$textRun = new TextRun($paragraphStyle);
127141
$textRun->setDocPart($this->getDocPart(), $this->getDocPartId());
128-
$this->elements[] = $textRun;
142+
$this->addElement($textRun);
129143

130144
return $textRun;
131145
}
@@ -148,7 +162,7 @@ public function addLink($linkSrc, $linkName = null, $fontStyle = null, $paragrap
148162
$link->setDocPart($this->getDocPart(), $this->getDocPartId());
149163
$rId = Media::addElement($elementDocPart, 'link', $linkSrc);
150164
$link->setRelationId($rId);
151-
$this->elements[] = $link;
165+
$this->addElement($link);
152166

153167
return $link;
154168
}
@@ -179,7 +193,7 @@ public function addTitle($text, $depth = 1)
179193
$bookmarkId = $data[1];
180194
$title->setAnchor($anchor);
181195
$title->setBookmarkId($bookmarkId);
182-
$this->elements[] = $title;
196+
$this->addElement($title);
183197

184198
return $title;
185199
}
@@ -198,7 +212,7 @@ public function addPreserveText($text, $fontStyle = null, $paragraphStyle = null
198212

199213
$preserveText = new PreserveText(String::toUTF8($text), $fontStyle, $paragraphStyle);
200214
$preserveText->setDocPart($this->getDocPart(), $this->getDocPartId());
201-
$this->elements[] = $preserveText;
215+
$this->addElement($preserveText);
202216

203217
return $preserveText;
204218
}
@@ -217,7 +231,7 @@ public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = nu
217231
for ($i = 1; $i <= $count; $i++) {
218232
$textBreak = new TextBreak($fontStyle, $paragraphStyle);
219233
$textBreak->setDocPart($this->getDocPart(), $this->getDocPartId());
220-
$this->elements[] = $textBreak;
234+
$this->addElement($textBreak);
221235
}
222236
}
223237

@@ -237,7 +251,7 @@ public function addListItem($text, $depth = 0, $fontStyle = null, $styleList = n
237251

238252
$listItem = new ListItem(String::toUTF8($text), $depth, $fontStyle, $styleList, $paragraphStyle);
239253
$listItem->setDocPart($this->getDocPart(), $this->getDocPartId());
240-
$this->elements[] = $listItem;
254+
$this->addElement($listItem);
241255

242256
return $listItem;
243257
}
@@ -253,7 +267,7 @@ public function addTable($style = null)
253267
$this->checkValidity('table');
254268

255269
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
256-
$this->elements[] = $table;
270+
$this->addElement($table);
257271

258272
return $table;
259273
}
@@ -275,7 +289,8 @@ public function addImage($src, $style = null, $isWatermark = false)
275289
$image->setDocPart($this->getDocPart(), $this->getDocPartId());
276290
$rId = Media::addElement($elementDocPart, 'image', $src, $image);
277291
$image->setRelationId($rId);
278-
$this->elements[] = $image;
292+
$this->addElement($image);
293+
279294
return $image;
280295
}
281296

@@ -307,7 +322,8 @@ public function addObject($src, $style = null)
307322
$object->setRelationId($rId);
308323
$rIdimg = Media::addElement($elementDocPart, 'image', $icon, new Image($icon));
309324
$object->setImageRelationId($rIdimg);
310-
$this->elements[] = $object;
325+
$this->addElement($object);
326+
311327
return $object;
312328
} else {
313329
throw new InvalidObjectException();
@@ -329,7 +345,7 @@ public function addFootnote($paragraphStyle = null)
329345

330346
$footnote->setDocPart('footnote', $this->getDocPartId());
331347
$footnote->setRelationId($rId);
332-
$this->elements[] = $footnote;
348+
$this->addElement($footnote);
333349

334350
return $footnote;
335351
}
@@ -349,7 +365,7 @@ public function addEndnote($paragraphStyle = null)
349365

350366
$endnote->setDocPart('endnote', $this->getDocPartId());
351367
$endnote->setRelationId($rId);
352-
$this->elements[] = $endnote;
368+
$this->addElement($endnote);
353369

354370
return $endnote;
355371
}
@@ -369,7 +385,7 @@ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = n
369385

370386
$checkBox = new CheckBox(String::toUTF8($name), String::toUTF8($text), $fontStyle, $paragraphStyle);
371387
$checkBox->setDocPart($this->getDocPart(), $this->getDocPartId());
372-
$this->elements[] = $checkBox;
388+
$this->addElement($checkBox);
373389

374390
return $checkBox;
375391
}
@@ -416,6 +432,16 @@ public function getDocPartId()
416432
return $this->docPartId;
417433
}
418434

435+
/**
436+
* Set element index and unique id, and add element into elements collection
437+
*/
438+
protected function addElement(AbstractElement $element)
439+
{
440+
$element->setElementIndex($this->countElements() + 1);
441+
$element->setElementId();
442+
$this->elements[] = $element;
443+
}
444+
419445
/**
420446
* Get all elements
421447
*
@@ -426,6 +452,54 @@ public function getElements()
426452
return $this->elements;
427453
}
428454

455+
/**
456+
* Count elements
457+
*
458+
* @return integer
459+
*/
460+
public function countElements()
461+
{
462+
return count($this->elements);
463+
}
464+
465+
/**
466+
* Get element index
467+
*
468+
* @return int
469+
*/
470+
public function getElementIndex()
471+
{
472+
return $this->elementIndex;
473+
}
474+
475+
/**
476+
* Set element index
477+
*
478+
* @param int $value
479+
*/
480+
public function setElementIndex($value)
481+
{
482+
$this->elementIndex = $value;
483+
}
484+
485+
/**
486+
* Get element unique ID
487+
*
488+
* @return string
489+
*/
490+
public function getElementId()
491+
{
492+
return $this->elementId;
493+
}
494+
495+
/**
496+
* Set element unique ID from 6 first digit of md5
497+
*/
498+
public function setElementId()
499+
{
500+
$this->elementId = substr(md5(rand()), 0, 6);
501+
}
502+
429503
/**
430504
* Get relation Id
431505
*

src/PhpWord/Element/Table.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Table extends AbstractElement
3535
/**
3636
* Table width
3737
*
38-
* @var int
38+
* @var integer
3939
*/
4040
private $width = null;
4141

@@ -44,7 +44,7 @@ class Table extends AbstractElement
4444
* Create a new table
4545
*
4646
* @param string $docPart
47-
* @param int $docPartId
47+
* @param integer $docPartId
4848
* @param mixed $style
4949
*/
5050
public function __construct($docPart, $docPartId, $style = null)
@@ -56,7 +56,7 @@ public function __construct($docPart, $docPartId, $style = null)
5656
/**
5757
* Add a row
5858
*
59-
* @param int $height
59+
* @param integer $height
6060
* @param mixed $style
6161
*/
6262
public function addRow($height = null, $style = null)
@@ -69,7 +69,7 @@ public function addRow($height = null, $style = null)
6969
/**
7070
* Add a cell
7171
*
72-
* @param int $width
72+
* @param integer $width
7373
* @param mixed $style
7474
* @return Cell
7575
*/
@@ -103,7 +103,7 @@ public function getStyle()
103103
/**
104104
* Set table width
105105
*
106-
* @param int $width
106+
* @param integer $width
107107
*/
108108
public function setWidth($width)
109109
{
@@ -113,10 +113,31 @@ public function setWidth($width)
113113
/**
114114
* Get table width
115115
*
116-
* @return int
116+
* @return integer
117117
*/
118118
public function getWidth()
119119
{
120120
return $this->width;
121121
}
122+
123+
/**
124+
* Get column count
125+
*
126+
* @return integer
127+
*/
128+
public function countColumns()
129+
{
130+
$columnCount = 0;
131+
if (is_array($this->rows)) {
132+
$rowCount = count($this->rows);
133+
for ($i = 0; $i < $rowCount; $i++) {
134+
$cellCount = count($this->rows[$i]->getCells());
135+
if ($columnCount < $cellCount) {
136+
$columnCount = $cellCount;
137+
}
138+
}
139+
}
140+
141+
return $columnCount;
142+
}
122143
}

0 commit comments

Comments
 (0)