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 implements PHPWord_Writer_IWriter
32: {
33: 34: 35: 36: 37:
38: private $_document;
39:
40: 41: 42: 43: 44:
45: private $_writerParts;
46:
47: 48: 49: 50: 51:
52: private $_drawingHashTable;
53:
54: 55: 56: 57: 58:
59: private $_useDiskCaching = false;
60:
61: 62: 63: 64: 65:
66: private $_diskCachingDirectory;
67:
68: 69: 70: 71: 72:
73: public function __construct(PHPWord $pPHPWord = null)
74: {
75:
76: $this->setPHPWord($pPHPWord);
77:
78:
79: $this->_diskCachingDirectory = './';
80:
81:
82: $this->_writerParts['content'] = new PHPWord_Writer_ODText_Content();
83: $this->_writerParts['manifest'] = new PHPWord_Writer_ODText_Manifest();
84: $this->_writerParts['meta'] = new PHPWord_Writer_ODText_Meta();
85: $this->_writerParts['mimetype'] = new PHPWord_Writer_ODText_Mimetype();
86: $this->_writerParts['styles'] = new PHPWord_Writer_ODText_Styles();
87:
88:
89:
90: foreach ($this->_writerParts as $writer) {
91: $writer->setParentWriter($this);
92: }
93:
94:
95: $this->_drawingHashTable = new PHPWord_HashTable();
96: }
97:
98: 99: 100: 101: 102: 103:
104: public function save($pFilename = null)
105: {
106: if (!is_null($this->_document)) {
107:
108: $originalFilename = $pFilename;
109: if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
110: $pFilename = @tempnam('./', 'phppttmp');
111: if ($pFilename == '') {
112: $pFilename = $originalFilename;
113: }
114: }
115:
116:
117:
118:
119: $objZip = new ZipArchive();
120:
121:
122: if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
123: if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
124: throw new Exception("Could not open " . $pFilename . " for writing.");
125: }
126: }
127:
128:
129:
130: $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->_document));
131:
132:
133: $objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->_document));
134:
135:
136: $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->_document));
137:
138:
139: $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document));
140:
141:
142: $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->_document));
143:
144:
145: for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
146: if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) {
147: $imageContents = null;
148: $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
149:
150: if (strpos($imagePath, 'zip://') !== false) {
151: $imagePath = substr($imagePath, 6);
152: $imagePathSplitted = explode('#', $imagePath);
153:
154: $imageZip = new ZipArchive();
155: $imageZip->open($imagePathSplitted[0]);
156: $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
157: $imageZip->close();
158: unset($imageZip);
159: } else {
160: $imageContents = file_get_contents($imagePath);
161: }
162:
163: $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
164: } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) {
165: ob_start();
166: call_user_func(
167: $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
168: $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
169: );
170: $imageContents = ob_get_contents();
171: ob_end_clean();
172:
173: $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
174: }
175: }
176:
177:
178: if ($objZip->close() === false) {
179: throw new Exception("Could not close zip file $pFilename.");
180: }
181:
182:
183: if ($originalFilename != $pFilename) {
184: if (copy($pFilename, $originalFilename) === false) {
185: throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
186: }
187: @unlink($pFilename);
188: }
189:
190: } else {
191: throw new Exception("PHPWord object unassigned.");
192: }
193: }
194:
195: 196: 197: 198: 199: 200:
201: public function getPHPWord()
202: {
203: if (!is_null($this->_document)) {
204: return $this->_document;
205: } else {
206: throw new Exception("No PHPWord assigned.");
207: }
208: }
209:
210: 211: 212: 213: 214: 215: 216:
217: public function setPHPWord(PHPWord $pPHPWord = null)
218: {
219: $this->_document = $pPHPWord;
220: return $this;
221: }
222:
223: 224: 225: 226: 227:
228: public function getDrawingHashTable()
229: {
230: return $this->_drawingHashTable;
231: }
232:
233: 234: 235: 236: 237: 238:
239: function getWriterPart($pPartName = '')
240: {
241: if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
242: return $this->_writerParts[strtolower($pPartName)];
243: } else {
244: return null;
245: }
246: }
247:
248: 249: 250: 251: 252:
253: public function getUseDiskCaching()
254: {
255: return $this->_useDiskCaching;
256: }
257:
258: 259: 260: 261: 262: 263: 264: 265:
266: public function setUseDiskCaching($pValue = false, $pDirectory = null)
267: {
268: $this->_useDiskCaching = $pValue;
269:
270: if (!is_null($pDirectory)) {
271: if (is_dir($pDirectory)) {
272: $this->_diskCachingDirectory = $pDirectory;
273: } else {
274: throw new Exception("Directory does not exist: $pDirectory");
275: }
276: }
277:
278: return $this;
279: }
280:
281: 282: 283: 284: 285:
286: public function getDiskCachingDirectory()
287: {
288: return $this->_diskCachingDirectory;
289: }
290: }