@@ -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 *
0 commit comments