PHPWord/src/PhpWord/Writer/Word2007.php

256 lines
9.3 KiB
PHP
Raw Normal View History

2012-05-06 18:25:40 +02:00
<?php
/**
* 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
* @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
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Exception\Exception;
2014-05-04 21:03:28 +04:00
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\PhpWord;
/**
* Word2007 writer
*/
class Word2007 extends AbstractWriter implements WriterInterface
2013-12-16 06:40:30 -05:00
{
/**
2014-04-05 19:02:49 +07:00
* Content types values
*
* @var array
*/
private $contentTypes = array('default' => array(), 'override' => array());
/**
2014-04-05 19:02:49 +07:00
* Document relationship
*
* @var array
*/
private $relationships = array();
2013-12-16 06:40:30 -05:00
/**
* Create new Word2007 writer
*
* @param \PhpOffice\PhpWord\PhpWord
*/
public function __construct(PhpWord $phpWord = null)
2013-12-16 06:40:30 -05:00
{
// Assign PhpWord
$this->setPhpWord($phpWord);
// Create parts
$parts = array('ContentTypes', 'RelsMain', 'RelsDocument', 'DocPropsApp', 'DocPropsCore', 'Document', 'Styles',
'Numbering', 'Settings', 'WebSettings', 'Header', 'Footer', 'Footnotes',
'Endnotes', 'FontTable', 'Theme');
foreach ($parts as $part) {
$partName = strtolower($part);
$partClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Part\\' . $part;
if (class_exists($partClass)) {
$partObject = new $partClass();
$partObject->setParentWriter($this);
$this->writerParts[$partName] = $partObject;
}
2013-12-16 06:40:30 -05:00
}
// Set package paths
$this->mediaPaths = array('image' => 'word/media/', 'object' => 'word/embeddings/');
2013-12-16 06:40:30 -05:00
}
/**
* Save document by name
*
* @param string $filename
*/
public function save($filename = null)
2013-12-16 06:40:30 -05:00
{
if (!is_null($this->phpWord)) {
$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
$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);
$this->registerContentTypes($sectionMedia);
2014-04-05 19:02:49 +07:00
foreach ($sectionMedia as $element) {
$this->relationships[] = $element;
2013-12-16 06:40:30 -05:00
}
}
// Add header/footer media files & relations
$this->addHeaderFooterMedia($objZip, 'header');
$this->addHeaderFooterMedia($objZip, 'footer');
2013-12-16 06:40:30 -05:00
// Add header/footer contents
$rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements
$sections = $this->phpWord->getSections();
foreach ($sections as $section) {
$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');
// Write parts
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->write());
$objZip->addFromString('_rels/.rels', $this->getWriterPart('relsmain')->write());
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docpropsapp')->write());
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('docpropscore')->write());
$objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('relsdocument')->write());
$objZip->addFromString('word/document.xml', $this->getWriterPart('document')->write());
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->write());
$objZip->addFromString('word/numbering.xml', $this->getWriterPart('numbering')->write());
$objZip->addFromString('word/settings.xml', $this->getWriterPart('settings')->write());
$objZip->addFromString('word/webSettings.xml', $this->getWriterPart('websettings')->write());
$objZip->addFromString('word/fontTable.xml', $this->getWriterPart('fonttable')->write());
$objZip->addFromString('word/theme/theme1.xml', $this->getWriterPart('theme')->write());
2013-12-16 06:40:30 -05:00
// Close file
if ($objZip->close() === false) {
throw new Exception("Could not close zip file $filename.");
2013-12-16 06:40:30 -05:00
}
$this->cleanupTempFile();
2013-12-16 06:40:30 -05:00
} else {
throw new Exception("PhpWord object unassigned.");
2013-12-16 06:40:30 -05: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-05 19:02:49 +07:00
* Add header/footer media files
*
* @param mixed $objZip
* @param string $docPart
*/
private function addHeaderFooterMedia($objZip, $docPart)
{
2014-04-05 19:02:49 +07:00
$elements = Media::getElements($docPart);
if (!empty($elements)) {
foreach ($elements as $file => $media) {
if (count($media) > 0) {
if (!empty($media)) {
$this->addFilesToPackage($objZip, $media);
$this->registerContentTypes($media);
}
2014-04-09 21:35:55 +07:00
$relsFile = "word/_rels/{$file}.xml.rels";
$objZip->addFromString($relsFile, $this->getWriterPart('rels')->writeMediaRels($media));
}
}
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-04-05 19:02:49 +07:00
* @param string $elmType
* @param integer $rId
2014-04-05 19:02:49 +07:00
*/
private function addHeaderFooterContent(Section &$section, $objZip, $elmType, &$rId)
2014-04-05 19:02:49 +07:00
{
$getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters';
$writeFunction = $elmType == 'header' ? 'writeHeader' : 'writeFooter';
$elmCount = ($section->getSectionId() - 1) * 3;
$elmObjects = $section->$getFunction();
foreach ($elmObjects as &$elmObject) {
2014-04-05 19:02:49 +07:00
$elmCount++;
$elmObject->setRelationId(++$rId);
2014-04-05 19:02:49 +07:00
$elmFile = "{$elmType}{$elmCount}.xml";
$objZip->addFromString("word/$elmFile", $this->getWriterPart($elmType)->$writeFunction($elmObject));
$this->contentTypes['override']["/word/$elmFile"] = $elmType;
$this->relationships[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
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
* @param string $noteType
2014-04-09 21:35:55 +07:00
*/
private function addNotes($objZip, &$rId, $noteType = 'footnote')
2014-04-09 21:35:55 +07:00
{
$noteType = ($noteType == 'endnote') ? 'endnote' : 'footnote';
$partName = "{$noteType}s";
$method = 'get' . $partName;
$collection = $this->phpWord->$method();
2014-04-09 21:35:55 +07:00
// Add footnotes media files, relations, and contents
if ($collection->countItems() > 0) {
$media = Media::getElements($noteType);
2014-04-09 21:35:55 +07:00
$this->addFilesToPackage($objZip, $media);
$this->registerContentTypes($media);
2014-04-09 21:35:55 +07:00
if (!empty($media)) {
$objZip->addFromString("word/_rels/{$partName}.xml.rels", $this->getWriterPart('rels')->writeMediaRels($media));
2014-04-09 21:35:55 +07:00
}
$elements = $collection->getItems();
$objZip->addFromString("word/{$partName}.xml", $this->getWriterPart($partName)->write($elements));
$this->contentTypes['override']["/word/{$partName}.xml"] = $partName;
$this->relationships[] = array('target' => "{$partName}.xml", 'type' => $partName, 'rID' => ++$rId);
2014-04-09 21:35:55 +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'];
if (!array_key_exists($extension, $this->contentTypes['default'])) {
$this->contentTypes['default'][$extension] = $medium['imageType'];
}
} elseif ($mediumType == 'object') {
if (!array_key_exists('bin', $this->contentTypes['default'])) {
$this->contentTypes['default']['bin'] = 'application/vnd.openxmlformats-officedocument.oleObject';
}
}
}
}
}