ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
OpenDocument.php
Go to the documentation of this file.
1 <?php
39 {
45  private $_writerParts = array();
46 
52  private $_spreadSheet;
53 
59  public function __construct(PHPExcel $pPHPExcel = null)
60  {
61  $this->setPHPExcel($pPHPExcel);
62 
63  $writerPartsArray = array(
64  'content' => 'PHPExcel_Writer_OpenDocument_Content',
65  'meta' => 'PHPExcel_Writer_OpenDocument_Meta',
66  'meta_inf' => 'PHPExcel_Writer_OpenDocument_MetaInf',
67  'mimetype' => 'PHPExcel_Writer_OpenDocument_Mimetype',
68  'settings' => 'PHPExcel_Writer_OpenDocument_Settings',
69  'styles' => 'PHPExcel_Writer_OpenDocument_Styles',
70  'thumbnails' => 'PHPExcel_Writer_OpenDocument_Thumbnails'
71  );
72 
73  foreach ($writerPartsArray as $writer => $class) {
74  $this->_writerParts[$writer] = new $class($this);
75  }
76  }
77 
84  public function getWriterPart($pPartName = '')
85  {
86  if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
87  return $this->_writerParts[strtolower($pPartName)];
88  } else {
89  return null;
90  }
91  }
92 
99  public function save($pFilename = NULL)
100  {
101  if (!$this->_spreadSheet) {
102  throw new PHPExcel_Writer_Exception('PHPExcel object unassigned.');
103  }
104 
105  // garbage collect
106  $this->_spreadSheet->garbageCollect();
107 
108  // If $pFilename is php://output or php://stdout, make it a temporary file...
109  $originalFilename = $pFilename;
110  if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
111  $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
112  if ($pFilename == '') {
113  $pFilename = $originalFilename;
114  }
115  }
116 
117  $objZip = $this->_createZip($pFilename);
118 
119  $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->writeManifest());
120  $objZip->addFromString('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->writeThumbnail());
121  $objZip->addFromString('content.xml', $this->getWriterPart('content')->write());
122  $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->write());
123  $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->write());
124  $objZip->addFromString('settings.xml', $this->getWriterPart('settings')->write());
125  $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->write());
126 
127  // Close file
128  if ($objZip->close() === false) {
129  throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
130  }
131 
132  // If a temporary file was used, copy it to the correct file stream
133  if ($originalFilename != $pFilename) {
134  if (copy($pFilename, $originalFilename) === false) {
135  throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
136  }
137  @unlink($pFilename);
138  }
139  }
140 
148  private function _createZip($pFilename)
149  {
150  // Create new ZIP file and open it for writing
151  $zipClass = PHPExcel_Settings::getZipClass();
152  $objZip = new $zipClass();
153 
154  // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
155  // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
156  $ro = new ReflectionObject($objZip);
157  $zipOverWrite = $ro->getConstant('OVERWRITE');
158  $zipCreate = $ro->getConstant('CREATE');
159 
160  if (file_exists($pFilename)) {
161  unlink($pFilename);
162  }
163  // Try opening the ZIP file
164  if ($objZip->open($pFilename, $zipOverWrite) !== true) {
165  if ($objZip->open($pFilename, $zipCreate) !== true) {
166  throw new PHPExcel_Writer_Exception("Could not open $pFilename for writing.");
167  }
168  }
169 
170  return $objZip;
171  }
172 
179  public function getPHPExcel()
180  {
181  if ($this->_spreadSheet !== null) {
182  return $this->_spreadSheet;
183  } else {
184  throw new PHPExcel_Writer_Exception('No PHPExcel assigned.');
185  }
186  }
187 
195  public function setPHPExcel(PHPExcel $pPHPExcel = null)
196  {
197  $this->_spreadSheet = $pPHPExcel;
198  return $this;
199  }
200 }
static getZipClass()
Return the name of the Zip handler Class that PHPExcel is configured to use (PCLZip or ZipArchive) or...
Definition: Settings.php:141
__construct(PHPExcel $pPHPExcel=null)
Create a new PHPExcel_Writer_OpenDocument.
static sys_get_temp_dir()
Get the systems temporary directory.
Definition: File.php:135
_createZip($pFilename)
Create zip object.
save($pFilename=NULL)
Save PHPExcel to file.
Create styles array
The data for the language used.
getWriterPart($pPartName='')
Get writer part.
getPHPExcel()
Get PHPExcel object.
setPHPExcel(PHPExcel $pPHPExcel=null)
Set PHPExcel object.