Skip to content

Commit 58a2849

Browse files
committed
Add reading of the settings part
1 parent d2b0b31 commit 58a2849

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class TemplateProcessor
4949
*/
5050
protected $tempDocumentMainPart;
5151

52+
/**
53+
* Content of settings part (in XML format) of the temporary document
54+
*
55+
* @var string
56+
*/
57+
protected $tempDocumentSettingsPart;
58+
5259
/**
5360
* Content of headers (in XML format) of the temporary document
5461
*
@@ -120,6 +127,7 @@ public function __construct($documentTemplate)
120127
}
121128

122129
$this->tempDocumentMainPart = $this->readPartWithRels($this->getMainPartName());
130+
$this->tempDocumentSettingsPart = $this->readPartWithRels($this->getSettingsPartName());
123131
$this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName());
124132
}
125133

@@ -792,6 +800,22 @@ public function deleteBlock($blockname)
792800
$this->replaceBlock($blockname, '');
793801
}
794802

803+
/**
804+
* Automatically Recalculate Fields on Open
805+
*
806+
* @param bool $update
807+
*/
808+
public function setUpdateFields($update = true)
809+
{
810+
$string = $update ? 'true' : 'false';
811+
$matches = array();
812+
if (preg_match('/<w:updateFields w:val=\"(true|false|1|0|on|off)\"\/>/', $this->tempDocumentSettingsPart, $matches)) {
813+
$this->tempDocumentSettingsPart = str_replace($matches[0], '<w:updateFields w:val="' . $string . '"/>', $this->tempDocumentSettingsPart);
814+
} else {
815+
$this->tempDocumentSettingsPart = str_replace('</w:settings>', '<w:updateFields w:val="' . $string . '"/></w:settings>', $this->tempDocumentSettingsPart);
816+
}
817+
}
818+
795819
/**
796820
* Saves the result document.
797821
*
@@ -806,6 +830,7 @@ public function save()
806830
}
807831

808832
$this->savePartWithRels($this->getMainPartName(), $this->tempDocumentMainPart);
833+
$this->savePartWithRels($this->getSettingsPartName(), $this->tempDocumentSettingsPart);
809834

810835
foreach ($this->tempDocumentFooters as $index => $xml) {
811836
$this->savePartWithRels($this->getFooterName($index), $xml);
@@ -943,6 +968,16 @@ protected function getMainPartName()
943968
return array_key_exists(1, $matches) ? $matches[1] : 'word/document.xml';
944969
}
945970

971+
/**
972+
* The name of the file containing the Settings part
973+
*
974+
* @return string
975+
*/
976+
protected function getSettingsPartName()
977+
{
978+
return 'word/settings.xml';
979+
}
980+
946981
/**
947982
* Get the name of the footer file for $index.
948983
*

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,18 @@ public function testShouldReturnFalseIfXmlBlockNotFound()
830830
$result = $templateProcessor->findContainingXmlBlockForMacro('${macro}', 'w:rPr');
831831
$this->assertFalse($result);
832832
}
833+
834+
public function testShouldMakeFieldsUpdateOnOpen()
835+
{
836+
$settingsPart = '<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
837+
<w:zoom w:percent="100"/>
838+
</w:settings>';
839+
$templateProcessor = new TestableTemplateProcesor(null, $settingsPart);
840+
841+
$templateProcessor->setUpdateFields(true);
842+
$this->assertContains('<w:updateFields w:val="true"/>', $templateProcessor->getSettingsPart());
843+
844+
$templateProcessor->setUpdateFields(false);
845+
$this->assertContains('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
846+
}
833847
}

tests/PhpWord/_includes/TestableTemplateProcesor.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
*/
2626
class TestableTemplateProcesor extends TemplateProcessor
2727
{
28-
public function __construct($mainPart = null)
28+
public function __construct($mainPart = null, $settingsPart = null)
2929
{
3030
$this->tempDocumentMainPart = $mainPart;
31+
$this->tempDocumentSettingsPart = $settingsPart;
3132
}
3233

3334
public function fixBrokenMacros($documentPart)
@@ -74,4 +75,12 @@ public function getMainPart()
7475
{
7576
return $this->tempDocumentMainPart;
7677
}
78+
79+
/**
80+
* @return string
81+
*/
82+
public function getSettingsPart()
83+
{
84+
return $this->tempDocumentSettingsPart;
85+
}
7786
}

0 commit comments

Comments
 (0)