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

Source for file PowerPoint2007.php

Documentation is available at PowerPoint2007.php

  1. /**
  2.  * PHPPowerPoint
  3.  *
  4.  * Copyright (c) 2009 - 2010 PHPPowerPoint
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA    USA
  19.  *
  20.  * @category   PHPPowerPoint
  21.  * @package    PHPPowerPoint_Writer_PowerPoint2007
  22.  * @copyright  Copyright (c) 2009 - 2010 PHPPowerPoint (http://www.codeplex.com/PHPPowerPoint)
  23.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  24.  * @version    0.1.0, 2009-04-27
  25.  */
  26.  
  27.  
  28. /** PHPPowerPoint */
  29. require_once 'PHPPowerPoint.php';
  30.  
  31. /** PHPPowerPoint_HashTable */
  32. require_once 'PHPPowerPoint/HashTable.php';
  33.  
  34. /** PHPPowerPoint_IComparable */
  35. require_once 'PHPPowerPoint/IComparable.php';
  36.  
  37. /** PHPPowerPoint_Slide */
  38. require_once 'PHPPowerPoint/Slide.php';
  39.  
  40. /** PHPPowerPoint_IWriter */
  41. require_once 'PHPPowerPoint/Writer/IWriter.php';
  42.  
  43. /** PHPPowerPoint_Shared_XMLWriter */
  44. require_once 'PHPPowerPoint/Shared/XMLWriter.php';
  45.  
  46. /** PHPPowerPoint_Writer_PowerPoint2007_WriterPart */
  47. require_once 'PHPPowerPoint/Writer/PowerPoint2007/WriterPart.php';
  48.  
  49. /** PHPPowerPoint_Writer_PowerPoint2007_ContentTypes */
  50. require_once 'PHPPowerPoint/Writer/PowerPoint2007/ContentTypes.php';
  51.  
  52. /** PHPPowerPoint_Writer_PowerPoint2007_DocProps */
  53. require_once 'PHPPowerPoint/Writer/PowerPoint2007/DocProps.php';
  54.  
  55. /** PHPPowerPoint_Writer_PowerPoint2007_Rels */
  56. require_once 'PHPPowerPoint/Writer/PowerPoint2007/Rels.php';
  57.  
  58. /** PHPPowerPoint_Writer_PowerPoint2007_Theme */
  59. require_once 'PHPPowerPoint/Writer/PowerPoint2007/Theme.php';
  60.  
  61. /** PHPPowerPoint_Writer_PowerPoint2007_Presentation */
  62. require_once 'PHPPowerPoint/Writer/PowerPoint2007/Presentation.php';
  63.  
  64. /** PHPPowerPoint_Writer_PowerPoint2007_Slide */
  65. require_once 'PHPPowerPoint/Writer/PowerPoint2007/Slide.php';
  66.  
  67. /** PHPPowerPoint_Writer_PowerPoint2007_Drawing */
  68. require_once 'PHPPowerPoint/Writer/PowerPoint2007/Drawing.php';
  69.  
  70. /** PHPPowerPoint_Writer_PowerPoint2007_LayoutPack */
  71. require_once 'PHPPowerPoint/Writer/PowerPoint2007/LayoutPack.php';
  72.  
  73. /** PHPPowerPoint_Writer_PowerPoint2007_LayoutPack_Default */
  74. require_once 'PHPPowerPoint/Writer/PowerPoint2007/LayoutPack/Default.php';
  75.  
  76.  
  77. /**
  78.  * PHPPowerPoint_Writer_PowerPoint2007
  79.  *
  80.  * @category   PHPPowerPoint
  81.  * @package    PHPPowerPoint_Writer_PowerPoint2007
  82.  * @copyright  Copyright (c) 2009 - 2010 PHPPowerPoint (http://www.codeplex.com/PHPPowerPoint)
  83.  */
  84. class PHPPowerPoint_Writer_PowerPoint2007 implements PHPPowerPoint_Writer_IWriter
  85. {
  86.     /**
  87.      * Office2003 compatibility
  88.      *
  89.      * @var boolean 
  90.      */
  91.     private $_office2003compatibility = false;
  92.  
  93.     /**
  94.      * Private writer parts
  95.      *
  96.      * @var PHPPowerPoint_Writer_PowerPoint2007_WriterPart[] 
  97.      */
  98.     private $_writerParts;
  99.  
  100.     /**
  101.      * Private PHPPowerPoint
  102.      *
  103.      * @var PHPPowerPoint 
  104.      */
  105.     private $_presentation;
  106.  
  107.     /**
  108.      * Private unique PHPPowerPoint_Worksheet_BaseDrawing HashTable
  109.      *
  110.      * @var PHPPowerPoint_HashTable 
  111.      */
  112.     private $_drawingHashTable;
  113.  
  114.     /**
  115.      * Use disk caching where possible?
  116.      *
  117.      * @var boolean 
  118.      */
  119.     private $_useDiskCaching = false;
  120.     
  121.     /**
  122.      * Disk caching directory
  123.      *
  124.      * @var string 
  125.      */
  126.     private $_diskCachingDirectory;
  127.     
  128.     /**
  129.      * Layout pack to use
  130.      * 
  131.      * @var PHPPowerPoint_Writer_PowerPoint2007_LayoutPack 
  132.      */
  133.     private $_layoutPack;
  134.  
  135.     /**
  136.      * Create a new PHPPowerPoint_Writer_PowerPoint2007
  137.      *
  138.      * @param     PHPPowerPoint    $pPHPPowerPoint 
  139.      */
  140.     public function __construct(PHPPowerPoint $pPHPPowerPoint null)
  141.     {
  142.         // Assign PHPPowerPoint
  143.         $this->setPHPPowerPoint($pPHPPowerPoint);
  144.         
  145.         // Set up disk caching location
  146.         $this->_diskCachingDirectory = './';
  147.         
  148.         // Set layout pack
  149.         $this->_layoutPack = new PHPPowerPoint_Writer_PowerPoint2007_LayoutPack_Default();
  150.  
  151.         // Initialise writer parts
  152.         $this->_writerParts['contenttypes']     new PHPPowerPoint_Writer_PowerPoint2007_ContentTypes();
  153.         $this->_writerParts['docprops']         new PHPPowerPoint_Writer_PowerPoint2007_DocProps();
  154.         $this->_writerParts['rels']             new PHPPowerPoint_Writer_PowerPoint2007_Rels();
  155.         $this->_writerParts['theme']             new PHPPowerPoint_Writer_PowerPoint2007_Theme();
  156.         $this->_writerParts['presentation']     new PHPPowerPoint_Writer_PowerPoint2007_Presentation();
  157.         $this->_writerParts['slide']             new PHPPowerPoint_Writer_PowerPoint2007_Slide();
  158.         $this->_writerParts['drawing']             new PHPPowerPoint_Writer_PowerPoint2007_Drawing();
  159.  
  160.         // Assign parent IWriter
  161.         foreach ($this->_writerParts as $writer{
  162.             $writer->setParentWriter($this);
  163.         }
  164.  
  165.         // Set HashTable variables
  166.         $this->_drawingHashTable             = new PHPPowerPoint_HashTable();
  167.     }
  168.  
  169.     /**
  170.      * Get writer part
  171.      *
  172.      * @param     string     $pPartName        Writer part name
  173.      * @return     PHPPowerPoint_Writer_PowerPoint2007_WriterPart 
  174.      */
  175.     function getWriterPart($pPartName ''{
  176.         if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
  177.             return $this->_writerParts[strtolower($pPartName)];
  178.         else {
  179.             return null;
  180.         }
  181.     }
  182.  
  183.     /**
  184.      * Save PHPPowerPoint to file
  185.      *
  186.      * @param     string         $pFileName 
  187.      * @throws     Exception
  188.      */
  189.     public function save($pFilename null)
  190.     {
  191.         if (!is_null($this->_presentation)) {
  192.             // If $pFilename is php://output or php://stdout, make it a temporary file...
  193.             $originalFilename $pFilename;
  194.             if (strtolower($pFilename== 'php://output' || strtolower($pFilename== 'php://stdout'{
  195.                 $pFilename @tempnam('./''phppttmp');
  196.                 if ($pFilename == ''{
  197.                     $pFilename $originalFilename;
  198.                 }
  199.             }
  200.  
  201.             // Create drawing dictionary
  202.             $this->_drawingHashTable->addFromSource(             $this->getWriterPart('Drawing')->allDrawings($this->_presentation)         );
  203.  
  204.             // Create new ZIP file and open it for writing
  205.             $objZip new ZipArchive();
  206.  
  207.             // Try opening the ZIP file
  208.             if ($objZip->open($pFilenameZIPARCHIVE::OVERWRITE!== true{
  209.                 if ($objZip->open($pFilenameZIPARCHIVE::CREATE!== true{
  210.                     throw new Exception("Could not open " $pFilename " for writing.");
  211.                 }
  212.             }
  213.  
  214.             // Add [Content_Types].xml to ZIP file
  215.             $objZip->addFromString('[Content_Types].xml',             $this->getWriterPart('ContentTypes')->writeContentTypes($this->_presentation));
  216.  
  217.             // Add relationships to ZIP file
  218.             $objZip->addFromString('_rels/.rels',                         $this->getWriterPart('Rels')->writeRelationships($this->_presentation));
  219.             $objZip->addFromString('ppt/_rels/presentation.xml.rels',     $this->getWriterPart('Rels')->writePresentationRelationships($this->_presentation));
  220.  
  221.             // Add document properties to ZIP file
  222.             $objZip->addFromString('docProps/app.xml',                 $this->getWriterPart('DocProps')->writeDocPropsApp($this->_presentation));
  223.             $objZip->addFromString('docProps/core.xml',             $this->getWriterPart('DocProps')->writeDocPropsCore($this->_presentation));
  224.  
  225.             // Add theme to ZIP file
  226.             $objZip->addFromString('ppt/theme/theme1.xml',             $this->getWriterPart('Theme')->writeTheme($this->_presentation));
  227.  
  228.             // Add slide master to ZIP file
  229.             $masterSlide $this->getLayoutPack()->getMasterSlide();
  230.             $objZip->addFromString('ppt/slideMasters/_rels/slideMaster1.xml.rels',     $this->getWriterPart('Rels')->writeSlideMasterRelationships());
  231.             $objZip->addFromString('ppt/slideMasters/slideMaster1.xml',             $masterSlide['body']);
  232.             
  233.             // Add slide layouts to ZIP file
  234.             $slideLayouts $this->getLayoutPack()->getLayouts();
  235.             for ($i 0$i count($slideLayouts)++$i{
  236.                 $objZip->addFromString('ppt/slideLayouts/_rels/slideLayout' ($i 1'.xml.rels',     $this->getWriterPart('Rels')->writeSlideLayoutRelationships());
  237.                 $objZip->addFromString('ppt/slideLayouts/slideLayout' ($i 1'.xml',                 $slideLayouts[$i]['body']);
  238.             }            
  239.             
  240.             // Add presentation to ZIP file
  241.             $objZip->addFromString('ppt/presentation.xml',             $this->getWriterPart('Presentation')->writePresentation($this->_presentation));
  242.  
  243.             // Add slides (drawings, ...)
  244.             for ($i 0$i $this->_presentation->getSlideCount()++$i{
  245.                 // Add slide
  246.                 $objZip->addFromString('ppt/slides/slide' ($i 1'.xml',     $this->getWriterPart('Slide')->writeSlide($this->_presentation->getSlide($i)));
  247.             }
  248.             
  249.             // Add slide relationships (drawings, ...)
  250.             for ($i 0$i $this->_presentation->getSlideCount()++$i{
  251.                 // Add relationships
  252.                 $objZip->addFromString('ppt/slides/_rels/slide' ($i 1'.xml.rels',     $this->getWriterPart('Rels')->writeSlideRelationships($this->_presentation->getSlide($i)($i 1)));
  253.             }
  254.  
  255.             // Add media
  256.             for ($i 0$i $this->getDrawingHashTable()->count()++$i{
  257.                 if ($this->getDrawingHashTable()->getByIndex($iinstanceof PHPPowerPoint_Shape_Drawing{
  258.                     $imageContents null;
  259.                     $imagePath $this->getDrawingHashTable()->getByIndex($i)->getPath();
  260.  
  261.                     if (strpos($imagePath'zip://'!== false{
  262.                         $imagePath substr($imagePath6);
  263.                         $imagePathSplitted explode('#'$imagePath);
  264.  
  265.                         $imageZip new ZipArchive();
  266.                         $imageZip->open($imagePathSplitted[0]);
  267.                         $imageContents $imageZip->getFromName($imagePathSplitted[1]);
  268.                         $imageZip->close();
  269.                         unset($imageZip);
  270.                     else {
  271.                         $imageContents file_get_contents($imagePath);
  272.                     }
  273.  
  274.                     $objZip->addFromString('ppt/media/' str_replace(' ''_'$this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())$imageContents);
  275.                 else if ($this->getDrawingHashTable()->getByIndex($iinstanceof PHPPowerPoint_Shape_MemoryDrawing{
  276.                     ob_start();
  277.                     call_user_func(
  278.                         $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
  279.                         $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
  280.                     );
  281.                     $imageContents ob_get_contents();
  282.                     ob_end_clean();
  283.  
  284.                     $objZip->addFromString('ppt/media/' str_replace(' ''_'$this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())$imageContents);
  285.                 }
  286.             }
  287.  
  288.             // Close file
  289.             if ($objZip->close(=== false{
  290.                 throw new Exception("Could not close zip file $pFilename.");
  291.             }
  292.  
  293.             // If a temporary file was used, copy it to the correct file stream
  294.             if ($originalFilename != $pFilename{
  295.                 if (copy($pFilename$originalFilename=== false{
  296.                     throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  297.                 }
  298.                 @unlink($pFilename);
  299.             }
  300.         else {
  301.             throw new Exception("PHPPowerPoint object unassigned.");
  302.         }
  303.     }
  304.  
  305.     /**
  306.      * Get PHPPowerPoint object
  307.      *
  308.      * @return PHPPowerPoint 
  309.      * @throws Exception
  310.      */
  311.     public function getPHPPowerPoint({
  312.         if (!is_null($this->_presentation)) {
  313.             return $this->_presentation;
  314.         else {
  315.             throw new Exception("No PHPPowerPoint assigned.");
  316.         }
  317.     }
  318.  
  319.     /**
  320.      * Get PHPPowerPoint object
  321.      *
  322.      * @param     PHPPowerPoint     $pPHPPowerPoint    PHPPowerPoint object
  323.      * @throws    Exception
  324.      */
  325.     public function setPHPPowerPoint(PHPPowerPoint $pPHPPowerPoint null{
  326.         $this->_presentation = $pPHPPowerPoint;
  327.     }
  328.  
  329.     /**
  330.      * Get PHPPowerPoint_Worksheet_BaseDrawing HashTable
  331.      *
  332.      * @return PHPPowerPoint_HashTable 
  333.      */
  334.     public function getDrawingHashTable({
  335.         return $this->_drawingHashTable;
  336.     }
  337.  
  338.     /**
  339.      * Get Office2003 compatibility
  340.      *
  341.      * @return boolean 
  342.      */
  343.     public function getOffice2003Compatibility({
  344.         return $this->_office2003compatibility;
  345.     }
  346.  
  347.     /**
  348.      * Set Pre-Calculate Formulas
  349.      *
  350.      * @param boolean $pValue    Office2003 compatibility?
  351.      */
  352.     public function setOffice2003Compatibility($pValue false{
  353.         $this->_office2003compatibility = $pValue;
  354.     }
  355.  
  356.     /**
  357.      * Get use disk caching where possible?
  358.      *
  359.      * @return boolean 
  360.      */
  361.     public function getUseDiskCaching({
  362.         return $this->_useDiskCaching;
  363.     }
  364.  
  365.     /**
  366.      * Set use disk caching where possible?
  367.      *
  368.      * @param     boolean     $pValue 
  369.      * @param    string        $pDirectory        Disk caching directory
  370.      * @throws    Exception    Exception when directory does not exist
  371.      */
  372.     public function setUseDiskCaching($pValue false$pDirectory null{
  373.         $this->_useDiskCaching = $pValue;
  374.         
  375.         if (!is_null($pDirectory)) {
  376.             if (is_dir($pDirectory)) {
  377.                 $this->_diskCachingDirectory = $pDirectory;
  378.             else {
  379.                 throw new Exception("Directory does not exist: $pDirectory");
  380.             }
  381.         }
  382.     }
  383.         
  384.     /**
  385.      * Get disk caching directory
  386.      *
  387.      * @return string 
  388.      */
  389.     public function getDiskCachingDirectory({
  390.         return $this->_diskCachingDirectory;
  391.     }
  392.     
  393.     /**
  394.      * Get layout pack to use
  395.      *
  396.      * @return PHPPowerPoint_Writer_PowerPoint2007_LayoutPack 
  397.      */
  398.     public function getLayoutPack({
  399.         return $this->_layoutPack;
  400.     }
  401.     
  402.     /**
  403.      * Set layout pack to use
  404.      *
  405.      * @param     PHPPowerPoint_Writer_PowerPoint2007_LayoutPack     $pValue 
  406.      */
  407.     public function setLayoutPack(PHPPowerPoint_Writer_PowerPoint2007_LayoutPack $pValue null{
  408.         $this->_layoutPack = $pValue;
  409.     }
  410. }

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