Completed
Pull Request — develop (#565)
by
unknown
06:14
created

Relationships   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 121
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 7 1
A writeRelationships() 0 38 3
B writePresentationRelationships() 0 54 7
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
    public function render()
17
    {
18
        $this->getZip()->addFromString('_rels/.rels', $this->writeRelationships());
19
        $this->getZip()->addFromString('ppt/_rels/presentation.xml.rels', $this->writePresentationRelationships());
20
21
        return $this->getZip();
22
    }
23
24
    /**
25
     * Write relationships to XML format
26
     *
27
     * @return string        XML Output
28
     * @throws \Exception
29
     */
30
    public function writeRelationships()
31
    {
32
        // Create XML writer
33
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
34
35
        // XML header
36
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
37
38
        // Relationships
39
        $objWriter->startElement('Relationships');
40
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
41
        // Relationship ppt/presentation.xml
42
        $this->writeRelationship($objWriter, 1, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', 'ppt/presentation.xml');
43
        // Relationship docProps/core.xml
44
        $this->writeRelationship($objWriter, 2, 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', 'docProps/core.xml');
45
        // Relationship docProps/app.xml
46
        $this->writeRelationship($objWriter, 3, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', 'docProps/app.xml');
47
        // Relationship docProps/custom.xml
48
        $this->writeRelationship($objWriter, 4, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties', 'docProps/custom.xml');
49
50
        $idxRelation = 5;
51
        // Thumbnail
52
        if ($this->getPresentation()->getPresentationProperties()->getThumbnailPath()) {
53
            $pathThumbnail = file_get_contents($this->getPresentation()->getPresentationProperties()->getThumbnailPath());
54
            $gdImage = imagecreatefromstring($pathThumbnail);
55
            if ($gdImage) {
56
                imagedestroy($gdImage);
57
                // Relationship docProps/thumbnail.jpeg
58
                $this->writeRelationship($objWriter, $idxRelation, 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail', 'docProps/thumbnail.jpeg');
59
            }
60
        }
61
        // ++$idxRelation
62
63
        $objWriter->endElement();
64
65
        // Return
66
        return $objWriter->getData();
67
    }
68
69
    /**
70
     * Write presentation relationships to XML format
71
     *
72
     * @return string        XML Output
73
     * @throws \Exception
74
     */
75
    public function writePresentationRelationships()
76
    {
77
        // Create XML writer
78
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
79
80
        // XML header
81
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
82
83
        // Relationships
84
        $objWriter->startElement('Relationships');
85
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
86
87
        // Relation id
88
        $relationId = 1;
89
90
        foreach ($this->getPresentation()->getAllMasterSlides() as $oMasterSlide) {
91
            // Relationship slideMasters/slideMasterX.xml
92
            $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
        $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', 'theme/theme1.xml');
98
99
        // Relationships with slides
100
        $slideCount = $this->getPresentation()->getSlideCount();
101
        for ($i = 0; $i < $slideCount; ++$i) {
102
            $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide', 'slides/slide' . ($i + 1) . '.xml');
103
        }
104
105
        $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps', 'presProps.xml');
106
        $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps', 'viewProps.xml');
107
        $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles', 'tableStyles.xml');
108
109
110
        // Comments Authors
111
        foreach ($this->getPresentation()->getAllSlides() as $oSlide) {
112
            foreach ($oSlide->getShapeCollection() as $oShape) {
113
                if (!($oShape instanceof Comment)) {
114
                    continue;
115
                }
116
                $oAuthor = $oShape->getAuthor();
117
                if (!($oAuthor instanceof Author)) {
118
                    continue;
119
                }
120
                $this->writeRelationship($objWriter, $relationId++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/commentAuthors', 'commentAuthors.xml');
121
                break 2;
122
            }
123
        }
124
        $objWriter->endElement();
125
126
        // Return
127
        return $objWriter->getData();
128
    }
129
}
130