Move OOXML specific feature from Media to Word2007\Base

This commit is contained in:
Ivan Lanin 2014-04-05 00:08:00 +07:00
parent b75403f9a1
commit d7c18fe4b8
16 changed files with 200 additions and 294 deletions

View File

@ -58,24 +58,6 @@ abstract class Container extends Element
*/ */
protected $elements = array(); protected $elements = array();
/**
* Document part type: section|header|footer
*
* Used by textrun and cell to determine where the element is located
* because it will affect the availability of other element, e.g. footnote
* will not be available when $docPart is header or footer.
*
* @var string
*/
protected $docPart = null;
/**
* Document part Id
*
* @var int
*/
protected $docPartId;
/** /**
* Relation Id * Relation Id
* *
@ -101,10 +83,11 @@ abstract class Container extends Element
} }
$text = String::toUTF8($text); $text = String::toUTF8($text);
$element = new Text($text, $fontStyle, $paragraphStyle); $textObject = new Text($text, $fontStyle, $paragraphStyle);
$this->elements[] = $element; $textObject->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->elements[] = $textObject;
return $element; return $textObject;
} }
/** /**
@ -117,15 +100,8 @@ abstract class Container extends Element
{ {
$this->checkValidity('textrun'); $this->checkValidity('textrun');
if ($this->container == 'cell') { $textRun = new TextRun($paragraphStyle);
$docPart = $this->docPart; $textRun->setDocPart($this->getDocPart(), $this->getDocPartId());
$docPartId = $this->docPartId;
} else {
$docPart = $this->container;
$docPartId = $this->containerId;
}
$textRun = new TextRun($paragraphStyle, $docPart, $docPartId);
$this->elements[] = $textRun; $this->elements[] = $textRun;
return $textRun; return $textRun;
@ -145,9 +121,8 @@ abstract class Container extends Element
$this->checkValidity('link'); $this->checkValidity('link');
$elementDocPart = $this->checkElementDocPart(); $elementDocPart = $this->checkElementDocPart();
$linkSrc = String::toUTF8($linkSrc); $link = new Link(String::toUTF8($linkSrc), String::toUTF8($linkName), $fontStyle, $paragraphStyle);
$linkName = String::toUTF8($linkName); $link->setDocPart($this->getDocPart(), $this->getDocPartId());
$link = new Link($linkSrc, $linkName, $fontStyle, $paragraphStyle);
$rID = Media::addMediaElement($elementDocPart, 'link', $linkSrc); $rID = Media::addMediaElement($elementDocPart, 'link', $linkSrc);
$link->setRelationId($rID); $link->setRelationId($rID);
$this->elements[] = $link; $this->elements[] = $link;
@ -167,14 +142,15 @@ abstract class Container extends Element
{ {
$this->checkValidity('title'); $this->checkValidity('title');
$text = String::toUTF8($text);
$styles = Style::getStyles(); $styles = Style::getStyles();
if (array_key_exists('Heading_' . $depth, $styles)) { if (array_key_exists('Heading_' . $depth, $styles)) {
$style = 'Heading' . $depth; $style = 'Heading' . $depth;
} else { } else {
$style = null; $style = null;
} }
$text = String::toUTF8($text);
$title = new Title($text, $depth, $style); $title = new Title($text, $depth, $style);
$title->setDocPart($this->getDocPart(), $this->getDocPartId());
$data = TOC::addTitle($text, $depth); $data = TOC::addTitle($text, $depth);
$anchor = $data[0]; $anchor = $data[0];
$bookmarkId = $data[1]; $bookmarkId = $data[1];
@ -197,11 +173,11 @@ abstract class Container extends Element
{ {
$this->checkValidity('preservetext'); $this->checkValidity('preservetext');
$text = String::toUTF8($text); $preserveText = new PreserveText(String::toUTF8($text), $fontStyle, $paragraphStyle);
$ptext = new PreserveText($text, $fontStyle, $paragraphStyle); $preserveText->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->elements[] = $ptext; $this->elements[] = $preserveText;
return $ptext; return $preserveText;
} }
/** /**
@ -216,7 +192,9 @@ abstract class Container extends Element
$this->checkValidity('textbreak'); $this->checkValidity('textbreak');
for ($i = 1; $i <= $count; $i++) { for ($i = 1; $i <= $count; $i++) {
$this->elements[] = new TextBreak($fontStyle, $paragraphStyle); $textBreak = new TextBreak($fontStyle, $paragraphStyle);
$textBreak->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->elements[] = $textBreak;
} }
} }
@ -234,8 +212,8 @@ abstract class Container extends Element
{ {
$this->checkValidity('listitem'); $this->checkValidity('listitem');
$text = String::toUTF8($text); $listItem = new ListItem(String::toUTF8($text), $depth, $fontStyle, $styleList, $paragraphStyle);
$listItem = new ListItem($text, $depth, $fontStyle, $styleList, $paragraphStyle); $listItem->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->elements[] = $listItem; $this->elements[] = $listItem;
return $listItem; return $listItem;
@ -251,7 +229,7 @@ abstract class Container extends Element
{ {
$this->checkValidity('table'); $this->checkValidity('table');
$table = new Table($this->container, $this->containerId, $style); $table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
$this->elements[] = $table; $this->elements[] = $table;
return $table; return $table;
@ -271,11 +249,10 @@ abstract class Container extends Element
$elementDocPart = $this->checkElementDocPart(); $elementDocPart = $this->checkElementDocPart();
$image = new Image($src, $style, $isWatermark); $image = new Image($src, $style, $isWatermark);
$image->setDocPart($this->getDocPart(), $this->getDocPartId());
if (!is_null($image->getSource())) { if (!is_null($image->getSource())) {
$rID = Media::addMediaElement($elementDocPart, 'image', $src, $image); $rID = Media::addMediaElement($elementDocPart, 'image', $src, $image);
if (is_int($rID)) { $image->setRelationId($rID);
$image->setRelationId($rID);
}
$this->elements[] = $image; $this->elements[] = $image;
return $image; return $image;
} else { } else {
@ -299,6 +276,7 @@ abstract class Container extends Element
$elementDocPart = $this->checkElementDocPart(); $elementDocPart = $this->checkElementDocPart();
$object = new Object($src, $style); $object = new Object($src, $style);
$object->setDocPart($this->getDocPart(), $this->getDocPartId());
if (!is_null($object->getSource())) { if (!is_null($object->getSource())) {
$inf = pathinfo($src); $inf = pathinfo($src);
$ext = $inf['extension']; $ext = $inf['extension'];
@ -306,15 +284,10 @@ abstract class Container extends Element
$ext = substr($ext, 0, -1); $ext = substr($ext, 0, -1);
} }
$icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png"); $icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png");
$rIDimg = Media::addMediaElement($elementDocPart, 'image', $icon, new Image($icon)); $rID = Media::addMediaElement($elementDocPart, 'object', $src);
$data = Media::addMediaElement($elementDocPart, 'object', $src);
$rID = $data[0];
$objectId = $data[1];
$object->setRelationId($rID); $object->setRelationId($rID);
$object->setObjectId($objectId); $rIDimg = Media::addMediaElement($elementDocPart, 'image', $icon, new Image($icon));
if (is_int($rIDimg)) { $object->setImageRelationId($rIDimg);
$object->setImageRelationId($rIDimg);
}
$this->elements[] = $object; $this->elements[] = $object;
return $object; return $object;
} else { } else {
@ -334,6 +307,7 @@ abstract class Container extends Element
$footnote = new FootnoteElement($paragraphStyle); $footnote = new FootnoteElement($paragraphStyle);
$refID = FootnoteCollection::addFootnoteElement($footnote); $refID = FootnoteCollection::addFootnoteElement($footnote);
$footnote->setDocPart($this->getDocPart(), $this->getDocPartId());
$footnote->setRelationId($refID); $footnote->setRelationId($refID);
$this->elements[] = $footnote; $this->elements[] = $footnote;
@ -353,17 +327,17 @@ abstract class Container extends Element
{ {
$this->checkValidity('checkbox'); $this->checkValidity('checkbox');
$name = String::toUTF8($name); $checkBox = new CheckBox(String::toUTF8($name), String::toUTF8($text), $fontStyle, $paragraphStyle);
$text = String::toUTF8($text); $checkBox->setDocPart($this->getDocPart(), $this->getDocPartId());
$element = new CheckBox($name, $text, $fontStyle, $paragraphStyle); $this->elements[] = $checkBox;
$this->elements[] = $element;
return $element; return $checkBox;
} }
/** /**
* Get section number * Get section number
* getFooterCount *
* @return array
*/ */
public function getSectionId() public function getSectionId()
{ {
@ -484,7 +458,7 @@ abstract class Container extends Element
$containers = $rules[0]; $containers = $rules[0];
$allowedDocParts = $rules[1]; $allowedDocParts = $rules[1];
foreach ($containers as $container) { foreach ($containers as $container) {
if ($this->container == $container && !in_array($this->docPart, $allowedDocParts)) { if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) {
throw new \BadMethodCallException(); throw new \BadMethodCallException();
} }
} }
@ -499,8 +473,8 @@ abstract class Container extends Element
private function checkElementDocPart() private function checkElementDocPart()
{ {
$isCellTextrun = in_array($this->container, array('cell', 'textrun')); $isCellTextrun = in_array($this->container, array('cell', 'textrun'));
$docPart = $isCellTextrun ? $this->docPart : $this->container; $docPart = $isCellTextrun ? $this->getDocPart() : $this->container;
$docPartId = $isCellTextrun ? $this->docPartId : $this->containerId; $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->containerId;
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');
return $inHeaderFooter ? $docPart . $docPartId : $docPart; return $inHeaderFooter ? $docPart . $docPartId : $docPart;

View File

@ -23,5 +23,6 @@ class Footer extends Container
{ {
$this->container = 'footer'; $this->container = 'footer';
$this->containerId = $sectionId; $this->containerId = $sectionId;
$this->setDocPart($this->container, $this->containerId);
} }
} }

View File

@ -42,6 +42,7 @@ class Header extends Container
{ {
$this->container = 'header'; $this->container = 'header';
$this->containerId = $sectionId; $this->containerId = $sectionId;
$this->setDocPart($this->container, $this->containerId);
} }
/** /**

View File

@ -52,6 +52,7 @@ class Section extends Container
{ {
$this->container = 'section'; $this->container = 'section';
$this->containerId = $sectionCount; $this->containerId = $sectionCount;
$this->setDocPart($this->container, $this->containerId);
$this->settings = new Settings(); $this->settings = new Settings();
$this->setSettings($settings); $this->setSettings($settings);
} }
@ -168,6 +169,7 @@ class Section extends Container
/** /**
* Create header * Create header
* *
* @return Header
* @deprecated 0.9.2 * @deprecated 0.9.2
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
@ -179,6 +181,7 @@ class Section extends Container
/** /**
* Create footer * Create footer
* *
* @return Footer
* @deprecated 0.9.2 * @deprecated 0.9.2
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */

View File

@ -42,8 +42,7 @@ class Cell extends Container
public function __construct($docPart, $docPartId, $width = null, $style = null) public function __construct($docPart, $docPartId, $width = null, $style = null)
{ {
$this->container = 'cell'; $this->container = 'cell';
$this->docPart = $docPart; $this->setDocPart($docPart, $docPartId);
$this->docPartId = $docPartId;
$this->width = $width; $this->width = $width;
$this->cellStyle = $this->setStyle(new CellStyle(), $style, true); $this->cellStyle = $this->setStyle(new CellStyle(), $style, true);
} }

View File

@ -16,6 +16,24 @@ namespace PhpOffice\PhpWord\Element;
*/ */
abstract class Element abstract class Element
{ {
/**
* Document part type: section|header|footer
*
* Used by textrun and cell container to determine where the element is
* located because it will affect the availability of other element,
* e.g. footnote will not be available when $docPart is header or footer.
*
* @var string
*/
private $docPart = 'section';
/**
* Document part Id
*
* @var integer
*/
private $docPartId = 1;
/** /**
* Set style value * Set style value
* *
@ -39,4 +57,46 @@ abstract class Element
return $style; return $style;
} }
/**
* Set doc part
*
* @param string $docPart
* @param integer $docPartId
*/
public function setDocPart($docPart, $docPartId = 1)
{
$this->docPart = $docPart;
$this->docPartId = $docPartId;
}
/**
* Get doc part
*
* @return string
*/
public function getDocPart()
{
return $this->docPart;
}
/**
* Get doc part Id
*
* @return integer
*/
public function getDocPartId()
{
return $this->docPartId;
}
/**
* Check if element is located in section doc part (as opposed to header/footer)
*
* @return boolean
*/
public function isInSection()
{
return ($this->docPart == 'section');
}
} }

View File

@ -44,13 +44,6 @@ class Object extends Element
*/ */
private $imageRelationId; private $imageRelationId;
/**
* Object ID
*
* @var int
*/
private $objectId;
/** /**
* Create a new Ole-Object Element * Create a new Ole-Object Element
* *
@ -135,19 +128,23 @@ class Object extends Element
* Get Object ID * Get Object ID
* *
* @return int * @return int
* @deprecated 0.9.2
* @codeCoverageIgnore
*/ */
public function getObjectId() public function getObjectId()
{ {
return $this->objectId; return $this->relationId + 1325353440;
} }
/** /**
* Set Object ID * Set Object ID
* *
* @param int $objId * @param int $objId
* @deprecated 0.9.2
* @codeCoverageIgnore
*/ */
public function setObjectId($objId) public function setObjectId($objId)
{ {
$this->objectId = $objId; $this->relationId = $objId;
} }
} }

