File Source for ContentTypes.php

PHPExcel_Writer_Excel2007

# Source for file ContentTypes.php

Documentation is available at ContentTypes.php

1. <?php
2. /**
3. * PHPExcel
4. *
5. * Copyright (c) 2006 - 2011 PHPExcel
6. *
7. * This library is free software; you can redistribute it and/or
8. * modify it under the terms of the GNU Lesser General Public
9. * License as published by the Free Software Foundation; either
10. * version 2.1 of the License, or (at your option) any later version.
11. *
12. * This library is distributed in the hope that it will be useful,
13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15. * Lesser General Public License for more details.
16. *
17. * You should have received a copy of the GNU Lesser General Public
18. * License along with this library; if not, write to the Free Software
19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20. *
21. * @category PHPExcel
22. * @package PHPExcel_Writer_Excel2007
23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25. * @version 1.7.6, 2011-02-27
26. */
29. /**
30. * PHPExcel_Writer_Excel2007_ContentTypes
31. *
32. * @category PHPExcel
33. * @package PHPExcel_Writer_Excel2007
34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
35. */
36. class PHPExcel_Writer_Excel2007_ContentTypes extends PHPExcel_Writer_Excel2007_WriterPart
37. {
38. /**
39. * Write content types to XML format
40. *
41. * @param PHPExcel $pPHPExcel
42. * @return string XML Output
43. * @throws Exception
44. */
45. public function writeContentTypes(PHPExcel $pPHPExcel = null)
46. {
47. // Create XML writer
48. $objWriter = null;
49. if ($this-> getParentWriter()->getUseDiskCaching()) {
50. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_DISK, $this-> getParentWriter()->getDiskCachingDirectory());
51. } else {
52. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_MEMORY);
53. }
55. // XML header
56. $objWriter->startDocument('1.0','UTF-8','yes');
58. // Types
59. $objWriter->startElement('Types');
60. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
62. // Theme
63. $this->_writeOverrideContentType(
64. $objWriter, '/xl/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml'
65. );
67. // Styles
68. $this->_writeOverrideContentType(
69. $objWriter, '/xl/styles.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml'
70. );
72. // Rels
73. $this->_writeDefaultContentType(
74. $objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml'
75. );
77. // XML
78. $this->_writeDefaultContentType(
79. $objWriter, 'xml', 'application/xml'
80. );
82. // VML
83. $this->_writeDefaultContentType(
84. $objWriter, 'vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing'
85. );
87. // Workbook
88. $this->_writeOverrideContentType(
89. $objWriter, '/xl/workbook.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml'
90. );
92. // DocProps
93. $this->_writeOverrideContentType(
94. $objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml'
95. );
97. $this->_writeOverrideContentType(
98. $objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml'
99. );
101. $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
102. if ( count($customPropertyList) > 0) {
103. $this->_writeOverrideContentType(
104. $objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml'
105. );
106. }
108. // Worksheets
109. $sheetCount = $pPHPExcel->getSheetCount();
110. for ($i = 0; $i < $sheetCount; ++$i) {
111. $this->_writeOverrideContentType(
112. $objWriter, '/xl/worksheets/sheet' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'
113. );
114. }
116. // Shared strings
117. $this->_writeOverrideContentType(
118. $objWriter, '/xl/sharedStrings.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'
119. );
121. // Add worksheet relationship content types
122. for ($i = 0; $i < $sheetCount; ++$i) {
123. if ($pPHPExcel->getSheet($i)->getDrawingCollection()->count() > 0) {
124. $this->_writeOverrideContentType(
125. $objWriter, '/xl/drawings/drawing' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.drawing+xml'
126. );
127. }
128. }
130. // Comments
131. for ($i = 0; $i < $sheetCount; ++$i) {
132. if ( count($pPHPExcel->getSheet($i)->getComments()) > 0) {
133. $this->_writeOverrideContentType(
134. $objWriter, '/xl/comments' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml'
135. );
136. }
137. }
139. // Add media content-types
140. $aMediaContentTypes = array();
141. $mediaCount = $this-> getParentWriter()->getDrawingHashTable()->count();
142. for ($i = 0; $i < $mediaCount; ++$i) {
143. $extension = '';
144. $mimeType = '';
146. if ($this-> getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
147. $extension = strtolower($this-> getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension());
148. $mimeType = $this->_getImageMimeType( $this-> getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath() );
149. } else if ($this-> getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
150. $extension = strtolower($this-> getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType());
151. $extension = explode('/', $extension);
152. $extension = $extension[1];
154. $mimeType = $this-> getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType();
155. }
157. if (!isset( $aMediaContentTypes[$extension]) ) {
158. $aMediaContentTypes[$extension] = $mimeType;
160. $this->_writeDefaultContentType(
161. $objWriter, $extension, $mimeType
162. );
163. }
164. }
166. $sheetCount = $pPHPExcel->getSheetCount();
167. for ($i = 0; $i < $sheetCount; ++$i) {
168. if ( count($pPHPExcel->getSheet()->getHeaderFooter()->getImages()) > 0) {
169. foreach ($pPHPExcel->getSheet()->getHeaderFooter()->getImages() as $image) {
170. if (!isset( $aMediaContentTypes[ strtolower($image->getExtension())]) ) {
171. $aMediaContentTypes[ strtolower($image->getExtension())] = $this->_getImageMimeType( $image->getPath() );
173. $this->_writeDefaultContentType(
174. $objWriter, strtolower($image->getExtension()), $aMediaContentTypes[ strtolower($image->getExtension())]
175. );
176. }
177. }
178. }
179. }
181. $objWriter->endElement();
183. // Return
184. return $objWriter->getData();
185. }
187. /**
188. * Get image mime type
189. *
190. * @param string $pFile Filename
191. * @return string Mime Type
192. * @throws Exception
193. */
194. private function _getImageMimeType($pFile = '')
195. {
196. if ( PHPExcel_Shared_File:: file_exists($pFile)) {
197. $image = getimagesize($pFile);
198. return image_type_to_mime_type($image[2]);
199. } else {
200. throw new Exception("File $pFile does not exist");
201. }
202. }
204. /**
205. * Write Default content type
206. *
207. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
208. * @param string $pPartname Part name
209. * @param string $pContentType Content type
210. * @throws Exception
211. */
212. private function _writeDefaultContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
213. {
214. if ($pPartname != '' && $pContentType != '') {
215. // Write content type
216. $objWriter->startElement('Default');
217. $objWriter->writeAttribute('Extension', $pPartname);
218. $objWriter->writeAttribute('ContentType', $pContentType);
219. $objWriter->endElement();
220. } else {
221. throw new Exception("Invalid parameters passed.");
222. }
223. }
225. /**
226. * Write Override content type
227. *
228. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
229. * @param string $pPartname Part name
230. * @param string $pContentType Content type
231. * @throws Exception
232. */
233. private function _writeOverrideContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
234. {
235. if ($pPartname != '' && $pContentType != '') {
236. // Write content type
237. $objWriter->startElement('Override');
238. $objWriter->writeAttribute('PartName', $pPartname);
239. $objWriter->writeAttribute('ContentType', $pContentType);
240. $objWriter->endElement();
241. } else {
242. throw new Exception("Invalid parameters passed.");
243. }
244. }
245. }

Documentation generated on Sun, 27 Feb 2011 16:29:12 -0800 by phpDocumentor 1.4.3