/
ODPresentation.php
186 lines (161 loc) · 5.48 KB
/
ODPresentation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* This file is part of PHPPresentation - A pure PHP library for reading and writing
* presentations documents.
*
* PHPPresentation is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
*
* @link https://github.com/PHPOffice/PHPPresentation
* @copyright 2009-2015 PHPPresentation contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpPresentation\Writer;
use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter;
use PhpOffice\PhpPresentation\HashTable;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\AbstractDrawing;
use PhpOffice\PhpPresentation\Shape\Table;
use DirectoryIterator;
/**
* ODPresentation writer
*/
class ODPresentation extends AbstractWriter implements WriterInterface
{
/**
* @var \PhpOffice\PhpPresentation\Shape\Chart[]
*/
public $chartArray = array();
/**
* Use disk caching where possible?
*
* @var boolean
*/
private $useDiskCaching = false;
/**
* Disk caching directory
*
* @var string
*/
private $diskCachingDirectory;
/**
* Create a new \PhpOffice\PhpPresentation\Writer\ODPresentation
*
* @param PhpPresentation $pPhpPresentation
* @throws \Exception
*/
public function __construct(PhpPresentation $pPhpPresentation = null)
{
// Assign PhpPresentation
$this->setPhpPresentation($pPhpPresentation);
// Set up disk caching location
$this->diskCachingDirectory = './';
// Set HashTable variables
$this->oDrawingHashTable = new HashTable();
$this->setZipAdapter(new ZipArchiveAdapter());
}
/**
* Save PhpPresentation to file
*
* @param string $pFilename
* @throws \Exception
*/
public function save($pFilename)
{
if (empty($pFilename)) {
throw new \Exception("Filename is empty");
}
// If $pFilename is php://output or php://stdout, make it a temporary file...
$originalFilename = $pFilename;
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam('./', 'phppttmp');
if ($pFilename == '') {
$pFilename = $originalFilename;
}
}
// Initialize HashTable
$this->getDrawingHashTable()->addFromSource($this->allDrawings());
// Initialize Zip
$oZip = $this->getZipAdapter();
$oZip->open($pFilename);
// Variables
$oPresentation = $this->getPhpPresentation();
$arrayChart = array();
$arrayFiles = array();
$oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation');
foreach ($oDir as $oFile) {
if (!$oFile->isFile()) {
continue;
}
$class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php');
$class = new \ReflectionClass($class);
if ($class->isAbstract() || !$class->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) {
continue;
}
$arrayFiles[$oFile->getBasename('.php')] = $class;
}
ksort($arrayFiles);
foreach ($arrayFiles as $o) {
$oService = $o->newInstance();
$oService->setZip($oZip);
$oService->setPresentation($oPresentation);
$oService->setDrawingHashTable($this->getDrawingHashTable());
$oService->setArrayChart($arrayChart);
$oZip = $oService->render();
$arrayChart = $oService->getArrayChart();
unset($oService);
}
// Close file
$oZip->close();
// If a temporary file was used, copy it to the correct file stream
if ($originalFilename != $pFilename) {
if (copy($pFilename, $originalFilename) === false) {
throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
}
if (@unlink($pFilename) === false) {
throw new \Exception('The file ' . $pFilename . ' could not be removed.');
}
}
}
/**
* Get use disk caching where possible?
*
* @return boolean
*/
public function hasDiskCaching()
{
return $this->useDiskCaching;
}
/**
* Set use disk caching where possible?
*
* @param boolean $pValue
* @param string $pDirectory Disk caching directory
* @throws \Exception
* @return \PhpOffice\PhpPresentation\Writer\ODPresentation
*/
public function setUseDiskCaching($pValue = false, $pDirectory = null)
{
$this->useDiskCaching = $pValue;
if (!is_null($pDirectory)) {
if (!is_dir($pDirectory)) {
throw new \Exception("Directory does not exist: $pDirectory");
}
$this->diskCachingDirectory = $pDirectory;
}
return $this;
}
/**
* Get disk caching directory
*
* @return string
*/
public function getDiskCachingDirectory()
{
return $this->diskCachingDirectory;
}
}