Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General : Fixed copying bug when presentation had multiple slides #786

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- PowerPoint2007 Writer : Fixed broken animation for first shape - [@shannan1989](https://github.com/shannan1989) in [#783](https://github.com/PHPOffice/PHPPresentation/pull/783)
- Samples : Allow to run without composer - [@pal-software](https://github.com/pal-software) in [#784](https://github.com/PHPOffice/PHPPresentation/pull/784)
- PowerPoint2007 Writer: Extract relations from nested ShapeContainerInterface objects - [@DennisBirkholz](https://github.com/DennisBirkholz) in [#785](https://github.com/PHPOffice/PHPPresentation/pull/785)
- General : Fixed copying bug when presentation had multiple slides [@dees040](https://github.com/dees040) in [#786](https://github.com/PHPOffice/PHPPresentation/pull/786)

## Miscellaneous

Expand Down
19 changes: 17 additions & 2 deletions src/PhpPresentation/PhpPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,26 @@ public function copy(): self
$copied = clone $this;

$slideCount = count($this->slideCollection);

// Because the rebindParent() method on AbstractSlide removes the slide
// from the parent (current $this which we're cloning) presentation, we
// save the collection. This way, after the copying has finished, we can
// return the slides to the original presentation.
$oldSlideCollection = $this->slideCollection;
$newSlideCollection = [];

for ($i = 0; $i < $slideCount; ++$i) {
$this->slideCollection[$i] = $this->slideCollection[$i]->copy();
$this->slideCollection[$i]->rebindParent($this);
$newSlideCollection[$i] = $oldSlideCollection[$i]->copy();
$newSlideCollection[$i]->rebindParent($copied);
}

// Give the copied presentation a copied slide collection which the
// copied slides have been rebind to the copied presentation.
$copied->slideCollection = $newSlideCollection;

// Return the original slides to the original presentation.
$this->slideCollection = $oldSlideCollection;

return $copied;
}

Expand Down
7 changes: 6 additions & 1 deletion tests/PhpPresentation/Tests/PhpPresentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ public function testAddExternalSlide(): void
public function testCopy(): void
{
$object = new PhpPresentation();
self::assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->copy());
$object->createSlide();

$copy = $object->copy();

self::assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $copy);
self::assertEquals(2, $copy->getSlideCount());
}

/**
Expand Down
Loading