Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
45 / 45 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Relationships | |
100.00% |
45 / 45 |
|
100.00% |
3 / 3 |
11 | |
100.00% |
1 / 1 |
render | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
writeRelationships | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
writePresentationRelationships | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPPresentation - A pure PHP library for reading and writing |
4 | * presentations documents. |
5 | * |
6 | * PHPPresentation is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPPresentation |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | declare(strict_types=1); |
19 | |
20 | namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; |
21 | |
22 | use PhpOffice\Common\Adapter\Zip\ZipInterface; |
23 | use PhpOffice\Common\XMLWriter; |
24 | use PhpOffice\PhpPresentation\Shape\Comment; |
25 | use PhpOffice\PhpPresentation\Shape\Comment\Author; |
26 | |
27 | class Relationships extends AbstractDecoratorWriter |
28 | { |
29 | /** |
30 | * Add relationships to ZIP file. |
31 | */ |
32 | public function render(): ZipInterface |
33 | { |
34 | $this->getZip()->addFromString('_rels/.rels', $this->writeRelationships()); |
35 | $this->getZip()->addFromString('ppt/_rels/presentation.xml.rels', $this->writePresentationRelationships()); |
36 | |
37 | return $this->getZip(); |
38 | } |
39 | |
40 | /** |
41 | * Write relationships to XML format. |
42 | * |
43 | * @return string XML Output |
44 | */ |
45 | protected function writeRelationships(): string |
46 | { |
47 | // Create XML writer |
48 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
49 | |
50 | // XML header |
51 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
52 | |
53 | // Relationships |
54 | $objWriter->startElement('Relationships'); |
55 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
56 | // Relationship ppt/presentation.xml |
57 | $this->writeRelationship($objWriter, 1, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', 'ppt/presentation.xml'); |
58 | // Relationship docProps/core.xml |
59 | $this->writeRelationship($objWriter, 2, 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', 'docProps/core.xml'); |
60 | // Relationship docProps/app.xml |
61 | $this->writeRelationship($objWriter, 3, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', 'docProps/app.xml'); |
62 | // Relationship docProps/custom.xml |
63 | $this->writeRelationship($objWriter, 4, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties', 'docProps/custom.xml'); |
64 | |
65 | $idxRelation = 5; |
66 | |
67 | // Relationship docProps/thumbnail.jpeg |
68 | $thumnbail = $this->getPresentation()->getPresentationProperties()->getThumbnail(); |
69 | if ($thumnbail) { |
70 | $gdImage = imagecreatefromstring($thumnbail); |
71 | if ($gdImage) { |
72 | imagedestroy($gdImage); |
73 | // Relationship docProps/thumbnail.jpeg |
74 | $this->writeRelationship($objWriter, $idxRelation, 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail', 'docProps/thumbnail.jpeg'); |
75 | // $idxRelation++; |
76 | } |
77 | } |
78 | |
79 | $objWriter->endElement(); |
80 | |
81 | // Return |
82 | return $objWriter->getData(); |
83 | } |
84 | |
85 | /** |
86 | * Write presentation relationships to XML format. |
87 | * |
88 | * @return string XML Output |
89 | */ |
90 | protected function writePresentationRelationships(): string |
91 | { |
92 | // Create XML writer |
93 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
94 | |
95 | // XML header |
96 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
97 | |
98 | // Relationships |
99 | $objWriter->startElement('Relationships'); |
100 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
101 | |
102 | // Relation id |
103 | $relationId = 1; |
104 | |
105 | foreach ($this->getPresentation()->getAllMasterSlides() as $oMasterSlide) { |
106 | // Relationship slideMasters/slideMasterX.xml |
107 | $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster', 'slideMasters/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml'); |
108 | } |
109 | |
110 | // Add slide theme (only one!) |
111 | // Relationship theme/theme1.xml |
112 | $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', 'theme/theme1.xml'); |
113 | |
114 | // Relationships with slides |
115 | $slideCount = $this->getPresentation()->getSlideCount(); |
116 | for ($i = 0; $i < $slideCount; ++$i) { |
117 | $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide', 'slides/slide' . ($i + 1) . '.xml'); |
118 | } |
119 | |
120 | $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps', 'presProps.xml'); |
121 | $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps', 'viewProps.xml'); |
122 | $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles', 'tableStyles.xml'); |
123 | |
124 | // Comments Authors |
125 | foreach ($this->getPresentation()->getAllSlides() as $oSlide) { |
126 | foreach ($oSlide->getShapeCollection() as $oShape) { |
127 | if (!($oShape instanceof Comment)) { |
128 | continue; |
129 | } |
130 | $oAuthor = $oShape->getAuthor(); |
131 | if (!($oAuthor instanceof Author)) { |
132 | continue; |
133 | } |
134 | $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/commentAuthors', 'commentAuthors.xml'); |
135 | |
136 | break 2; |
137 | } |
138 | } |
139 | $objWriter->endElement(); |
140 | |
141 | // Return |
142 | return $objWriter->getData(); |
143 | } |
144 | } |