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_Rels
|
|
|
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_Rels extends PHPExcel_Writer_Excel2007_WriterPart
|
|
|
37 |
{
|
|
|
38 |
/**
|
|
|
39 |
* Write relationships to XML format
|
|
|
40 |
*
|
|
|
41 |
* @param PHPExcel $pPHPExcel
|
|
|
42 |
* @return string XML Output
|
|
|
43 |
* @throws PHPExcel_Writer_Exception
|
|
|
44 |
*/
|
|
|
45 |
public function writeRelationships(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 |
}
|
|
|
54 |
|
|
|
55 |
// XML header
|
|
|
56 |
$objWriter->startDocument('1.0','UTF-8','yes');
|
|
|
57 |
|
|
|
58 |
// Relationships
|
|
|
59 |
$objWriter->startElement('Relationships');
|
|
|
60 |
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
|
|
|
61 |
|
|
|
62 |
$customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
|
|
|
63 |
if (!empty($customPropertyList)) {
|
|
|
64 |
// Relationship docProps/app.xml
|
|
|
65 |
$this->_writeRelationship(
|
|
|
66 |
$objWriter,
|
|
|
67 |
4,
|
|
|
68 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
|
|
|
69 |
'docProps/custom.xml'
|
|
|
70 |
);
|
|
|
71 |
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// Relationship docProps/app.xml
|
|
|
75 |
$this->_writeRelationship(
|
|
|
76 |
$objWriter,
|
|
|
77 |
3,
|
|
|
78 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
|
|
|
79 |
'docProps/app.xml'
|
|
|
80 |
);
|
|
|
81 |
|
|
|
82 |
// Relationship docProps/core.xml
|
|
|
83 |
$this->_writeRelationship(
|
|
|
84 |
$objWriter,
|
|
|
85 |
2,
|
|
|
86 |
'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
|
|
|
87 |
'docProps/core.xml'
|
|
|
88 |
);
|
|
|
89 |
|
|
|
90 |
// Relationship xl/workbook.xml
|
|
|
91 |
$this->_writeRelationship(
|
|
|
92 |
$objWriter,
|
|
|
93 |
1,
|
|
|
94 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
|
|
|
95 |
'xl/workbook.xml'
|
|
|
96 |
);
|
|
|
97 |
|
|
|
98 |
$objWriter->endElement();
|
|
|
99 |
|
|
|
100 |
// Return
|
|
|
101 |
return $objWriter->getData();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Write workbook relationships to XML format
|
|
|
106 |
*
|
|
|
107 |
* @param PHPExcel $pPHPExcel
|
|
|
108 |
* @return string XML Output
|
|
|
109 |
* @throws PHPExcel_Writer_Exception
|
|
|
110 |
*/
|
|
|
111 |
public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
|
|
|
112 |
{
|
|
|
113 |
// Create XML writer
|
|
|
114 |
$objWriter = null;
|
|
|
115 |
if ($this->getParentWriter()->getUseDiskCaching()) {
|
|
|
116 |
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
|
|
|
117 |
} else {
|
|
|
118 |
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// XML header
|
|
|
122 |
$objWriter->startDocument('1.0','UTF-8','yes');
|
|
|
123 |
|
|
|
124 |
// Relationships
|
|
|
125 |
$objWriter->startElement('Relationships');
|
|
|
126 |
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
|
|
|
127 |
|
|
|
128 |
// Relationship styles.xml
|
|
|
129 |
$this->_writeRelationship(
|
|
|
130 |
$objWriter,
|
|
|
131 |
1,
|
|
|
132 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
|
|
|
133 |
'styles.xml'
|
|
|
134 |
);
|
|
|
135 |
|
|
|
136 |
// Relationship theme/theme1.xml
|
|
|
137 |
$this->_writeRelationship(
|
|
|
138 |
$objWriter,
|
|
|
139 |
2,
|
|
|
140 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
|
|
|
141 |
'theme/theme1.xml'
|
|
|
142 |
);
|
|
|
143 |
|
|
|
144 |
// Relationship sharedStrings.xml
|
|
|
145 |
$this->_writeRelationship(
|
|
|
146 |
$objWriter,
|
|
|
147 |
3,
|
|
|
148 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
|
|
|
149 |
'sharedStrings.xml'
|
|
|
150 |
);
|
|
|
151 |
|
|
|
152 |
// Relationships with sheets
|
|
|
153 |
$sheetCount = $pPHPExcel->getSheetCount();
|
|
|
154 |
for ($i = 0; $i < $sheetCount; ++$i) {
|
|
|
155 |
$this->_writeRelationship(
|
|
|
156 |
$objWriter,
|
|
|
157 |
($i + 1 + 3),
|
|
|
158 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
|
|
|
159 |
'worksheets/sheet' . ($i + 1) . '.xml'
|
|
|
160 |
);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
$objWriter->endElement();
|
|
|
164 |
|
|
|
165 |
// Return
|
|
|
166 |
return $objWriter->getData();
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Write worksheet relationships to XML format
|
|
|
171 |
*
|
|
|
172 |
* Numbering is as follows:
|
|
|
173 |
* rId1 - Drawings
|
|
|
174 |
* rId_hyperlink_x - Hyperlinks
|
|
|
175 |
*
|
|
|
176 |
* @param PHPExcel_Worksheet $pWorksheet
|
|
|
177 |
* @param int $pWorksheetId
|
|
|
178 |
* @param boolean $includeCharts Flag indicating if we should write charts
|
|
|
179 |
* @return string XML Output
|
|
|
180 |
* @throws PHPExcel_Writer_Exception
|
|
|
181 |
*/
|
|
|
182 |
public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1, $includeCharts = FALSE)
|
|
|
183 |
{
|
|
|
184 |
// Create XML writer
|
|
|
185 |
$objWriter = null;
|
|
|
186 |
if ($this->getParentWriter()->getUseDiskCaching()) {
|
|
|
187 |
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
|
|
|
188 |
} else {
|
|
|
189 |
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
// XML header
|
|
|
193 |
$objWriter->startDocument('1.0','UTF-8','yes');
|
|
|
194 |
|
|
|
195 |
// Relationships
|
|
|
196 |
$objWriter->startElement('Relationships');
|
|
|
197 |
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
|
|
|
198 |
|
|
|
199 |
// Write drawing relationships?
|
|
|
200 |
$d = 0;
|
|
|
201 |
if ($includeCharts) {
|
|
|
202 |
$charts = $pWorksheet->getChartCollection();
|
|
|
203 |
} else {
|
|
|
204 |
$charts = array();
|
|
|
205 |
}
|
|
|
206 |
if (($pWorksheet->getDrawingCollection()->count() > 0) ||
|
|
|
207 |
(count($charts) > 0)) {
|
|
|
208 |
$this->_writeRelationship(
|
|
|
209 |
$objWriter,
|
|
|
210 |
++$d,
|
|
|
211 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
|
|
|
212 |
'../drawings/drawing' . $pWorksheetId . '.xml'
|
|
|
213 |
);
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
// Write chart relationships?
|
|
|
217 |
// $chartCount = 0;
|
|
|
218 |
// $charts = $pWorksheet->getChartCollection();
|
|
|
219 |
// echo 'Chart Rels: ' , count($charts) , '<br />';
|
|
|
220 |
// if (count($charts) > 0) {
|
|
|
221 |
// foreach($charts as $chart) {
|
|
|
222 |
// $this->_writeRelationship(
|
|
|
223 |
// $objWriter,
|
|
|
224 |
// ++$d,
|
|
|
225 |
// 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
|
|
|
226 |
// '../charts/chart' . ++$chartCount . '.xml'
|
|
|
227 |
// );
|
|
|
228 |
// }
|
|
|
229 |
// }
|
|
|
230 |
//
|
|
|
231 |
// Write hyperlink relationships?
|
|
|
232 |
$i = 1;
|
|
|
233 |
foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
|
|
|
234 |
if (!$hyperlink->isInternal()) {
|
|
|
235 |
$this->_writeRelationship(
|
|
|
236 |
$objWriter,
|
|
|
237 |
'_hyperlink_' . $i,
|
|
|
238 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
|
|
|
239 |
$hyperlink->getUrl(),
|
|
|
240 |
'External'
|
|
|
241 |
);
|
|
|
242 |
|
|
|
243 |
++$i;
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
// Write comments relationship?
|
|
|
248 |
$i = 1;
|
|
|
249 |
if (count($pWorksheet->getComments()) > 0) {
|
|
|
250 |
$this->_writeRelationship(
|
|
|
251 |
$objWriter,
|
|
|
252 |
'_comments_vml' . $i,
|
|
|
253 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
|
|
|
254 |
'../drawings/vmlDrawing' . $pWorksheetId . '.vml'
|
|
|
255 |
);
|
|
|
256 |
|
|
|
257 |
$this->_writeRelationship(
|
|
|
258 |
$objWriter,
|
|
|
259 |
'_comments' . $i,
|
|
|
260 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
|
|
|
261 |
'../comments' . $pWorksheetId . '.xml'
|
|
|
262 |
);
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
// Write header/footer relationship?
|
|
|
266 |
$i = 1;
|
|
|
267 |
if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
|
|
|
268 |
$this->_writeRelationship(
|
|
|
269 |
$objWriter,
|
|
|
270 |
'_headerfooter_vml' . $i,
|
|
|
271 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
|
|
|
272 |
'../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
|
|
|
273 |
);
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
$objWriter->endElement();
|
|
|
277 |
|
|
|
278 |
// Return
|
|
|
279 |
return $objWriter->getData();
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
/**
|
|
|
283 |
* Write drawing relationships to XML format
|
|
|
284 |
*
|
|
|
285 |
* @param PHPExcel_Worksheet $pWorksheet
|
|
|
286 |
* @param int &$chartRef Chart ID
|
|
|
287 |
* @param boolean $includeCharts Flag indicating if we should write charts
|
|
|
288 |
* @return string XML Output
|
|
|
289 |
* @throws PHPExcel_Writer_Exception
|
|
|
290 |
*/
|
|
|
291 |
public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $includeCharts = FALSE)
|
|
|
292 |
{
|
|
|
293 |
// Create XML writer
|
|
|
294 |
$objWriter = null;
|
|
|
295 |
if ($this->getParentWriter()->getUseDiskCaching()) {
|
|
|
296 |
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
|
|
|
297 |
} else {
|
|
|
298 |
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
// XML header
|
|
|
302 |
$objWriter->startDocument('1.0','UTF-8','yes');
|
|
|
303 |
|
|
|
304 |
// Relationships
|
|
|
305 |
$objWriter->startElement('Relationships');
|
|
|
306 |
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
|
|
|
307 |
|
|
|
308 |
// Loop through images and write relationships
|
|
|
309 |
$i = 1;
|
|
|
310 |
$iterator = $pWorksheet->getDrawingCollection()->getIterator();
|
|
|
311 |
while ($iterator->valid()) {
|
|
|
312 |
if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
|
|
|
313 |
|| $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
|
|
|
314 |
// Write relationship for image drawing
|
|
|
315 |
$this->_writeRelationship(
|
|
|
316 |
$objWriter,
|
|
|
317 |
$i,
|
|
|
318 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
|
|
|
319 |
'../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
|
|
|
320 |
);
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
$iterator->next();
|
|
|
324 |
++$i;
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
if ($includeCharts) {
|
|
|
328 |
// Loop through charts and write relationships
|
|
|
329 |
$chartCount = $pWorksheet->getChartCount();
|
|
|
330 |
if ($chartCount > 0) {
|
|
|
331 |
for ($c = 0; $c < $chartCount; ++$c) {
|
|
|
332 |
$this->_writeRelationship(
|
|
|
333 |
$objWriter,
|
|
|
334 |
$i++,
|
|
|
335 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
|
|
|
336 |
'../charts/chart' . ++$chartRef . '.xml'
|
|
|
337 |
);
|
|
|
338 |
}
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
$objWriter->endElement();
|
|
|
343 |
|
|
|
344 |
// Return
|
|
|
345 |
return $objWriter->getData();
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* Write header/footer drawing relationships to XML format
|
|
|
350 |
*
|
|
|
351 |
* @param PHPExcel_Worksheet $pWorksheet
|
|
|
352 |
* @return string XML Output
|
|
|
353 |
* @throws PHPExcel_Writer_Exception
|
|
|
354 |
*/
|
|
|
355 |
public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
|
|
|
356 |
{
|
|
|
357 |
// Create XML writer
|
|
|
358 |
$objWriter = null;
|
|
|
359 |
if ($this->getParentWriter()->getUseDiskCaching()) {
|
|
|
360 |
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
|
|
|
361 |
} else {
|
|
|
362 |
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
// XML header
|
|
|
366 |
$objWriter->startDocument('1.0','UTF-8','yes');
|
|
|
367 |
|
|
|
368 |
// Relationships
|
|
|
369 |
$objWriter->startElement('Relationships');
|
|
|
370 |
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
|
|
|
371 |
|
|
|
372 |
// Loop through images and write relationships
|
|
|
373 |
foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
|
|
|
374 |
// Write relationship for image drawing
|
|
|
375 |
$this->_writeRelationship(
|
|
|
376 |
$objWriter,
|
|
|
377 |
$key,
|
|
|
378 |
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
|
|
|
379 |
'../media/' . $value->getIndexedFilename()
|
|
|
380 |
);
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
$objWriter->endElement();
|
|
|
384 |
|
|
|
385 |
// Return
|
|
|
386 |
return $objWriter->getData();
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
/**
|
|
|
390 |
* Write Override content type
|
|
|
391 |
*
|
|
|
392 |
* @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
|
|
|
393 |
* @param int $pId Relationship ID. rId will be prepended!
|
|
|
394 |
* @param string $pType Relationship type
|
|
|
395 |
* @param string $pTarget Relationship target
|
|
|
396 |
* @param string $pTargetMode Relationship target mode
|
|
|
397 |
* @throws PHPExcel_Writer_Exception
|
|
|
398 |
*/
|
|
|
399 |
private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
|
|
|
400 |
{
|
|
|
401 |
if ($pType != '' && $pTarget != '') {
|
|
|
402 |
// Write relationship
|
|
|
403 |
$objWriter->startElement('Relationship');
|
|
|
404 |
$objWriter->writeAttribute('Id', 'rId' . $pId);
|
|
|
405 |
$objWriter->writeAttribute('Type', $pType);
|
|
|
406 |
$objWriter->writeAttribute('Target', $pTarget);
|
|
|
407 |
|
|
|
408 |
if ($pTargetMode != '') {
|
|
|
409 |
$objWriter->writeAttribute('TargetMode', $pTargetMode);
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
$objWriter->endElement();
|
|
|
413 |
} else {
|
|
|
414 |
throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
|
|
|
415 |
}
|
|
|
416 |
}
|
|
|
417 |
}
|