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
|
2014-05-04 17:55:54 +07:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL
|
2012-05-06 23:25:37 +02:00
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Writer;
|
|
|
|
|
|
2014-04-18 23:12:51 +07:00
|
|
|
use PhpOffice\PhpWord\Media;
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2014-04-18 23:12:51 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* ODText writer
|
2014-04-17 16:19:23 +07:00
|
|
|
*
|
|
|
|
|
* @since 0.7.0
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
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-04-30 08:54:38 +07:00
|
|
|
// Create parts
|
|
|
|
|
$parts = array('Content', 'Manifest', 'Meta', 'Mimetype', 'Styles');
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
|
$partName = strtolower($part);
|
|
|
|
|
$partClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Part\\' . $part;
|
|
|
|
|
if (class_exists($partClass)) {
|
|
|
|
|
$partObject = new $partClass();
|
|
|
|
|
$partObject->setParentWriter($this);
|
|
|
|
|
$this->writerParts[$partName] = $partObject;
|
|
|
|
|
}
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
2014-04-18 23:12:51 +07:00
|
|
|
|
|
|
|
|
// Set package paths
|
|
|
|
|
$this->mediaPaths = array('image' => 'Pictures/');
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
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
|
|
|
|
2014-04-18 23:12:51 +07:00
|
|
|
// Add section media files
|
|
|
|
|
$sectionMedia = Media::getElements('section');
|
|
|
|
|
if (!empty($sectionMedia)) {
|
|
|
|
|
$this->addFilesToPackage($objZip, $sectionMedia);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add parts
|
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
|
|
|
}
|