Skip to content

Commit

Permalink
Word2007 Writer: Added support for multiples comment for the same text (
Browse files Browse the repository at this point in the history
#2665)

* Adds support for multiple comments on the same text object.

* Fix method name.

* Fixed some feedbacks

---------

Co-authored-by: Rodrigo Queipo <rodrigoq@gmail.com>
  • Loading branch information
Progi1984 and rodrigoq authored Aug 30, 2024
1 parent b0ed3db commit 8392134
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 74 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.x/1.3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- RTF Writer : Support for Table Border Style fixing [#345](https://github.com/PHPOffice/PHPWord/issues/345) by [@Progi1984](https://github.com/Progi1984) in [#2656](https://github.com/PHPOffice/PHPWord/pull/2656)
- Word2007 Reader: Support the page break (<w:lastRenderedPageBreak/>) by [@stanolacko](https://github.com/stanolacko) in [#2662](https://github.com/PHPOffice/PHPWord/pull/2662)
- MsDoc Reader: Support for UTF-8 characters by [@Progi1984] fixing [#881](https://github.com/PHPOffice/PHPWord/issues/881), [#1454](https://github.com/PHPOffice/PHPWord/issues/1454), [#1817](https://github.com/PHPOffice/PHPWord/issues/1817), [#1927](https://github.com/PHPOffice/PHPWord/issues/1927), [#2383](https://github.com/PHPOffice/PHPWord/issues/2383), [#2565](https://github.com/PHPOffice/PHPWord/issues/2565) in [#2664](https://github.com/PHPOffice/PHPWord/pull/2664)
- Word2007 Writer: Added support for multiples comment for the same text by [@rodrigoq](https://github.com/rodrigoq) fixing [#2109](https://github.com/PHPOffice/PHPWord/issues/2109) in [#2665](https://github.com/PHPOffice/PHPWord/pull/2665)

### Bug fixes

Expand Down
17 changes: 16 additions & 1 deletion samples/Sample_37_Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$imageComment->addText('Hey, Mars does look ');
$imageComment->addText('red', ['color' => 'FF0000']);
$phpWord->addComment($commentOnImage);
$image = $section->addImage('resources/_mars.jpg');
$image = $section->addImage(__DIR__ . '/resources/_mars.jpg');
$image->setCommentRangeStart($commentOnImage);

$section->addTextBreak(2);
Expand All @@ -56,6 +56,21 @@
$comment1->setEndElement($anotherText);
$phpWord->addComment($comment1);

// We can also do things the other way round, link the comment to the element
$lastText = $section->addText('with a last text and two comments');

$comment1 = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment1->addText('Comment 1', ['bold' => true]);
$comment1->setStartElement($lastText);
$comment1->setEndElement($lastText);
$phpWord->addComment($comment1);

$comment2 = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment2->addText('Comment 2', ['bold' => true]);
$comment2->setStartElement($lastText);
$comment2->setEndElement($lastText);
$phpWord->addComment($comment2);

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
Expand Down
28 changes: 11 additions & 17 deletions src/PhpWord/Collection/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,33 @@
* Collection abstract class.
*
* @since 0.10.0
* @template T
*/
abstract class AbstractCollection
{
/**
* Items.
*
* @var \PhpOffice\PhpWord\Element\AbstractContainer[]
* @var T[]
*/
private $items = [];

/**
* Get items.
*
* @return \PhpOffice\PhpWord\Element\AbstractContainer[]
* @return T[]
*/
public function getItems()
public function getItems(): array
{
return $this->items;
}

/**
* Get item by index.
*
* @param int $index
*
* @return ?\PhpOffice\PhpWord\Element\AbstractContainer
* @return ?T
*/
public function getItem($index)
public function getItem(int $index)
{
if (array_key_exists($index, $this->items)) {
return $this->items[$index];
Expand All @@ -60,10 +59,9 @@ public function getItem($index)
/**
* Set item.
*
* @param int $index
* @param ?\PhpOffice\PhpWord\Element\AbstractContainer $item
* @param ?T $item
*/
public function setItem($index, $item): void
public function setItem(int $index, $item): void
{
if (array_key_exists($index, $this->items)) {
$this->items[$index] = $item;
Expand All @@ -73,11 +71,9 @@ public function setItem($index, $item): void
/**
* Add new item.
*
* @param \PhpOffice\PhpWord\Element\AbstractContainer $item
*
* @return int
* @param T $item
*/
public function addItem($item)
public function addItem($item): int
{
$index = $this->countItems();
$this->items[$index] = $item;
Expand All @@ -87,10 +83,8 @@ public function addItem($item)

/**
* Get item count.
*
* @return int
*/
public function countItems()
public function countItems(): int
{
return count($this->items);
}
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Collection/Bookmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

namespace PhpOffice\PhpWord\Collection;

use PhpOffice\PhpWord\Element\Bookmark;

/**
* Bookmarks collection.
*
* @since 0.12.0
* @extends AbstractCollection<Bookmark>
*/
class Bookmarks extends AbstractCollection
{
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Collection/Charts.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

namespace PhpOffice\PhpWord\Collection;

use PhpOffice\PhpWord\Element\Chart;

/**
* Charts collection.
*
* @since 0.12.0
* @extends AbstractCollection<Chart>
*/
class Charts extends AbstractCollection
{
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Collection/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

namespace PhpOffice\PhpWord\Collection;

use PhpOffice\PhpWord\Element\Comment;

/**
* Comments collection.
*
* @since 0.12.0
* @extends AbstractCollection<Comment>
*/
class Comments extends AbstractCollection
{
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Collection/Endnotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

namespace PhpOffice\PhpWord\Collection;

use PhpOffice\PhpWord\Element\Endnote;

/**
* Endnotes collection.
*
* @since 0.10.0
* @extends AbstractCollection<Endnote>
*/
class Endnotes extends AbstractCollection
{
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Collection/Footnotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

namespace PhpOffice\PhpWord\Collection;

use PhpOffice\PhpWord\Element\Footnote;

/**
* Footnotes collection.
*
* @since 0.10.0
* @extends AbstractCollection<Footnote>
*/
class Footnotes extends AbstractCollection
{
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Collection/Titles.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

namespace PhpOffice\PhpWord\Collection;

use PhpOffice\PhpWord\Element\Title;

/**
* Titles collection.
*
* @since 0.10.0
* @extends AbstractCollection<Title>
*/
class Titles extends AbstractCollection
{
Expand Down
90 changes: 72 additions & 18 deletions src/PhpWord/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

use DateTime;
use InvalidArgumentException;
use PhpOffice\PhpWord\Collection\Comments;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;

/**
* Element abstract class.
Expand All @@ -32,7 +34,7 @@ abstract class AbstractElement
/**
* PhpWord object.
*
* @var ?\PhpOffice\PhpWord\PhpWord
* @var ?PhpWord
*/
protected $phpWord;

Expand Down Expand Up @@ -131,25 +133,25 @@ abstract class AbstractElement
protected $collectionRelation = false;

/**
* The start position for the linked comment.
* The start position for the linked comments.
*
* @var Comment
* @var Comments
*/
protected $commentRangeStart;
protected $commentsRangeStart;

/**
* The end position for the linked comment.
* The end position for the linked comments.
*
* @var Comment
* @var Comments
*/
protected $commentRangeEnd;
protected $commentsRangeEnd;

/**
* Get PhpWord.
*
* @return ?\PhpOffice\PhpWord\PhpWord
* @return ?PhpWord
*/
public function getPhpWord()
public function getPhpWord(): ?PhpWord
{
return $this->phpWord;
}
Expand Down Expand Up @@ -287,14 +289,28 @@ public function getNestedLevel()
return $this->nestedLevel;
}

/**
* Get comments start.
*
* @return Comments
*/
public function getCommentsRangeStart(): ?Comments
{
return $this->commentsRangeStart;
}

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

return null;
}

/**
Expand All @@ -305,18 +321,44 @@ public function setCommentRangeStart(Comment $value): void
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 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;
}
}
$idxItem = $this->commentsRangeStart->addItem($value);
$this->commentsRangeStart->getItem($idxItem)->setStartElement($this);
}

/**
* Get comments end.
*
* @return Comments
*/
public function getCommentsRangeEnd(): ?Comments
{
return $this->commentsRangeEnd;
}

/**
* Get comment end.
*
* @return Comment
*/
public function getCommentRangeEnd()
public function getCommentRangeEnd(): ?Comment
{
return $this->commentRangeEnd;
if ($this->commentsRangeEnd != null) {
return $this->commentsRangeEnd->getItem($this->commentsRangeEnd->countItems());
}

return null;
}

/**
Expand All @@ -327,8 +369,20 @@ public function setCommentRangeEnd(Comment $value): void
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 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;
}
}
$idxItem = $this->commentsRangeEnd->addItem($value);
$this->commentsRangeEnd->getItem($idxItem)->setEndElement($this);
}

/**
Expand Down Expand Up @@ -428,7 +482,7 @@ public function isInSection()
* Set new style value.
*
* @param mixed $styleObject Style object
* @param null|array|\PhpOffice\PhpWord\Style|string $styleValue Style value
* @param null|array|string|Style $styleValue Style value
* @param bool $returnObject Always return object
*
* @return mixed
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 @@ -83,9 +83,7 @@ public function getInitials()
public function setStartElement(AbstractElement $value): void
{
$this->startElement = $value;
if ($value->getCommentRangeStart() == null) {
$value->setCommentRangeStart($this);
}
$value->setCommentRangeStart($this);
}

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

/**
Expand Down
1 change: 0 additions & 1 deletion src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function __call($function, $args)
if (in_array($function, $addCollection)) {
$key = ucfirst(str_replace('add', '', $function) . 's');

/** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */
$collectionObject = $this->collections[$key];

return $collectionObject->addItem($args[0] ?? null);
Expand Down
Loading

0 comments on commit 8392134

Please sign in to comment.