Skip to content

Commit

Permalink
Merge pull request #372 from Fake51/develop
Browse files Browse the repository at this point in the history
Bugfixes for masterslide issues: issue #330, problems with broken powerpoints needing repair, missing images in shapes
  • Loading branch information
Progi1984 authored Jun 15, 2017
2 parents 312b7a2 + a9d4bf4 commit 53bbaea
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/PhpPresentation/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ protected function allDrawings()
// Get an array of all drawings
$aDrawings = array();

// Loop trough PhpPresentation
foreach ($this->getPhpPresentation()->getAllSlides() as $oSlide) {
// Loop through PhpPresentation
foreach (array_merge($this->getPhpPresentation()->getAllSlides(), $this->getPhpPresentation()->getAllMasterSlides()) as $oSlide) {
$arrayReturn = $this->iterateCollection($oSlide->getShapeCollection()->getIterator());
$aDrawings = array_merge($aDrawings, $arrayReturn);
}

return $aDrawings;
}

Expand Down
2 changes: 2 additions & 0 deletions src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ protected function writeDrawingRelations(AbstractSlideAlias $pSlideMaster, $objW
$iterator->next();
}
}

return $relId;
}

/**
Expand Down
22 changes: 20 additions & 2 deletions src/PhpPresentation/Writer/PowerPoint2007/PptSlideMasters.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpOffice\PhpPresentation\Slide;
use PhpOffice\PhpPresentation\Slide\SlideMaster;
use PhpOffice\PhpPresentation\Style\SchemeColor;
use PhpOffice\PhpPresentation\Slide\Background\Image;

class PptSlideMasters extends AbstractSlide
{
Expand All @@ -24,6 +25,12 @@ public function render()
$this->oZip->addFromString('ppt/slideMasters/_rels/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml.rels', $this->writeSlideMasterRelationships($oMasterSlide));
// Add the information from the masterSlide to the ZIP file
$this->oZip->addFromString('ppt/slideMasters/slideMaster' . $oMasterSlide->getRelsIndex() . '.xml', $this->writeSlideMaster($oMasterSlide));

// Add background image slide
$oBkgImage = $oMasterSlide->getBackground();
if ($oBkgImage instanceof Image) {
$this->oZip->addFromString('ppt/media/' . $oBkgImage->getIndexedFilename($oMasterSlide->getRelsIndex()), file_get_contents($oBkgImage->getPath()));
}
}

return $this->oZip;
Expand Down Expand Up @@ -54,12 +61,23 @@ public function writeSlideMasterRelationships(SlideMaster $oMasterSlide)
// Save the used relationId
$slideLayout->relationId = 'rId' . $relId;
}

// Write drawing relationships?
$this->writeDrawingRelations($oMasterSlide, $objWriter, $relId);
$relId = $this->writeDrawingRelations($oMasterSlide, $objWriter, ++$relId);

// Write background relationships?
$oBackground = $oMasterSlide->getBackground();
if ($oBackground instanceof Image) {
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $oBackground->getIndexedFilename($oMasterSlide->getRelsIndex()));
$oBackground->relationId = 'rId' . $relId;

$relId++;
}

// TODO: Write hyperlink relationships?
// TODO: Write comment relationships
// Relationship theme/theme1.xml
$this->writeRelationship($objWriter, ++$relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', '../theme/theme' . $oMasterSlide->getRelsIndex() . '.xml');
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', '../theme/theme' . $oMasterSlide->getRelsIndex() . '.xml');
$objWriter->endElement();
// Return
return $objWriter->getData();
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpPresentation/Tests/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* This file is part of PHPPresentation - A pure PHP library for reading and writing
* presentations documents.
*
* PHPPresentation is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
*
* @copyright 2009-2017 PHPPresentation contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
* @link https://github.com/PHPOffice/PHPPresentation
*/

namespace PhpOffice\PhpPresentation\Tests\Writer;

use PhpOffice\PhpPresentation\Writer;