View File

@ -37,21 +37,6 @@ class Row extends Element
*/ */
private $cells = array(); private $cells = array();
/**
* Table holder
*
* @var string
*/
private $docPart;
/**
* Section/Header/Footer count
*
* @var int
*/
private $docPartId;
/** /**
* Create a new table row * Create a new table row
* *
@ -62,8 +47,7 @@ class Row extends Element
*/ */
public function __construct($docPart, $docPartId, $height = null, $style = null) public function __construct($docPart, $docPartId, $height = null, $style = null)
{ {
$this->docPart = $docPart; $this->setDocPart($docPart, $docPartId);
$this->docPartId = $docPartId;
$this->height = $height; $this->height = $height;
$this->style = $this->setStyle(new RowStyle(), $style, true); $this->style = $this->setStyle(new RowStyle(), $style, true);
} }
@ -76,7 +60,7 @@ class Row extends Element
*/ */
public function addCell($width = null, $style = null) public function addCell($width = null, $style = null)
{ {
$cell = new Cell($this->docPart, $this->docPartId, $width, $style); $cell = new Cell($this->getDocPart(), $this->getDocPartId(), $width, $style);
$this->cells[] = $cell; $this->cells[] = $cell;
return $cell; return $cell;
} }

View File

@ -31,20 +31,6 @@ class Table extends Element
*/ */
private $rows = array(); private $rows = array();
/**
* Table holder
*
* @var string
*/
private $docPart = null;
/**
* Table holder count
*
* @var int
*/
private $docPartId;
/** /**
* Table width * Table width
* *
@ -62,8 +48,7 @@ class Table extends Element
*/ */
public function __construct($docPart, $docPartId, $style = null) public function __construct($docPart, $docPartId, $style = null)
{ {
$this->docPart = $docPart; $this->setDocPart($docPart, $docPartId);
$this->docPartId = $docPartId;
$this->style = $this->setStyle(new TableStyle(), $style); $this->style = $this->setStyle(new TableStyle(), $style);
} }
@ -75,7 +60,7 @@ class Table extends Element
*/ */
public function addRow($height = null, $style = null) public function addRow($height = null, $style = null)
{ {
$row = new Row($this->docPart, $this->docPartId, $height, $style); $row = new Row($this->getDocPart(), $this->getDocPartId(), $height, $style);
$this->rows[] = $row; $this->rows[] = $row;
return $row; return $row;
} }

