Skip to content

Commit

Permalink
Handle nested Group/ShapeContainerInterface objects when writing rela…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
DennisBirkholz committed Dec 8, 2021
1 parent f5907e1 commit f9ce2d5
Showing 1 changed file with 30 additions and 47 deletions.
77 changes: 30 additions & 47 deletions src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PhpOffice\PhpPresentation\Shape\RichText\Run;
use PhpOffice\PhpPresentation\Shape\RichText\TextElement;
use PhpOffice\PhpPresentation\Shape\Table as ShapeTable;
use PhpOffice\PhpPresentation\ShapeContainerInterface;
use PhpOffice\PhpPresentation\Slide;
use PhpOffice\PhpPresentation\Slide\Background\Image;
use PhpOffice\PhpPresentation\Slide\Note;
Expand Down Expand Up @@ -99,56 +100,38 @@ protected function writeSlideRelationships(Slide $pSlide): string

// Write drawing relationships?
if ($pSlide->getShapeCollection()->count() > 0) {
$collections = [$pSlide->getShapeCollection()->getIterator()];

// Loop trough images and write relationships
$iterator = $pSlide->getShapeCollection()->getIterator();
while ($iterator->valid()) {
if ($iterator->current() instanceof Media) {
// Write relationship for image drawing
$iterator->current()->relationId = 'rId' . $relId;
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/video', '../media/' . $iterator->current()->getIndexedFilename());
++$relId;
$this->writeRelationship($objWriter, $relId, 'http://schemas.microsoft.com/office/2007/relationships/media', '../media/' . $iterator->current()->getIndexedFilename());
++$relId;
} elseif ($iterator->current() instanceof ShapeDrawing\AbstractDrawingAdapter) {
// Write relationship for image drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $iterator->current()->getIndexedFilename());
$iterator->current()->relationId = 'rId' . $relId;
++$relId;
} elseif ($iterator->current() instanceof ShapeChart) {
// Write relationship for chart drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart', '../charts/' . $iterator->current()->getIndexedFilename());

$iterator->current()->relationId = 'rId' . $relId;

++$relId;
} elseif ($iterator->current() instanceof Group) {
$iterator2 = $iterator->current()->getShapeCollection()->getIterator();
while ($iterator2->valid()) {
if ($iterator2->current() instanceof Media) {
// Write relationship for image drawing
$iterator2->current()->relationId = 'rId' . $relId;
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/video', '../media/' . $iterator2->current()->getIndexedFilename());
++$relId;
$this->writeRelationship($objWriter, $relId, 'http://schemas.microsoft.com/office/2007/relationships/media', '../media/' . $iterator2->current()->getIndexedFilename());
++$relId;
} elseif ($iterator2->current() instanceof ShapeDrawing\AbstractDrawingAdapter) {
// Write relationship for image drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $iterator2->current()->getIndexedFilename());
$iterator2->current()->relationId = 'rId' . $relId;

++$relId;
} elseif ($iterator2->current() instanceof ShapeChart) {
// Write relationship for chart drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart', '../charts/' . $iterator2->current()->getIndexedFilename());
$iterator2->current()->relationId = 'rId' . $relId;

++$relId;
}
$iterator2->next();
while (\count($collections)) {
$iterator = \array_shift($collections);

while ($iterator->valid()) {
$currentShape = $iterator->current();

if ($currentShape instanceof Media) {
// Write relationship for image drawing
$currentShape->relationId = 'rId' . $relId;
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/video', '../media/' . $currentShape->getIndexedFilename());
++$relId;
$this->writeRelationship($objWriter, $relId, 'http://schemas.microsoft.com/office/2007/relationships/media', '../media/' . $currentShape->getIndexedFilename());
++$relId;
} elseif ($currentShape instanceof ShapeDrawing\AbstractDrawingAdapter) {
// Write relationship for image drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . $currentShape->getIndexedFilename());
$currentShape->relationId = 'rId' . $relId;
++$relId;
} elseif ($currentShape instanceof ShapeChart) {
// Write relationship for chart drawing
$this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart', '../charts/' . $currentShape->getIndexedFilename());
$currentShape->relationId = 'rId' . $relId;
++$relId;
} elseif ($currentShape instanceof ShapeContainerInterface) {
$collections[] = $currentShape->getShapeCollection()->getIterator();
}
}

$iterator->next();
$iterator->next();
}
}
}

Expand Down

0 comments on commit f9ce2d5

Please sign in to comment.