2012-05-06 18:25:40 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
* word processing documents.
|
|
|
|
|
*
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2012-05-06 18:25:40 +02:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2012-05-06 18:25:40 +02:00
|
|
|
*/
|
2014-03-12 10:09:42 -04:00
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Writer;
|
|
|
|
|
|
2014-04-16 17:39:26 +04:00
|
|
|
use PhpOffice\PhpWord\Element\Section;
|
2014-04-18 01:58:03 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-05-04 21:03:28 +04:00
|
|
|
use PhpOffice\PhpWord\Media;
|
|
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Word2007 writer
|
|
|
|
|
*/
|
2014-04-08 00:23:49 +07:00
|
|
|
class Word2007 extends AbstractWriter implements WriterInterface
|
2013-12-16 06:40:30 -05:00
|
|
|
{
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
2014-04-05 19:02:49 +07:00
|
|
|
* Content types values
|
2014-03-24 00:26:10 +07:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2014-05-06 01:45:13 +07:00
|
|
|
private $contentTypes = array('default' => array(), 'override' => array());
|
2014-03-24 00:26:10 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-04-05 19:02:49 +07:00
|
|
|
* Document relationship
|
2014-03-24 00:26:10 +07:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2014-05-06 01:45:13 +07:00
|
|
|
private $relationships = array();
|
2013-12-16 06:40:30 -05:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Create new Word2007 writer
|
|
|
|
|
*
|
2014-04-16 17:22:30 +04:00
|
|
|
* @param \PhpOffice\PhpWord\PhpWord
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
public function __construct(PhpWord $phpWord = null)
|
2013-12-16 06:40:30 -05:00
|
|
|
{
|
2014-03-30 16:31:01 +07:00
|
|
|
// Assign PhpWord
|
|
|
|
|
$this->setPhpWord($phpWord);
|
|
|
|
|
|
2014-04-30 08:54:38 +07:00
|
|
|
// Create parts
|
2014-05-06 08:50:55 +07:00
|
|
|
$this->parts = array(
|
|
|
|
|
'ContentTypes' => '[Content_Types].xml',
|
|
|
|
|
'Rels' => '_rels/.rels',
|
|
|
|
|
'DocPropsApp' => 'docProps/app.xml',
|
|
|
|
|
'DocPropsCore' => 'docProps/core.xml',
|
|
|
|
|
'RelsDocument' => 'word/_rels/document.xml.rels',
|
|
|
|
|
'Document' => 'word/document.xml',
|
|
|
|
|
'Styles' => 'word/styles.xml',
|
|
|
|
|
'Numbering' => 'word/numbering.xml',
|
|
|
|
|
'Settings' => 'word/settings.xml',
|
|
|
|
|
'WebSettings' => 'word/webSettings.xml',
|
|
|
|
|
'FontTable' => 'word/fontTable.xml',
|
|
|
|
|
'Theme' => 'word/theme/theme1.xml',
|
|
|
|
|
'RelsPart' => '',
|
|
|
|
|
'Header' => '',
|
|
|
|
|
'Footer' => '',
|
|
|
|
|
'Footnotes' => '',
|
|
|
|
|
'Endnotes' => '',
|
|
|
|
|
);
|
|
|
|
|
foreach (array_keys($this->parts) as $partName) {
|
|
|
|
|
$partClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Part\\' . $partName;
|
2014-04-30 08:54:38 +07:00
|
|
|
if (class_exists($partClass)) {
|
|
|
|
|
$partObject = new $partClass();
|
|
|
|
|
$partObject->setParentWriter($this);
|
2014-05-06 08:50:55 +07:00
|
|
|
$this->writerParts[strtolower($partName)] = $partObject;
|
2014-04-30 08:54:38 +07:00
|
|
|
}
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
2014-04-18 23:12:51 +07:00
|
|
|
|
|
|
|
|
// Set package paths
|
|
|
|
|
$this->mediaPaths = array('image' => 'word/media/', 'object' => 'word/embeddings/');
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Save document by name
|
|
|
|
|
*
|
2014-04-08 03:03:14 +07:00
|
|
|
* @param string $filename
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-04-08 03:03:14 +07:00
|
|
|
public function save($filename = null)
|
2013-12-16 06:40:30 -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-16 06:40:30 -05:00
|
|
|
|
2014-04-05 19:02:49 +07:00
|
|
|
// Content types
|
2014-05-06 01:45:13 +07:00
|
|
|
$this->contentTypes['default'] = array(
|
2014-04-05 19:02:49 +07:00
|
|
|
'rels' => 'application/vnd.openxmlformats-package.relationships+xml',
|
|
|
|
|
'xml' => 'application/xml',
|
|
|
|
|
);
|
2013-12-16 06:40:30 -05:00
|
|
|
|
2014-04-05 19:02:49 +07:00
|
|
|
// Add section media files
|
|
|
|
|
$sectionMedia = Media::getElements('section');
|
|
|
|
|
if (!empty($sectionMedia)) {
|
|
|
|
|
$this->addFilesToPackage($objZip, $sectionMedia);
|
2014-04-18 23:12:51 +07:00
|
|
|
$this->registerContentTypes($sectionMedia);
|
2014-04-05 19:02:49 +07:00
|
|
|
foreach ($sectionMedia as $element) {
|
2014-05-06 01:45:13 +07:00
|
|
|
$this->relationships[] = $element;
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-04 00:29:57 +07:00
|
|
|
// Add header/footer media files & relations
|
|
|
|
|
$this->addHeaderFooterMedia($objZip, 'header');
|
|
|
|
|
$this->addHeaderFooterMedia($objZip, 'footer');
|
2013-12-16 06:40:30 -05:00
|
|
|
|
2014-04-04 00:29:57 +07:00
|
|
|
// Add header/footer contents
|
2014-04-08 16:09:31 +07:00
|
|
|
$rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements
|
2014-04-03 11:34:06 +07:00
|
|
|
$sections = $this->phpWord->getSections();
|
|
|
|
|
foreach ($sections as $section) {
|
2014-04-08 16:09:31 +07:00
|
|
|
$this->addHeaderFooterContent($section, $objZip, 'header', $rId);
|
|
|
|
|
$this->addHeaderFooterContent($section, $objZip, 'footer', $rId);
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
|
|
|
|
|
2014-04-09 21:35:55 +07:00
|
|
|
$this->addNotes($objZip, $rId, 'footnote');
|
|
|
|
|
$this->addNotes($objZip, $rId, 'endnote');
|
2014-03-07 23:08:09 +01:00
|
|
|
|
2014-04-30 08:54:38 +07:00
|
|
|
// Write parts
|
2014-05-06 08:50:55 +07:00
|
|
|
foreach ($this->parts as $partName => $fileName) {
|
|
|
|
|
if ($fileName != '') {
|
|
|
|
|
$objZip->addFromString($fileName, $this->getWriterPart($partName)->write());
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-16 06:40:30 -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-16 06:40:30 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-30 16:31:01 +07:00
|
|
|
$this->cleanupTempFile();
|
2013-12-16 06:40:30 -05:00
|
|
|
} else {
|
2014-03-22 10:06:08 +04:00
|
|
|
throw new Exception("PhpWord object unassigned.");
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 01:45:13 +07:00
|
|
|
/**
|
|
|
|
|
* Get content types
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getContentTypes()
|
|
|
|
|
{
|
|
|
|
|
return $this->contentTypes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get content types
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getRelationships()
|
|
|
|
|
{
|
|
|
|
|
return $this->relationships;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-04 00:29:57 +07:00
|
|
|
/**
|
2014-05-06 08:50:55 +07:00
|
|
|
* Add header/footer media files, e.g. footer1.xml.rels
|
2014-04-05 00:08:00 +07:00
|
|
|
*
|
|
|
|
|
* @param mixed $objZip
|
|
|
|
|
* @param string $docPart
|
2014-04-04 00:29:57 +07:00
|
|
|
*/
|
|
|
|
|
private function addHeaderFooterMedia($objZip, $docPart)
|
|
|
|
|
{
|
2014-04-05 19:02:49 +07:00
|
|
|
$elements = Media::getElements($docPart);
|
2014-04-04 00:29:57 +07:00
|
|
|
if (!empty($elements)) {
|
|
|
|
|
foreach ($elements as $file => $media) {
|
|
|
|
|
if (count($media) > 0) {
|
|
|
|
|
if (!empty($media)) {
|
|
|
|
|
$this->addFilesToPackage($objZip, $media);
|
2014-04-18 23:12:51 +07:00
|
|
|
$this->registerContentTypes($media);
|
2014-04-04 00:29:57 +07:00
|
|
|
}
|
2014-05-06 08:50:55 +07:00
|
|
|
|
|
|
|
|
$writerPart = $this->getWriterPart('relspart')->setMedia($media);
|
|
|
|
|
$objZip->addFromString("word/_rels/{$file}.xml.rels", $writerPart->write());
|
2014-04-04 00:29:57 +07:00
|
|
|
}
|
|
|
|
|
}
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-05 19:02:49 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add header/footer content
|
|
|
|
|
*
|
2014-04-06 15:19:09 +07:00
|
|
|
* @param mixed $objZip
|
2014-05-06 08:50:55 +07:00
|
|
|
* @param string $elmType header|footer
|
2014-04-08 16:09:31 +07:00
|
|
|
* @param integer $rId
|
2014-04-05 19:02:49 +07:00
|
|
|
*/
|
2014-04-08 16:09:31 +07:00
|
|
|
private function addHeaderFooterContent(Section &$section, $objZip, $elmType, &$rId)
|
2014-04-05 19:02:49 +07:00
|
|
|
{
|
|
|
|
|
$getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters';
|
|
|
|
|
$elmCount = ($section->getSectionId() - 1) * 3;
|
2014-05-06 08:50:55 +07:00
|
|
|
$elements = $section->$getFunction();
|
|
|
|
|
foreach ($elements as &$element) {
|
2014-04-05 19:02:49 +07:00
|
|
|
$elmCount++;
|
2014-05-06 08:50:55 +07:00
|
|
|
$element->setRelationId(++$rId);
|
|
|
|
|
$elmFile = "{$elmType}{$elmCount}.xml"; // e.g. footer1.xml
|
2014-05-06 01:45:13 +07:00
|
|
|
$this->contentTypes['override']["/word/$elmFile"] = $elmType;
|
|
|
|
|
$this->relationships[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
|
2014-05-06 08:50:55 +07:00
|
|
|
|
|
|
|
|
$writerPart = $this->getWriterPart($elmType)->setElement($element);
|
|
|
|
|
$objZip->addFromString("word/$elmFile", $writerPart->write());
|
2014-04-05 19:02:49 +07:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-09 21:35:55 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add footnotes/endnotes
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $objZip
|
|
|
|
|
* @param integer $rId
|
2014-05-04 00:57:44 +07:00
|
|
|
* @param string $noteType
|
2014-04-09 21:35:55 +07:00
|
|
|
*/
|
2014-05-04 00:57:44 +07:00
|
|
|
private function addNotes($objZip, &$rId, $noteType = 'footnote')
|
2014-04-09 21:35:55 +07:00
|
|
|
{
|
2014-05-04 00:57:44 +07:00
|
|
|
$noteType = ($noteType == 'endnote') ? 'endnote' : 'footnote';
|
2014-05-06 01:45:13 +07:00
|
|
|
$partName = "{$noteType}s";
|
|
|
|
|
$method = 'get' . $partName;
|
2014-05-04 00:57:44 +07:00
|
|
|
$collection = $this->phpWord->$method();
|
2014-04-09 21:35:55 +07:00
|
|
|
|
|
|
|
|
// Add footnotes media files, relations, and contents
|
2014-05-04 00:57:44 +07:00
|
|
|
if ($collection->countItems() > 0) {
|
|
|
|
|
$media = Media::getElements($noteType);
|
2014-04-09 21:35:55 +07:00
|
|
|
$this->addFilesToPackage($objZip, $media);
|
2014-04-18 23:12:51 +07:00
|
|
|
$this->registerContentTypes($media);
|
2014-05-06 01:45:13 +07:00
|
|
|
$this->contentTypes['override']["/word/{$partName}.xml"] = $partName;
|
|
|
|
|
$this->relationships[] = array('target' => "{$partName}.xml", 'type' => $partName, 'rID' => ++$rId);
|
2014-05-06 08:50:55 +07:00
|
|
|
|
|
|
|
|
// Write relationships file, e.g. word/_rels/footnotes.xml
|
|
|
|
|
if (!empty($media)) {
|
|
|
|
|
$writerPart = $this->getWriterPart('relspart')->setMedia($media);
|
|
|
|
|
$objZip->addFromString("word/_rels/{$partName}.xml.rels", $writerPart->write());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write content file, e.g. word/footnotes.xml
|
|
|
|
|
$writerPart = $this->getWriterPart($partName)->setElements($collection->getItems());
|
|
|
|
|
$objZip->addFromString("word/{$partName}.xml", $writerPart->write());
|
2014-04-09 21:35:55 +07:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-18 23:12:51 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register content types for each media
|
|
|
|
|
*
|
|
|
|
|
* @param array $media
|
|
|
|
|
*/
|
|
|
|
|
private function registerContentTypes($media)
|
|
|
|
|
{
|
|
|
|
|
foreach ($media as $medium) {
|
|
|
|
|
$mediumType = $medium['type'];
|
|
|
|
|
if ($mediumType == 'image') {
|
|
|
|
|
$extension = $medium['imageExtension'];
|
2014-05-06 01:45:13 +07:00
|
|
|
if (!array_key_exists($extension, $this->contentTypes['default'])) {
|
|
|
|
|
$this->contentTypes['default'][$extension] = $medium['imageType'];
|
2014-04-18 23:12:51 +07:00
|
|
|
}
|
|
|
|
|
} elseif ($mediumType == 'object') {
|
2014-05-06 01:45:13 +07:00
|
|
|
if (!array_key_exists('bin', $this->contentTypes['default'])) {
|
|
|
|
|
$this->contentTypes['default']['bin'] = 'application/vnd.openxmlformats-officedocument.oleObject';
|
2014-04-18 23:12:51 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-11 19:26:56 +07:00
|
|
|
}
|