Skip to content

Commit 5b381bc

Browse files
authored
Merge branch 'develop' into various_html_parsing_fixes
2 parents 9cd5ab7 + 604e60c commit 5b381bc

File tree

21 files changed

+494
-38
lines changed

21 files changed

+494
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ v0.15.0 (?? ??? 2018)
99
- Parsing of "align" HTML attribute - @troosan #1231
1010
- Parse formatting inside HTML lists - @troosan @samimussbach #1239 #945 #1215 #508
1111
- Parsing of CSS `direction` instruction, HTML `lang` attribute, formatting inside table cell - @troosan #1273 #1252 #1254
12+
- Add support for Track changes @Cip @troosan #354 #1262
1213

1314
### Fixed
1415
- fix reading of docx default style - @troosan #1238

docs/elements.rst

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ column shows the containers while the rows lists the elements.
3131
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
3232
| 11 | Watermark | - | v | - | - | - | - |
3333
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
34-
| 12 | Object | v | v | v | v | v | v |
34+
| 12 | OLEObject | v | v | v | v | v | v |
3535
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
3636
| 13 | TOC | v | - | - | - | - | - |
3737
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
@@ -77,6 +77,13 @@ italics, etc) or other elements, e.g. images or links. The syntaxes are as follo
7777

7878
For available styling options see :ref:`font-style` and :ref:`paragraph-style`.
7979

80+
If you want to enable track changes on added text you can mark it as INSERTED or DELETED by a specific user at a given time:
81+
82+
.. code-block:: php
83+
84+
$text = $section->addText('Hello World!');
85+
$text->setChanged(\PhpOffice\PhpWord\Element\ChangedElement::TYPE_INSERTED, 'Fred', (new \DateTime()));
86+
8087
Titles
8188
~~~~~~
8289

@@ -276,11 +283,11 @@ Objects
276283
-------
277284

278285
You can add OLE embeddings, such as Excel spreadsheets or PowerPoint
279-
presentations to the document by using ``addObject`` method.
286+
presentations to the document by using ``addOLEObject`` method.
280287

281288
.. code-block:: php
282289
283-
$section->addObject($src, [$style]);
290+
$section->addOLEObject($src, [$style]);
284291
285292
Table of contents
286293
-----------------
@@ -309,7 +316,7 @@ Footnotes & endnotes
309316
You can create footnotes with ``addFootnote`` and endnotes with
310317
``addEndnote`` in texts or textruns, but it's recommended to use textrun
311318
to have better layout. You can use ``addText``, ``addLink``,
312-
``addTextBreak``, ``addImage``, ``addObject`` on footnotes and endnotes.
319+
``addTextBreak``, ``addImage``, ``addOLEObject`` on footnotes and endnotes.
313320

314321
On textrun:
315322

@@ -465,4 +472,28 @@ The comment can contain formatted text. Once the comment has been added, it can
465472
// link the comment to the text you just created
466473
$text->setCommentStart($comment);
467474
468-
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.
475+
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.
476+
477+
Track Changes
478+
-------------
479+
480+
Track changes can be set on text elements. There are 2 ways to set the change information on an element.
481+
Either by calling the `setChangeInfo()`, or by setting the `TrackChange` instance on the element with `setTrackChange()`.
482+
483+
.. code-block:: php
484+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
485+
486+
// New portrait section
487+
$section = $phpWord->addSection();
488+
$textRun = $section->addTextRun();
489+
490+
$text = $textRun->addText('Hello World! Time to ');
491+
492+
$text = $textRun->addText('wake ', array('bold' => true));
493+
$text->setChangeInfo(TrackChange::INSERTED, 'Fred', time() - 1800);
494+
495+
$text = $textRun->addText('up');
496+
$text->setTrackChange(new TrackChange(TrackChange::INSERTED, 'Fred'));
497+
498+
$text = $textRun->addText('go to sleep');
499+
$text->setChangeInfo(TrackChange::DELETED, 'Barney', new \DateTime('@' . (time() - 3600)));

samples/Sample_16_Object.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$section = $phpWord->addSection();
1010
$section->addText('You can open this OLE object by double clicking on the icon:');
1111
$section->addTextBreak(2);
12-
$section->addObject('resources/_sheet.xls');
12+
$section->addOLEObject('resources/_sheet.xls');
1313