View File

@ -31,11 +31,9 @@ class TextRun extends Container
* @param string $docPart section|header|footer * @param string $docPart section|header|footer
* @param int $docPartId * @param int $docPartId
*/ */
public function __construct($paragraphStyle = null, $docPart = 'section', $docPartId = 1) public function __construct($paragraphStyle = null)
{ {
$this->container = 'textrun'; $this->container = 'textrun';
$this->docPart = $docPart;
$this->docPartId = $docPartId;
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle); $this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
} }

View File

@ -23,13 +23,6 @@ class Media
*/ */
private static $media = array(); private static $media = array();
/**
* ObjectID Counter
*
* @var integer
*/
private static $objectId = 1325353440;
/** /**
* Add new media element * Add new media element
* *
@ -37,7 +30,7 @@ class Media
* @param string $mediaType image|object|link * @param string $mediaType image|object|link
* @param string $source * @param string $source
* @param Image $image * @param Image $image
* @return integer|array * @return integer
*/ */
public static function addMediaElement($container, $mediaType, $source, Image $image = null) public static function addMediaElement($container, $mediaType, $source, Image $image = null)
{ {
@ -84,18 +77,9 @@ class Media
$mediaData['type'] = $mediaType; $mediaData['type'] = $mediaType;
$mediaData['rID'] = $relId; $mediaData['rID'] = $relId;
self::$media[$container][$mediaId] = $mediaData; self::$media[$container][$mediaId] = $mediaData;
if ($mediaType === 'object') { return $relId;
return array($relId, ++self::$objectId);
} else {
return $relId;
}
} else { } else {
if ($mediaType === 'object') { return self::$media[$container][$mediaId]['rID'];
$relId = self::$media[$container][$mediaId]['rID'];
return array($relId, ++self::$objectId);
} else {
return self::$media[$container][$mediaId]['rID'];
}
} }
} }
@ -246,7 +230,6 @@ class Media
/** /**
* Get Header Media Elements * Get Header Media Elements
* *
* @param string $prefix header|footer
* @return array * @return array
* @deprecated 0.9.2 * @deprecated 0.9.2
* @codeCoverageIgnore * @codeCoverageIgnore

View File

@ -428,8 +428,8 @@ class Word2007 extends Reader implements IReader
* Return item of array * Return item of array
* *
* @param array $array * @param array $array
* @param mixed $key * @param integer $key
* @return mixed|null * @return string
*/ */
private static function arrayItem($array, $key = 0) private static function arrayItem($array, $key = 0)
{ {

View File

@ -250,7 +250,7 @@ class Word2007 extends Writer implements IWriter
* Check content types * Check content types
* *
* @param mixed $objZip * @param mixed $objZip
* @param mixed $element * @param mixed $elements
*/ */
private function addFilesToPackage($objZip, $elements) private function addFilesToPackage($objZip, $elements)
{ {
@ -277,6 +277,9 @@ class Word2007 extends Writer implements IWriter
/** /**
* Add header/footer media elements * Add header/footer media elements
*
* @param mixed $objZip
* @param string $docPart
*/ */
private function addHeaderFooterMedia($objZip, $docPart) private function addHeaderFooterMedia($objZip, $docPart)
{ {

View File

@ -93,7 +93,7 @@ class Base extends WriterPart
*/ */
protected function writeLink(XMLWriter $xmlWriter, Link $link, $withoutP = false) protected function writeLink(XMLWriter $xmlWriter, Link $link, $withoutP = false)
{ {
$rID = $link->getRelationId(); $rID = $link->getRelationId() + ($link->isInSection() ? 6 : 0);
$linkName = $link->getLinkName(); $linkName = $link->getLinkName();
if (is_null($linkName)) { if (is_null($linkName)) {
$linkName = $link->getLinkSrc(); $linkName = $link->getLinkSrc();
@ -417,7 +417,7 @@ class Base extends WriterPart
*/ */
protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = false) protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = false)
{ {
$rId = $image->getRelationId(); $rId = $image->getRelationId() + ($image->isInSection() ? 6 : 0);
$style = $image->getStyle(); $style = $image->getStyle();
$width = $style->getWidth(); $width = $style->getWidth();
@ -550,10 +550,10 @@ class Base extends WriterPart
*/ */
protected function writeObject(XMLWriter $xmlWriter, Object $object, $withoutP = false) protected function writeObject(XMLWriter $xmlWriter, Object $object, $withoutP = false)
{ {
$rIdObject = $object->getRelationId(); $rIdObject = $object->getRelationId() + ($object->isInSection() ? 6 : 0);
$rIdImage = $object->getImageRelationId(); $rIdImage = $object->getImageRelationId() + ($object->isInSection() ? 6 : 0);
$shapeId = md5($rIdObject . '_' . $rIdImage); $shapeId = md5($rIdObject . '_' . $rIdImage);
$objectId = $object->getObjectId(); $objectId = $object->getRelationId() + 1325353440;
$style = $object->getStyle(); $style = $object->getStyle();
$align = $style->getAlign(); $align = $style->getAlign();

View File

@ -26,173 +26,76 @@ class ContentTypes extends WriterPart
*/ */
public function writeContentTypes($imageTypes, $objectTypes, $cHdrs, $footers) public function writeContentTypes($imageTypes, $objectTypes, $cHdrs, $footers)
{ {
// Create XML writer
$xmlWriter = $this->getXmlWriter();
// XML header $OpenXMLPrefix = 'application/vnd.openxmlformats-';
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $WordMLPrefix = $OpenXMLPrefix . 'officedocument.wordprocessingml.';
// Types $defaults = array(
$xmlWriter->startElement('Types'); 'rels' => $OpenXMLPrefix . 'package.relationships+xml',
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); 'xml' => 'application/xml',
// Rels
$this->writeDefaultContentType(
$xmlWriter,
'rels',
'application/vnd.openxmlformats-package.relationships+xml'
); );
if (is_array($imageTypes)) {
// XML $defaults = array_merge($defaults, $imageTypes);
$this->writeDefaultContentType(
$xmlWriter,
'xml',
'application/xml'
);
// Add media content-types
foreach ($imageTypes as $key => $value) {
$this->writeDefaultContentType($xmlWriter, $key, $value);
} }
// Add embedding content-types
if (count($objectTypes) > 0) { if (count($objectTypes) > 0) {
$this->writeDefaultContentType( $defaults['bin'] = $OpenXMLPrefix . 'officedocument.oleObject';
$xmlWriter,
'bin',
'application/vnd.openxmlformats-officedocument.oleObject'
);
} }
$overrides = array(
// DocProps '/docProps/core.xml' => $OpenXMLPrefix . 'package.core-properties+xml',
$this->writeOverrideContentType( '/docProps/app.xml' => $OpenXMLPrefix . 'officedocument.extended-properties+xml',
$xmlWriter, '/word/document.xml' => $WordMLPrefix . 'document.main+xml',
'/docProps/app.xml', '/word/styles.xml' => $WordMLPrefix . 'styles+xml',
'application/vnd.openxmlformats-officedocument.extended-properties+xml' '/word/numbering.xml' => $WordMLPrefix . 'numbering+xml',
'/word/settings.xml' => $WordMLPrefix . 'settings+xml',
'/word/theme/theme1.xml' => $OpenXMLPrefix . 'officedocument.theme+xml',
'/word/webSettings.xml' => $WordMLPrefix . 'webSettings+xml',
'/word/fontTable.xml' => $WordMLPrefix . 'fontTable+xml',
'/word/footnotes.xml' => $WordMLPrefix . 'footnotes+xml',
); );
$this->writeOverrideContentType(
$xmlWriter,
'/docProps/core.xml',
'application/vnd.openxmlformats-package.core-properties+xml'
);
// Document
$this->writeOverrideContentType(
$xmlWriter,
'/word/document.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'
);
// Styles
$this->writeOverrideContentType(
$xmlWriter,
'/word/styles.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml'
);
// Numbering
$this->writeOverrideContentType(
$xmlWriter,
'/word/numbering.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml'
);
// Settings
$this->writeOverrideContentType(
$xmlWriter,
'/word/settings.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml'
);
// Theme1
$this->writeOverrideContentType(
$xmlWriter,
'/word/theme/theme1.xml',
'application/vnd.openxmlformats-officedocument.theme+xml'
);
// WebSettings
$this->writeOverrideContentType(
$xmlWriter,
'/word/webSettings.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml'
);
// Font Table
$this->writeOverrideContentType(
$xmlWriter,
'/word/fontTable.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml'
);
// Footnotes
$this->writeOverrideContentType(
$xmlWriter,
'/word/footnotes.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml'
);
for ($i = 1; $i <= $cHdrs; $i++) { for ($i = 1; $i <= $cHdrs; $i++) {
$this->writeOverrideContentType( $overrides["/word/header{$i}.xml"] = $WordMLPrefix . 'header+xml';
$xmlWriter,
'/word/header' . $i . '.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml'
);
} }
for ($i = 1; $i <= count($footers); $i++) { for ($i = 1; $i <= count($footers); $i++) {
if (!is_null($footers[$i])) { if (!is_null($footers[$i])) {
$this->writeOverrideContentType( $overrides["/word/footer{$i}.xml"] = $WordMLPrefix . 'footer+xml';
$xmlWriter,
'/word/footer' . $i . '.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml'
);
} }
} }
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('Types');
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
foreach ($defaults as $key => $value)
{
$this->writeContentType($xmlWriter, true, $key, $value);
}
foreach ($overrides as $key => $value)
{
$this->writeContentType($xmlWriter, false, $key, $value);
}
$xmlWriter->endElement(); $xmlWriter->endElement();
// Return
return $xmlWriter->getData(); return $xmlWriter->getData();
} }
/** /**
* Write Default XML element * Write content types element
* *
* @param XMLWriter $xmlWriter XML Writer * @param XMLWriter $xmlWriter XML Writer
* @param string $pPartname Part name * @param boolean $isDefault
* @param string $pContentType Content type * @param string $partName Part name
* @param string $contentType Content type
* @throws Exception * @throws Exception
*/ */
private function writeDefaultContentType(XMLWriter $xmlWriter, $pPartname = '', $pContentType = '') private function writeContentType(XMLWriter $xmlWriter, $isDefault, $partName = '', $contentType = '')
{ {
if ($pPartname != '' && $pContentType != '') { if ($partName != '' && $contentType != '') {
// Write content type $element = $isDefault ? 'Default' : 'Override';
$xmlWriter->startElement('Default'); $partAttribute = $isDefault ? 'Extension' : 'PartName';
$xmlWriter->writeAttribute('Extension', $pPartname); $xmlWriter->startElement($element);
$xmlWriter->writeAttribute('ContentType', $pContentType); $xmlWriter->writeAttribute($partAttribute, $partName);
$xmlWriter->endElement(); $xmlWriter->writeAttribute('ContentType', $contentType);
} else {
throw new Exception("Invalid parameters passed.");
}
}
/**
* Write Override XML element
*
* @param XMLWriter $xmlWriter
* @param string $pPartname Part name
* @param string $pContentType Content type
* @throws Exception
*/
private function writeOverrideContentType(XMLWriter $xmlWriter, $pPartname = '', $pContentType = '')
{
if ($pPartname != '' && $pContentType != '') {
// Write content type
$xmlWriter->startElement('Override');
$xmlWriter->writeAttribute('PartName', $pPartname);
$xmlWriter->writeAttribute('ContentType', $pContentType);
$xmlWriter->endElement(); $xmlWriter->endElement();
} else { } else {
throw new Exception("Invalid parameters passed."); throw new Exception("Invalid parameters passed.");
@ -215,4 +118,32 @@ class ContentTypes extends WriterPart
throw new Exception("File $pFile does not exist"); throw new Exception("File $pFile does not exist");
} }
} }
/**
* Write Default XML element
*
* @param XMLWriter $xmlWriter
* @param string $partName Part name
* @param string $contentType Content type
* @deprecated 0.9.2
* @codeCoverageIgnore
*/
private function writeDefaultContentType(XMLWriter $xmlWriter, $partName = '', $contentType = '')
{
$this->writeContentType($xmlWriter, true, $partName, $contentType);
}
/**
* Write Override XML element
*
* @param XMLWriter $xmlWriter
* @param string $partName Part name
* @param string $contentType Content type
* @deprecated 0.9.2
* @codeCoverageIgnore
*/
private function writeOverrideContentType(XMLWriter $xmlWriter, $partName = '', $contentType = '')
{
$this->writeContentType($xmlWriter, false, $partName, $contentType);
}
} }

View File

@ -83,17 +83,4 @@ class ObjectTest extends \PHPUnit_Framework_TestCase
$oObject->setImageRelationId($iVal); $oObject->setImageRelationId($iVal);
$this->assertEquals($oObject->getImageRelationId(), $iVal); $this->assertEquals($oObject->getImageRelationId(), $iVal);
} }
/**
* Set/get object relation Id
*/
public function testObjectId()
{
$src = __DIR__ . "/../_files/documents/sheet.xls";
$oObject = new Object($src);
$iVal = rand(1, 1000);
$oObject->setObjectId($iVal);
$this->assertEquals($oObject->getObjectId(), $iVal);
}
} }