This commit is contained in:
Roman Syroeshko 2015-08-16 20:20:13 +04:00
parent 7c371e1d08
commit 8dc276a0db
1 changed files with 29 additions and 12 deletions

View File

@ -82,16 +82,16 @@ class TemplateProcessor
$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'));
}
/**
@ -175,7 +175,7 @@ class TemplateProcessor
*/
public function cloneRow($search, $numberOfClones)
{
if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
if ('${' !== substr($search, 0, 2) && '}' !== substr($search, -1)) {
$search = '${' . $search . '}';
}
@ -345,6 +345,31 @@ class TemplateProcessor
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.
*
@ -356,14 +381,6 @@ class TemplateProcessor
*/
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 . '}';
}