PHPPowerPoint_Writer
[ class tree: PHPPowerPoint_Writer ] [ index: PHPPowerPoint_Writer ] [ all elements ]

Source for file Serialized.php

Documentation is available at Serialized.php

  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
  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    0.1.0, 2009-04-27
  26.  */
  27.  
  28.  
  29. /** PHPPowerPoint */
  30. require_once 'PHPPowerPoint.php';
  31.  
  32. /** PHPPowerPoint_HashTable */
  33. require_once 'PHPPowerPoint/HashTable.php';
  34.  
  35. /** PHPPowerPoint_IComparable */
  36. require_once 'PHPPowerPoint/IComparable.php';
  37.  
  38. /** PHPPowerPoint_Slide */
  39. require_once 'PHPPowerPoint/Slide.php';
  40.  
  41. /** PHPPowerPoint_IWriter */
  42. require_once 'PHPPowerPoint/Writer/IWriter.php';
  43.  
  44.  
  45. /**
  46.  * PHPPowerPoint_Writer_Serialized
  47.  *
  48.  * @category   PHPPowerPoint
  49.  * @package    PHPPowerPoint_Writer
  50.  * @copyright  Copyright (c) 2009 - 2010 PHPPowerPoint (http://www.codeplex.com/PHPPowerPoint)
  51.  */
  52. class PHPPowerPoint_Writer_Serialized implements PHPPowerPoint_Writer_IWriter
  53. {
  54.     /**
  55.      * Private PHPPowerPoint
  56.      *
  57.      * @var PHPPowerPoint 
  58.      */
  59.     private $_presentation;
  60.  
  61.     /**
  62.      * Create a new PHPPowerPoint_Writer_Serialized
  63.      *
  64.      * @param     PHPPowerPoint    $pPHPPowerPoint 
  65.      */
  66.     public function __construct(PHPPowerPoint $pPHPPowerPoint null)
  67.     {
  68.         // Assign PHPPowerPoint
  69.         $this->setPHPPowerPoint($pPHPPowerPoint);
  70.     }
  71.  
  72.     /**
  73.      * Save PHPPowerPoint to file
  74.      *
  75.      * @param     string         $pFileName 
  76.      * @throws     Exception
  77.      */
  78.     public function save($pFilename null)
  79.     {
  80.         if (!is_null($this->_presentation)) {
  81.             // Create new ZIP file and open it for writing
  82.             $objZip new ZipArchive();
  83.  
  84.             // Try opening the ZIP file
  85.             if ($objZip->open($pFilenameZIPARCHIVE::OVERWRITE!== true{
  86.                 if ($objZip->open($pFilenameZIPARCHIVE::CREATE!== true{
  87.                     throw new Exception("Could not open " $pFilename " for writing.");
  88.                 }
  89.             }
  90.  
  91.             // Add media
  92.             $slideCount $this->_presentation->getSlideCount();
  93.             for ($i 0$i $slideCount++$i{
  94.                 for ($j 0$j $this->_presentation->getSlide($i)->getShapeCollection()->count()++$j{
  95.                     if ($this->_presentation->getSlide($i)->getShapeCollection()->offsetGet($jinstanceof PHPPowerPoint_Shape_BaseDrawing{
  96.                         $imgTemp $this->_presentation->getSlide($i)->getShapeCollection()->offsetGet($j);
  97.                         $objZip->addFromString('media/' $imgTemp->getFilename()file_get_contents($imgTemp->getPath()));
  98.                     }
  99.                 }
  100.             }
  101.  
  102.             // Add PHPPowerPoint.xml to the document, which represents a PHP serialized PHPPowerPoint object
  103.             $objZip->addFromString('PHPPowerPoint.xml'$this->_writeSerialized($this->_presentation$pFilename));
  104.  
  105.             // Close file
  106.             if ($objZip->close(=== false{
  107.                 throw new Exception("Could not close zip file $pFilename.");
  108.             }
  109.         else {
  110.             throw new Exception("PHPPowerPoint object unassigned.");
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * Get PHPPowerPoint object
  116.      *
  117.      * @return PHPPowerPoint 
  118.      * @throws Exception
  119.      */
  120.     public function getPHPPowerPoint({
  121.         if (!is_null($this->_presentation)) {
  122.             return $this->_presentation;
  123.         else {
  124.             throw new Exception("No PHPPowerPoint assigned.");
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Get PHPPowerPoint object
  130.      *
  131.      * @param     PHPPowerPoint     $pPHPPowerPoint    PHPPowerPoint object
  132.      * @throws    Exception
  133.      */
  134.     public function setPHPPowerPoint(PHPPowerPoint $pPHPPowerPoint null{
  135.         $this->_presentation = $pPHPPowerPoint;
  136.     }
  137.  
  138.     /**
  139.      * Serialize PHPPowerPoint object to XML
  140.      *
  141.      * @param     PHPPowerPoint    $pPHPPowerPoint 
  142.      * @param     string        $pFilename 
  143.      * @return     string         XML Output
  144.      * @throws     Exception
  145.      */
  146.     private function _writeSerialized(PHPPowerPoint $pPHPPowerPoint null$pFilename '')
  147.     {
  148.         // Clone $pPHPPowerPoint
  149.         $pPHPPowerPoint clone $pPHPPowerPoint;
  150.  
  151.         // Update media links
  152.         $slideCount $pPHPPowerPoint->getSlideCount();
  153.         for ($i 0$i $slideCount++$i{
  154.             for ($j 0$j $pPHPPowerPoint->getSlide($i)->getShapeCollection()->count()++$j{
  155.                 if ($pPHPPowerPoint->getSlide($i)->getShapeCollection()->offsetGet($jinstanceof PHPPowerPoint_Shape_BaseDrawing{
  156.                     $imgTemp =$pPHPPowerPoint->getSlide($i)->getShapeCollection()->offsetGet($j);
  157.                     $imgTemp->setPath('zip://' $pFilename '#media/' $imgTemp->getFilename()false);
  158.                 }
  159.             }
  160.         }
  161.  
  162.         // Create XML writer
  163.         $objWriter new xmlWriter();
  164.         $objWriter->openMemory();
  165.         $objWriter->setIndent(true);
  166.  
  167.         // XML header
  168.         $objWriter->startDocument('1.0','UTF-8','yes');
  169.  
  170.         // PHPPowerPoint
  171.         $objWriter->startElement('PHPPowerPoint');
  172.         $objWriter->writeAttribute('version''0.1.0');
  173.  
  174.             // Comment
  175.             $objWriter->writeComment('This file has been generated using PHPPowerPoint v0.1.0 (http://www.codeplex.com/PHPPowerPoint). It contains a base64 encoded serialized version of the PHPPowerPoint internal object.');
  176.  
  177.             // Data
  178.             $objWriter->startElement('data');
  179.                 $objWriter->writeCDatabase64_encode(serialize($pPHPPowerPoint)) );
  180.             $objWriter->endElement();
  181.  
  182.         $objWriter->endElement();
  183.  
  184.         // Return
  185.         return $objWriter->outputMemory(true);
  186.     }
  187. }

Documentation generated on Sat, 25 Apr 2009 11:37:56 +0200 by phpDocumentor 1.4.1