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\PhpPresentation\HashTable; |
22
|
|
|
use PhpOffice\PhpPresentation\PhpPresentation; |
23
|
|
|
use PhpOffice\PhpPresentation\Shape\AbstractDrawing; |
24
|
|
|
use PhpOffice\PhpPresentation\Shape\Table; |
25
|
|
|
use DirectoryIterator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ODPresentation writer |
29
|
|
|
*/ |
30
|
|
|
class ODPresentation extends AbstractWriter implements WriterInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var \PhpOffice\PhpPresentation\Shape\Chart[] |
34
|
|
|
*/ |
35
|
|
|
public $chartArray = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Use disk caching where possible? |
39
|
|
|
* |
40
|
|
|
* @var boolean |
41
|
|
|
*/ |
42
|
|
|
private $useDiskCaching = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Disk caching directory |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $diskCachingDirectory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a new \PhpOffice\PhpPresentation\Writer\ODPresentation |
53
|
|
|
* |
54
|
|
|
* @param PhpPresentation $pPhpPresentation |
55
|
|
|
*/ |
56
|
66 |
|
public function __construct(PhpPresentation $pPhpPresentation = null) |
57
|
|
|
{ |
58
|
|
|
// Assign PhpPresentation |
59
|
66 |
|
$this->setPhpPresentation($pPhpPresentation); |
60
|
|
|
|
61
|
|
|
// Set up disk caching location |
62
|
66 |
|
$this->diskCachingDirectory = './'; |
63
|
|
|
|
64
|
|
|
// Set HashTable variables |
65
|
66 |
|
$this->oDrawingHashTable = new HashTable(); |
66
|
|
|
|
67
|
66 |
|
$this->setZipAdapter(new ZipArchiveAdapter()); |
68
|
66 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Save PhpPresentation to file |
72
|
|
|
* |
73
|
|
|
* @param string $pFilename |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
62 |
|
public function save($pFilename) |
77
|
|
|
{ |
78
|
62 |
|
if (empty($pFilename)) { |
79
|
1 |
|
throw new \Exception("Filename is empty"); |
80
|
|
|
} |
81
|
|
|
// If $pFilename is php://output or php://stdout, make it a temporary file... |
82
|
61 |
|
$originalFilename = $pFilename; |
83
|
61 |
|
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { |
84
|
|
|
$pFilename = @tempnam('./', 'phppttmp'); |
85
|
|
|
if ($pFilename == '') { |
86
|
|
|
$pFilename = $originalFilename; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Initialize HashTable |
91
|
61 |
|
$this->getDrawingHashTable()->addFromSource($this->allDrawings()); |
92
|
|
|
|
93
|
|
|
// Initialize Zip |
94
|
61 |
|
$oZip = $this->getZipAdapter(); |
95
|
61 |
|
$oZip->open($pFilename); |
96
|
|
|
|
97
|
|
|
// Variables |
98
|
61 |
|
$oPresentation = $this->getPhpPresentation(); |
99
|
61 |
|
$arrayChart = array(); |
100
|
|
|
|
101
|
61 |
|
$arrayFiles = array(); |
102
|
61 |
|
$oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation'); |
103
|
61 |
|
foreach ($oDir as $oFile) { |
104
|
61 |
|
if (!$oFile->isFile()) { |
105
|
61 |
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
61 |
|
$class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php'); |
109
|
61 |
|
$o = new \ReflectionClass($class); |
110
|
|
|
|
111
|
61 |
|
if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) { |
112
|
61 |
|
continue; |
113
|
|
|
} |
114
|
61 |
|
$arrayFiles[$oFile->getBasename('.php')] = $o; |
115
|
|
|
} |
116
|
|
|
|
117
|
61 |
|
ksort($arrayFiles); |
118
|
|
|
|
119
|
61 |
|
foreach ($arrayFiles as $o) { |
120
|
61 |
|
$oService = $o->newInstance(); |
121
|
61 |
|
$oService->setZip($oZip); |
122
|
61 |
|
$oService->setPresentation($oPresentation); |
123
|
61 |
|
$oService->setDrawingHashTable($this->getDrawingHashTable()); |
124
|
61 |
|
$oService->setArrayChart($arrayChart); |
125
|
61 |
|
$oZip = $oService->render(); |
126
|
61 |
|
$arrayChart = $oService->getArrayChart(); |
127
|
61 |
|
unset($oService); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Close file |
131
|
59 |
|
$oZip->close(); |
132
|
|
|
|
133
|
|
|
// If a temporary file was used, copy it to the correct file stream |
134
|
59 |
|
if ($originalFilename != $pFilename) { |
135
|
|
|
if (copy($pFilename, $originalFilename) === false) { |
136
|
|
|
throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename."); |
137
|
|
|
} |
138
|
|
|
if (@unlink($pFilename) === false) { |
139
|
|
|
throw new \Exception('The file ' . $pFilename . ' could not be removed.'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
59 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get use disk caching where possible? |
146
|
|
|
* |
147
|
|
|
* @return boolean |
148
|
|
|
*/ |
149
|
1 |
|
public function hasDiskCaching() |
150
|
|
|
{ |
151
|
1 |
|
return $this->useDiskCaching; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set use disk caching where possible? |
156
|
|
|
* |
157
|
|
|
* @param boolean $pValue |
158
|
|
|
* @param string $pDirectory Disk caching directory |
159
|
|
|
* @throws \Exception |
160
|
|
|
* @return \PhpOffice\PhpPresentation\Writer\ODPresentation |
161
|
|
|
*/ |
162
|
2 |
|
public function setUseDiskCaching($pValue = false, $pDirectory = null) |
163
|
|
|
{ |
164
|
2 |
|
$this->useDiskCaching = $pValue; |
165
|
|
|
|
166
|
2 |
|
if (!is_null($pDirectory)) { |
167
|
2 |
|
if (!is_dir($pDirectory)) { |
168
|
1 |
|
throw new \Exception("Directory does not exist: $pDirectory"); |
169
|
|
|
} |
170
|
1 |
|
$this->diskCachingDirectory = $pDirectory; |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get disk caching directory |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
2 |
|
public function getDiskCachingDirectory() |
182
|
|
|
{ |
183
|
2 |
|
return $this->diskCachingDirectory; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|