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: 32: 33: 34: 35:
36: class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
37: {
38: 39: 40: 41: 42: 43: 44:
45: public function writeRelationships(PHPExcel $pPHPExcel = null)
46: {
47:
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:
56: $objWriter->startDocument('1.0','UTF-8','yes');
57:
58:
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:
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:
75: $this->_writeRelationship(
76: $objWriter,
77: 3,
78: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
79: 'docProps/app.xml'
80: );
81:
82:
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:
91: $this->_writeRelationship(
92: $objWriter,
93: 1,
94: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
95: 'xl/workbook.xml'
96: );
97:
98: if($pPHPExcel->hasRibbon()){
99: $this->_writeRelationShip(
100: $objWriter,
101: 5,
102: 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility',
103: $pPHPExcel->getRibbonXMLData('target')
104: );
105: }
106:
107: $objWriter->endElement();
108:
109:
110: return $objWriter->getData();
111: }
112:
113: 114: 115: 116: 117: 118: 119:
120: public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
121: {
122:
123: $objWriter = null;
124: if ($this->getParentWriter()->getUseDiskCaching()) {
125: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
126: } else {
127: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
128: }
129:
130:
131: $objWriter->startDocument('1.0','UTF-8','yes');
132:
133:
134: $objWriter->startElement('Relationships');
135: $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
136:
137:
138: $this->_writeRelationship(
139: $objWriter,
140: 1,
141: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
142: 'styles.xml'
143: );
144:
145:
146: $this->_writeRelationship(
147: $objWriter,
148: 2,
149: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
150: 'theme/theme1.xml'
151: );
152:
153:
154: $this->_writeRelationship(
155: $objWriter,
156: 3,
157: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
158: 'sharedStrings.xml'
159: );
160:
161:
162: $sheetCount = $pPHPExcel->getSheetCount();
163: for ($i = 0; $i < $sheetCount; ++$i) {
164: $this->_writeRelationship(
165: $objWriter,
166: ($i + 1 + 3),
167: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
168: 'worksheets/sheet' . ($i + 1) . '.xml'
169: );
170: }
171:
172:
173: if($pPHPExcel->hasMacros()){
174: $this->_writeRelationShip(
175: $objWriter,
176: ($i + 1 + 3),
177: 'http://schemas.microsoft.com/office/2006/relationships/vbaProject',
178: 'vbaProject.bin'
179: );
180: ++$i;
181: }
182:
183: $objWriter->endElement();
184:
185:
186: return $objWriter->getData();
187: }
188:
189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201:
202: public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1, $includeCharts = FALSE)
203: {
204:
205: $objWriter = null;
206: if ($this->getParentWriter()->getUseDiskCaching()) {
207: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
208: } else {
209: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
210: }
211:
212:
213: $objWriter->startDocument('1.0','UTF-8','yes');
214:
215:
216: $objWriter->startElement('Relationships');
217: $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
218:
219:
220: $d = 0;
221: if ($includeCharts) {
222: $charts = $pWorksheet->getChartCollection();
223: } else {
224: $charts = array();
225: }
226: if (($pWorksheet->getDrawingCollection()->count() > 0) ||
227: (count($charts) > 0)) {
228: $this->_writeRelationship(
229: $objWriter,
230: ++$d,
231: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
232: '../drawings/drawing' . $pWorksheetId . '.xml'
233: );
234: }
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252: $i = 1;
253: foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
254: if (!$hyperlink->isInternal()) {
255: $this->_writeRelationship(
256: $objWriter,
257: '_hyperlink_' . $i,
258: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
259: $hyperlink->getUrl(),
260: 'External'
261: );
262:
263: ++$i;
264: }
265: }
266:
267:
268: $i = 1;
269: if (count($pWorksheet->getComments()) > 0) {
270: $this->_writeRelationship(
271: $objWriter,
272: '_comments_vml' . $i,
273: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
274: '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
275: );
276:
277: $this->_writeRelationship(
278: $objWriter,
279: '_comments' . $i,
280: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
281: '../comments' . $pWorksheetId . '.xml'
282: );
283: }
284:
285:
286: $i = 1;
287: if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
288: $this->_writeRelationship(
289: $objWriter,
290: '_headerfooter_vml' . $i,
291: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
292: '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
293: );
294: }
295:
296: $objWriter->endElement();
297:
298:
299: return $objWriter->getData();
300: }
301:
302: 303: 304: 305: 306: 307: 308: 309: 310:
311: public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $includeCharts = FALSE)
312: {
313:
314: $objWriter = null;
315: if ($this->getParentWriter()->getUseDiskCaching()) {
316: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
317: } else {
318: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
319: }
320:
321:
322: $objWriter->startDocument('1.0','UTF-8','yes');
323:
324:
325: $objWriter->startElement('Relationships');
326: $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
327:
328:
329: $i = 1;
330: $iterator = $pWorksheet->getDrawingCollection()->getIterator();
331: while ($iterator->valid()) {
332: if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
333: || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
334:
335: $this->_writeRelationship(
336: $objWriter,
337: $i,
338: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
339: '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
340: );
341: }
342:
343: $iterator->next();
344: ++$i;
345: }
346:
347: if ($includeCharts) {
348:
349: $chartCount = $pWorksheet->getChartCount();
350: if ($chartCount > 0) {
351: for ($c = 0; $c < $chartCount; ++$c) {
352: $this->_writeRelationship(
353: $objWriter,
354: $i++,
355: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
356: '../charts/chart' . ++$chartRef . '.xml'
357: );
358: }
359: }
360: }
361:
362: $objWriter->endElement();
363:
364:
365: return $objWriter->getData();
366: }
367:
368: 369: 370: 371: 372: 373: 374:
375: public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
376: {
377:
378: $objWriter = null;
379: if ($this->getParentWriter()->getUseDiskCaching()) {
380: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
381: } else {
382: $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
383: }
384:
385:
386: $objWriter->startDocument('1.0','UTF-8','yes');
387:
388:
389: $objWriter->startElement('Relationships');
390: $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
391:
392:
393: foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
394:
395: $this->_writeRelationship(
396: $objWriter,
397: $key,
398: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
399: '../media/' . $value->getIndexedFilename()
400: );
401: }
402:
403: $objWriter->endElement();
404:
405:
406: return $objWriter->getData();
407: }
408:
409: 410: 411: 412: 413: 414: 415: 416: 417: 418:
419: private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
420: {
421: if ($pType != '' && $pTarget != '') {
422:
423: $objWriter->startElement('Relationship');
424: $objWriter->writeAttribute('Id', 'rId' . $pId);
425: $objWriter->writeAttribute('Type', $pType);
426: $objWriter->writeAttribute('Target', $pTarget);
427:
428: if ($pTargetMode != '') {
429: $objWriter->writeAttribute('TargetMode', $pTargetMode);
430: }
431:
432: $objWriter->endElement();
433: } else {
434: throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
435: }
436: }
437: }
438: