@@ -49,6 +49,13 @@ class TemplateProcessor
49
49
*/
50
50
protected $ tempDocumentMainPart ;
51
51
52
+ /**
53
+ * Content of settings part (in XML format) of the temporary document
54
+ *
55
+ * @var string
56
+ */
57
+ protected $ tempDocumentSettingsPart ;
58
+
52
59
/**
53
60
* Content of headers (in XML format) of the temporary document
54
61
*
@@ -120,6 +127,7 @@ public function __construct($documentTemplate)
120
127
}
121
128
122
129
$ this ->tempDocumentMainPart = $ this ->readPartWithRels ($ this ->getMainPartName ());
130
+ $ this ->tempDocumentSettingsPart = $ this ->readPartWithRels ($ this ->getSettingsPartName ());
123
131
$ this ->tempDocumentContentTypes = $ this ->zipClass ->getFromName ($ this ->getDocumentContentTypesName ());
124
132
}
125
133
@@ -792,6 +800,22 @@ public function deleteBlock($blockname)
792
800
$ this ->replaceBlock ($ blockname , '' );
793
801
}
794
802
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
+
795
819
/**
796
820
* Saves the result document.
797
821
*
@@ -806,6 +830,7 @@ public function save()
806
830
}
807
831
808
832
$ this ->savePartWithRels ($ this ->getMainPartName (), $ this ->tempDocumentMainPart );
833
+ $ this ->savePartWithRels ($ this ->getSettingsPartName (), $ this ->tempDocumentSettingsPart );
809
834
810
835
foreach ($ this ->tempDocumentFooters as $ index => $ xml ) {
811
836
$ this ->savePartWithRels ($ this ->getFooterName ($ index ), $ xml );
@@ -943,6 +968,16 @@ protected function getMainPartName()
943
968
return array_key_exists (1 , $ matches ) ? $ matches [1 ] : 'word/document.xml ' ;
944
969
}
945
970
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
+
946
981
/**
947
982
* Get the name of the footer file for $index.
948
983
*
0 commit comments