/**
* Mock class for AbstractWriter
*
*/
class AbstractWriter extends Writer\AbstractWriter

This comment has been minimized.

Copy link
@Quix0r

Quix0r Jul 4, 2017

Is this really an abstract class? Then please add abstract to it.

This comment has been minimized.

Copy link
@Progi1984

Progi1984 Jul 5, 2017

Author Member

@Quix0r Could you open an issue for that ? please :)

{
/**
* public wrapper for protected method
*
* @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing[] All drawings in PhpPresentation
* @throws \Exception
*/
public function allDrawings()
{
return parent::allDrawings();
}
}
25 changes: 25 additions & 0 deletions tests/PhpPresentation/Tests/Writer/AbstractWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

namespace PhpOffice\PhpPresentation\Tests\Writer;

use PhpOffice\PhpPresentation\PhpPresentation;

require 'AbstractWriter.php';

/**
* Test class for AbstractWriter
*
Expand All @@ -36,4 +40,25 @@ public function testConstruct()
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Writer\\AbstractWriter', $oStubWriter->setZipAdapter($oStubZip));
$this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $oStubWriter->getZipAdapter());
}

/**
* Test all drawings method
*/
public function testAllDrawingsIncludesMasterSlides()
{
$presentation = new PhpPresentation();

$activeSlide = $presentation->getActiveSlide();
$activeSlide->createDrawingShape();

$masterSlides = $presentation->getAllMasterSlides();
$masterSlide = $masterSlides[0];
$masterSlide->createDrawingShape();

$writer = $this->getMockForAbstractClass('PhpOffice\\PhpPresentation\\Tests\\Writer\\AbstractWriter');
$writer->setPhpPresentation($presentation);

$drawings = $writer->allDrawings();
$this->assertEquals(2, count($drawings), 'Number of drawings should equal two: one from normal slide and one from master slide');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace PhpPresentation\Tests\Writer\PowerPoint2007;

use PhpOffice\PhpPresentation\Writer\PowerPoint2007\PptSlideMasters;
use PhpOffice\PhpPresentation\Slide\SlideLayout;
use PhpOffice\PhpPresentation\Shape\Drawing\File as ShapeDrawingFile;

/**
* Test class for PowerPoint2007
*
* @coversDefaultClass PowerPoint2007
*/
class PptSlideMastersTest extends \PHPUnit_Framework_TestCase
{
public function testWriteSlideMasterRelationships()
{
$writer = new PptSlideMasters();
$slideMaster = $this->getMockBuilder('PhpOffice\\PhpPresentation\\Slide\\SlideMaster')
->setMethods(array('getAllSlideLayouts', 'getRelsIndex', 'getShapeCollection'))
->getMock();

$layouts = array(new SlideLayout($slideMaster));

$slideMaster->expects($this->once())
->method('getAllSlideLayouts')
->will($this->returnValue($layouts));

$collection = new \ArrayObject();
$collection[] = new ShapeDrawingFile();
$collection[] = new ShapeDrawingFile();
$collection[] = new ShapeDrawingFile();

$slideMaster->expects($this->exactly(2))
->method('getShapeCollection')
->will($this->returnValue($collection));

$data = $writer->writeSlideMasterRelationships($slideMaster);

$dom = new \DomDocument();
$dom->loadXml($data);

$xpath = new \DomXpath($dom);
$xpath->registerNamespace('r', 'http://schemas.openxmlformats.org/package/2006/relationships');
$list = $xpath->query('//r:Relationship');

$this->assertEquals(5, $list->length);

$this->assertEquals('rId1', $list->item(0)->getAttribute('Id'));
$this->assertEquals('rId2', $list->item(1)->getAttribute('Id'));
$this->assertEquals('rId3', $list->item(2)->getAttribute('Id'));
$this->assertEquals('rId4', $list->item(3)->getAttribute('Id'));
$this->assertEquals('rId5', $list->item(4)->getAttribute('Id'));
}
}

0 comments on commit 53bbaea

Please sign in to comment.