Subversion Repositories eFlore/Applications.cel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2388 jpm 1
<?php
2
/**
3
 * PHPExcel
4
 *
5
 * Copyright (c) 2006 - 2013 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 - 2013 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
 
29
/**
30
 * PHPExcel_Writer_Excel2007_ContentTypes
31
 *
32
 * @category   PHPExcel
33
 * @package    PHPExcel_Writer_Excel2007
34
 * @copyright  Copyright (c) 2006 - 2013 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
	 * @param	boolean		$includeCharts	Flag indicating if we should include drawing details for charts
43
	 * @return 	string 						XML Output
44
	 * @throws 	PHPExcel_Writer_Exception
45
	 */
46
	public function writeContentTypes(PHPExcel $pPHPExcel = null, $includeCharts = FALSE)
47
	{
48
		// Create XML writer
49
		$objWriter = null;
50
		if ($this->getParentWriter()->getUseDiskCaching()) {
51
			$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
52
		} else {
53
			$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
54
		}
55
 
56
		// XML header
57
		$objWriter->startDocument('1.0','UTF-8','yes');
58
 
59
		// Types
60
		$objWriter->startElement('Types');
61
		$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
62
 
63
			// Theme
64
			$this->_writeOverrideContentType(
65
				$objWriter, '/xl/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml'
66
			);
67
 
68
			// Styles
69
			$this->_writeOverrideContentType(
70
				$objWriter, '/xl/styles.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml'
71
			);
72
 
73
			// Rels
74
			$this->_writeDefaultContentType(
75
				$objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml'
76
			);
77
 
78
			// XML
79
			$this->_writeDefaultContentType(
80
				$objWriter, 'xml', 'application/xml'
81
			);
82
 
83
			// VML
84
			$this->_writeDefaultContentType(
85
				$objWriter, 'vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing'
86
			);
87
 
88
			// Workbook
89
			$this->_writeOverrideContentType(
90
				$objWriter, '/xl/workbook.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml'
91
			);
92
 
93
			// DocProps
94
			$this->_writeOverrideContentType(
95
				$objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml'
96
			);
97
 
98
			$this->_writeOverrideContentType(
99
				$objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml'
100
			);
101
 
102
			$customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
103
			if (!empty($customPropertyList)) {
104
				$this->_writeOverrideContentType(
105
					$objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml'
106
				);
107
			}
108
 
109
			// Worksheets
110
			$sheetCount = $pPHPExcel->getSheetCount();
111
			for ($i = 0; $i < $sheetCount; ++$i) {
112
				$this->_writeOverrideContentType(
113
					$objWriter, '/xl/worksheets/sheet' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'
114
				);
115
			}
116
 
117
			// Shared strings
118
			$this->_writeOverrideContentType(
119
				$objWriter, '/xl/sharedStrings.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'
120
			);
121
 
122
			// Add worksheet relationship content types
123
			$chart = 1;
124
			for ($i = 0; $i < $sheetCount; ++$i) {
125
				$drawings = $pPHPExcel->getSheet($i)->getDrawingCollection();
126
				$drawingCount = count($drawings);
127
				$chartCount = ($includeCharts) ? $pPHPExcel->getSheet($i)->getChartCount() : 0;
128
 
129
				//	We need a drawing relationship for the worksheet if we have either drawings or charts
130
				if (($drawingCount > 0) || ($chartCount > 0)) {
131
					$this->_writeOverrideContentType(
132
						$objWriter, '/xl/drawings/drawing' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.drawing+xml'
133
					);
134
				}
135
 
136
				//	If we have charts, then we need a chart relationship for every individual chart
137
				if ($chartCount > 0) {
138
					for ($c = 0; $c < $chartCount; ++$c) {
139
						$this->_writeOverrideContentType(
140
							$objWriter, '/xl/charts/chart' . $chart++ . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml'
141
						);
142
					}
143
				}
144
			}
145
 
146
			// Comments
147
			for ($i = 0; $i < $sheetCount; ++$i) {
148
				if (count($pPHPExcel->getSheet($i)->getComments()) > 0) {
149
					$this->_writeOverrideContentType(
150
						$objWriter, '/xl/comments' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml'
151
					);
152
				}
153
			}
154
 
155
			// Add media content-types
156
			$aMediaContentTypes = array();
157
			$mediaCount = $this->getParentWriter()->getDrawingHashTable()->count();
158
			for ($i = 0; $i < $mediaCount; ++$i) {
159
				$extension 	= '';
160
				$mimeType 	= '';
161
 
162
				if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
163
					$extension 	= strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension());
164
					$mimeType 	= $this->_getImageMimeType( $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath() );
165
				} else if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
