Remove some duplication on Word2007 writers

This commit is contained in:
Ivan Lanin 2014-04-03 06:54:45 +07:00
parent 237625d22a
commit 04e6dbc86a
7 changed files with 247 additions and 509 deletions

View File

@ -9,7 +9,9 @@
namespace PhpOffice\PhpWord\Writer\Word2007; namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Container\Container;
use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Link; use PhpOffice\PhpWord\Element\Link;
@ -75,27 +77,10 @@ class Base extends WriterPart
*/ */
protected function writeTextRun(XMLWriter $xmlWriter, TextRun $textrun) protected function writeTextRun(XMLWriter $xmlWriter, TextRun $textrun)
{ {
$elements = $textrun->getElements();
$styleParagraph = $textrun->getParagraphStyle(); $styleParagraph = $textrun->getParagraphStyle();
$xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
$this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph);
if (count($elements) > 0) { $this->writeContainerElements($xmlWriter, $textrun);
foreach ($elements as $element) {
if ($element instanceof Text) {
$this->writeText($xmlWriter, $element, true);
} elseif ($element instanceof Link) {
$this->writeLink($xmlWriter, $element, true);
} elseif ($element instanceof TextBreak) {
$xmlWriter->writeElement('w:br');
} elseif ($element instanceof Image) {
$this->writeImage($xmlWriter, $element, true);
} elseif ($element instanceof Object) {
$this->writeObject($xmlWriter, $element, true);
} elseif ($element instanceof Footnote) {
$this->writeFootnote($xmlWriter, $element, true);
}
}
}
$xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
} }
@ -254,28 +239,31 @@ class Base extends WriterPart
* @param XMLWriter $xmlWriter * @param XMLWriter $xmlWriter
* @param TextBreak $element * @param TextBreak $element
*/ */
protected function writeTextBreak($xmlWriter, TextBreak $element = null) protected function writeTextBreak(XMLWriter $xmlWriter, TextBreak $element = null, $withoutP = false)
{ {
$hasStyle = false; if (!$withoutP) {
$styleFont = null; $hasStyle = false;
$styleParagraph = null; $styleFont = null;
if (!is_null($element)) { $styleParagraph = null;
$styleFont = $element->getFontStyle(); if (!is_null($element)) {
$styleParagraph = $element->getParagraphStyle(); $styleFont = $element->getFontStyle();
$hasStyle = !is_null($styleFont) || !is_null($styleParagraph); $styleParagraph = $element->getParagraphStyle();
} $hasStyle = !is_null($styleFont) || !is_null($styleParagraph);
if ($hasStyle) { }
$xmlWriter->startElement('w:p'); if ($hasStyle) {
$this->writeInlineParagraphStyle($xmlWriter, $styleParagraph); $xmlWriter->startElement('w:p');
if (!is_null($styleFont)) { $this->writeInlineParagraphStyle($xmlWriter, $styleParagraph);
$xmlWriter->startElement('w:pPr'); if (!is_null($styleFont)) {
$this->writeInlineFontStyle($xmlWriter, $styleFont); $xmlWriter->startElement('w:pPr');
$xmlWriter->endElement(); // w:pPr $this->writeInlineFontStyle($xmlWriter, $styleFont);
$xmlWriter->endElement(); // w:pPr
}
$xmlWriter->endElement(); // w:p
} else {
$xmlWriter->writeElement('w:p');
} }
$xmlWriter->endElement(); // w:p
} else { } else {
// Null element. No paragraph nor font style $xmlWriter->writeElement('w:br');
$xmlWriter->writeElement('w:p', null);
} }
} }
@ -316,16 +304,16 @@ class Base extends WriterPart
*/ */
protected function writeTable(XMLWriter $xmlWriter, Table $table) protected function writeTable(XMLWriter $xmlWriter, Table $table)
{ {
$_rows = $table->getRows(); $rows = $table->getRows();
$_cRows = count($_rows); $cRows = count($rows);
if ($_cRows > 0) { if ($cRows > 0) {
$xmlWriter->startElement('w:tbl'); $xmlWriter->startElement('w:tbl');
// Table grid // Table grid
$cellWidths = array(); $cellWidths = array();
for ($i = 0; $i < $_cRows; $i++) { for ($i = 0; $i < $cRows; $i++) {
$row = $_rows[$i]; $row = $rows[$i];
$cells = $row->getCells(); $cells = $row->getCells();
if (count($cells) <= count($cellWidths)) { if (count($cells) <= count($cellWidths)) {
continue; continue;
@ -368,8 +356,8 @@ class Base extends WriterPart
} }
// Table rows // Table rows
for ($i = 0; $i < $_cRows; $i++) { for ($i = 0; $i < $cRows; $i++) {
$row = $_rows[$i]; $row = $rows[$i];
$height = $row->getHeight(); $height = $row->getHeight();
$rowStyle = $row->getStyle(); $rowStyle = $row->getStyle();
$tblHeader = $rowStyle->getTblHeader(); $tblHeader = $rowStyle->getTblHeader();
@ -377,7 +365,6 @@ class Base extends WriterPart
$exactHeight = $rowStyle->getExactHeight(); $exactHeight = $rowStyle->getExactHeight();
$xmlWriter->startElement('w:tr'); $xmlWriter->startElement('w:tr');
if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) { if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) {
$xmlWriter->startElement('w:trPr'); $xmlWriter->startElement('w:trPr');
if (!is_null($height)) { if (!is_null($height)) {
@ -398,55 +385,23 @@ class Base extends WriterPart
} }
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
foreach ($row->getCells() as $cell) { foreach ($row->getCells() as $cell) {
$xmlWriter->startElement('w:tc');
$cellStyle = $cell->getStyle(); $cellStyle = $cell->getStyle();
$width = $cell->getWidth(); $width = $cell->getWidth();
$xmlWriter->startElement('w:tc');
$xmlWriter->startElement('w:tcPr'); $xmlWriter->startElement('w:tcPr');
$xmlWriter->startElement('w:tcW'); $xmlWriter->startElement('w:tcW');
$xmlWriter->writeAttribute('w:w', $width); $xmlWriter->writeAttribute('w:w', $width);
$xmlWriter->writeAttribute('w:type', 'dxa'); $xmlWriter->writeAttribute('w:type', 'dxa');
$xmlWriter->endElement(); $xmlWriter->endElement(); // w:tcW
if ($cellStyle instanceof Cell) { if ($cellStyle instanceof Cell) {
$this->writeCellStyle($xmlWriter, $cellStyle); $this->writeCellStyle($xmlWriter, $cellStyle);
} }
$xmlWriter->endElement(); // w:tcPr
$xmlWriter->endElement(); $this->writeContainerElements($xmlWriter, $cell);
$xmlWriter->endElement(); // w:tc
$_elements = $cell->getElements();
if (count($_elements) > 0) {
foreach ($_elements as $element) {
if ($element instanceof Text) {
$this->writeText($xmlWriter, $element);
} elseif ($element instanceof TextRun) {
$this->writeTextRun($xmlWriter, $element);
} elseif ($element instanceof Link) {
$this->writeLink($xmlWriter, $element);
} elseif ($element instanceof PreserveText) {
$this->writePreserveText($xmlWriter, $element);
} elseif ($element instanceof TextBreak) {
$this->writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof ListItem) {
$this->writeListItem($xmlWriter, $element);
} elseif ($element instanceof Image) {
$this->writeImage($xmlWriter, $element);
} elseif ($element instanceof Object) {
$this->writeObject($xmlWriter, $element);
} elseif ($element instanceof CheckBox) {
$this->writeCheckBox($xmlWriter, $element);
}
}
} else {
$this->writeTextBreak($xmlWriter);
}
$xmlWriter->endElement();
} }
$xmlWriter->endElement(); $xmlWriter->endElement(); // w:tr
} }
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
@ -953,98 +908,35 @@ class Base extends WriterPart
{ {
$bgColor = $style->getBgColor(); $bgColor = $style->getBgColor();
$brdCol = $style->getBorderColor(); $brdCol = $style->getBorderColor();
$brdSz = $style->getBorderSize(); $brdSz = $style->getBorderSize();
$bTop = (!is_null($brdSz[0])) ? true : false;
$bLeft = (!is_null($brdSz[1])) ? true : false;
$bRight = (!is_null($brdSz[2])) ? true : false;
$bBottom = (!is_null($brdSz[3])) ? true : false;
$bInsH = (!is_null($brdSz[4])) ? true : false;
$bInsV = (!is_null($brdSz[5])) ? true : false;
$borders = ($bTop || $bLeft || $bRight || $bBottom || $bInsH || $bInsV) ? true : false;
$cellMargin = $style->getCellMargin(); $cellMargin = $style->getCellMargin();
$mTop = (!is_null($cellMargin[0])) ? true : false;
$mLeft = (!is_null($cellMargin[1])) ? true : false;
$mRight = (!is_null($cellMargin[2])) ? true : false;
$mBottom = (!is_null($cellMargin[3])) ? true : false;
$margins = ($mTop || $mLeft || $mRight || $mBottom) ? true : false;
if ($margins || $borders) { // If any of the borders/margins is set, process them
$xmlWriter->startElement('w:tblPr'); $hasBorders = false;
if ($margins) { for ($i = 0; $i < 6; $i++) {
$xmlWriter->startElement('w:tblCellMar'); if (!is_null($brdSz[$i])) {
if ($mTop) { $hasBorders = true;
$xmlWriter->startElement('w:top'); break;
$xmlWriter->writeAttribute('w:w', $cellMargin[0]);
$xmlWriter->writeAttribute('w:type', 'dxa');
$xmlWriter->endElement();
}
if ($mLeft) {
$xmlWriter->startElement('w:left');
$xmlWriter->writeAttribute('w:w', $cellMargin[1]);
$xmlWriter->writeAttribute('w:type', 'dxa');
$xmlWriter->endElement();
}
if ($mRight) {
$xmlWriter->startElement('w:right');
$xmlWriter->writeAttribute('w:w', $cellMargin[2]);
$xmlWriter->writeAttribute('w:type', 'dxa');
$xmlWriter->endElement();
}
if ($mBottom) {
$xmlWriter->startElement('w:bottom');
$xmlWriter->writeAttribute('w:w', $cellMargin[3]);
$xmlWriter->writeAttribute('w:type', 'dxa');
$xmlWriter->endElement();
}
$xmlWriter->endElement();
} }
if ($borders) { }
$hasMargins = false;
for ($i = 0; $i < 4; $i++) {
if (!is_null($cellMargin[$i])) {
$hasMargins = true;
break;
}
}
if ($hasMargins || $hasBorders) {
$xmlWriter->startElement('w:tblPr');
if ($hasMargins) {
$xmlWriter->startElement('w:tblCellMar');
$this->writeMarginBorder($xmlWriter, $cellMargin);
$xmlWriter->endElement(); // w:tblCellMar
}
if ($hasBorders) {
$xmlWriter->startElement('w:tblBorders'); $xmlWriter->startElement('w:tblBorders');
if ($bTop) { $this->writeMarginBorder($xmlWriter, $brdSz, $brdCol);
$xmlWriter->startElement('w:top'); $xmlWriter->endElement(); // w:tblBorders
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[0]);
$xmlWriter->writeAttribute('w:color', $brdCol[0]);
$xmlWriter->endElement();
}
if ($bLeft) {
$xmlWriter->startElement('w:left');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[1]);
$xmlWriter->writeAttribute('w:color', $brdCol[1]);
$xmlWriter->endElement();
}
if ($bRight) {
$xmlWriter->startElement('w:right');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[2]);
$xmlWriter->writeAttribute('w:color', $brdCol[2]);
$xmlWriter->endElement();
}
if ($bBottom) {
$xmlWriter->startElement('w:bottom');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[3]);
$xmlWriter->writeAttribute('w:color', $brdCol[3]);
$xmlWriter->endElement();
}
if ($bInsH) {
$xmlWriter->startElement('w:insideH');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[4]);
$xmlWriter->writeAttribute('w:color', $brdCol[4]);
$xmlWriter->endElement();
}
if ($bInsV) {
$xmlWriter->startElement('w:insideV');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[5]);
$xmlWriter->writeAttribute('w:color', $brdCol[5]);
$xmlWriter->endElement();
}
$xmlWriter->endElement();
} }
$xmlWriter->endElement(); // w:tblPr $xmlWriter->endElement(); // w:tblPr
} }
@ -1077,61 +969,37 @@ class Base extends WriterPart
*/ */
protected function writeRowStyle(XMLWriter $xmlWriter, $type, TableStyle $style) protected function writeRowStyle(XMLWriter $xmlWriter, $type, TableStyle $style)
{ {
$brdSz = $style->getBorderSize();
$brdCol = $style->getBorderColor();
$bgColor = $style->getBgColor(); $bgColor = $style->getBgColor();
$bTop = (!is_null($brdSz[0])) ? true : false;
$bLeft = (!is_null($brdSz[1])) ? true : false;
$bRight = (!is_null($brdSz[2])) ? true : false;
$bBottom = (!is_null($brdSz[3])) ? true : false;
$xmlWriter->startElement('w:tblStylePr'); $xmlWriter->startElement('w:tblStylePr');
$xmlWriter->writeAttribute('w:type', $type); $xmlWriter->writeAttribute('w:type', $type);
$xmlWriter->startElement('w:tcPr'); $xmlWriter->startElement('w:tcPr');
if (!is_null($bgColor)) { if (!is_null($bgColor)) {
$xmlWriter->startElement('w:shd'); $xmlWriter->startElement('w:shd');
$xmlWriter->writeAttribute('w:val', 'clear'); $xmlWriter->writeAttribute('w:val', 'clear');
$xmlWriter->writeAttribute('w:color', 'auto'); $xmlWriter->writeAttribute('w:color', 'auto');
$xmlWriter->writeAttribute('w:fill', $bgColor); $xmlWriter->writeAttribute('w:fill', $bgColor);
$xmlWriter->endElement(); $xmlWriter->endElement(); // w:shd
} }
$xmlWriter->startElement('w:tcBorders'); // Borders
if ($bTop) { $brdSz = $style->getBorderSize();
$xmlWriter->startElement('w:top'); $brdCol = $style->getBorderColor();
$xmlWriter->writeAttribute('w:val', 'single'); $hasBorders = false;
$xmlWriter->writeAttribute('w:sz', $brdSz[0]); for ($i = 0; $i < 6; $i++) {
$xmlWriter->writeAttribute('w:color', $brdCol[0]); if (!is_null($brdSz[$i])) {
$xmlWriter->endElement(); $hasBorders = true;
break;
}
} }
if ($bLeft) { if ($hasBorders) {
$xmlWriter->startElement('w:left'); $xmlWriter->startElement('w:tcBorders');
$xmlWriter->writeAttribute('w:val', 'single'); $this->writeMarginBorder($xmlWriter, $brdSz, $brdCol);
$xmlWriter->writeAttribute('w:sz', $brdSz[1]); $xmlWriter->endElement(); // w:tcBorders
$xmlWriter->writeAttribute('w:color', $brdCol[1]);
$xmlWriter->endElement();
} }
if ($bRight) {
$xmlWriter->startElement('w:right');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[2]);
$xmlWriter->writeAttribute('w:color', $brdCol[2]);
$xmlWriter->endElement();
}
if ($bBottom) {
$xmlWriter->startElement('w:bottom');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[3]);
$xmlWriter->writeAttribute('w:color', $brdCol[3]);
$xmlWriter->endElement();
}
$xmlWriter->endElement();
$xmlWriter->endElement(); $xmlWriter->endElement(); // w:tcPr
$xmlWriter->endElement(); // w:tblStylePr
$xmlWriter->endElement();
} }
/** /**
@ -1147,14 +1015,15 @@ class Base extends WriterPart
$textDir = $style->getTextDirection(); $textDir = $style->getTextDirection();
$brdSz = $style->getBorderSize(); $brdSz = $style->getBorderSize();
$brdCol = $style->getBorderColor(); $brdCol = $style->getBorderColor();
$hasBorders = false;
for ($i = 0; $i < 4; $i++) {
if (!is_null($brdSz[$i])) {
$hasBorders = true;
break;
}
}
$bTop = (!is_null($brdSz[0])) ? true : false; $styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $hasBorders) ? true : false;
$bLeft = (!is_null($brdSz[1])) ? true : false;
$bRight = (!is_null($brdSz[2])) ? true : false;
$bBottom = (!is_null($brdSz[3])) ? true : false;
$borders = ($bTop || $bLeft || $bRight || $bBottom) ? true : false;
$styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $borders) ? true : false;
if ($styles) { if ($styles) {
if (!is_null($textDir)) { if (!is_null($textDir)) {
@ -1177,54 +1046,11 @@ class Base extends WriterPart
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
if ($borders) { if ($hasBorders) {
$_defaultColor = $style->getDefaultBorderColor(); $defaultColor = $style->getDefaultBorderColor();
$xmlWriter->startElement('w:tcBorders'); $xmlWriter->startElement('w:tcBorders');
if ($bTop) { $this->writeMarginBorder($xmlWriter, $brdSz, $brdCol, array('defaultColor' => $defaultColor));
if (is_null($brdCol[0])) {
$brdCol[0] = $_defaultColor;
}
$xmlWriter->startElement('w:top');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[0]);
$xmlWriter->writeAttribute('w:color', $brdCol[0]);
$xmlWriter->endElement();
}
if ($bLeft) {
if (is_null($brdCol[1])) {
$brdCol[1] = $_defaultColor;
}
$xmlWriter->startElement('w:left');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[1]);
$xmlWriter->writeAttribute('w:color', $brdCol[1]);
$xmlWriter->endElement();
}
if ($bRight) {
if (is_null($brdCol[2])) {
$brdCol[2] = $_defaultColor;
}
$xmlWriter->startElement('w:right');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[2]);
$xmlWriter->writeAttribute('w:color', $brdCol[2]);
$xmlWriter->endElement();
}
if ($bBottom) {
if (is_null($brdCol[3])) {
$brdCol[3] = $_defaultColor;
}
$xmlWriter->startElement('w:bottom');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $brdSz[3]);
$xmlWriter->writeAttribute('w:color', $brdCol[3]);
$xmlWriter->endElement();
}
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
} }
@ -1337,4 +1163,94 @@ class Base extends WriterPart
} }
} }
} }
/**
* Write container elements
*
* @param XMLWriter $xmlWriter
* @param Container $container
* @param Container $textBreak Add text break when no element found
*/
protected function writeContainerElements(XMLWriter $xmlWriter, Container $container)
{
$allowedElements = array(
'Section' => array('Text', 'TextRun', 'Link', 'Title', 'TextBreak', 'ListItem', 'Table', 'Image', 'Object', 'CheckBox', 'Footnote', 'TOC'),
'Header' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'CheckBox'),
'Footer' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'CheckBox'),
'Cell' => array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'Image', 'Object', 'CheckBox', 'Footnote'),
'TextRun' => array('Text', 'Link', 'TextBreak', 'Image', 'Object', 'Footnote'),
'Footnote' => array('Text', 'Link', 'TextBreak', 'Image', 'Object'),
);
$containerName = get_class($container);
$containerName = substr($containerName, strrpos($containerName, '\\') + 1);
if (array_key_exists($containerName, $allowedElements)) {
$containerElements = $allowedElements[$containerName];
} else {
throw new Exception('Invalid container.');
}
$elements = $container->getElements();
if (count($elements) > 0) {
foreach ($elements as $element) {
$elmName = get_class($element);
$elmName = substr($elmName, strrpos($elmName, '\\') + 1);
if (in_array($elmName, $containerElements)) {
$method = "write{$elmName}";
// Image on Header could be watermark
if ($containerName == 'Header' && $elmName == 'Image') {
if ($element->getIsWatermark()) {
$method = "writeWatermark";
}
}
if (in_array($containerName, array('TextRun', 'Footnote'))) {
$this->$method($xmlWriter, $element, true);
} else {
$this->$method($xmlWriter, $element);
}
}
}
} else {
if ($containerName == 'Cell') {
$this->writeTextBreak($xmlWriter);
}
}
}
/**
* Write margin or border
*
* @param XMLWriter $xmlWriter
* @param boolean $isBorder
* @param array $sizes
* @param array $colors
*/
protected function writeMarginBorder(XMLWriter $xmlWriter, $sizes, $colors = array(), $attributes = array())
{
$sides = array('top', 'left', 'right', 'bottom', 'insideH', 'insideV');
$sizeCount = count($sizes) - 1;
for ($i = 0; $i < $sizeCount; $i++) {
if (!is_null($sizes[$i])) {
$xmlWriter->startElement('w:' . $sides[$i]);
if (!empty($colors)) {
if (is_null($colors[$i]) && !empty($attributes)) {
if (array_key_exists('defaultColor', $attributes))
$colors[$i] = $attributes['defaultColor'];
}
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $sizes[$i]);
$xmlWriter->writeAttribute('w:color', $colors[$i]);
if (!empty($attributes)) {
if (array_key_exists('space', $attributes)) {
$xmlWriter->writeAttribute('w:space', '24');
}
}
} else {
$xmlWriter->writeAttribute('w:w', $sizes[$i]);
$xmlWriter->writeAttribute('w:type', 'dxa');
}
$xmlWriter->endElement();
}
}
}
} }

