Skip to content

Commit 53bbaea

Browse files
authored
Merge pull request #372 from Fake51/develop
Bugfixes for masterslide issues: issue #330, problems with broken powerpoints needing repair, missing images in shapes
2 parents 312b7a2 + a9d4bf4 commit 53bbaea

File tree

6 files changed

+143
-4
lines changed

6 files changed

+143
-4
lines changed

src/PhpPresentation/Writer/AbstractWriter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ protected function allDrawings()
9696
// Get an array of all drawings
9797
$aDrawings = array();
9898

99-
// Loop trough PhpPresentation
100-
foreach ($this->getPhpPresentation()->getAllSlides() as $oSlide) {
99+
// Loop through PhpPresentation
100+
foreach (array_merge($this->getPhpPresentation()->getAllSlides(), $this->getPhpPresentation()->getAllMasterSlides()) as $oSlide) {
101101
$arrayReturn = $this->iterateCollection($oSlide->getShapeCollection()->getIterator());
102102
$aDrawings = array_merge($aDrawings, $arrayReturn);
103103
}
104+
104105
return $aDrawings;
105106
}
106107

src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ protected function writeDrawingRelations(AbstractSlideAlias $pSlideMaster, $objW
108108
$iterator->next();
109109
}
110110
}
111+
112+
return $relId;
111113
}
112114

113115
/**

src/PhpPresentation/Writer/PowerPoint2007/PptSlideMasters.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpOffice\PhpPresentation\Slide;
1212
use PhpOffice\PhpPresentation\Slide\SlideMaster;
1313
use PhpOffice\PhpPresentation\Style\SchemeColor;
14+
use PhpOffice\PhpPresentation\Slide\Background\Image;
1415

1516
class PptSlideMasters extends AbstractSlide
1617
{
@@ -24,6 +25,12 @@ public function render()
2425
$this->oZip->addFromString('ppt/slideMasters/_rels/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml.rels', $this->writeSlideMasterRelationships($oMasterSlide));
2526
// Add the information from the masterSlide to the ZIP file
2627
$this->oZip->addFromString('ppt/slideMasters/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml', $this->writeSlideMaster($oMasterSlide));
28+
29+
// Add background image slide
30+
$oBkgImage = $oMasterSlide->getBackground();
31+
if ($oBkgImage instanceof Image) {
32+
$this->oZip->addFromString('ppt/media/' . $oBkgImage->getIndexedFilename($oMasterSlide->getRelsIndex()), file_get_contents($oBkgImage->getPath()));
33+
}
2734
}
2835

2936
return $this->oZip;
@@ -54,12 +61,23 @@ public function writeSlideMasterRelationships(SlideMaster $oMasterSlide)
5461
// Save the used relationId
5562
$slideLayout->relationId = 'rId' . $relId;
5663
}
64+
5765
// Write drawing relationships?
58-
$this->writeDrawingRelations($oMasterSlide, $objWriter, $relId);
66+
$relId = $this->writeDrawingRelations($oMasterSlide, $objWriter, ++$relId);
67+
68+
// Write background relationships?
69+
$oBackground = $oMasterSlide->getBackground();
70+
if ($oBackground instanceof Image) {
71+
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $oBackground->getIndexedFilename($oMasterSlide->getRelsIndex()));
72+
$oBackground->relationId = 'rId' . $relId;
73+
74+
$relId++;
75+
}
76+
5977
// TODO: Write hyperlink relationships?
6078
// TODO: Write comment relationships
6179
// Relationship theme/theme1.xml
62-
$this->writeRelationship($objWriter, ++$relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', '../theme/theme' . $oMasterSlide->getRelsIndex() . '.xml');
80+
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', '../theme/theme' . $oMasterSlide->getRelsIndex() . '.xml');
6381
$objWriter->endElement();
6482
// Return
6583
return $objWriter->getData();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
* @copyright 2009-2017 PHPPresentation contributors
14+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15+
* @link https://github.com/PHPOffice/PHPPresentation
16+
*/
17+
18+
namespace PhpOffice\PhpPresentation\Tests\Writer;
19+
20+
use PhpOffice\PhpPresentation\Writer;
21+
22+
/**
23+
* Mock class for AbstractWriter
24+
*
25+
*/
26+
class AbstractWriter extends Writer\AbstractWriter
27+
{
28+
/**
29+
* public wrapper for protected method
30+
*
31+
* @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing[] All drawings in PhpPresentation
32+
* @throws \Exception
33+
*/
34+
public function allDrawings()
35+
{
36+
return parent::allDrawings();
37+
}
38+
}

