Skip to content

Commit 18cb0b2

Browse files
authored
Add support for comments (#1067)
Just a writer for now, reader to be done
1 parent be6b600 commit 18cb0b2

23 files changed

+726
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Thumbs.db
66
Desktop.ini
77
.idea
88
_build
9+
/build
910
phpunit.xml
1011
composer.lock
1112
composer.phar

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"require-dev": {
4343
"phpunit/phpunit": "3.7.*",
4444
"phpdocumentor/phpdocumentor":"2.*",
45+
"twig/twig":"1.27",
4546
"squizlabs/php_codesniffer": "1.*",
4647
"phpmd/phpmd": "2.*",
4748
"phploc/phploc": "2.*",

docs/elements.rst

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,27 @@ Available line style attributes:
398398
- ``endArrow``. End type of arrow: block, open, classic, diamond, oval.
399399
- ``width``. Line-object width in pt.
400400
- ``height``. Line-object height in pt.
401-
- ``flip``. Flip the line element: true, false.
401+
- ``flip``. Flip the line element: true, false.
402+
403+
Comments
404+
---------
405+
406+
Comments can be added to a document by using ``addComment``.
407+
The comment can contain formatted text. Once the comment has been added, it can be linked to any to any element.
408+
409+
.. code-block:: php
410+
411+
// first create a comment
412+
$comment= new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
413+
$comment->addText('Test', array('bold' => true));
414+
415+
// add it to the document
416+
$phpWord->addComment($comment);
417+
418+
$textrun = $section->addTextRun();
419+
$textrun->addText('This ');
420+
$text = $textrun->addText('is');
421+
// link the comment to the text you just created
422+
$text->setCommentStart($comment);
423+
424+
If no end is set for a comment using the ``setCommentEnd``, the comment will be ended automatically at the end of the element it is started on.

docs/intro.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Features
4949
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
5050
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
5151
- Insert form fields (textinput, checkbox, and dropdown)
52+
- Insert comments
5253
- Create document from templates
5354
- Use XSL 1.0 style sheets to transform headers, main document part, and footers of an OOXML template
5455
- ... and many more features on progress
@@ -102,6 +103,8 @@ Writers
102103
+---------------------------+----------------------+--------+-------+-------+--------+-------+
103104
| | Endnote || | || |
104105
+---------------------------+----------------------+--------+-------+-------+--------+-------+
106+
| | Comments || | | | |
107+
+---------------------------+----------------------+--------+-------+-------+--------+-------+
105108
| **Graphs** | 2D basic graphs || | | | |
106109
+---------------------------+----------------------+--------+-------+-------+--------+-------+
107110
| | 2D advanced graphs | | | | | |
@@ -161,6 +164,8 @@ Readers
161164
+---------------------------+----------------------+--------+-------+-------+-------+-------+
162165
| | Endnote || | | | |
163166
+---------------------------+----------------------+--------+-------+-------+-------+-------+
167+
| | Comments | | | | | |
168+
+---------------------------+----------------------+--------+-------+-------+-------+-------+
164169
| **Graphs** | 2D basic graphs | | | | | |
165170
+---------------------------+----------------------+--------+-------+-------+-------+-------+
166171
| | 2D advanced graphs | | | | | |

samples/Sample_37_Comments.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// New Word Document
5+
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
6+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
8+
// A comment
9+
$comment = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
10+
$comment->addText('Test', array('bold' => true));
11+
$phpWord->addComment($comment);
12+
13+
$section = $phpWord->addSection();
14+
15+
$textrun = $section->addTextRun();
16+
$textrun->addText('This ');
17+
$text = $textrun->addText('is');
18+
$text->setCommentRangeStart($comment);
19+
$textrun->addText(' a test');
20+
21+
$section->addTextBreak(2);
22+
23+
// Let's create a comment that we will link to a start element and an end element
24+
$commentWithStartAndEnd = new \PhpOffice\PhpWord\Element\Comment('Foo Bar', new \DateTime(), '');
25+
$commentWithStartAndEnd->addText('A comment with a start and an end');
26+
$phpWord->addComment($commentWithStartAndEnd);
27+
28+
$textrunWithEnd = $section->addTextRun();
29+
$textrunWithEnd->addText('This ');
30+
$textToStartOn = $textrunWithEnd->addText('is', array('bold' => true));
31+
$textToStartOn->setCommentRangeStart($commentWithStartAndEnd);
32+
$textrunWithEnd->addText(' another', array('italic' => true));
33+
$textToEndOn = $textrunWithEnd->addText(' test');
34+
$textToEndOn->setCommentRangeEnd($commentWithStartAndEnd);
35+
36+
$section->addTextBreak(2);
37+
38+
// Let's add a comment on an image
39+
$commentOnImage = new \PhpOffice\PhpWord\Element\Comment('Mr Smart', new \DateTime(), '');
40+
$imageComment = $commentOnImage->addTextRun();
41+
$imageComment->addText('Hey, Mars does look ');
42+
$imageComment->addText('red', array('color' => 'FF0000'));
43+
$phpWord->addComment($commentOnImage);
44+
$image = $section->addImage('resources/_mars.jpg');
45+
$image->setCommentRangeStart($commentOnImage);
46+
47+
$section->addTextBreak(2);
48+
49+
// We can also do things the other way round, link the comment to the element
50+
$anotherText = $section->addText("another text");
51+
52+
$comment1 = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
53+
$comment1->addText('Test', array('bold' => true));
54+
$comment1->setStartElement($anotherText);
55+
$comment1->setEndElement($anotherText);
56+
$phpWord->addComment($comment1);
57+
58+
59+
// Save file
60+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
61+
if (!CLI) {
62+
include_once 'Sample_Footer.php';
63+
}

src/PhpWord/Collection/Comments.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2016 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Collection;
19+
20+
/**
21+
* Comments collection
22+
*
23+
* @since 0.12.0
24+
*/
25+
class Comments extends AbstractCollection
26+
{
27+
}

src/PhpWord/Element/AbstractContainer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ abstract class AbstractContainer extends AbstractElement
5858
protected $elements = array();
5959

6060
/**
61-
* Container type Section|Header|Footer|Footnote|Endnote|Cell|TextRun|TextBox|ListItemRun
61+
* Container type Section|Header|Footer|Footnote|Endnote|Cell|TextRun|TextBox|ListItemRun|TrackChange
6262
*
6363
* @var string
6464
*/
@@ -83,7 +83,7 @@ public function __call($function, $args)
8383
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object',
8484
'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field',
8585
'Line', 'Shape', 'Title', 'TOC', 'PageBreak',
86-
'Chart', 'FormField', 'SDT'
86+
'Chart', 'FormField', 'SDT', 'Comment'
8787
);
8888
$functions = array();
8989
foreach ($elements as $element) {
@@ -188,7 +188,7 @@ public function countElements()
188188
private function checkValidity($method)
189189
{
190190
$generalContainers = array(
191-
'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun',
191+
'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun', 'TrackChange',
192192
);
193193

194194
$validContainers = array(
@@ -203,7 +203,8 @@ private function checkValidity($method)
203203
'Shape' => $generalContainers,
204204
'FormField' => $generalContainers,
205205
'SDT' => $generalContainers,
206-
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
206+
'TrackChange' => $generalContainers,
207+
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox', 'TrackChange'),
207208
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
208209
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
209210
'Table' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),

src/PhpWord/Element/AbstractElement.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,26 @@ abstract class AbstractElement
108108
protected $mediaRelation = false;
109109

110110
/**
111-
* Is part of collection; true for Title, Footnote, Endnote, and Chart
111+
* Is part of collection; true for Title, Footnote, Endnote, Chart, and Comment
112112
*
113113
* @var bool
114114
*/
115115
protected $collectionRelation = false;
116116

117+
/**
118+
* The start position for the linked comment
119+
*
120+
* @var Comment
121+
*/
122+
protected $commentRangeStart;
123+
124+
/**
125+
* The end position for the linked comment
126+
*
127+
* @var Comment
128+
*/
129+
protected $commentRangeEnd;
130+
117131
/**
118132
* Get PhpWord
119133
*
@@ -265,6 +279,55 @@ public function getNestedLevel()
265279
return $this->nestedLevel;
266280
}
267281

282+
/**
283+
* Get comment start
284+
*
285+
* @return Comment
286+
*/
287+
public function getCommentRangeStart()
288+
{
289+
return $this->commentRangeStart;
290+
}
291+
292+
/**
293+
* Set comment start
294+
*
295+
* @param Comment $value
296+
*/
297+
public function setCommentRangeStart(Comment $value)
298+
{
299+
if ($this instanceof Comment) {
300+
throw new \InvalidArgumentException("Cannot set a Comment on a Comment");
301+
}
302+
$this->commentRangeStart= $value;
303+
$this->commentRangeStart->setStartElement($this);
304+
}
305+
306+
/**
307+
* Get comment end
308+
*
309+
* @return Comment
310+
*/
311+
public function getCommentRangeEnd()
312+
{
313+
return $this->commentRangeEnd;
314+
}
315+
316+
/**
317+
* Set comment end
318+
*
319+
* @param Comment $value
320+
* @return void
321+
*/
322+
public function setCommentRangeEnd(Comment $value)
323+
{
324+
if ($this instanceof Comment) {
325+
throw new \InvalidArgumentException("Cannot set a Comment on a Comment");
326+
}
327+
$this->commentRangeEnd= $value;
328+
$this->commentRangeEnd->setEndElement($this);
329+
}
330+
268331
/**
269332
* Set parent container
270333
*

0 commit comments

Comments
 (0)