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