View File

@ -12,18 +12,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\TOC; use PhpOffice\PhpWord\TOC;
use PhpOffice\PhpWord\Container\Section; use PhpOffice\PhpWord\Container\Section;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Link;
use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\PageBreak; use PhpOffice\PhpWord\Element\PageBreak;
use PhpOffice\PhpWord\Element\ListItem;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\Object;
use PhpOffice\PhpWord\Element\Footnote;
use PhpOffice\PhpWord\Element\CheckBox;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Paragraph;
@ -61,44 +50,15 @@ class Document extends Base
$xmlWriter->startElement('w:body'); $xmlWriter->startElement('w:body');
$_sections = $phpWord->getSections(); $sections = $phpWord->getSections();
$countSections = count($_sections); $countSections = count($sections);
$pSection = 0; $pSection = 0;
if ($countSections > 0) { if ($countSections > 0) {
foreach ($_sections as $section) { foreach ($sections as $section) {
$pSection++; $pSection++;
$_elements = $section->getElements(); $this->writeContainerElements($xmlWriter, $section);
foreach ($_elements as $element) {
if ($element instanceof Text) {
$this->writeText($xmlWriter, $element);
} elseif ($element instanceof TextRun) {
$this->writeTextRun($xmlWriter, $element);
} elseif ($element instanceof Link) {
$this->writeLink($xmlWriter, $element);
} elseif ($element instanceof Title) {
$this->writeTitle($xmlWriter, $element);
} elseif ($element instanceof TextBreak) {
$this->writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof PageBreak) {
$this->writePageBreak($xmlWriter);
} elseif ($element instanceof ListItem) {
$this->writeListItem($xmlWriter, $element);
} elseif ($element instanceof Table) {
$this->writeTable($xmlWriter, $element);
} elseif ($element instanceof Image) {
$this->writeImage($xmlWriter, $element);
} elseif ($element instanceof Object) {
$this->writeObject($xmlWriter, $element);
} elseif ($element instanceof TOC) {
$this->writeTOC($xmlWriter);
} elseif ($element instanceof Footnote) {
$this->writeFootnote($xmlWriter, $element);
} elseif ($element instanceof CheckBox) {
$this->writeCheckBox($xmlWriter, $element);
}
}
if ($pSection == $countSections) { if ($pSection == $countSections) {
$this->writeEndSection($xmlWriter, $section); $this->writeEndSection($xmlWriter, $section);
@ -139,8 +99,8 @@ class Document extends Base
private function writeEndSection(XMLWriter $xmlWriter, Section $section) private function writeEndSection(XMLWriter $xmlWriter, Section $section)
{ {
$settings = $section->getSettings(); $settings = $section->getSettings();
$_headers = $section->getHeaders(); $headers = $section->getHeaders();
$_footer = $section->getFooter(); $footer = $section->getFooter();
$pgSzW = $settings->getPageSizeW(); $pgSzW = $settings->getPageSizeW();
$pgSzH = $settings->getPageSizeH(); $pgSzH = $settings->getPageSizeH();
$orientation = $settings->getOrientation(); $orientation = $settings->getOrientation();
@ -161,43 +121,45 @@ class Document extends Base
$xmlWriter->startElement('w:sectPr'); $xmlWriter->startElement('w:sectPr');
foreach ($_headers as &$_header) { // Section break
$rId = $_header->getRelationId();
$xmlWriter->startElement('w:headerReference');
$xmlWriter->writeAttribute('w:type', $_header->getType());
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$xmlWriter->endElement();
}
if ($section->hasDifferentFirstPage()) {
$xmlWriter->startElement('w:titlePg');
$xmlWriter->endElement();
}
if (!is_null($breakType)) { if (!is_null($breakType)) {
$xmlWriter->startElement('w:type'); $xmlWriter->startElement('w:type');
$xmlWriter->writeAttribute('w:val', $breakType); $xmlWriter->writeAttribute('w:val', $breakType);
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
if (!is_null($_footer)) { // Header reference
$rId = $_footer->getRelationId(); foreach ($headers as &$header) {
$rId = $header->getRelationId();
$xmlWriter->startElement('w:headerReference');
$xmlWriter->writeAttribute('w:type', $header->getType());
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$xmlWriter->endElement();
}
if ($section->hasDifferentFirstPage()) {
$xmlWriter->startElement('w:titlePg');
$xmlWriter->endElement();
}
// Footer reference
if (!is_null($footer)) {
$rId = $footer->getRelationId();
$xmlWriter->startElement('w:footerReference'); $xmlWriter->startElement('w:footerReference');
$xmlWriter->writeAttribute('w:type', 'default'); $xmlWriter->writeAttribute('w:type', 'default');
$xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
// Page size & orientation
$xmlWriter->startElement('w:pgSz'); $xmlWriter->startElement('w:pgSz');
$xmlWriter->writeAttribute('w:w', $pgSzW); $xmlWriter->writeAttribute('w:w', $pgSzW);
$xmlWriter->writeAttribute('w:h', $pgSzH); $xmlWriter->writeAttribute('w:h', $pgSzH);
if (!is_null($orientation) && strtolower($orientation) != 'portrait') { if (!is_null($orientation) && strtolower($orientation) != 'portrait') {
$xmlWriter->writeAttribute('w:orient', $orientation); $xmlWriter->writeAttribute('w:orient', $orientation);
} }
$xmlWriter->endElement(); // w:pgSz
$xmlWriter->endElement(); // Margins
$xmlWriter->startElement('w:pgMar'); $xmlWriter->startElement('w:pgMar');
$xmlWriter->writeAttribute('w:top', $marginTop); $xmlWriter->writeAttribute('w:top', $marginTop);
$xmlWriter->writeAttribute('w:right', $marginRight); $xmlWriter->writeAttribute('w:right', $marginRight);
@ -208,48 +170,19 @@ class Document extends Base
$xmlWriter->writeAttribute('w:gutter', '0'); $xmlWriter->writeAttribute('w:gutter', '0');
$xmlWriter->endElement(); $xmlWriter->endElement();
// Borders
if (!is_null($borders[0]) || !is_null($borders[1]) || !is_null($borders[2]) || !is_null($borders[3])) { $hasBorders = false;
for ($i = 0; $i < 4; $i++) {
if (!is_null($borders[$i])) {
$hasBorders = true;
break;
}
}
if ($hasBorders) {
$borderColor = $settings->getBorderColor(); $borderColor = $settings->getBorderColor();
$xmlWriter->startElement('w:pgBorders'); $xmlWriter->startElement('w:pgBorders');
$xmlWriter->writeAttribute('w:offsetFrom', 'page'); $xmlWriter->writeAttribute('w:offsetFrom', 'page');
$this->writeMarginBorder($xmlWriter, $borders, $borderColor, array('space' => '24'));
if (!is_null($borders[0])) {
$xmlWriter->startElement('w:top');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $borders[0]);
$xmlWriter->writeAttribute('w:space', '24');
$xmlWriter->writeAttribute('w:color', $borderColor[0]);
$xmlWriter->endElement();
}
if (!is_null($borders[1])) {
$xmlWriter->startElement('w:left');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $borders[1]);
$xmlWriter->writeAttribute('w:space', '24');
$xmlWriter->writeAttribute('w:color', $borderColor[1]);
$xmlWriter->endElement();
}
if (!is_null($borders[2])) {
$xmlWriter->startElement('w:right');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $borders[2]);
$xmlWriter->writeAttribute('w:space', '24');
$xmlWriter->writeAttribute('w:color', $borderColor[2]);
$xmlWriter->endElement();
}
if (!is_null($borders[3])) {
$xmlWriter->startElement('w:bottom');
$xmlWriter->writeAttribute('w:val', 'single');
$xmlWriter->writeAttribute('w:sz', $borders[3]);
$xmlWriter->writeAttribute('w:space', '24');
$xmlWriter->writeAttribute('w:color', $borderColor[3]);
$xmlWriter->endElement();
}
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
@ -260,12 +193,12 @@ class Document extends Base
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
// Columns
$xmlWriter->startElement('w:cols'); $xmlWriter->startElement('w:cols');
$xmlWriter->writeAttribute('w:num', $colsNum); $xmlWriter->writeAttribute('w:num', $colsNum);
$xmlWriter->writeAttribute('w:space', $colsSpace); $xmlWriter->writeAttribute('w:space', $colsSpace);
$xmlWriter->endElement(); $xmlWriter->endElement();
$xmlWriter->endElement(); $xmlWriter->endElement();
} }
@ -274,7 +207,7 @@ class Document extends Base
* *
* @param XMLWriter $xmlWriter * @param XMLWriter $xmlWriter
*/ */
private function writePageBreak(XMLWriter $xmlWriter) protected function writePageBreak(XMLWriter $xmlWriter, PageBreak $pagebreak)
{ {
$xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
$xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
@ -290,7 +223,7 @@ class Document extends Base
* *
* @param XMLWriter $xmlWriter * @param XMLWriter $xmlWriter
*/ */
private function writeTOC(XMLWriter $xmlWriter) protected function writeTOC(XMLWriter $xmlWriter, TOC $toc)
{ {
$titles = TOC::getTitles(); $titles = TOC::getTitles();
$styleFont = TOC::getStyleFont(); $styleFont = TOC::getStyleFont();

View File

@ -20,9 +20,9 @@ class DocumentRels extends Base
/** /**
* Write word/_rels/document.xml.rels * Write word/_rels/document.xml.rels
* *
* @param array $_relsCollection * @param array $relsCollection
*/ */
public function writeDocumentRels($_relsCollection) public function writeDocumentRels($relsCollection)
{ {
// Create XML writer // Create XML writer
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
@ -34,73 +34,42 @@ class DocumentRels extends Base
$xmlWriter->startElement('Relationships'); $xmlWriter->startElement('Relationships');
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
// Relationship word/document.xml // Write static files
$this->writeRel( $staticFiles = array(
$xmlWriter, 'styles' => 'styles.xml',
1, 'numbering' => 'numbering.xml',
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles', 'settings' => 'settings.xml',
'styles.xml' 'theme' => 'theme/theme1.xml',
'webSettings' => 'webSettings.xml',
'fontTable' => 'fontTable.xml',
); );
$i = 0;
foreach ($staticFiles as $type => $file) {
$i++;
$schema = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $type;
$this->writeRel($xmlWriter, $i, $schema, $file);
}
// Relationship word/numbering.xml // Write media relationship (image, oleObject, hyperlink)
$this->writeRel( $this->writeMediaRels($xmlWriter, $relsCollection);
$xmlWriter,
2,
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering',
'numbering.xml'
);
// Relationship word/settings.xml
$this->writeRel(
$xmlWriter,
3,
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings',
'settings.xml'
);
// Relationship word/settings.xml
$this->writeRel(
$xmlWriter,
4,
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
'theme/theme1.xml'
);
// Relationship word/settings.xml
$this->writeRel(
$xmlWriter,
5,
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings',
'webSettings.xml'
);
// Relationship word/settings.xml
$this->writeRel(
$xmlWriter,
6,
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable',
'fontTable.xml'
);
$this->writeMediaRels($xmlWriter, $_relsCollection);
$xmlWriter->endElement(); // Relationships $xmlWriter->endElement(); // Relationships
// Return
return $xmlWriter->getData(); return $xmlWriter->getData();
} }
/** /**
* Write header footer rels word/_rels/*.xml.rels * Write header footer rels word/_rels/*.xml.rels
* *
* @param array $_relsCollection * @param array $relsCollection
*/ */
public function writeHeaderFooterRels($_relsCollection) public function writeHeaderFooterRels($relsCollection)
{ {
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('Relationships'); $xmlWriter->startElement('Relationships');
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
$this->writeMediaRels($xmlWriter, $_relsCollection); $this->writeMediaRels($xmlWriter, $relsCollection);
$xmlWriter->endElement(); $xmlWriter->endElement();
return $xmlWriter->getData(); return $xmlWriter->getData();

View File

@ -10,15 +10,6 @@
namespace PhpOffice\PhpWord\Writer\Word2007; namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Container\Footer as FooterElement; use PhpOffice\PhpWord\Container\Footer as FooterElement;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Link;
use PhpOffice\PhpWord\Element\PreserveText;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\ListItem;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\CheckBox;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
/** /**
@ -50,29 +41,7 @@ class Footer extends Base
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
$xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
$_elements = $footer->getElements(); $this->writeContainerElements($xmlWriter, $footer);
foreach ($_elements as $element) {
if ($element instanceof Text) {
$this->writeText($xmlWriter, $element);
} elseif ($element instanceof TextRun) {
$this->writeTextRun($xmlWriter, $element);
} elseif ($element instanceof Link) {
$this->writeLink($xmlWriter, $element);
} elseif ($element instanceof PreserveText) {
$this->writePreserveText($xmlWriter, $element);
} elseif ($element instanceof TextBreak) {
$this->writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof ListItem) {
$this->writeListItem($xmlWriter, $element);
} elseif ($element instanceof Table) {
$this->writeTable($xmlWriter, $element);
} elseif ($element instanceof Image) {
$this->writeImage($xmlWriter, $element);
} elseif ($element instanceof CheckBox) {
$this->writeCheckBox($xmlWriter, $element);
}
}
$xmlWriter->endElement(); $xmlWriter->endElement();

View File

@ -110,23 +110,9 @@ class Footnotes extends Base
$xmlWriter->writeRaw(' '); $xmlWriter->writeRaw(' ');
$xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:t
$xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:r
// Actual footnote contents
$elements = $footnote->getElements(); $this->writeContainerElements($xmlWriter, $footnote);
if (count($elements) > 0) {
foreach ($elements as $element) {
if ($element instanceof Text) {
$this->writeText($xmlWriter, $element, true);
} elseif ($element instanceof Link) {
$this->writeLink($xmlWriter, $element, true);
} elseif ($element instanceof Image) {
$this->writeImage($xmlWriter, $element, true);
} elseif ($element instanceof Object) {
$this->writeObject($xmlWriter, $element, true);
} elseif ($element instanceof TextBreak) {
$xmlWriter->writeElement('w:br');
}
}
}
$xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
$xmlWriter->endElement(); // w:footnote $xmlWriter->endElement(); // w:footnote
} }

View File

@ -20,15 +20,15 @@ class FootnotesRels extends Base
/** /**
* Write word/_rels/footnotes.xml.rels * Write word/_rels/footnotes.xml.rels
* *
* @param mixed $_relsCollection * @param mixed $relsCollection
*/ */
public function writeFootnotesRels($_relsCollection) public function writeFootnotesRels($relsCollection)
{ {
$xmlWriter = $this->getXmlWriter(); $xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('Relationships'); $xmlWriter->startElement('Relationships');
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
$this->writeMediaRels($xmlWriter, $_relsCollection); $this->writeMediaRels($xmlWriter, $relsCollection);
$xmlWriter->endElement(); $xmlWriter->endElement();
return $xmlWriter->getData(); return $xmlWriter->getData();

View File

@ -10,15 +10,6 @@
namespace PhpOffice\PhpWord\Writer\Word2007; namespace PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Container\Header as HeaderElement; use PhpOffice\PhpWord\Container\Header as HeaderElement;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Link;
use PhpOffice\PhpWord\Element\PreserveText;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\ListItem;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\CheckBox;
use PhpOffice\PhpWord\Shared\XMLWriter; use PhpOffice\PhpWord\Shared\XMLWriter;
/** /**
@ -50,33 +41,7 @@ class Header extends Base
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
$xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
$_elements = $header->getElements(); $this->writeContainerElements($xmlWriter, $header);
foreach ($_elements as $element) {
if ($element instanceof Text) {
$this->writeText($xmlWriter, $element);
} elseif ($element instanceof TextRun) {
$this->writeTextRun($xmlWriter, $element);
} elseif ($element instanceof Link) {
$this->writeLink($xmlWriter, $element);
} elseif ($element instanceof PreserveText) {
$this->writePreserveText($xmlWriter, $element);
} elseif ($element instanceof TextBreak) {
$this->writeTextBreak($xmlWriter, $element);
} elseif ($element instanceof ListItem) {
$this->writeListItem($xmlWriter, $element);
} elseif ($element instanceof Table) {
$this->writeTable($xmlWriter, $element);
} elseif ($element instanceof Image) {
if (!$element->getIsWatermark()) {
$this->writeImage($xmlWriter, $element);
} else {
$this->writeWatermark($xmlWriter, $element);
}
} elseif ($element instanceof CheckBox) {
$this->writeCheckBox($xmlWriter, $element);
}
}
$xmlWriter->endElement(); $xmlWriter->endElement();