Skip to content

Add support for Track changes #1262

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

Merged
merged 27 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
commit
  • Loading branch information
Cip committed Dec 22, 2017
commit 82eeb06eedc67de8aa79a2703e3186879d4365fe
2 changes: 1 addition & 1 deletion src/PhpWord/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ abstract class AbstractElement
*
* @var object
*/
private $changed;
public $changed;

/**
* Parent container type
Expand Down
49 changes: 37 additions & 12 deletions src/PhpWord/Reader/ODText/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function read(PhpWord $phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);

$trackedChanges = [];

$nodes = $xmlReader->getElements('office:body/office:text/*');
if ($nodes->length > 0) {
Expand All @@ -48,26 +50,38 @@ public function read(PhpWord $phpWord)
$section->addTitle($node->nodeValue, $depth);
break;
case 'text:p': // Paragraph
//\Yii::info($node, 'debug');
//\Yii::info($node->nodeValue, 'debug');
//\Yii::info($node->hasChildNodes(), 'debug');

$children = $node->childNodes;
foreach ($children as $child) {
switch($child->nodeName){
case 'text:change-start':
\Yii::info($child->getAttribute('text:change-id'), 'debug');
break;
$changeId = $child->getAttribute('text:change-id');
if(isset($trackedChanges[$changeId])) {
$changed = $trackedChanges[$changeId];
}
break;
case 'text:change-end':
\Yii::info($child->getAttribute('text:change-id'), 'debug');
break;
unset($changed);
break;
case 'text:change':
\Yii::info($child->getAttribute('text:change-id'), 'debug');
break;
$changeId = $child->getAttribute('text:change-id');
if(isset($trackedChanges[$changeId])) {
$changed = $trackedChanges[$changeId];
}
break;
}
}

$section->addText($node->nodeValue);
$element = $section->addText($node->nodeValue);
if(isset($changed)) {
$element->changed = $changed['changed'];
if(isset($changed['textNodes'])) {
foreach ($changed['textNodes'] as $changedNode) {
$element = $section->addText($changedNode->nodeValue);
$element->changed = $changed['changed'];
}
}
}

break;
case 'text:list': // List
$listItems = $xmlReader->getElements('text:list-item/text:p', $node);
Expand All @@ -77,7 +91,18 @@ public function read(PhpWord $phpWord)
}
break;
case 'text:tracked-changes':

$changedRegions = $xmlReader->getElements('text:changed-region', $node);
foreach ($changedRegions as $changedRegion) {
$type = ($changedRegion->firstChild->nodeName == 'text:insertion')?\PhpOffice\PhpWord\Element\ChangedElement::TYPE_INSERTED:\PhpOffice\PhpWord\Element\ChangedElement::TYPE_DELETED;
$author = $xmlReader->getElements('office:change-info/dc:creator', $changedRegion->firstChild)[0]->nodeValue;
$date = $xmlReader->getElements('office:change-info/dc:date', $changedRegion->firstChild)[0]->nodeValue;
$date = preg_replace('/\.\d+$/', '', $date);
$date = \DateTime::createFromFormat ('Y-m-d\TH:i:s', $date);
$changed = new \PhpOffice\PhpWord\Element\ChangedElement($type, $author, $date);
$textNodes = $xmlReader->getElements('text:deletion/text:p', $changedRegion);
$trackedChanges[$changedRegion->getAttribute('text:id')] = ['changed'=>$changed,
'textNodes'=>$textNodes];
}
break;
}
}
Expand Down