17
17
18
18
namespace PhpOffice \PhpWord \Writer \RTF \Part ;
19
19
20
+ use PhpOffice \PhpWord \Element \Footer ;
20
21
use PhpOffice \PhpWord \Settings ;
21
22
use PhpOffice \PhpWord \Writer \RTF \Element \Container ;
22
23
use PhpOffice \PhpWord \Writer \RTF \Style \Section as SectionStyleWriter ;
@@ -105,11 +106,36 @@ private function writeFormatting()
105
106
$ content .= '\lang ' . $ langId ;
106
107
$ content .= '\kerning1 ' ; // Point size (in half-points) above which to kern character pairs
107
108
$ content .= '\fs ' . (Settings::getDefaultFontSize () * 2 ); // Set the font size in half-points
109
+ if ($ docSettings ->hasEvenAndOddHeaders ()) {
110
+ $ content .= '\\facingp ' ;
111
+ }
108
112
$ content .= PHP_EOL ;
109
113
110
114
return $ content ;
111
115
}
112
116
117
+ /**
118
+ * Write titlepg directive if any "f" headers or footers
119
+ *
120
+ * @param \PhpOffice\PhpWord\Element\Section $section
121
+ * @return string
122
+ */
123
+ private static function writeTitlepg ($ section )
124
+ {
125
+ foreach ($ section ->getHeaders () as $ header ) {
126
+ if ($ header ->getType () === Footer::FIRST ) {
127
+ return '\\titlepg ' . PHP_EOL ;
128
+ }
129
+ }
130
+ foreach ($ section ->getFooters () as $ header ) {
131
+ if ($ header ->getType () === Footer::FIRST ) {
132
+ return '\\titlepg ' . PHP_EOL ;
133
+ }
134
+ }
135
+
136
+ return '' ;
137
+ }
138
+
113
139
/**
114
140
* Write sections
115
141
*
@@ -120,10 +146,53 @@ private function writeSections()
120
146
$ content = '' ;
121
147
122
148
$ sections = $ this ->getParentWriter ()->getPhpWord ()->getSections ();
149
+ $ evenOdd = $ this ->getParentWriter ()->getPhpWord ()->getSettings ()->hasEvenAndOddHeaders ();
123
150
foreach ($ sections as $ section ) {
124
151
$ styleWriter = new SectionStyleWriter ($ section ->getStyle ());
125
152
$ styleWriter ->setParentWriter ($ this ->getParentWriter ());
126
153
$ content .= $ styleWriter ->write ();
154
+ $ content .= self ::writeTitlepg ($ section );
155
+
156
+ foreach ($ section ->getHeaders () as $ header ) {
157
+ $ type = $ header ->getType ();
158
+ if ($ evenOdd || $ type !== FOOTER ::EVEN ) {
159
+ $ content .= '{ \\header ' ;
160
+ if ($ type === Footer::FIRST ) {
161
+ $ content .= 'f ' ;
162
+ } elseif ($ evenOdd ) {
163
+ $ content .= ($ type === FOOTER ::EVEN ) ? 'l ' : 'r ' ;
164
+ }
165
+ foreach ($ header ->getElements () as $ element ) {
166
+ $ cl = get_class ($ element );
167
+ $ cl2 = str_replace ('Element ' , 'Writer \\RTF \\Element ' , $ cl );
168
+ if (class_exists ($ cl2 )) {
169
+ $ elementWriter = new $ cl2 ($ this ->getParentWriter (), $ element );
170
+ $ content .= $ elementWriter ->write ();
171
+ }
172
+ }
173
+ $ content .= '} ' . PHP_EOL ;
174
+ }
175
+ }
176
+ foreach ($ section ->getFooters () as $ footer ) {
177
+ $ type = $ footer ->getType ();
178
+ if ($ evenOdd || $ type !== FOOTER ::EVEN ) {
179
+ $ content .= '{ \\footer ' ;
180
+ if ($ type === Footer::FIRST ) {
181
+ $ content .= 'f ' ;
182
+ } elseif ($ evenOdd ) {
183
+ $ content .= ($ type === FOOTER ::EVEN ) ? 'l ' : 'r ' ;
184
+ }
185
+ foreach ($ footer ->getElements () as $ element ) {
186
+ $ cl = get_class ($ element );
187
+ $ cl2 = str_replace ('Element ' , 'Writer \\RTF \\Element ' , $ cl );
188
+ if (class_exists ($ cl2 )) {
189
+ $ elementWriter = new $ cl2 ($ this ->getParentWriter (), $ element );
190
+ $ content .= $ elementWriter ->write ();
191
+ }
192
+ }
193
+ $ content .= '} ' . PHP_EOL ;
194
+ }
195
+ }
127
196
128
197
$ elementWriter = new Container ($ this ->getParentWriter (), $ section );
129
198
$ content .= $ elementWriter ->write ();
0 commit comments