Completed
Pull Request — develop (#186)
by
unknown
07:55
created

Relationships::writeRelationships()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

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