Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for multiple comments on the same text object. #2112

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 64 additions & 12 deletions src/PhpWord/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,18 @@ abstract class AbstractElement
protected $collectionRelation = false;

/**
* The start position for the linked comment
* The start position for the linked comments
*
* @var Comment
* @var \PHPOffice\PHPWord\Collection\Comments
*/
protected $commentRangeStart;
protected $commentsRangeStart;

/**
* The end position for the linked comment
* The end position for the linked comments
*
* @var Comment
* @var \PHPOffice\PHPWord\Collection\Comments
*/
protected $commentRangeEnd;
protected $commentsRangeEnd;

/**
* Get PhpWord
Expand Down Expand Up @@ -287,14 +287,28 @@ public function getNestedLevel()
return $this->nestedLevel;
}

/**
* Get comments start
*
* @return \PhpOffice\PhpWord\Collection\Comments
*/
public function getCommentsRangeStart()
{
return $this->commentsRangeStart;
}

/**
* Get comment start
*
* @return Comment
*/
public function getCommentRangeStart()
{
return $this->commentRangeStart;
if ($this->commentsRangeStart != null) {
return $this->commentsRangeStart->getItem($this->commentsRangeStart->countItems());
}

return null;
}

/**
Expand All @@ -307,8 +321,30 @@ public function setCommentRangeStart(Comment $value)
if ($this instanceof Comment) {
throw new \InvalidArgumentException('Cannot set a Comment on a Comment');
}
$this->commentRangeStart = $value;
$this->commentRangeStart->setStartElement($this);
if ($this->commentsRangeStart == null) {
$this->commentsRangeStart = new \PhpOffice\PhpWord\Collection\Comments();
}
// Set ID early to avoid duplicates.
if ($value->getElementId() == null) {
$value->setElementId();
}
foreach ($this->commentsRangeStart->getItems() as $comment) {
if ($value->getElementId() == $comment->getElementId()) {
return;
}
}
$this->commentsRangeStart->addItem($value);
$this->commentsRangeStart->getItem($this->commentsRangeStart->countItems())->setStartElement($this);
}

/**
* Get comments end
*
* @return \PhpOffice\PhpWord\Collection\Comments
*/
public function getCommentsRangeEnd()
{
return $this->commentsRangeEnd;
}

/**
Expand All @@ -318,7 +354,11 @@ public function setCommentRangeStart(Comment $value)
*/
public function getCommentRangeEnd()
{
return $this->commentRangeEnd;
if ($this->commentsRangeEnd != null) {
return $this->commentsRangeEnd->getItem($this->commentsRangeEnd->countItems());
}

return null;
}

/**
Expand All @@ -331,8 +371,20 @@ public function setCommentRangeEnd(Comment $value)
if ($this instanceof Comment) {
throw new \InvalidArgumentException('Cannot set a Comment on a Comment');
}
$this->commentRangeEnd = $value;
$this->commentRangeEnd->setEndElement($this);
if ($this->commentsRangeEnd == null) {
$this->commentsRangeEnd = new \PhpOffice\PhpWord\Collection\Comments();
}
// Set ID early to avoid duplicates.
if ($value->getElementId() == null) {
$value->setElementId();
}
foreach ($this->commentsRangeEnd->getItems() as $comment) {
if ($value->getElementId() == $comment->getElementId()) {
return;
}
}
$this->commentsRangeEnd->addItem($value);
$this->commentsRangeEnd->getItem($this->commentsRangeEnd->countItems())->setEndElement($this);
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/PhpWord/Element/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ public function getInitials()
public function setStartElement(AbstractElement $value)
{
$this->startElement = $value;
if ($value->getCommentRangeStart() == null) {
$value->setCommentRangeStart($this);
}
$value->setCommentRangeStart($this);
}

/**
Expand All @@ -105,9 +103,7 @@ public function getStartElement()
public function setEndElement(AbstractElement $value)
{
$this->endElement = $value;
if ($value->getCommentRangeEnd() == null) {
$value->setCommentRangeEnd($this);
}
$value->setCommentRangeEnd($this);
}

/**
Expand Down
49 changes: 20 additions & 29 deletions src/PhpWord/Writer/Word2007/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,10 @@ protected function endElementP()
*/
protected function writeCommentRangeStart()
{
if ($this->element->getCommentRangeStart() != null) {
$comment = $this->element->getCommentRangeStart();
//only set the ID if it is not yet set, otherwise it will overwrite it
if ($comment->getElementId() == null) {
$comment->setElementId();
if ($this->element->getCommentsRangeStart() != null) {
foreach ($this->element->getCommentsRangeStart()->getItems() as $comment) {
$this->xmlWriter->writeElementBlock('w:commentRangeStart', array('w:id' => $comment->getElementId()));
}

$this->xmlWriter->writeElementBlock('w:commentRangeStart', array('w:id' => $comment->getElementId()));
}
}

Expand All @@ -138,28 +134,23 @@ protected function writeCommentRangeStart()
*/
protected function writeCommentRangeEnd()
{
if ($this->element->getCommentRangeEnd() != null) {
$comment = $this->element->getCommentRangeEnd();
//only set the ID if it is not yet set, otherwise it will overwrite it, this should normally not happen
if ($comment->getElementId() == null) {
$comment->setElementId(); // @codeCoverageIgnore
} // @codeCoverageIgnore

$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
$this->xmlWriter->endElement();
} elseif ($this->element->getCommentRangeStart() != null && $this->element->getCommentRangeStart()->getEndElement() == null) {
$comment = $this->element->getCommentRangeStart();
//only set the ID if it is not yet set, otherwise it will overwrite it, this should normally not happen
if ($comment->getElementId() == null) {
$comment->setElementId(); // @codeCoverageIgnore
} // @codeCoverageIgnore

$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
$this->xmlWriter->endElement();
if ($this->element->getCommentsRangeEnd() != null) {
foreach ($this->element->getCommentsRangeEnd()->getItems() as $comment) {
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
$this->xmlWriter->endElement();
}
}
if ($this->element->getCommentsRangeStart() != null) {
foreach ($this->element->getCommentsRangeStart()->getItems() as $comment) {
if ($comment->getEndElement() == null) {
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
$this->xmlWriter->endElement();
}
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/PhpWord/Element/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@ public function testConstructDefault()
$this->assertEquals($oText, $oComment->getEndElement());
}

/**
* Two comments on same text
*/
public function testTwoCommentsOnSameText()
{
$section = new Section(0);
$text = $section->addText('Text');

$comment1 = new Comment('Author1', new \DateTime(), 'A1');
$comment1->addText('Comment1');

$comment2 = new Comment('Author2', new \DateTime(), 'A2');
$comment2->addText('Comment2');

$comment1->setStartElement($text);
$comment2->setStartElement($text);

$text->setCommentRangeStart($comment1);
$text->setCommentRangeEnd($comment1);

$text->setCommentRangeStart($comment2);
$text->setCommentRangeEnd($comment2);

$this->assertEquals(2, $text->getCommentsRangeStart()->countItems());
$this->assertEquals(2, $text->getCommentsRangeEnd()->countItems());

$this->assertEquals($text->getCommentsRangeStart()->getItem(1)->getElementId(), $comment1->getElementId());
$this->assertEquals($text->getCommentsRangeEnd()->getItem(1)->getElementId(), $comment1->getElementId());

$this->assertEquals($text->getCommentsRangeStart()->getItem(2)->getElementId(), $comment2->getElementId());
$this->assertEquals($text->getCommentsRangeEnd()->getItem(2)->getElementId(), $comment2->getElementId());
}

/**
* Add text
*/
Expand Down