|
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
|
|
|
* @link https://github.com/PHPOffice/PHPPresentation |
|
14
|
|
|
* @copyright 2009-2015 PHPPresentation contributors |
|
15
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace PhpOffice\PhpPresentation\Writer; |
|
19
|
|
|
|
|
20
|
|
|
use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter; |
|
21
|
|
|
use PhpOffice\Common\XMLWriter; |
|
22
|
|
|
use PhpOffice\PhpPresentation\PhpPresentation; |
|
23
|
|
|
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* \PhpOffice\PhpPresentation\Writer\Serialized |
|
27
|
|
|
*/ |
|
28
|
|
|
class Serialized extends AbstractWriter implements WriterInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Create a new \PhpOffice\PhpPresentation\Writer\Serialized |
|
32
|
|
|
* |
|
33
|
|
|
* @param \PhpOffice\PhpPresentation\PhpPresentation $pPhpPresentation |
|
34
|
|
|
*/ |
|
35
|
7 |
|
public function __construct(PhpPresentation $pPhpPresentation = null) |
|
36
|
|
|
{ |
|
37
|
|
|
// Set PhpPresentation |
|
38
|
7 |
|
$this->setPhpPresentation($pPhpPresentation); |
|
39
|
|
|
|
|
40
|
|
|
// Set ZIP Adapter |
|
41
|
7 |
|
$this->setZipAdapter(new ZipArchiveAdapter()); |
|
42
|
7 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Save PhpPresentation to file |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $pFilename |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
5 |
|
public function save($pFilename) |
|
51
|
|
|
{ |
|
52
|
5 |
|
if (empty($pFilename)) { |
|
53
|
1 |
|
throw new \Exception("Filename is empty."); |
|
54
|
|
|
} |
|
55
|
4 |
|
$oPresentation = $this->getPhpPresentation(); |
|
56
|
|
|
|
|
57
|
|
|
// Create new ZIP file and open it for writing |
|
58
|
3 |
|
$objZip = $this->getZipAdapter(); |
|
59
|
|
|
|
|
60
|
|
|
// Try opening the ZIP file |
|
61
|
3 |
|
$objZip->open($pFilename); |
|
62
|
|
|
|
|
63
|
|
|
// Add media |
|
64
|
2 |
|
$slideCount = $oPresentation->getSlideCount(); |
|
65
|
2 |
|
for ($i = 0; $i < $slideCount; ++$i) { |
|
66
|
2 |
|
for ($j = 0; $j < $oPresentation->getSlide($i)->getShapeCollection()->count(); ++$j) { |
|
67
|
2 |
|
if ($oPresentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawingAdapter) { |
|
68
|
2 |
|
$imgTemp = $oPresentation->getSlide($i)->getShapeCollection()->offsetGet($j); |
|
69
|
2 |
|
$objZip->addFromString('media/' . $imgTemp->getIndexedFilename(), file_get_contents($imgTemp->getPath())); |
|
70
|
2 |
|
} |
|
71
|
2 |
|
} |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Add PhpPresentation.xml to the document, which represents a PHP serialized PhpPresentation object |
|
75
|
2 |
|
$objZip->addFromString('PhpPresentation.xml', $this->writeSerialized($oPresentation, $pFilename)); |
|
76
|
|
|
|
|
77
|
|
|
// Close file |
|
78
|
2 |
|
$objZip->close(); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Serialize PhpPresentation object to XML |
|
83
|
|
|
* |
|
84
|
|
|
* @param PhpPresentation $pPhpPresentation |
|
85
|
|
|
* @param string $pFilename |
|
86
|
|
|
* @return string XML Output |
|
87
|
|
|
* @throws \Exception |
|
88
|
|
|
*/ |
|
89
|
2 |
|
private function writeSerialized(PhpPresentation $pPhpPresentation = null, $pFilename = '') |
|
90
|
|
|
{ |
|
91
|
|
|
// Clone $pPhpPresentation |
|
92
|
2 |
|
$pPhpPresentation = clone $pPhpPresentation; |
|
93
|
|
|
|
|
94
|
|
|
// Update media links |
|
95
|
2 |
|
$slideCount = $pPhpPresentation->getSlideCount(); |
|
96
|
2 |
|
for ($i = 0; $i < $slideCount; ++$i) { |
|
97
|
2 |
|
for ($j = 0; $j < $pPhpPresentation->getSlide($i)->getShapeCollection()->count(); ++$j) { |
|
98
|
2 |
|
if ($pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawingAdapter) { |
|
99
|
2 |
|
$pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j)->setPath('zip://' . $pFilename . '#media/' . $pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j)->getIndexedFilename(), false); |
|
100
|
2 |
|
} |
|
101
|
2 |
|
} |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Create XML writer |
|
105
|
2 |
|
$objWriter = new XMLWriter(); |
|
106
|
2 |
|
$objWriter->openMemory(); |
|
107
|
2 |
|
$objWriter->setIndent(true); |
|
108
|
|
|
|
|
109
|
|
|
// XML header |
|
110
|
2 |
|
$objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
111
|
|
|
|
|
112
|
|
|
// PhpPresentation |
|
113
|
2 |
|
$objWriter->startElement('PhpPresentation'); |
|
114
|
2 |
|
$objWriter->writeAttribute('version', '##VERSION##'); |
|
115
|
|
|
|
|
116
|
|
|
// Comment |
|
117
|
2 |
|
$objWriter->writeComment('This file has been generated using PhpPresentation v##VERSION## (http://github.com/PHPOffice/PhpPresentation). It contains a base64 encoded serialized version of the PhpPresentation internal object.'); |
|
118
|
|
|
|
|
119
|
|
|
// Data |
|
120
|
2 |
|
$objWriter->startElement('data'); |
|
121
|
2 |
|
$objWriter->writeCData(base64_encode(serialize($pPhpPresentation))); |
|
122
|
2 |
|
$objWriter->endElement(); |
|
123
|
|
|
|
|
124
|
2 |
|
$objWriter->endElement(); |
|
125
|
|
|
|
|
126
|
|
|
// Return |
|
127
|
2 |
|
return $objWriter->getData(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|