2012-05-06 23:25:37 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* PHPWord
|
2012-05-06 23:25:37 +02:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
|
|
|
|
* @copyright 2014 PHPWord
|
|
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2012-05-06 23:25:37 +02:00
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Writer;
|
|
|
|
|
|
2014-03-31 01:13:02 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Content;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Manifest;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Meta;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Mimetype;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Styles;
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* ODText writer
|
|
|
|
|
*/
|
2014-04-08 00:23:49 +07:00
|
|
|
class ODText extends AbstractWriter implements WriterInterface
|
2012-05-06 23:25:37 +02:00
|
|
|
{
|
2013-12-11 14:45:44 -05:00
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Create new ODText writer
|
2014-04-03 11:34:06 +07:00
|
|
|
*
|
2014-04-16 17:22:30 +04:00
|
|
|
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
public function __construct(PhpWord $phpWord = null)
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-03-22 10:06:08 +04:00
|
|
|
// Assign PhpWord
|
|
|
|
|
$this->setPhpWord($phpWord);
|
2013-12-11 14:45:44 -05:00
|
|
|
|
2014-03-30 16:31:01 +07:00
|
|
|
// Set writer parts
|
|
|
|
|
$this->writerParts['content'] = new Content();
|
|
|
|
|
$this->writerParts['manifest'] = new Manifest();
|
|
|
|
|
$this->writerParts['meta'] = new Meta();
|
|
|
|
|
$this->writerParts['mimetype'] = new Mimetype();
|
|
|
|
|
$this->writerParts['styles'] = new Styles();
|
|
|
|
|
foreach ($this->writerParts as $writer) {
|
2013-12-11 14:45:44 -05:00
|
|
|
$writer->setParentWriter($this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-22 10:06:08 +04:00
|
|
|
* Save PhpWord to file
|
2013-12-11 14:45:44 -05:00
|
|
|
*
|
2014-04-08 03:03:14 +07:00
|
|
|
* @param string $filename
|
2014-04-16 17:22:30 +04:00
|
|
|
* @throws \PhpOffice\PhpWord\Exception\Exception
|
2014-04-09 21:35:55 +07:00
|
|
|
* @todo Not in \ZipArchive::CM_STORE mode
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-04-08 03:03:14 +07:00
|
|
|
public function save($filename = null)
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-03-30 16:31:01 +07:00
|
|
|
if (!is_null($this->phpWord)) {
|
2014-04-08 03:03:14 +07:00
|
|
|
$filename = $this->getTempFile($filename);
|
|
|
|
|
$objZip = $this->getZipArchive($filename);
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Add mimetype to ZIP file
|
2014-04-09 21:35:55 +07:00
|
|
|
$objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype());
|
2014-03-30 16:31:01 +07:00
|
|
|
$objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->phpWord));
|
|
|
|
|
$objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->phpWord));
|
|
|
|
|
$objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord));
|
2014-04-09 21:35:55 +07:00
|
|
|
$objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest());
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
|
if ($objZip->close() === false) {
|
2014-04-08 03:03:14 +07:00
|
|
|
throw new Exception("Could not close zip file $filename.");
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-30 16:31:01 +07:00
|
|
|
$this->cleanupTempFile();
|
2013-12-11 14:45:44 -05:00
|
|
|
} else {
|
2014-03-23 10:32:08 +04:00
|
|
|
throw new Exception("PhpWord object unassigned.");
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
2014-03-11 19:26:56 +07:00
|
|
|
}
|