Skip to content

Commit

Permalink
Merged #513.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Syroeshko committed Aug 16, 2015
1 parent 7c371e1 commit 8dc276a
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ public function __construct($documentTemplate)
$this->zipClass = new ZipArchive();
$this->zipClass->open($this->temporaryDocumentFilename);
$index = 1;
while ($this->zipClass->locateName($this->getHeaderName($index)) !== false) {
while (false !== $this->zipClass->locateName($this->getHeaderName($index))) {
$this->temporaryDocumentHeaders[$index] = $this->zipClass->getFromName($this->getHeaderName($index));
$index++;
}
$index = 1;
while ($this->zipClass->locateName($this->getFooterName($index)) !== false) {
while (false !== $this->zipClass->locateName($this->getFooterName($index))) {
$this->temporaryDocumentFooters[$index] = $this->zipClass->getFromName($this->getFooterName($index));
$index++;
}
$this->temporaryDocumentMainPart = $this->zipClass->getFromName('word/document.xml');
$this->tempDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName('word/document.xml'));
}

/**
Expand Down Expand Up @@ -175,7 +175,7 @@ public function getVariables()
*/
public function cloneRow($search, $numberOfClones)
{
if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
if ('${' !== substr($search, 0, 2) && '}' !== substr($search, -1)) {
$search = '${' . $search . '}';
}

Expand Down Expand Up @@ -345,6 +345,31 @@ public function saveAs($fileName)
unlink($tempFileName);
}

/**
* Finds parts of broken macros and sticks them together.
* Macros, while being edited, could be implicitly broken by some of the word processors.
*
* @since 0.13.0
*
* @param string $documentPart The document part in XML representation.
*
* @return string
*/
protected function fixBrokenMacros($documentPart)
{
$fixedDocumentPart = $documentPart;

$fixedDocumentPart = preg_replace_callback(
'|\$\{([^\}]+)\}|U',
function ($match) {
return strip_tags($match[0]);
},
$fixedDocumentPart
);

return $fixedDocumentPart;
}

/**
* Find and replace placeholders in the given XML section.
*
Expand All @@ -356,14 +381,6 @@ public function saveAs($fileName)
*/
protected function setValueForPart($documentPartXML, $search, $replace, $limit)
{
$pattern = '|\$\{([^\}]+)\}|U';
preg_match_all($pattern, $documentPartXML, $matches);
foreach ($matches[0] as $value) {
$valueCleaned = preg_replace('/<[^>]+>/', '', $value);
$valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned);
$documentPartXML = str_replace($value, $valueCleaned, $documentPartXML);
}

if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
$search = '${' . $search . '}';
}
Expand Down

0 comments on commit 8dc276a

Please sign in to comment.