/branches/v1.7.3/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php

# · PHP · 238 lines · 130 code · 28 blank · 80 comment · 27 complexity · 7201b80ef51370965fa93d60712a758f MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_ContentTypes
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel2007_ContentTypes extends PHPExcel_Writer_Excel2007_WriterPart
  35. {
  36. /**
  37. * Write content types to XML format
  38. *
  39. * @param PHPExcel $pPHPExcel
  40. * @return string XML Output
  41. * @throws Exception
  42. */
  43. public function writeContentTypes(PHPExcel $pPHPExcel = null)
  44. {
  45. // Create XML writer
  46. $objWriter = null;
  47. if ($this->getParentWriter()->getUseDiskCaching()) {
  48. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  49. } else {
  50. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  51. }
  52. // XML header
  53. $objWriter->startDocument('1.0','UTF-8','yes');
  54. // Types
  55. $objWriter->startElement('Types');
  56. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
  57. // Theme
  58. $this->_writeOverrideContentType(
  59. $objWriter, '/xl/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml'
  60. );
  61. // Styles
  62. $this->_writeOverrideContentType(
  63. $objWriter, '/xl/styles.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml'
  64. );
  65. // Rels
  66. $this->_writeDefaultContentType(
  67. $objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml'
  68. );
  69. // XML
  70. $this->_writeDefaultContentType(
  71. $objWriter, 'xml', 'application/xml'
  72. );
  73. // VML
  74. $this->_writeDefaultContentType(
  75. $objWriter, 'vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing'
  76. );
  77. // Workbook
  78. $this->_writeOverrideContentType(
  79. $objWriter, '/xl/workbook.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml'
  80. );
  81. // DocProps
  82. $this->_writeOverrideContentType(
  83. $objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml'
  84. );
  85. $this->_writeOverrideContentType(
  86. $objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml'
  87. );
  88. // Worksheets
  89. $sheetCount = $pPHPExcel->getSheetCount();
  90. for ($i = 0; $i < $sheetCount; ++$i) {
  91. $this->_writeOverrideContentType(
  92. $objWriter, '/xl/worksheets/sheet' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'
  93. );
  94. }
  95. // Shared strings
  96. $this->_writeOverrideContentType(
  97. $objWriter, '/xl/sharedStrings.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'
  98. );
  99. // Add worksheet relationship content types
  100. for ($i = 0; $i < $sheetCount; ++$i) {
  101. if ($pPHPExcel->getSheet($i)->getDrawingCollection()->count() > 0) {
  102. $this->_writeOverrideContentType(
  103. $objWriter, '/xl/drawings/drawing' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.drawing+xml'
  104. );
  105. }
  106. }
  107. // Comments
  108. for ($i = 0; $i < $sheetCount; ++$i) {
  109. if (count($pPHPExcel->getSheet($i)->getComments()) > 0) {
  110. $this->_writeOverrideContentType(
  111. $objWriter, '/xl/comments' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml'
  112. );
  113. }
  114. }
  115. // Add media content-types
  116. $aMediaContentTypes = array();
  117. $mediaCount = $this->getParentWriter()->getDrawingHashTable()->count();
  118. for ($i = 0; $i < $mediaCount; ++$i) {
  119. $extension = '';
  120. $mimeType = '';
  121. if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
  122. $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension());
  123. $mimeType = $this->_getImageMimeType( $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath() );
  124. } else if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
  125. $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType());
  126. $extension = explode('/', $extension);
  127. $extension = $extension[1];
  128. $mimeType = $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType();
  129. }
  130. if (!isset( $aMediaContentTypes[$extension]) ) {
  131. $aMediaContentTypes[$extension] = $mimeType;
  132. $this->_writeDefaultContentType(
  133. $objWriter, $extension, $mimeType
  134. );
  135. }
  136. }
  137. $sheetCount = $pPHPExcel->getSheetCount();
  138. for ($i = 0; $i < $sheetCount; ++$i) {
  139. if (count($pPHPExcel->getSheet()->getHeaderFooter()->getImages()) > 0) {
  140. foreach ($pPHPExcel->getSheet()->getHeaderFooter()->getImages() as $image) {
  141. if (!isset( $aMediaContentTypes[strtolower($image->getExtension())]) ) {
  142. $aMediaContentTypes[strtolower($image->getExtension())] = $this->_getImageMimeType( $image->getPath() );
  143. $this->_writeDefaultContentType(
  144. $objWriter, strtolower($image->getExtension()), $aMediaContentTypes[strtolower($image->getExtension())]
  145. );
  146. }
  147. }
  148. }
  149. }
  150. $objWriter->endElement();
  151. // Return
  152. return $objWriter->getData();
  153. }
  154. /**
  155. * Get image mime type
  156. *
  157. * @param string $pFile Filename
  158. * @return string Mime Type
  159. * @throws Exception
  160. */
  161. private function _getImageMimeType($pFile = '')
  162. {
  163. if (PHPExcel_Shared_File::file_exists($pFile)) {
  164. $image = getimagesize($pFile);
  165. return image_type_to_mime_type($image[2]);
  166. } else {
  167. throw new Exception("File $pFile does not exist");
  168. }
  169. }
  170. /**
  171. * Write Default content type
  172. *
  173. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  174. * @param string $pPartname Part name
  175. * @param string $pContentType Content type
  176. * @throws Exception
  177. */
  178. private function _writeDefaultContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
  179. {
  180. if ($pPartname != '' && $pContentType != '') {
  181. // Write content type
  182. $objWriter->startElement('Default');
  183. $objWriter->writeAttribute('Extension', $pPartname);
  184. $objWriter->writeAttribute('ContentType', $pContentType);
  185. $objWriter->endElement();
  186. } else {
  187. throw new Exception("Invalid parameters passed.");
  188. }
  189. }
  190. /**
  191. * Write Override content type
  192. *
  193. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  194. * @param string $pPartname Part name
  195. * @param string $pContentType Content type
  196. * @throws Exception
  197. */
  198. private function _writeOverrideContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
  199. {
  200. if ($pPartname != '' && $pContentType != '') {
  201. // Write content type
  202. $objWriter->startElement('Override');
  203. $objWriter->writeAttribute('PartName', $pPartname);
  204. $objWriter->writeAttribute('ContentType', $pContentType);
  205. $objWriter->endElement();
  206. } else {
  207. throw new Exception("Invalid parameters passed.");
  208. }
  209. }
  210. }