Completed
Pull Request — develop (#565)
by
unknown
07:15
created

Relationships::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\XMLWriter;
6
use PhpOffice\PhpPresentation\Shape\Comment;
7
use PhpOffice\PhpPresentation\Shape\Comment\Author;
8
9
class Relationships extends AbstractDecoratorWriter
10
{
11
    /**
12
     * Add relationships to ZIP file
13
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
14
     * @throws \Exception
15
     */
16 112
    public function render()
17
    {
18 112
        $this->getZip()->addFromString('_rels/.rels', $this->writeRelationships());
19 112
        $this->getZip()->addFromString('ppt/_rels/presentation.xml.rels', $this->writePresentationRelationships());
20
21 112
        return $this->getZip();
22
    }
23
24
    /**
25
     * Write relationships to XML format
26
     *
27
     * @return string        XML Output
28
     * @throws \Exception
29
     */
30 112
    public function writeRelationships()
31
    {
32
        // Create XML writer
33 112
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
34
35
        // XML header
36 112
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
37
38
        // Relationships
39 112
        $objWriter->startElement('Relationships');
40 112
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
41
        // Relationship ppt/presentation.xml
42 112
        $this->writeRelationship($objWriter, 1, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', 'ppt/presentation.xml');
43
        // Relationship docProps/core.xml
44 112
        $this->writeRelationship($objWriter, 2, 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', 'docProps/core.xml');
45
        // Relationship docProps/app.xml
46 112
        $this->writeRelationship($objWriter, 3, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', 'docProps/app.xml');
47
        // Relationship docProps/custom.xml
48 112
        $this->writeRelationship($objWriter, 4, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties', 'docProps/custom.xml');
49
50 112
        $idxRelation = 5;
51
        // Thumbnail
52 112
        if ($this->getPresentation()->getPresentationProperties()->getThumbnailPath()) {
53 2
            $pathThumbnail = file_get_contents($this->getPresentation()->getPresentationProperties()->getThumbnailPath());
54 2
            $gdImage = imagecreatefromstring($pathThumbnail);
55 2
            if ($gdImage) {
56 2
                imagedestroy($gdImage);
57
                // Relationship docProps/thumbnail.jpeg
58 2
                $this->writeRelationship($objWriter, $idxRelation, 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail', 'docProps/thumbnail.jpeg');
59
            }
60
        }
61
        // ++$idxRelation
62
63 112
        $objWriter->endElement();
64
65
        // Return
66 112
        return $objWriter->getData();
67
    }
68
69
    /**
70
     * Write presentation relationships to XML format
71
     *
72
     * @return string        XML Output
73
     * @throws \Exception
74
     */
75 112
    public function writePresentationRelationships()
76
    {
77
        // Create XML writer
78 112
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
79
80
        // XML header
81 112
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
82
83
        // Relationships
84 112
        $objWriter->startElement('Relationships');
85 112
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
86
87
        // Relation id
88 112
        $relationId = 1;
89
90 112
        foreach ($this->getPresentation()->getAllMasterSlides() as $oMasterSlide) {
91
            // Relationship slideMasters/slideMasterX.xml
92 112
            $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster', 'slideMasters/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml');
93
        }
94
95
        // Add slide theme (only one!)
96
        // Relationship theme/theme1.xml
97 112
        $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', 'theme/theme1.xml');
98
99
        // Relationships with slides
100 112
        $slideCount = $this->getPresentation()->getSlideCount();
101 112
        for ($i = 0; $i < $slideCount; ++$i) {
102 112
            $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide', 'slides/slide' . ($i + 1) . '.xml');
103
        }
104
105 112
        $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps', 'presProps.xml');
106 112
        $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps', 'viewProps.xml');
107 112
        $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles', 'tableStyles.xml');
108
109
110
        // Comments Authors
111 112
        foreach ($this->getPresentation()->getAllSlides() as $oSlide) {
112 112
            foreach ($oSlide->getShapeCollection() as $oShape) {
113 85
                if (!($oShape instanceof Comment)) {
114 79
                    continue;
115
                }
116 6
                $oAuthor = $oShape->getAuthor();
117 6
                if (!($oAuthor instanceof Author)) {
118 2
                    continue;
119
                }
120 4
                $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/commentAuthors', 'commentAuthors.xml');
121 112
                break 2;
122
            }
123
        }
124 112
        $objWriter->endElement();
125
126
        // Return
127 112
        return $objWriter->getData();
128
    }
129
}
130