Add reading of the settings part

This commit is contained in:
troosan 2019-02-04 23:59:37 +01:00
parent d2b0b317e0
commit 58a2849e38
3 changed files with 59 additions and 1 deletions

View File

@ -49,6 +49,13 @@ class TemplateProcessor
*/ */
protected $tempDocumentMainPart; protected $tempDocumentMainPart;
/**
* Content of settings part (in XML format) of the temporary document
*
* @var string
*/
protected $tempDocumentSettingsPart;
/** /**
* Content of headers (in XML format) of the temporary document * Content of headers (in XML format) of the temporary document
* *
@ -120,6 +127,7 @@ class TemplateProcessor
} }
$this->tempDocumentMainPart = $this->readPartWithRels($this->getMainPartName()); $this->tempDocumentMainPart = $this->readPartWithRels($this->getMainPartName());
$this->tempDocumentSettingsPart = $this->readPartWithRels($this->getSettingsPartName());
$this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName()); $this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName());
} }
@ -792,6 +800,22 @@ class TemplateProcessor
$this->replaceBlock($blockname, ''); $this->replaceBlock($blockname, '');
} }
/**
* Automatically Recalculate Fields on Open
*
* @param bool $update
*/
public function setUpdateFields($update = true)
{
$string = $update ? 'true' : 'false';
$matches = array();
if (preg_match('/<w:updateFields w:val=\"(true|false|1|0|on|off)\"\/>/', $this->tempDocumentSettingsPart, $matches)) {
$this->tempDocumentSettingsPart = str_replace($matches[0], '<w:updateFields w:val="' . $string . '"/>', $this->tempDocumentSettingsPart);
} else {
$this->tempDocumentSettingsPart = str_replace('</w:settings>', '<w:updateFields w:val="' . $string . '"/></w:settings>', $this->tempDocumentSettingsPart);
}
}
/** /**
* Saves the result document. * Saves the result document.
* *
@ -806,6 +830,7 @@ class TemplateProcessor
} }
$this->savePartWithRels($this->getMainPartName(), $this->tempDocumentMainPart); $this->savePartWithRels($this->getMainPartName(), $this->tempDocumentMainPart);
$this->savePartWithRels($this->getSettingsPartName(), $this->tempDocumentSettingsPart);
foreach ($this->tempDocumentFooters as $index => $xml) { foreach ($this->tempDocumentFooters as $index => $xml) {
$this->savePartWithRels($this->getFooterName($index), $xml); $this->savePartWithRels($this->getFooterName($index), $xml);
@ -943,6 +968,16 @@ class TemplateProcessor
return array_key_exists(1, $matches) ? $matches[1] : 'word/document.xml'; return array_key_exists(1, $matches) ? $matches[1] : 'word/document.xml';
} }
/**
* The name of the file containing the Settings part
*
* @return string
*/
protected function getSettingsPartName()
{
return 'word/settings.xml';
}
/** /**
* Get the name of the footer file for $index. * Get the name of the footer file for $index.
* *

View File

@ -830,4 +830,18 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$result = $templateProcessor->findContainingXmlBlockForMacro('${macro}', 'w:rPr'); $result = $templateProcessor->findContainingXmlBlockForMacro('${macro}', 'w:rPr');
$this->assertFalse($result); $this->assertFalse($result);
} }
public function testShouldMakeFieldsUpdateOnOpen()
{
$settingsPart = '<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:zoom w:percent="100"/>
</w:settings>';
$templateProcessor = new TestableTemplateProcesor(null, $settingsPart);
$templateProcessor->setUpdateFields(true);
$this->assertContains('<w:updateFields w:val="true"/>', $templateProcessor->getSettingsPart());
$templateProcessor->setUpdateFields(false);
$this->assertContains('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
}
} }

View File

@ -25,9 +25,10 @@ namespace PhpOffice\PhpWord;
*/ */
class TestableTemplateProcesor extends TemplateProcessor class TestableTemplateProcesor extends TemplateProcessor
{ {
public function __construct($mainPart = null) public function __construct($mainPart = null, $settingsPart = null)
{ {
$this->tempDocumentMainPart = $mainPart; $this->tempDocumentMainPart = $mainPart;
$this->tempDocumentSettingsPart = $settingsPart;
} }
public function fixBrokenMacros($documentPart) public function fixBrokenMacros($documentPart)
@ -74,4 +75,12 @@ class TestableTemplateProcesor extends TemplateProcessor
{ {
return $this->tempDocumentMainPart; return $this->tempDocumentMainPart;
} }
/**
* @return string
*/
public function getSettingsPart()
{
return $this->tempDocumentSettingsPart;
}
} }