Skip to content

Commit c4a9d81

Browse files
rodrigoqProgi1984
authored andcommitted
Adds support for multiple comments on the same text object.
1 parent b0ed3db commit c4a9d81

File tree

3 files changed

+87
-48
lines changed

3 files changed

+87
-48
lines changed

src/PhpWord/Element/AbstractElement.php

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ abstract class AbstractElement
131131
protected $collectionRelation = false;
132132

133133
/**
134-
* The start position for the linked comment.
134+
* The start position for the linked comments
135135
*
136-
* @var Comment
136+
* @var \PHPOffice\PHPWord\Collection\Comments
137137
*/
138-
protected $commentRangeStart;
138+
protected $commentsRangeStart;
139139

140140
/**
141-
* The end position for the linked comment.
141+
* The end position for the linked comments
142142
*
143-
* @var Comment
143+
* @var \PHPOffice\PHPWord\Collection\Comments
144144
*/
145-
protected $commentRangeEnd;
145+
protected $commentsRangeEnd;
146146

147147
/**
148148
* Get PhpWord.
@@ -288,13 +288,27 @@ public function getNestedLevel()
288288
}
289289

290290
/**
291-
* Get comment start.
291+
* Get comments start
292+
*
293+
* @return \PhpOffice\PhpWord\Collection\Comments
294+
*/
295+
public function getCommentsRangeStart()
296+
{
297+
return $this->commentsRangeStart;
298+
}
299+
300+
/**
301+
* Get comment start
292302
*
293303
* @return Comment
294304
*/
295305
public function getCommentRangeStart()
296306
{
297-
return $this->commentRangeStart;
307+
if ($this->commentsRangeStart != null) {
308+
return $this->commentsRangeStart->getItem($this->commentsRangeStart->countItems());
309+
}
310+
311+
return null;
298312
}
299313

300314
/**
@@ -305,8 +319,30 @@ public function setCommentRangeStart(Comment $value): void
305319
if ($this instanceof Comment) {
306320
throw new InvalidArgumentException('Cannot set a Comment on a Comment');
307321
}
308-
$this->commentRangeStart = $value;
309-
$this->commentRangeStart->setStartElement($this);
322+
if ($this->commentsRangeStart == null) {
323+
$this->commentsRangeStart = new \PhpOffice\PhpWord\Collection\Comments();
324+
}
325+
// Set ID early to avoid duplicates.
326+
if ($value->getElementId() == null) {
327+
$value->setElementId();
328+
}
329+
foreach ($this->commentsRangeStart->getItems() as $comment) {
330+
if ($value->getElementId() == $comment->getElementId()) {
331+
return;
332+
}
333+
}
334+
$this->commentsRangeStart->addItem($value);
335+
$this->commentsRangeStart->getItem($this->commentsRangeStart->countItems())->setStartElement($this);
336+
}
337+
338+
/**
339+
* Get comments end
340+
*
341+
* @return \PhpOffice\PhpWord\Collection\Comments
342+
*/
343+
public function getCommentsRangeEnd()
344+
{
345+
return $this->commentsRangeEnd;
310346
}
311347

312348
/**
@@ -316,7 +352,11 @@ public function setCommentRangeStart(Comment $value): void
316352
*/
317353
public function getCommentRangeEnd()
318354
{
319-
return $this->commentRangeEnd;
355+
if ($this->commentsRangeEnd != null) {
356+
return $this->commentsRangeEnd->getItem($this->commentsRangeEnd->countItems());
357+
}
358+
359+
return null;
320360
}
321361

