[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Writer/ -> OpenDocument.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Writer_OpenDocument
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Writer_OpenDocument
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_Writer_OpenDocument extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  29  {
  30      /**
  31       * Private writer parts
  32       *
  33       * @var PHPExcel_Writer_OpenDocument_WriterPart[]
  34       */
  35      private $writerParts = array();
  36  
  37      /**
  38       * Private PHPExcel
  39       *
  40       * @var PHPExcel
  41       */
  42      private $spreadSheet;
  43  
  44      /**
  45       * Create a new PHPExcel_Writer_OpenDocument
  46       *
  47       * @param PHPExcel $pPHPExcel
  48       */
  49      public function __construct(PHPExcel $pPHPExcel = null)
  50      {
  51          $this->setPHPExcel($pPHPExcel);
  52  
  53          $writerPartsArray = array(
  54              'content'    => 'PHPExcel_Writer_OpenDocument_Content',
  55              'meta'       => 'PHPExcel_Writer_OpenDocument_Meta',
  56              'meta_inf'   => 'PHPExcel_Writer_OpenDocument_MetaInf',
  57              'mimetype'   => 'PHPExcel_Writer_OpenDocument_Mimetype',
  58              'settings'   => 'PHPExcel_Writer_OpenDocument_Settings',
  59              'styles'     => 'PHPExcel_Writer_OpenDocument_Styles',
  60              'thumbnails' => 'PHPExcel_Writer_OpenDocument_Thumbnails'
  61          );
  62  
  63          foreach ($writerPartsArray as $writer => $class) {
  64              $this->writerParts[$writer] = new $class($this);
  65          }
  66      }
  67  
  68      /**
  69       * Get writer part
  70       *
  71       * @param  string  $pPartName  Writer part name
  72       * @return PHPExcel_Writer_Excel2007_WriterPart
  73       */
  74      public function getWriterPart($pPartName = '')
  75      {
  76          if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) {
  77              return $this->writerParts[strtolower($pPartName)];
  78          } else {
  79              return null;
  80          }
  81      }
  82  
  83      /**
  84       * Save PHPExcel to file
  85       *
  86       * @param  string  $pFilename
  87       * @throws PHPExcel_Writer_Exception
  88       */
  89      public function save($pFilename = null)
  90      {
  91          if (!$this->spreadSheet) {
  92              throw new PHPExcel_Writer_Exception('PHPExcel object unassigned.');
  93          }
  94  
  95          // garbage collect
  96          $this->spreadSheet->garbageCollect();
  97  
  98          // If $pFilename is php://output or php://stdout, make it a temporary file...
  99          $originalFilename = $pFilename;
 100          if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
 101              $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
 102              if ($pFilename == '') {
 103                  $pFilename = $originalFilename;
 104              }
 105          }
 106  
 107          $objZip = $this->createZip($pFilename);
 108  
 109          $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->writeManifest());
 110          $objZip->addFromString('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->writeThumbnail());
 111          $objZip->addFromString('content.xml', $this->getWriterPart('content')->write());
 112          $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->write());
 113          $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->write());
 114          $objZip->addFromString('settings.xml', $this->getWriterPart('settings')->write());
 115          $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->write());
 116  
 117          // Close file
 118          if ($objZip->close() === false) {
 119              throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
 120          }
 121  
 122          // If a temporary file was used, copy it to the correct file stream
 123          if ($originalFilename != $pFilename) {
 124              if (copy($pFilename, $originalFilename) === false) {
 125                  throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
 126              }
 127              @unlink($pFilename);
 128          }
 129      }
 130  
 131      /**
 132       * Create zip object
 133       *
 134       * @param string $pFilename
 135       * @throws PHPExcel_Writer_Exception
 136       * @return ZipArchive
 137       */
 138      private function createZip($pFilename)
 139      {
 140          // Create new ZIP file and open it for writing
 141          $zipClass = PHPExcel_Settings::getZipClass();
 142          $objZip = new $zipClass();
 143  
 144          // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
 145          // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
 146          $ro = new ReflectionObject($objZip);
 147          $zipOverWrite = $ro->getConstant('OVERWRITE');
 148          $zipCreate = $ro->getConstant('CREATE');
 149  
 150          if (file_exists($pFilename)) {
 151              unlink($pFilename);
 152          }
 153          // Try opening the ZIP file
 154          if ($objZip->open($pFilename, $zipOverWrite) !== true) {
 155              if ($objZip->open($pFilename, $zipCreate) !== true) {
 156                  throw new PHPExcel_Writer_Exception("Could not open $pFilename for writing.");
 157              }
 158          }
 159  
 160          return $objZip;
 161      }
 162  
 163      /**
 164       * Get PHPExcel object
 165       *
 166       * @return PHPExcel
 167       * @throws PHPExcel_Writer_Exception
 168       */
 169      public function getPHPExcel()
 170      {
 171          if ($this->spreadSheet !== null) {
 172              return $this->spreadSheet;
 173          } else {
 174              throw new PHPExcel_Writer_Exception('No PHPExcel assigned.');
 175          }
 176      }
 177  
 178      /**
 179       * Set PHPExcel object
 180       *
 181       * @param  PHPExcel  $pPHPExcel  PHPExcel object
 182       * @throws PHPExcel_Writer_Exception
 183       * @return PHPExcel_Writer_Excel2007
 184       */
 185      public function setPHPExcel(PHPExcel $pPHPExcel = null)
 186      {
 187          $this->spreadSheet = $pPHPExcel;
 188          return $this;
 189      }
 190  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1