1 | <?php |
||
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 | * @throws \Exception |
||
56 | */ |
||
57 | 69 | public function __construct(PhpPresentation $pPhpPresentation = null) |
|
70 | |||
71 | /** |
||
72 | * Save PhpPresentation to file |
||
73 | * |
||
74 | * @param string $pFilename |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | 65 | public function save($pFilename) |
|
78 | { |
||
79 | 65 | if (empty($pFilename)) { |
|
80 | 1 | throw new \Exception("Filename is empty"); |
|
81 | } |
||
82 | // If $pFilename is php://output or php://stdout, make it a temporary file... |
||
83 | 64 | $originalFilename = $pFilename; |
|
84 | 64 | if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { |
|
85 | $pFilename = @tempnam('./', 'phppttmp'); |
||
86 | if ($pFilename == '') { |
||
87 | $pFilename = $originalFilename; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | // Initialize HashTable |
||
92 | 64 | $this->getDrawingHashTable()->addFromSource($this->allDrawings()); |
|
93 | |||
94 | // Initialize Zip |
||
95 | 64 | $oZip = $this->getZipAdapter(); |
|
96 | 64 | $oZip->open($pFilename); |
|
97 | |||
98 | // Variables |
||
99 | 64 | $oPresentation = $this->getPhpPresentation(); |
|
100 | 64 | $arrayChart = array(); |
|
101 | |||
102 | 64 | $arrayFiles = array(); |
|
103 | 64 | $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation'); |
|
104 | 64 | foreach ($oDir as $oFile) { |
|
105 | 64 | if (!$oFile->isFile()) { |
|
106 | 64 | continue; |
|
107 | } |
||
108 | |||
109 | 64 | $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php'); |
|
110 | 64 | $class = new \ReflectionClass($class); |
|
111 | |||
112 | 64 | if ($class->isAbstract() || !$class->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) { |
|
113 | 64 | continue; |
|
114 | } |
||
115 | 64 | $arrayFiles[$oFile->getBasename('.php')] = $class; |
|
116 | 64 | } |
|
117 | |||
118 | 64 | ksort($arrayFiles); |
|
119 | |||
120 | 64 | foreach ($arrayFiles as $o) { |
|
121 | 64 | $oService = $o->newInstance(); |
|
122 | 64 | $oService->setZip($oZip); |
|
123 | 64 | $oService->setPresentation($oPresentation); |
|
124 | 64 | $oService->setDrawingHashTable($this->getDrawingHashTable()); |
|
125 | 64 | $oService->setArrayChart($arrayChart); |
|
126 | 64 | $oZip = $oService->render(); |
|
127 | 64 | $arrayChart = $oService->getArrayChart(); |
|
128 | 64 | unset($oService); |
|
129 | 64 | } |
|
130 | |||
131 | // Close file |
||
132 | 62 | $oZip->close(); |
|
133 | |||
134 | // If a temporary file was used, copy it to the correct file stream |
||
135 | 62 | if ($originalFilename != $pFilename) { |
|
136 | if (copy($pFilename, $originalFilename) === false) { |
||
137 | throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename."); |
||
138 | } |
||
139 | if (@unlink($pFilename) === false) { |
||
140 | throw new \Exception('The file ' . $pFilename . ' could not be removed.'); |
||
141 | } |
||
142 | } |
||
143 | 62 | } |
|
144 | |||
145 | /** |
||
146 | * Get use disk caching where possible? |
||
147 | * |
||
148 | * @return boolean |
||
149 | */ |
||
150 | 1 | public function hasDiskCaching() |
|
154 | |||
155 | /** |
||
156 | * Set use disk caching where possible? |
||
157 | * |
||
158 | * @param boolean $pValue |
||
159 | * @param string $pDirectory Disk caching directory |
||
160 | * @throws \Exception |
||
161 | * @return \PhpOffice\PhpPresentation\Writer\ODPresentation |
||
162 | */ |
||
163 | 2 | public function setUseDiskCaching($pValue = false, $pDirectory = null) |
|
164 | { |
||
165 | 2 | $this->useDiskCaching = $pValue; |
|
166 | |||
167 | 2 | if (!is_null($pDirectory)) { |
|
168 | 2 | if (!is_dir($pDirectory)) { |
|
169 | 1 | throw new \Exception("Directory does not exist: $pDirectory"); |
|
170 | } |
||
171 | 1 | $this->diskCachingDirectory = $pDirectory; |
|
172 | 1 | } |
|
173 | |||
174 | 1 | return $this; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get disk caching directory |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | 2 | public function getDiskCachingDirectory() |
|
186 | } |
||
187 |