322362
/**
@@ -327,8 +367,20 @@ public function setCommentRangeEnd(Comment $value): void
327367
if ($this instanceof Comment) {
328368
throw new InvalidArgumentException('Cannot set a Comment on a Comment');
329369
}
330-
$this->commentRangeEnd = $value;
331-
$this->commentRangeEnd->setEndElement($this);
370+
if ($this->commentsRangeEnd == null) {
371+
$this->commentsRangeEnd = new \PhpOffice\PhpWord\Collection\Comments();
372+
}
373+
// Set ID early to avoid duplicates.
374+
if ($value->getElementId() == null) {
375+
$value->setElementId();
376+
}
377+
foreach ($this->commentsRangeEnd->getItems() as $comment) {
378+
if ($value->getElementId() == $comment->getElementId()) {
379+
return;
380+
}
381+
}
382+
$this->commentsRangeEnd->addItem($value);
383+
$this->commentsRangeEnd->getItem($this->commentsRangeEnd->countItems())->setEnd($this);
332384
}
333385

334386
/**

src/PhpWord/Element/Comment.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ public function getInitials()
8383
public function setStartElement(AbstractElement $value): void
8484
{
8585
$this->startElement = $value;
86-
if ($value->getCommentRangeStart() == null) {
87-
$value->setCommentRangeStart($this);
88-
}
86+
$value->setCommentRangeStart($this);
8987
}
9088

9189
/**
@@ -104,9 +102,7 @@ public function getStartElement()
104102
public function setEndElement(AbstractElement $value): void
105103
{
106104
$this->endElement = $value;
107-
if ($value->getCommentRangeEnd() == null) {
108-
$value->setCommentRangeEnd($this);
109-
}
105+
$value->setCommentRangeEnd($this);
110106
}
111107

112108
/**

src/PhpWord/Writer/Word2007/Element/AbstractElement.php

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,10 @@ protected function endElementP(): void
126126
*/
127127
protected function writeCommentRangeStart(): void
128128
{
129-
if ($this->element->getCommentRangeStart() != null) {
130-
$comment = $this->element->getCommentRangeStart();
131-
//only set the ID if it is not yet set, otherwise it will overwrite it
132-
if ($comment->getElementId() == null) {
133-
$comment->setElementId();
129+
if ($this->element->getCommentsRangeStart() != null) {
130+
foreach ($this->element->getCommentsRangeStart()->getItems() as $comment) {
131+
$this->xmlWriter->writeElementBlock('w:commentRangeStart', array('w:id' => $comment->getElementId()));
134132
}
135-
136-
$this->xmlWriter->writeElementBlock('w:commentRangeStart', ['w:id' => $comment->getElementId()]);
137133
}
138134
}
139135

@@ -142,28 +138,23 @@ protected function writeCommentRangeStart(): void
142138
*/
143139
protected function writeCommentRangeEnd(): void
144140
{
145-
if ($this->element->getCommentRangeEnd() != null) {
146-
$comment = $this->element->getCommentRangeEnd();
147-
//only set the ID if it is not yet set, otherwise it will overwrite it, this should normally not happen
148-
if ($comment->getElementId() == null) {
149-
$comment->setElementId(); // @codeCoverageIgnore
150-
} // @codeCoverageIgnore
151-
152-
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', ['w:id' => $comment->getElementId()]);
153-
$this->xmlWriter->startElement('w:r');
154-
$this->xmlWriter->writeElementBlock('w:commentReference', ['w:id' => $comment->getElementId()]);
155-
$this->xmlWriter->endElement();
156-
} elseif ($this->element->getCommentRangeStart() != null && $this->element->getCommentRangeStart()->getEndElement() == null) {
157-
$comment = $this->element->getCommentRangeStart();
158-
//only set the ID if it is not yet set, otherwise it will overwrite it, this should normally not happen
159-
if ($comment->getElementId() == null) {
160-
$comment->setElementId(); // @codeCoverageIgnore
161-
} // @codeCoverageIgnore
162-
163-
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', ['w:id' => $comment->getElementId()]);
164-
$this->xmlWriter->startElement('w:r');
165-
$this->xmlWriter->writeElementBlock('w:commentReference', ['w:id' => $comment->getElementId()]);
166-
$this->xmlWriter->endElement();
141+
if ($this->element->getCommentsRangeEnd() != null) {
142+
foreach ($this->element->getCommentsRangeEnd()->getItems() as $comment) {
143+
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
144+
$this->xmlWriter->startElement('w:r');
145+
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
146+
$this->xmlWriter->endElement();
147+
}
148+
}
149+
if ($this->element->getCommentsRangeStart() != null) {
150+
foreach ($this->element->getCommentsRangeStart()->getItems() as $comment) {
151+
if ($comment->getEndElement() == null) {
152+
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
153+
$this->xmlWriter->startElement('w:r');
154+
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
155+
$this->xmlWriter->endElement();
156+
}
157+
}
167158
}
168159
}
169160

0 commit comments

Comments
 (0)