|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPPowerPoint |
| 4 | + * |
| 5 | + * Copyright (c) 2009 - 2010 PHPPowerPoint |
| 6 | + * |
| 7 | + * This library is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU Lesser General Public |
| 9 | + * License as published by the Free Software Foundation; either |
| 10 | + * version 2.1 of the License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this library; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + * |
| 21 | + * @category PHPPowerPoint |
| 22 | + * @package PHPPowerPoint_Writer_PowerPoint2007 |
| 23 | + * @copyright Copyright (c) 2009 - 2010 PHPPowerPoint (http://www.codeplex.com/PHPPowerPoint) |
| 24 | + * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
| 25 | + * @version ##VERSION##, ##DATE## |
| 26 | + */ |
| 27 | + |
| 28 | +/** |
| 29 | + * ODPresentation writer |
| 30 | + */ |
| 31 | +class PHPPowerPoint_Writer_ODPresentation implements PHPPowerPoint_Writer_IWriter |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Private PHPPowerPoint |
| 35 | + * |
| 36 | + * @var PHPPowerPoint |
| 37 | + */ |
| 38 | + private $_presentation; |
| 39 | + |
| 40 | + /** |
| 41 | + * Private writer parts |
| 42 | + * |
| 43 | + * @var PHPPowerPoint_Writer_ODPresentation_WriterPart[] |
| 44 | + */ |
| 45 | + private $_writerParts; |
| 46 | + |
| 47 | + /** |
| 48 | + * Private unique PHPPowerPoint_Worksheet_BaseDrawing HashTable |
| 49 | + * |
| 50 | + * @var PHPPowerPoint_HashTable |
| 51 | + */ |
| 52 | + private $_drawingHashTable; |
| 53 | + |
| 54 | + /** |
| 55 | + * Use disk caching where possible? |
| 56 | + * |
| 57 | + * @var boolean |
| 58 | + */ |
| 59 | + private $_useDiskCaching = false; |
| 60 | + |
| 61 | + /** |
| 62 | + * Disk caching directory |
| 63 | + * |
| 64 | + * @var string |
| 65 | + */ |
| 66 | + private $_diskCachingDirectory; |
| 67 | + |
| 68 | + /** |
| 69 | + * Create a new PHPPowerPoint_Writer_ODPresentation |
| 70 | + * |
| 71 | + * @param PHPPowerPoint $pPHPPowerPoint |
| 72 | + */ |
| 73 | + public function __construct(PHPPowerPoint $pPHPPowerPoint = null) |
| 74 | + { |
| 75 | + // Assign PHPPowerPoint |
| 76 | + $this->setPHPPowerPoint($pPHPPowerPoint); |
| 77 | + |
| 78 | + // Set up disk caching location |
| 79 | + $this->_diskCachingDirectory = './'; |
| 80 | + |
| 81 | + // Initialise writer parts |
| 82 | + $this->_writerParts['content'] = new PHPPowerPoint_Writer_ODPresentation_Content(); |
| 83 | + $this->_writerParts['manifest'] = new PHPPowerPoint_Writer_ODPresentation_Manifest(); |
| 84 | + $this->_writerParts['meta'] = new PHPPowerPoint_Writer_ODPresentation_Meta(); |
| 85 | + $this->_writerParts['mimetype'] = new PHPPowerPoint_Writer_ODPresentation_Mimetype(); |
| 86 | + $this->_writerParts['styles'] = new PHPPowerPoint_Writer_ODPresentation_Styles(); |
| 87 | + |
| 88 | + $this->_writerParts['drawing'] = new PHPPowerPoint_Writer_ODPresentation_Drawing(); |
| 89 | + |
| 90 | + // Assign parent IWriter |
| 91 | + foreach ($this->_writerParts as $writer) { |
| 92 | + $writer->setParentWriter($this); |
| 93 | + } |
| 94 | + |
| 95 | + // Set HashTable variables |
| 96 | + $this->_drawingHashTable = new PHPPowerPoint_HashTable(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Save PHPPowerPoint to file |
| 101 | + * |
| 102 | + * @param string $pFilename |
| 103 | + * @throws Exception |
| 104 | + */ |
| 105 | + public function save($pFilename) |
| 106 | + { |
| 107 | + if (empty($pFilename)) { |
| 108 | + throw new Exception("Filename is empty"); |
| 109 | + } |
| 110 | + if (!is_null($this->_presentation)) { |
| 111 | + // If $pFilename is php://output or php://stdout, make it a temporary file... |
| 112 | + $originalFilename = $pFilename; |
| 113 | + if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { |
| 114 | + $pFilename = @tempnam('./', 'phppttmp'); |
| 115 | + if ($pFilename == '') { |
| 116 | + $pFilename = $originalFilename; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Create drawing dictionary |
| 121 | + $this->_drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->_presentation)); |
| 122 | + |
| 123 | + // Create new ZIP file and open it for writing |
| 124 | + $objZip = new ZipArchive(); |
| 125 | + |
| 126 | + // Try opening the ZIP file |
| 127 | + if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) { |
| 128 | + if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) { |
| 129 | + throw new Exception("Could not open " . $pFilename . " for writing."); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Add mimetype to ZIP file |
| 134 | + //@todo Not in ZIPARCHIVE::CM_STORE mode |
| 135 | + $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype()); |
| 136 | + |
| 137 | + // Add content.xml to ZIP file |
| 138 | + $objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->_presentation)); |
| 139 | + |
| 140 | + // Add meta.xml to ZIP file |
| 141 | + $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->_presentation)); |
| 142 | + |
| 143 | + // Add styles.xml to ZIP file |
| 144 | + $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->_presentation)); |
| 145 | + |
| 146 | + // Add META-INF/manifest.xml |
| 147 | + $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest()); |
| 148 | + |
| 149 | + // Add media |
| 150 | + $arrMedia = array(); |
| 151 | + for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { |
| 152 | + if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPPowerPoint_Shape_Drawing) { |
| 153 | + if (!in_array(md5($this->getDrawingHashTable()->getByIndex($i)->getPath()), $arrMedia)) { |
| 154 | + $arrMedia[] = md5($this->getDrawingHashTable()->getByIndex($i)->getPath()); |
| 155 | + |
| 156 | + $imageContents = null; |
| 157 | + $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath(); |
| 158 | + |
| 159 | + if (strpos($imagePath, 'zip://') !== false) { |
| 160 | + $imagePath = substr($imagePath, 6); |
| 161 | + $imagePathSplitted = explode('#', $imagePath); |
| 162 | + |
| 163 | + $imageZip = new ZipArchive(); |
| 164 | + $imageZip->open($imagePathSplitted[0]); |
| 165 | + $imageContents = $imageZip->getFromName($imagePathSplitted[1]); |
| 166 | + $imageZip->close(); |
| 167 | + unset($imageZip); |
| 168 | + } else { |
| 169 | + $imageContents = file_get_contents($imagePath); |
| 170 | + } |
| 171 | + |
| 172 | + $objZip->addFromString('Pictures/' . md5($this->getDrawingHashTable()->getByIndex($i)->getPath()).'.'.$this->getDrawingHashTable()->getByIndex($i)->getExtension(), $imageContents); |
| 173 | + } |
| 174 | + } elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPPowerPoint_Shape_MemoryDrawing) { |
| 175 | + if (!in_array(md5($this->getDrawingHashTable()->getByIndex($i)->getPath()), $arrMedia)) { |
| 176 | + $arrMedia[] = md5($this->getDrawingHashTable()->getByIndex($i)->getPath()); |
| 177 | + ob_start(); |
| 178 | + call_user_func($this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), $this->getDrawingHashTable()->getByIndex($i)->getImageResource()); |
| 179 | + $imageContents = ob_get_contents(); |
| 180 | + ob_end_clean(); |
| 181 | + |
| 182 | + $objZip->addFromString('Pictures/' . md5($this->getDrawingHashTable()->getByIndex($i)->getPath()).'.'.$this->getDrawingHashTable()->getByIndex($i)->getExtension(), $imageContents); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // Close file |
| 188 | + if ($objZip->close() === false) { |
| 189 | + throw new Exception("Could not close zip file $pFilename."); |
| 190 | + } |
| 191 | + |
| 192 | + // If a temporary file was used, copy it to the correct file stream |
| 193 | + if ($originalFilename != $pFilename) { |
| 194 | + if (copy($pFilename, $originalFilename) === false) { |
| 195 | + throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); |
| 196 | + } |
| 197 | + @unlink($pFilename); |
| 198 | + } |
| 199 | + |
| 200 | + } else { |
| 201 | + throw new Exception("PHPPowerPoint object unassigned."); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Get PHPPowerPoint object |
| 207 | + * |
| 208 | + * @return PHPPowerPoint |
| 209 | + * @throws Exception |
| 210 | + */ |
| 211 | + public function getPHPPowerPoint() |
| 212 | + { |
| 213 | + if (!is_null($this->_presentation)) { |
| 214 | + return $this->_presentation; |
| 215 | + } else { |
| 216 | + throw new Exception("No PHPPowerPoint assigned."); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Get PHPPowerPoint object |
| 222 | + * |
| 223 | + * @param PHPPowerPoint $pPHPPowerPoint PHPPowerPoint object |
| 224 | + * @throws Exception |
| 225 | + * @return PHPPowerPoint_Writer_PowerPoint2007 |
| 226 | + */ |
| 227 | + public function setPHPPowerPoint(PHPPowerPoint $pPHPPowerPoint = null) |
| 228 | + { |
| 229 | + $this->_presentation = $pPHPPowerPoint; |
| 230 | + |
| 231 | + return $this; |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Get PHPPowerPoint_Worksheet_BaseDrawing HashTable |
| 236 | + * |
| 237 | + * @return PHPPowerPoint_HashTable |
| 238 | + */ |
| 239 | + public function getDrawingHashTable() |
| 240 | + { |
| 241 | + return $this->_drawingHashTable; |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Get writer part |
| 246 | + * |
| 247 | + * @param string $pPartName Writer part name |
| 248 | + * @return PHPPowerPoint_Writer_ODPresentation_WriterPart |
| 249 | + */ |
| 250 | + public function getWriterPart($pPartName = '') |
| 251 | + { |
| 252 | + if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) { |
| 253 | + return $this->_writerParts[strtolower($pPartName)]; |
| 254 | + } else { |
| 255 | + return null; |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Get use disk caching where possible? |
| 261 | + * |
| 262 | + * @return boolean |
| 263 | + */ |
| 264 | + public function getUseDiskCaching() |
| 265 | + { |
| 266 | + return $this->_useDiskCaching; |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Set use disk caching where possible? |
| 271 | + * |
| 272 | + * @param boolean $pValue |
| 273 | + * @param string $pDirectory Disk caching directory |
| 274 | + * @throws Exception Exception when directory does not exist |
| 275 | + * @return PHPPowerPoint_Writer_PowerPoint2007 |
| 276 | + */ |
| 277 | + public function setUseDiskCaching($pValue = false, $pDirectory = null) |
| 278 | + { |
| 279 | + $this->_useDiskCaching = $pValue; |
| 280 | + |
| 281 | + if (!is_null($pDirectory)) { |
| 282 | + if (is_dir($pDirectory)) { |
| 283 | + $this->_diskCachingDirectory = $pDirectory; |
| 284 | + } else { |
| 285 | + throw new Exception("Directory does not exist: $pDirectory"); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + return $this; |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * Get disk caching directory |
| 294 | + * |
| 295 | + * @return string |
| 296 | + */ |
| 297 | + public function getDiskCachingDirectory() |
| 298 | + { |
| 299 | + return $this->_diskCachingDirectory; |
| 300 | + } |
| 301 | +} |
0 commit comments