tests/PhpPresentation/Tests/Writer/AbstractWriterTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
namespace PhpOffice\PhpPresentation\Tests\Writer;
1919

20+
use PhpOffice\PhpPresentation\PhpPresentation;
21+
22+
require 'AbstractWriter.php';
23+
2024
/**
2125
* Test class for AbstractWriter
2226
*
@@ -36,4 +40,25 @@ public function testConstruct()
3640
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Writer\\AbstractWriter', $oStubWriter->setZipAdapter($oStubZip));
3741
$this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $oStubWriter->getZipAdapter());
3842
}
43+
44+
/**
45+
* Test all drawings method
46+
*/
47+
public function testAllDrawingsIncludesMasterSlides()
48+
{
49+
$presentation = new PhpPresentation();
50+
51+
$activeSlide = $presentation->getActiveSlide();
52+
$activeSlide->createDrawingShape();
53+
54+
$masterSlides = $presentation->getAllMasterSlides();
55+
$masterSlide = $masterSlides[0];
56+
$masterSlide->createDrawingShape();
57+
58+
$writer = $this->getMockForAbstractClass('PhpOffice\\PhpPresentation\\Tests\\Writer\\AbstractWriter');
59+
$writer->setPhpPresentation($presentation);
60+
61+
$drawings = $writer->allDrawings();
62+
$this->assertEquals(2, count($drawings), 'Number of drawings should equal two: one from normal slide and one from master slide');
63+
}
3964
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PhpPresentation\Tests\Writer\PowerPoint2007;
4+
5+
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\PptSlideMasters;
6+
use PhpOffice\PhpPresentation\Slide\SlideLayout;
7+
use PhpOffice\PhpPresentation\Shape\Drawing\File as ShapeDrawingFile;
8+
9+
/**
10+
* Test class for PowerPoint2007
11+
*
12+
* @coversDefaultClass PowerPoint2007
13+
*/
14+
class PptSlideMastersTest extends \PHPUnit_Framework_TestCase
15+
{
16+
public function testWriteSlideMasterRelationships()
17+
{
18+
$writer = new PptSlideMasters();
19+
$slideMaster = $this->getMockBuilder('PhpOffice\\PhpPresentation\\Slide\\SlideMaster')
20+
->setMethods(array('getAllSlideLayouts', 'getRelsIndex', 'getShapeCollection'))
21+
->getMock();
22+
23+
$layouts = array(new SlideLayout($slideMaster));
24+
25+
$slideMaster->expects($this->once())
26+
->method('getAllSlideLayouts')
27+
->will($this->returnValue($layouts));
28+
29+
$collection = new \ArrayObject();
30+
$collection[] = new ShapeDrawingFile();
31+
$collection[] = new ShapeDrawingFile();
32+
$collection[] = new ShapeDrawingFile();
33+
34+
$slideMaster->expects($this->exactly(2))
35+
->method('getShapeCollection')
36+
->will($this->returnValue($collection));
37+
38+
$data = $writer->writeSlideMasterRelationships($slideMaster);
39+
40+
$dom = new \DomDocument();
41+
$dom->loadXml($data);
42+
43+
$xpath = new \DomXpath($dom);
44+
$xpath->registerNamespace('r', 'http://schemas.openxmlformats.org/package/2006/relationships');
45+
$list = $xpath->query('//r:Relationship');
46+
47+
$this->assertEquals(5, $list->length);
48+
49+
$this->assertEquals('rId1', $list->item(0)->getAttribute('Id'));
50+
$this->assertEquals('rId2', $list->item(1)->getAttribute('Id'));
51+
$this->assertEquals('rId3', $list->item(2)->getAttribute('Id'));
52+
$this->assertEquals('rId4', $list->item(3)->getAttribute('Id'));
53+
$this->assertEquals('rId5', $list->item(4)->getAttribute('Id'));
54+
}
55+
}

0 commit comments

Comments
 (0)