166
					$extension 	= strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType());
167
					$extension 	= explode('/', $extension);
168
					$extension 	= $extension[1];
169
 
170
					$mimeType 	= $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType();
171
				}
172
 
173
				if (!isset( $aMediaContentTypes[$extension]) ) {
174
						$aMediaContentTypes[$extension] = $mimeType;
175
 
176
						$this->_writeDefaultContentType(
177
							$objWriter, $extension, $mimeType
178
						);
179
				}
180
			}
181
 
182
			$sheetCount = $pPHPExcel->getSheetCount();
183
			for ($i = 0; $i < $sheetCount; ++$i) {
184
				if (count($pPHPExcel->getSheet()->getHeaderFooter()->getImages()) > 0) {
185
					foreach ($pPHPExcel->getSheet()->getHeaderFooter()->getImages() as $image) {
186
						if (!isset( $aMediaContentTypes[strtolower($image->getExtension())]) ) {
187
							$aMediaContentTypes[strtolower($image->getExtension())] = $this->_getImageMimeType( $image->getPath() );
188
 
189
							$this->_writeDefaultContentType(
190
								$objWriter, strtolower($image->getExtension()), $aMediaContentTypes[strtolower($image->getExtension())]
191
							);
192
						}
193
					}
194
				}
195
			}
196
 
197
		$objWriter->endElement();
198
 
199
		// Return
200
		return $objWriter->getData();
201
	}
202
 
203
	/**
204
	 * Get image mime type
205
	 *
206
	 * @param 	string	$pFile	Filename
207
	 * @return 	string	Mime Type
208
	 * @throws 	PHPExcel_Writer_Exception
209
	 */
210
	private function _getImageMimeType($pFile = '')
211
	{
212
		if (PHPExcel_Shared_File::file_exists($pFile)) {
213
			$image = getimagesize($pFile);
214
			return image_type_to_mime_type($image[2]);
215
		} else {
216
			throw new PHPExcel_Writer_Exception("File $pFile does not exist");
217
		}
218
	}
219
 
220
	/**
221
	 * Write Default content type
222
	 *
223
	 * @param 	PHPExcel_Shared_XMLWriter 	$objWriter 		XML Writer
224
	 * @param 	string 						$pPartname 		Part name
225
	 * @param 	string 						$pContentType 	Content type
226
	 * @throws 	PHPExcel_Writer_Exception
227
	 */
228
	private function _writeDefaultContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
229
	{
230
		if ($pPartname != '' && $pContentType != '') {
231
			// Write content type
232
			$objWriter->startElement('Default');
233
			$objWriter->writeAttribute('Extension', 	$pPartname);
234
			$objWriter->writeAttribute('ContentType', 	$pContentType);
235
			$objWriter->endElement();
236
		} else {
237
			throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
238
		}
239
	}
240
 
241
	/**
242
	 * Write Override content type
243
	 *
244
	 * @param 	PHPExcel_Shared_XMLWriter 	$objWriter 		XML Writer
245
	 * @param 	string 						$pPartname 		Part name
246
	 * @param 	string 						$pContentType 	Content type
247
	 * @throws 	PHPExcel_Writer_Exception
248
	 */
249
	private function _writeOverrideContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
250
	{
251
		if ($pPartname != '' && $pContentType != '') {
252
			// Write content type
253
			$objWriter->startElement('Override');
254
			$objWriter->writeAttribute('PartName', 		$pPartname);
255
			$objWriter->writeAttribute('ContentType', 	$pContentType);
256
			$objWriter->endElement();
257
		} else {
258
			throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
259
		}
260
	}
261
}