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\XMLWriter; |
21
|
|
|
use PhpOffice\PhpPresentation\PhpPresentation; |
22
|
|
|
use PhpOffice\PhpPresentation\Shape\AbstractDrawing; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* \PhpOffice\PhpPresentation\Writer\Serialized |
26
|
|
|
*/ |
27
|
|
|
class Serialized implements WriterInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Private PhpPresentation |
31
|
|
|
* |
32
|
|
|
* @var \PhpOffice\PhpPresentation\PhpPresentation |
33
|
|
|
*/ |
34
|
|
|
private $presentation; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new \PhpOffice\PhpPresentation\Writer\Serialized |
38
|
|
|
* |
39
|
|
|
* @param \PhpOffice\PhpPresentation\PhpPresentation $pPhpPresentation |
40
|
|
|
*/ |
41
|
7 |
|
public function __construct(PhpPresentation $pPhpPresentation = null) |
42
|
|
|
{ |
43
|
|
|
// Assign PhpPresentation |
44
|
7 |
|
$this->setPhpPresentation($pPhpPresentation); |
45
|
7 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Save PhpPresentation to file |
49
|
|
|
* |
50
|
|
|
* @param string $pFilename |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
5 |
|
public function save($pFilename) |
54
|
|
|
{ |
55
|
5 |
|
if (empty($pFilename)) { |
56
|
1 |
|
throw new \Exception("Filename is empty."); |
57
|
|
|
} |
58
|
4 |
|
if (!is_null($this->presentation)) { |
59
|
|
|
// Create new ZIP file and open it for writing |
60
|
3 |
|
$objZip = new \ZipArchive(); |
61
|
|
|
|
62
|
|
|
// Try opening the ZIP file |
63
|
3 |
|
if ($objZip->open($pFilename, \ZipArchive::CREATE) !== true) { |
64
|
2 |
|
if ($objZip->open($pFilename, \ZipArchive::OVERWRITE) !== true) { |
65
|
1 |
|
throw new \Exception("Could not open " . $pFilename . " for writing."); |
66
|
|
|
} |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
// Add media |
70
|
2 |
|
$slideCount = $this->presentation->getSlideCount(); |
71
|
2 |
|
for ($i = 0; $i < $slideCount; ++$i) { |
72
|
2 |
|
for ($j = 0; $j < $this->presentation->getSlide($i)->getShapeCollection()->count(); ++$j) { |
73
|
2 |
|
if ($this->presentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawing) { |
74
|
2 |
|
$imgTemp = $this->presentation->getSlide($i)->getShapeCollection()->offsetGet($j); |
75
|
2 |
|
$objZip->addFromString('media/' . $imgTemp->getFilename(), file_get_contents($imgTemp->getPath())); |
76
|
2 |
|
} |
77
|
2 |
|
} |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
// Add PhpPresentation.xml to the document, which represents a PHP serialized PhpPresentation object |
81
|
2 |
|
$objZip->addFromString('PhpPresentation.xml', $this->writeSerialized($this->presentation, $pFilename)); |
82
|
|
|
|
83
|
|
|
// Close file |
84
|
2 |
|
if ($objZip->close() === false) { |
85
|
|
|
throw new \Exception("Could not close zip file $pFilename."); |
86
|
|
|
} |
87
|
2 |
|
} else { |
88
|
1 |
|
throw new \Exception("PhpPresentation object unassigned."); |
89
|
|
|
} |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get PhpPresentation object |
94
|
|
|
* |
95
|
|
|
* @return PhpPresentation |
96
|
|
|
* @throws \Exception |
97
|
|
|
*/ |
98
|
2 |
|
public function getPhpPresentation() |
99
|
|
|
{ |
100
|
2 |
|
if (!is_null($this->presentation)) { |
101
|
1 |
|
return $this->presentation; |
102
|
|
|
} else { |
103
|
1 |
|
throw new \Exception("No PhpPresentation assigned."); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get PhpPresentation object |
109
|
|
|
* |
110
|
|
|
* @param PhpPresentation $pPhpPresentation PhpPresentation object |
111
|
|
|
* @throws \Exception |
112
|
|
|
* @return \PhpOffice\PhpPresentation\Writer\Serialized |
113
|
|
|
*/ |
114
|
7 |
|
public function setPhpPresentation(PhpPresentation $pPhpPresentation = null) |
115
|
|
|
{ |
116
|
7 |
|
$this->presentation = $pPhpPresentation; |
117
|
|
|
|
118
|
7 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Serialize PhpPresentation object to XML |
123
|
|
|
* |
124
|
|
|
* @param PhpPresentation $pPhpPresentation |
125
|
|
|
* @param string $pFilename |
126
|
|
|
* @return string XML Output |
127
|
|
|
* @throws \Exception |
128
|
|
|
*/ |
129
|
2 |
|
private function writeSerialized(PhpPresentation $pPhpPresentation = null, $pFilename = '') |
130
|
|
|
{ |
131
|
|
|
// Clone $pPhpPresentation |
132
|
2 |
|
$pPhpPresentation = clone $pPhpPresentation; |
133
|
|
|
|
134
|
|
|
// Update media links |
135
|
2 |
|
$slideCount = $pPhpPresentation->getSlideCount(); |
136
|
2 |
|
for ($i = 0; $i < $slideCount; ++$i) { |
137
|
2 |
|
for ($j = 0; $j < $pPhpPresentation->getSlide($i)->getShapeCollection()->count(); ++$j) { |
138
|
2 |
|
if ($pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawing) { |
139
|
2 |
|
$pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j)->setPath('zip://' . $pFilename . '#media/' . $pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j)->getFilename(), false); |
140
|
2 |
|
} |
141
|
2 |
|
} |
142
|
2 |
|
} |
143
|
|
|
|
144
|
|
|
// Create XML writer |
145
|
2 |
|
$objWriter = new XMLWriter(); |
146
|
2 |
|
$objWriter->openMemory(); |
147
|
2 |
|
$objWriter->setIndent(true); |
148
|
|
|
|
149
|
|
|
// XML header |
150
|
2 |
|
$objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
151
|
|
|
|
152
|
|
|
// PhpPresentation |
153
|
2 |
|
$objWriter->startElement('PhpPresentation'); |
154
|
2 |
|
$objWriter->writeAttribute('version', '##VERSION##'); |
155
|
|
|
|
156
|
|
|
// Comment |
157
|
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.'); |
158
|
|
|
|
159
|
|
|
// Data |
160
|
2 |
|
$objWriter->startElement('data'); |
161
|
2 |
|
$objWriter->writeCData(base64_encode(serialize($pPhpPresentation))); |
162
|
2 |
|
$objWriter->endElement(); |
163
|
|
|
|
164
|
2 |
|
$objWriter->endElement(); |
165
|
|
|
|
166
|
|
|
// Return |
167
|
2 |
|
return $objWriter->getData(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|