Completed
Push — develop ( 344c77...9b66e5 )
by Franck
28s queued 12s
created

PptSlideMasters   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 95.8%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 14
dl 0
loc 223
ccs 114
cts 119
cp 0.958
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A writeSlideMasterRelationships() 0 38 3
C writeSlideMaster() 0 144 8
A render() 0 17 3
1
<?php
2
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
3
4
use PhpOffice\Common\Drawing as CommonDrawing;
5
use PhpOffice\Common\XMLWriter;
6
use PhpOffice\PhpPresentation\Shape\AbstractDrawing;
7
use PhpOffice\PhpPresentation\Shape\Chart as ShapeChart;
8
use PhpOffice\PhpPresentation\Shape\Comment;
9
use PhpOffice\PhpPresentation\Shape\RichText;
10
use PhpOffice\PhpPresentation\Shape\Table as ShapeTable;
11
use PhpOffice\PhpPresentation\Slide;
12
use PhpOffice\PhpPresentation\Slide\SlideMaster;
13
use PhpOffice\PhpPresentation\Style\SchemeColor;
14
use PhpOffice\PhpPresentation\Slide\Background\Image;
15
16
class PptSlideMasters extends AbstractSlide
17
{
18
    /**
19
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
20
     * @throws \Exception
21
     */
22 112
    public function render()
23
    {
24 112
        foreach ($this->oPresentation->getAllMasterSlides() as $oMasterSlide) {
25
            // Add the relations from the masterSlide to the ZIP file
26 112
            $this->oZip->addFromString('ppt/slideMasters/_rels/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml.rels', $this->writeSlideMasterRelationships($oMasterSlide));
27
            // Add the information from the masterSlide to the ZIP file
28 112
            $this->oZip->addFromString('ppt/slideMasters/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml', $this->writeSlideMaster($oMasterSlide));
29
30
            // Add background image slide
31 112
            $oBkgImage = $oMasterSlide->getBackground();
32 112
            if ($oBkgImage instanceof Image) {
33
                $this->oZip->addFromString('ppt/media/' . $oBkgImage->getIndexedFilename($oMasterSlide->getRelsIndex()), file_get_contents($oBkgImage->getPath()));
34
            }
35
        }
36
37 112
        return $this->oZip;
38
    }
39
40
    /**
41
     * Write slide master relationships to XML format
42
     *
43
     * @param SlideMaster $oMasterSlide
44
     * @return string XML Output
45
     * @throws \Exception
46
     * @internal param int $masterId Master slide id
47
     */
48 113
    public function writeSlideMasterRelationships(SlideMaster $oMasterSlide)
49
    {
50
        // Create XML writer
51 113
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
52
        // XML header
53 113
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
54
        // Relationships
55 113
        $objWriter->startElement('Relationships');
56 113
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
57
        // Starting relation id
58 113
        $relId = 0;
59
        // Write all the relations to the Layout Slides
60 113
        foreach ($oMasterSlide->getAllSlideLayouts() as $slideLayout) {
61 113
            $this->writeRelationship($objWriter, ++$relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout', '../slideLayouts/slideLayout' . $slideLayout->layoutNr . '.xml');
62
            // Save the used relationId
63 113
            $slideLayout->relationId = 'rId' . $relId;
64
        }
65
66
        // Write drawing relationships?
67 113
        $relId = $this->writeDrawingRelations($oMasterSlide, $objWriter, ++$relId);
68
69
        // Write background relationships?
70 113
        $oBackground = $oMasterSlide->getBackground();
71 113
        if ($oBackground instanceof Image) {
72
            $this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $oBackground->getIndexedFilename($oMasterSlide->getRelsIndex()));
73
            $oBackground->relationId = 'rId' . $relId;
74
75
            $relId++;
76
        }
77
78
        // TODO: Write hyperlink relationships?
79
        // TODO: Write comment relationships
80
        // Relationship theme/theme1.xml
81 113
        $this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', '../theme/theme' . $oMasterSlide->getRelsIndex() . '.xml');
82 113
        $objWriter->endElement();
83
        // Return
84 113
        return $objWriter->getData();
85
    }
86
87
    /**
88
     * Write slide to XML format
89
     *
90
     * @param  \PhpOffice\PhpPresentation\Slide\SlideMaster $pSlide
91
     * @return string XML Output
92
     * @throws \Exception
93
     */
94 112
    public function writeSlideMaster(SlideMaster $pSlide)
95
    {
96
        // Create XML writer
97 112
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
98
        // XML header
99 112
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
100
        // p:sldMaster
101 112
        $objWriter->startElement('p:sldMaster');
102 112
        $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
103 112
        $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
104 112
        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
105
        // p:sldMaster\p:cSld
106 112
        $objWriter->startElement('p:cSld');
107
        // Background
108 112
        $this->writeSlideBackground($pSlide, $objWriter);
109
        // p:sldMaster\p:cSld\p:spTree
110 112
        $objWriter->startElement('p:spTree');
111
        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr
112 112
        $objWriter->startElement('p:nvGrpSpPr');
113
        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvPr
114 112
        $objWriter->startElement('p:cNvPr');
115 112
        $objWriter->writeAttribute('id', '1');
116 112
        $objWriter->writeAttribute('name', '');
117 112
        $objWriter->endElement();
118
        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvGrpSpPr
119 112
        $objWriter->writeElement('p:cNvGrpSpPr', null);
120
        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr\p:nvPr
121 112
        $objWriter->writeElement('p:nvPr', null);
122
        // p:sldMaster\p:cSld\p:spTree\p:nvGrpSpPr
123 112
        $objWriter->endElement();
124
        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr
125 112
        $objWriter->startElement('p:grpSpPr');
126
        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm
127 112
        $objWriter->startElement('a:xfrm');
128
        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:off
129 112
        $objWriter->startElement('a:off');
130 112
        $objWriter->writeAttribute('x', 0);
131 112
        $objWriter->writeAttribute('y', 0);
132 112
        $objWriter->endElement();
133
        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:ext
134 112
        $objWriter->startElement('a:ext');
135 112
        $objWriter->writeAttribute('cx', 0);
136 112
        $objWriter->writeAttribute('cy', 0);
137 112
        $objWriter->endElement();
138
        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chOff
139 112
        $objWriter->startElement('a:chOff');
140 112
        $objWriter->writeAttribute('x', 0);
141 112
        $objWriter->writeAttribute('y', 0);
142 112
        $objWriter->endElement();
143
        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chExt
144 112
        $objWriter->startElement('a:chExt');
145 112
        $objWriter->writeAttribute('cx', 0);
146 112
        $objWriter->writeAttribute('cy', 0);
147 112
        $objWriter->endElement();
148
        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\a:xfrm\
149 112
        $objWriter->endElement();
150
        // p:sldMaster\p:cSld\p:spTree\p:grpSpPr\
151 112
        $objWriter->endElement();
152
        // Loop shapes
153 112
        $this->writeShapeCollection($objWriter, $pSlide->getShapeCollection());
154
        // p:sldMaster\p:cSld\p:spTree\
155 112
        $objWriter->endElement();
156
        // p:sldMaster\p:cSld\
157 112
        $objWriter->endElement();
158
159
        // p:sldMaster\p:clrMap
160 112
        $objWriter->startElement('p:clrMap');
161 112
        foreach ($pSlide->colorMap->getMapping() as $n => $v) {
162 112
            $objWriter->writeAttribute($n, $v);
163
        }
164 112
        $objWriter->endElement();
165
        // p:sldMaster\p:clrMap\
166
167
        // p:sldMaster\p:sldLayoutIdLst
168 112
        $objWriter->startElement('p:sldLayoutIdLst');
169 112
        foreach ($pSlide->getAllSlideLayouts() as $layout) {
170
            /* @var $layout Slide\SlideLayout */
171 112
            $objWriter->startElement('p:sldLayoutId');
172 112
            $objWriter->writeAttribute('id', $layout->layoutId);
173 112
            $objWriter->writeAttribute('r:id', $layout->relationId);
174 112
            $objWriter->endElement();
175
        }
176 112
        $objWriter->endElement();
177
        // p:sldMaster\p:sldLayoutIdLst\
178
179
        // p:sldMaster\p:txStyles
180 112
        $objWriter->startElement('p:txStyles');
181
        foreach (array(
182 112
                     'p:titleStyle' => $pSlide->getTextStyles()->getTitleStyle(),
183 112
                     'p:bodyStyle' => $pSlide->getTextStyles()->getBodyStyle(),
184 112
                     'p:otherStyle' => $pSlide->getTextStyles()->getOtherStyle()
185
                 ) as $startElement => $stylesArray) {
186
            // titleStyle
187 112
            $objWriter->startElement($startElement);
188 112
            foreach ($stylesArray as $lvl => $oParagraph) {
189
                /** @var RichText\Paragraph $oParagraph */
190 112
                $elementName = ($lvl == 0 ? 'a:defPPr' : 'a:lvl' . $lvl . 'pPr');
191 112
                $objWriter->startElement($elementName);
192 112
                $objWriter->writeAttribute('algn', $oParagraph->getAlignment()->getHorizontal());
193 112
                $objWriter->writeAttributeIf(
194 112
                    $oParagraph->getAlignment()->getMarginLeft() != 0,
195 112
                    'marL',
196 112
                    CommonDrawing::pixelsToEmu($oParagraph->getAlignment()->getMarginLeft())
197
                );
198 112
                $objWriter->writeAttributeIf(
199 112
                    $oParagraph->getAlignment()->getMarginRight() != 0,
200 112
                    'marR',
201 112
                    CommonDrawing::pixelsToEmu($oParagraph->getAlignment()->getMarginRight())
202
                );
203 112
                $objWriter->writeAttributeIf(
204 112
                    $oParagraph->getAlignment()->getIndent() != 0,
205 112
                    'indent',
206 112
                    CommonDrawing::pixelsToEmu($oParagraph->getAlignment()->getIndent())
207
                );
208 112
                $objWriter->startElement('a:defRPr');
209 112
                $objWriter->writeAttributeIf($oParagraph->getFont()->getSize() != 10, 'sz', $oParagraph->getFont()->getSize() * 100);
210 112
                $objWriter->writeAttributeIf($oParagraph->getFont()->isBold(), 'b', 1);
211 112
                $objWriter->writeAttributeIf($oParagraph->getFont()->isItalic(), 'i', 1);
212 112
                $objWriter->writeAttribute('kern', '1200');
213 112
                if ($oParagraph->getFont()->getColor() instanceof SchemeColor) {
214 112
                    $objWriter->startElement('a:solidFill');
215 112
                    $objWriter->startElement('a:schemeClr');
216 112
                    $objWriter->writeAttribute('val', $oParagraph->getFont()->getColor()->getValue());
217 112
                    $objWriter->endElement();
218 112
                    $objWriter->endElement();
219
                }
220 112
                $objWriter->endElement();
221 112
                $objWriter->endElement();
222
            }
223 112
            $objWriter->writeElement('a:extLst');
224 112
            $objWriter->endElement();
225
        }
226 112
        $objWriter->endElement();
227
        // p:sldMaster\p:txStyles\
228
229 112
        if (!is_null($pSlide->getTransition())) {
230
            $this->writeSlideTransition($objWriter, $pSlide->getTransition());
231
        }
232
233
        // p:sldMaster\
234 112
        $objWriter->endElement();
235
236 112
        return $objWriter->getData();
237
    }
238
}
239