1414
// Save file
1515
echo write($phpWord, basename(__FILE__, '.php'), $writers);

samples/Sample_39_TrackChanges.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
use PhpOffice\PhpWord\Element\TrackChange;
3+
4+
include_once 'Sample_Header.php';
5+
6+
// New Word Document
7+
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
8+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
9+
10+
// New portrait section
11+
$section = $phpWord->addSection();
12+
$textRun = $section->addTextRun();
13+
14+
$text = $textRun->addText('Hello World! Time to ');
15+
16+
$text = $textRun->addText('wake ', array('bold' => true));
17+
$text->setChangeInfo(TrackChange::INSERTED, 'Fred', time() - 1800);
18+
19+
$text = $textRun->addText('up');
20+
$text->setTrackChange(new TrackChange(TrackChange::INSERTED, 'Fred'));
21+
22+
$text = $textRun->addText('go to sleep');
23+
$text->setChangeInfo(TrackChange::DELETED, 'Barney', new \DateTime('@' . (time() - 3600)));
24+
25+
// Save file
26+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
27+
if (!CLI) {
28+
include_once 'Sample_Footer.php';
29+
}
-4.45 KB
Binary file not shown.
-412 Bytes
Binary file not shown.

src/PhpWord/Element/AbstractContainer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function __call($function, $args)
8181
{
8282
$elements = array(
8383
'Text', 'TextRun', 'Bookmark', 'Link', 'PreserveText', 'TextBreak',
84-
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object',
84+
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'OLEObject',
8585
'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field',
8686
'Line', 'Shape', 'Title', 'TOC', 'PageBreak',
8787
'Chart', 'FormField', 'SDT', 'Comment',
@@ -157,7 +157,7 @@ protected function addElement($elementName)
157157
/**
158158
* Get all elements
159159
*
160-
* @return array
160+
* @return \PhpOffice\PhpWord\Element\AbstractElement[]
161161
*/
162162
public function getElements()
163163
{

src/PhpWord/Element/AbstractElement.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ abstract class AbstractElement
100100
*/
101101
private $parent;
102102

103+
/**
104+
* changed element info
105+
*
106+
* @var TrackChange
107+
*/
108+
private $trackChange;
109+
103110
/**
104111
* Parent container type
105112
*
@@ -438,6 +445,38 @@ protected function setNewStyle($styleObject, $styleValue = null, $returnObject =
438445
return $style;
439446
}
440447

448+
/**
449+
* Sets the trackChange information
450+
*
451+
* @param TrackChange $trackChange
452+
*/
453+
public function setTrackChange(TrackChange $trackChange)
454+
{
455+
$this->trackChange = $trackChange;
456+
}
457+
458+
/**
459+
* Gets the trackChange information
460+
*
461+
* @return TrackChange
462+
*/
463+
public function getTrackChange()
464+
{
465+
return $this->trackChange;
466+
}
467+
468+
/**
469+
* Set changed
470+
*
471+
* @param string $type INSERTED|DELETED
472+
* @param string $author
473+
* @param null|int|\DateTime $date allways in UTC
474+
*/
475+
public function setChangeInfo($type, $author, $date = null)
476+
{
477+
$this->trackChange = new TrackChange($type, $author, $date);
478+
}
479+
441480
/**
442481
* Set enum value
443482
*

src/PhpWord/Element/Comment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ class Comment extends TrackChange
5555
* Create a new Comment Element
5656
*
5757
* @param string $author
58-
* @param \DateTime $date
58+
* @param null|\DateTime $date
5959
* @param string $initials
6060
*/
6161
public function __construct($author, $date = null, $initials = null)
6262
{
63-
parent::__construct($author, $date);
63+
parent::__construct(null, $author, $date);
6464
$this->initials = $initials;
6565
}
6666

src/PhpWord/Element/Image.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ public function getImageStringData($base64 = false)
334334
// Read image binary data and convert to hex/base64 string
335335
if ($this->sourceType == self::SOURCE_GD) {
336336
$imageResource = call_user_func($this->imageCreateFunc, $actualSource);
337+
if ($this->imageType === 'image/png') {
338+
// PNG images need to preserve alpha channel information
339+
imagesavealpha($imageResource, true);
340+
}
337341
ob_start();
338342
call_user_func($this->imageFunc, $imageResource);
339343
$imageBinary = ob_get_contents();

0 commit comments

Comments
 (0)