@@ -35,125 +35,154 @@ class TOC extends Element
35
35
public function write ()
36
36
{
37
37
$ titles = $ this ->element ->getTitles ();
38
+ $ writeFieldMark = true ;
39
+
40
+ foreach ($ titles as $ title ) {
41
+ $ this ->writeTitle ($ title , $ writeFieldMark );
42
+ if ($ writeFieldMark ) {
43
+ $ writeFieldMark = false ;
44
+ }
45
+ }
46
+
47
+ $ this ->xmlWriter ->startElement ('w:p ' );
48
+ $ this ->xmlWriter ->startElement ('w:r ' );
49
+ $ this ->xmlWriter ->startElement ('w:fldChar ' );
50
+ $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , 'end ' );
51
+ $ this ->xmlWriter ->endElement ();
52
+ $ this ->xmlWriter ->endElement ();
53
+ $ this ->xmlWriter ->endElement ();
54
+ }
55
+
56
+ /**
57
+ * Write title
58
+ */
59
+ private function writeTitle ($ title , $ writeFieldMark )
60
+ {
38
61
$ tocStyle = $ this ->element ->getStyleTOC ();
39
62
$ fontStyle = $ this ->element ->getStyleFont ();
40
63
$ isObject = ($ fontStyle instanceof Font) ? true : false ;
64
+ $ anchor = '_Toc ' . ($ title ->getBookmarkId () + 252634154 );
65
+ $ indent = ($ title ->getDepth () - 1 ) * $ tocStyle ->getIndent ();
41
66
42
- $ tocFieldWritten = false ;
43
- foreach ($ titles as $ title ) {
44
- $ indent = ($ title ->getDepth () - 1 ) * $ tocStyle ->getIndent ();
45
- $ anchor = '_Toc ' . ($ title ->getBookmarkId () + 252634154 );
67
+ $ this ->xmlWriter ->startElement ('w:p ' );
46
68
47
- $ this ->xmlWriter ->startElement ('w:p ' );
69
+ // Write style and field mark
70
+ $ this ->writeStyle ($ indent );
71
+ if ($ writeFieldMark ) {
72
+ $ this ->writeFieldMark ();
73
+ }
48
74
49
- // Style
50
- $ this ->xmlWriter ->startElement ('w:pPr ' );
75
+ // Hyperlink
76
+ $ this ->xmlWriter ->startElement ('w:hyperlink ' );
77
+ $ this ->xmlWriter ->writeAttribute ('w:anchor ' , $ anchor );
78
+ $ this ->xmlWriter ->writeAttribute ('w:history ' , '1 ' );
51
79
52
- // Paragraph
53
- if ($ isObject && !is_null ($ fontStyle ->getParagraphStyle ())) {
54
- $ styleWriter = new ParagraphStyleWriter ($ this ->xmlWriter , $ fontStyle ->getParagraphStyle ());
55
- $ styleWriter ->write ();
56
- }
80
+ // Title text
81
+ $ this ->xmlWriter ->startElement ('w:r ' );
82
+ if ($ isObject ) {
83
+ $ styleWriter = new FontStyleWriter ($ this ->xmlWriter , $ fontStyle );
84
+ $ styleWriter ->write ();
85
+ }
86
+ $ this ->xmlWriter ->startElement ('w:t ' );
87
+ $ this ->xmlWriter ->writeRaw ($ title ->getText ());
88
+ $ this ->xmlWriter ->endElement ();
89
+ $ this ->xmlWriter ->endElement (); // w:r
57
90
58
- // Font
59
- if (!empty ($ fontStyle ) && !$ isObject ) {
60
- $ this ->xmlWriter ->startElement ('w:rPr ' );
61
- $ this ->xmlWriter ->startElement ('w:rStyle ' );
62
- $ this ->xmlWriter ->writeAttribute ('w:val ' , $ fontStyle );
63
- $ this ->xmlWriter ->endElement ();
64
- $ this ->xmlWriter ->endElement (); // w:rPr
65
- }
91
+ $ this ->xmlWriter ->startElement ('w:r ' );
92
+ $ this ->xmlWriter ->writeElement ('w:tab ' , null );
93
+ $ this ->xmlWriter ->endElement ();
66
94
67
- // Tab
68
- $ this ->xmlWriter ->startElement ('w:tabs ' );
69
- $ styleWriter = new TabStyleWriter ( $ this ->xmlWriter , $ tocStyle );
70
- $ styleWriter -> write ();
71
- $ this ->xmlWriter ->endElement ();
95
+ $ this -> xmlWriter -> startElement ( ' w:r ' );
96
+ $ this ->xmlWriter ->startElement ('w:fldChar ' );
97
+ $ this ->xmlWriter -> writeAttribute ( ' w:fldCharType ' , ' begin ' );
98
+ $ this -> xmlWriter -> endElement ();
99
+ $ this ->xmlWriter ->endElement ();
72
100
73
- // Indent
74
- if ( $ indent > 0 ) {
75
- $ this ->xmlWriter ->startElement ( ' w:ind ' );
76
- $ this ->xmlWriter ->writeAttribute ( ' w:left ' , $ indent );
77
- $ this ->xmlWriter ->endElement ();
78
- }
101
+ $ this -> xmlWriter -> startElement ( ' w:r ' );
102
+ $ this -> xmlWriter -> startElement ( ' w:instrText ' );
103
+ $ this ->xmlWriter ->writeAttribute ( ' xml:space ' , ' preserve ' );
104
+ $ this ->xmlWriter ->writeRaw ( ' PAGEREF ' . $ anchor . ' \h ' );
105
+ $ this ->xmlWriter ->endElement ();
106
+ $ this -> xmlWriter -> endElement ();
79
107
80
- $ this ->xmlWriter ->endElement (); // w:pPr
81
-
82
- if ($ tocFieldWritten !== true ) {
83
- $ tocFieldWritten = true ;
84
- $ minDepth = $ this ->element ->getMinDepth ();
85
- $ maxDepth = $ this ->element ->getMaxDepth ();
86
-
87
- $ this ->xmlWriter ->startElement ('w:r ' );
88
- $ this ->xmlWriter ->startElement ('w:fldChar ' );
89
- $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , 'begin ' );
90
- $ this ->xmlWriter ->endElement ();
91
- $ this ->xmlWriter ->endElement ();
92
-
93
- $ this ->xmlWriter ->startElement ('w:r ' );
94
- $ this ->xmlWriter ->startElement ('w:instrText ' );
95
- $ this ->xmlWriter ->writeAttribute ('xml:space ' , 'preserve ' );
96
- $ this ->xmlWriter ->writeRaw ("TOC \o {$ minDepth }- {$ maxDepth } \h \z \u " );
97
- $ this ->xmlWriter ->endElement ();
98
- $ this ->xmlWriter ->endElement ();
99
-
100
- $ this ->xmlWriter ->startElement ('w:r ' );
101
- $ this ->xmlWriter ->startElement ('w:fldChar ' );
102
- $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , 'separate ' );
103
- $ this ->xmlWriter ->endElement ();
104
- $ this ->xmlWriter ->endElement ();
105
- }
108
+ $ this ->xmlWriter ->startElement ('w:r ' );
109
+ $ this ->xmlWriter ->startElement ('w:fldChar ' );
110
+ $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , 'end ' );
111
+ $ this ->xmlWriter ->endElement ();
112
+ $ this ->xmlWriter ->endElement ();
106
113
107
- $ this ->xmlWriter ->startElement ('w:hyperlink ' );
108
- $ this ->xmlWriter ->writeAttribute ('w:anchor ' , $ anchor );
109
- $ this ->xmlWriter ->writeAttribute ('w:history ' , '1 ' );
114
+ $ this ->xmlWriter ->endElement (); // w:hyperlink
110
115
111
- $ this ->xmlWriter ->startElement ('w:r ' );
116
+ $ this ->xmlWriter ->endElement (); // w:p
117
+ }
112
118
113
- if ($ isObject ) {
114
- $ styleWriter = new FontStyleWriter ($ this ->xmlWriter , $ fontStyle );
115
- $ styleWriter ->write ();
116
- }
119
+ /**
120
+ * Write style
121
+ */
122
+ private function writeStyle ($ indent )
123
+ {
124
+ $ tocStyle = $ this ->element ->getStyleTOC ();
125
+ $ fontStyle = $ this ->element ->getStyleFont ();
126
+ $ isObject = ($ fontStyle instanceof Font) ? true : false ;
117
127
118
- $ this ->xmlWriter ->startElement ('w:t ' );
119
- $ this ->xmlWriter ->writeRaw ($ title ->getText ());
120
- $ this ->xmlWriter ->endElement ();
121
- $ this ->xmlWriter ->endElement ();
128
+ $ this ->xmlWriter ->startElement ('w:pPr ' );
122
129
123
- $ this ->xmlWriter ->startElement ('w:r ' );
124
- $ this ->xmlWriter ->writeElement ('w:tab ' , null );
125
- $ this ->xmlWriter ->endElement ();
130
+ // Paragraph
131
+ if ($ isObject && !is_null ($ fontStyle ->getParagraphStyle ())) {
132
+ $ styleWriter = new ParagraphStyleWriter ($ this ->xmlWriter , $ fontStyle ->getParagraphStyle ());
133
+ $ styleWriter ->write ();
134
+ }
126
135
127
- $ this ->xmlWriter ->startElement ('w:r ' );
128
- $ this ->xmlWriter ->startElement ('w:fldChar ' );
129
- $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , 'begin ' );
130
- $ this ->xmlWriter ->endElement ();
136
+ // Font
137
+ if (!empty ($ fontStyle ) && !$ isObject ) {
138
+ $ this ->xmlWriter ->startElement ('w:rPr ' );
139
+ $ this ->xmlWriter ->startElement ('w:rStyle ' );
140
+ $ this ->xmlWriter ->writeAttribute ('w:val ' , $ fontStyle );
131
141
$ this ->xmlWriter ->endElement ();
142
+ $ this ->xmlWriter ->endElement (); // w:rPr
143
+ }
132
144
133
- $ this ->xmlWriter ->startElement ('w:r ' );
134
- $ this ->xmlWriter ->startElement ('w:instrText ' );
135
- $ this ->xmlWriter ->writeAttribute ('xml:space ' , 'preserve ' );
136
- $ this ->xmlWriter ->writeRaw ('PAGEREF ' . $ anchor . ' \h ' );
137
- $ this ->xmlWriter ->endElement ();
138
- $ this ->xmlWriter ->endElement ();
145
+ // Tab
146
+ $ this ->xmlWriter ->startElement ('w:tabs ' );
147
+ $ styleWriter = new TabStyleWriter ($ this ->xmlWriter , $ tocStyle );
148
+ $ styleWriter ->write ();
149
+ $ this ->xmlWriter ->endElement ();
139
150
140
- $ this -> xmlWriter -> startElement ( ' w:r ' );
141
- $ this -> xmlWriter -> startElement ( ' w:fldChar ' );
142
- $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , ' end ' );
143
- $ this ->xmlWriter ->endElement ( );
151
+ // Indent
152
+ if ( $ indent > 0 ) {
153
+ $ this ->xmlWriter ->startElement ('w:ind ' );
154
+ $ this ->xmlWriter ->writeAttribute ( ' w:left ' , $ indent );
144
155
$ this ->xmlWriter ->endElement ();
156
+ }
145
157
146
- $ this ->xmlWriter ->endElement (); // w:hyperlink
158
+ $ this ->xmlWriter ->endElement (); // w:pPr
159
+ }
147
160
148
- $ this ->xmlWriter ->endElement (); // w:p
149
- }
161
+ /**
162
+ * Write TOC Field
163
+ */
164
+ private function writeFieldMark ()
165
+ {
166
+ $ minDepth = $ this ->element ->getMinDepth ();
167
+ $ maxDepth = $ this ->element ->getMaxDepth ();
150
168
151
- $ this ->xmlWriter ->startElement ('w:p ' );
152
169
$ this ->xmlWriter ->startElement ('w:r ' );
153
170
$ this ->xmlWriter ->startElement ('w:fldChar ' );
154
- $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , 'end ' );
171
+ $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , 'begin ' );
172
+ $ this ->xmlWriter ->endElement ();
173
+ $ this ->xmlWriter ->endElement ();
174
+
175
+ $ this ->xmlWriter ->startElement ('w:r ' );
176
+ $ this ->xmlWriter ->startElement ('w:instrText ' );
177
+ $ this ->xmlWriter ->writeAttribute ('xml:space ' , 'preserve ' );
178
+ $ this ->xmlWriter ->writeRaw ("TOC \o {$ minDepth }- {$ maxDepth } \h \z \u " );
155
179
$ this ->xmlWriter ->endElement ();
156
180
$ this ->xmlWriter ->endElement ();
181
+
182
+ $ this ->xmlWriter ->startElement ('w:r ' );
183
+ $ this ->xmlWriter ->startElement ('w:fldChar ' );
184
+ $ this ->xmlWriter ->writeAttribute ('w:fldCharType ' , 'separate ' );
185
+ $ this ->xmlWriter ->endElement ();
157
186
$ this ->xmlWriter ->endElement ();
158
187
}
159
188
}
0 commit comments