1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27:
28: 29: 30:
31: class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
32: {
33: 34: 35: 36: 37: 38: 39:
40: public function writeContent(PHPWord $pPHPWord = null)
41: {
42:
43: $objWriter = null;
44: if ($this->getParentWriter()->getUseDiskCaching()) {
45: $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
46: } else {
47: $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY);
48: }
49:
50:
51: $objWriter->startDocument('1.0', 'UTF-8');
52:
53:
54: $objWriter->startElement('office:document-content');
55: $objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
56: $objWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0');
57: $objWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0');
58: $objWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0');
59: $objWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0');
60: $objWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
61: $objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
62: $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
63: $objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
64: $objWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0');
65: $objWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0');
66: $objWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0');
67: $objWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0');
68: $objWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML');
69: $objWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0');
70: $objWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0');
71: $objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
72: $objWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer');
73: $objWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc');
74: $objWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events');
75: $objWriter->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms');
76: $objWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
77: $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
78: $objWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report');
79: $objWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2');
80: $objWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
81: $objWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
82: $objWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table');
83: $objWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0');
84: $objWriter->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0');
85: $objWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/');
86: $objWriter->writeAttribute('office:version', '1.2');
87:
88:
89: $_sections = $pPHPWord->getSections();
90: $countSections = count($_sections);
91: if ($countSections > 0) {
92: $pSection = 0;
93: $numPStyles = 0;
94: $numFStyles = 0;
95:
96: foreach ($_sections as $section) {
97: $pSection++;
98: $_elements = $section->getElements();
99:
100: foreach ($_elements as $element) {
101: if ($element instanceof PHPWord_Section_Text) {
102: $fStyle = $element->getFontStyle();
103: $pStyle = $element->getParagraphStyle();
104:
105: if ($fStyle instanceof PHPWord_Style_Font) {
106: $numFStyles++;
107:
108: $arrStyle = array(
109: 'color' => $fStyle->getColor(),
110: 'name' => $fStyle->getName()
111: );
112: $pPHPWord->addFontStyle('T' . $numFStyles, $arrStyle);
113: $element->setFontStyle('T' . $numFStyles);
114: } elseif ($pStyle instanceof PHPWord_Style_Paragraph) {
115: $numPStyles++;
116:
117: $pPHPWord->addParagraphStyle('P' . $numPStyles, array());
118: $element->setParagraph('P' . $numPStyles);
119: }
120: }
121: }
122: }
123: }
124:
125:
126: $objWriter->startElement('office:font-face-decls');
127: $arrFonts = array();
128:
129: $styles = PHPWord_Style::getStyles();
130: $numFonts = 0;
131: if (count($styles) > 0) {
132: foreach ($styles as $styleName => $style) {
133:
134: if ($style instanceof PHPWord_Style_Font) {
135: $numFonts++;
136: $name = $style->getName();
137: if (!in_array($name, $arrFonts)) {
138: $arrFonts[] = $name;
139:
140:
141: $objWriter->startElement('style:font-face');
142: $objWriter->writeAttribute('style:name', $name);
143: $objWriter->writeAttribute('svg:font-family', $name);
144: $objWriter->endElement();
145: }
146: }
147: }
148: if (!in_array(PHPWord::DEFAULT_FONT_NAME, $arrFonts)) {
149: $objWriter->startElement('style:font-face');
150: $objWriter->writeAttribute('style:name', PHPWord::DEFAULT_FONT_NAME);
151: $objWriter->writeAttribute('svg:font-family', PHPWord::DEFAULT_FONT_NAME);
152: $objWriter->endElement();
153: }
154: }
155: $objWriter->endElement();
156:
157: $objWriter->startElement('office:automatic-styles');
158: $styles = PHPWord_Style::getStyles();
159: $numPStyles = 0;
160: if (count($styles) > 0) {
161: foreach ($styles as $styleName => $style) {
162: if (preg_match('#^T[0-9]+$#', $styleName) != 0
163: || preg_match('#^P[0-9]+$#', $styleName) != 0
164: ) {
165:
166: if ($style instanceof PHPWord_Style_Font) {
167: $objWriter->startElement('style:style');
168: $objWriter->writeAttribute('style:name', $styleName);
169: $objWriter->writeAttribute('style:family', 'text');
170:
171: $objWriter->startElement('style:text-properties');
172: $objWriter->writeAttribute('fo:color', '#' . $style->getColor());
173: $objWriter->writeAttribute('style:font-name', $style->getName());
174: $objWriter->writeAttribute('style:font-name-complex', $style->getName());
175: $objWriter->endElement();
176: $objWriter->endElement();
177: }
178: if ($style instanceof PHPWord_Style_Paragraph) {
179: $numPStyles++;
180:
181: $objWriter->startElement('style:style');
182: $objWriter->writeAttribute('style:name', $styleName);
183: $objWriter->writeAttribute('style:family', 'paragraph');
184: $objWriter->writeAttribute('style:parent-style-name', 'Standard');
185: $objWriter->writeAttribute('style:master-page-name', 'Standard');
186:
187: $objWriter->startElement('style:paragraph-properties');
188: $objWriter->writeAttribute('style:page-number', 'auto');
189: $objWriter->endElement();
190: $objWriter->endElement();
191: }
192: }
193: }
194:
195: if ($numPStyles == 0) {
196:
197: $objWriter->startElement('style:style');
198: $objWriter->writeAttribute('style:name', 'P1');
199: $objWriter->writeAttribute('style:family', 'paragraph');
200: $objWriter->writeAttribute('style:parent-style-name', 'Standard');
201: $objWriter->writeAttribute('style:master-page-name', 'Standard');
202:
203: $objWriter->startElement('style:paragraph-properties');
204: $objWriter->writeAttribute('style:page-number', 'auto');
205: $objWriter->endElement();
206: $objWriter->endElement();
207: }
208: }
209: $objWriter->endElement();
210:
211:
212: $objWriter->startElement('office:body');
213:
214: $objWriter->startElement('office:text');
215:
216: $objWriter->startElement('text:sequence-decls');
217:
218: $objWriter->startElement('text:sequence-decl');
219: $objWriter->writeAttribute('text:display-outline-level', 0);
220: $objWriter->writeAttribute('text:name', 'Illustration');
221: $objWriter->endElement();
222:
223: $objWriter->startElement('text:sequence-decl');
224: $objWriter->writeAttribute('text:display-outline-level', 0);
225: $objWriter->writeAttribute('text:name', 'Table');
226: $objWriter->endElement();
227:
228: $objWriter->startElement('text:sequence-decl');
229: $objWriter->writeAttribute('text:display-outline-level', 0);
230: $objWriter->writeAttribute('text:name', 'Text');
231: $objWriter->endElement();
232:
233: $objWriter->startElement('text:sequence-decl');
234: $objWriter->writeAttribute('text:display-outline-level', 0);
235: $objWriter->writeAttribute('text:name', 'Drawing');
236: $objWriter->endElement();
237: $objWriter->endElement();
238:
239: $_sections = $pPHPWord->getSections();
240: $countSections = count($_sections);
241: $pSection = 0;
242:
243: if ($countSections > 0) {
244: foreach ($_sections as $section) {
245: $pSection++;
246:
247: $_elements = $section->getElements();
248:
249: foreach ($_elements as $element) {
250: if ($element instanceof PHPWord_Section_Text) {
251: $this->_writeText($objWriter, $element);
252: } 253: 254: 255: 256: 257: 258: elseif ($element instanceof PHPWord_Section_TextBreak) {
259: $this->_writeTextBreak($objWriter);
260: } 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273:
274: else {
275: print_r($element);
276: echo '<br />';
277: }
278: }
279:
280: if ($pSection == $countSections) {
281: $this->_writeEndSection($objWriter, $section);
282: } else {
283: $this->_writeSection($objWriter, $section);
284: }
285: }
286: }
287: $objWriter->endElement();
288: $objWriter->endElement();
289: $objWriter->endElement();
290:
291:
292: return $objWriter->getData();
293: }
294:
295: protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false)
296: {
297: $styleFont = $text->getFontStyle();
298: $styleParagraph = $text->getParagraphStyle();
299:
300: $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
301:
302: if ($SfIsObject) {
303:
304: die('PHPWord : $SfIsObject wouldn\'t be an object');
305: } else {
306:
307: $objWriter->startElement('text:p');
308: if (empty($styleFont)) {
309: if (empty($styleParagraph)) {
310: $objWriter->writeAttribute('text:style-name', 'P1');
311: } else {
312: $objWriter->writeAttribute('text:style-name', $text->getParagraphStyle());
313: }
314: $objWriter->writeRaw($text->getText());
315: } else {
316: if (empty($styleParagraph)) {
317: $objWriter->writeAttribute('text:style-name', 'Standard');
318: } else {
319: $objWriter->writeAttribute('text:style-name', $text->getParagraphStyle());
320: }
321:
322: $objWriter->startElement('text:span');
323: $objWriter->writeAttribute('text:style-name', $styleFont);
324: $objWriter->writeRaw($text->getText());
325: $objWriter->endElement();
326: }
327: $objWriter->endElement();
328: }
329: }
330:
331: protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
332: {
333: $objWriter->startElement('text:p');
334: $objWriter->writeAttribute('text:style-name', 'Standard');
335: $objWriter->endElement();
336: }
337:
338: private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section)
339: {
340: }
341: }
342: