2014-04-26 11:14:27 +07: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.
|
2014-04-26 11:14:27 +07:00
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @see https://github.com/PHPOffice/PHPWord
|
|
|
|
|
* @copyright 2010-2017 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-04-26 11:14:27 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
|
|
|
|
|
2016-01-23 19:16:34 +04:00
|
|
|
use PhpOffice\Common\XMLWriter;
|
2018-02-18 02:10:10 +03:00
|
|
|
use PhpOffice\PhpWord\SimpleType\TblWidth;
|
2014-05-13 14:54:40 +07:00
|
|
|
use PhpOffice\PhpWord\Style\Table as TableStyle;
|
2015-10-10 19:06:23 +04:00
|
|
|
use PhpOffice\PhpWord\Writer\Word2007\Element\TableAlignment;
|
2014-05-13 01:32:44 +07:00
|
|
|
|
2014-04-26 11:14:27 +07:00
|
|
|
/**
|
|
|
|
|
* Table style writer
|
|
|
|
|
*
|
|
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
|
|
|
|
class Table extends AbstractStyle
|
|
|
|
|
{
|
|
|
|
|
/**
|
2014-05-13 14:54:40 +07:00
|
|
|
* @var int Table width
|
2014-04-26 11:14:27 +07:00
|
|
|
*/
|
2014-05-13 14:54:40 +07:00
|
|
|
private $width;
|
2014-04-26 11:14:27 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Write style.
|
2014-04-26 11:14:27 +07:00
|
|
|
*/
|
|
|
|
|
public function write()
|
|
|
|
|
{
|
2014-05-11 11:23:46 +07:00
|
|
|
$style = $this->getStyle();
|
2014-05-07 09:47:02 +07:00
|
|
|
$xmlWriter = $this->getXmlWriter();
|
2014-05-08 19:25:29 +07:00
|
|
|
|
2014-05-15 14:41:08 +07:00
|
|
|
if ($style instanceof TableStyle) {
|
2014-05-13 14:54:40 +07:00
|
|
|
$this->writeStyle($xmlWriter, $style);
|
|
|
|
|
} elseif (is_string($style)) {
|
|
|
|
|
$xmlWriter->startElement('w:tblPr');
|
|
|
|
|
$xmlWriter->startElement('w:tblStyle');
|
|
|
|
|
$xmlWriter->writeAttribute('w:val', $style);
|
|
|
|
|
$xmlWriter->endElement();
|
2015-10-10 19:06:23 +04:00
|
|
|
if (null !== $this->width) {
|
2018-02-18 02:10:10 +03:00
|
|
|
$this->writeTblWidth($xmlWriter, 'w:tblW', TblWidth::PERCENT, $this->width);
|
2014-05-13 14:54:40 +07:00
|
|
|
}
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-13 01:32:44 +07:00
|
|
|
|
2014-05-13 14:54:40 +07:00
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Write full style.
|
|
|
|
|
*
|
2016-01-23 19:16:34 +04:00
|
|
|
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
2014-07-03 15:40:24 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Style\Table $style
|
2014-05-13 14:54:40 +07:00
|
|
|
*/
|
|
|
|
|
private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
|
|
|
|
|
{
|
2014-05-13 01:32:44 +07:00
|
|
|
// w:tblPr
|
|
|
|
|
$xmlWriter->startElement('w:tblPr');
|
|
|
|
|
|
2015-10-10 19:06:23 +04:00
|
|
|
// Table alignment
|
|
|
|
|
if ('' !== $style->getAlignment()) {
|
|
|
|
|
$tableAlignment = new TableAlignment($style->getAlignment());
|
|
|
|
|
$xmlWriter->startElement($tableAlignment->getName());
|
|
|
|
|
foreach ($tableAlignment->getAttributes() as $attributeName => $attributeValue) {
|
|
|
|
|
$xmlWriter->writeAttribute($attributeName, $attributeValue);
|
|
|
|
|
}
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
2014-05-13 01:32:44 +07:00
|
|
|
|
2018-02-18 02:10:10 +03:00
|
|
|
$this->writeTblWidth($xmlWriter, 'w:tblW', $style->getUnit(), $style->getWidth());
|
|
|
|
|
$this->writeTblWidth($xmlWriter, 'w:tblCellSpacing', TblWidth::TWIP, $style->getCellSpacing());
|
2018-02-14 00:39:37 +01:00
|
|
|
$this->writeLayout($xmlWriter, $style->getLayout());
|
2014-05-13 14:54:40 +07:00
|
|
|
$this->writeMargin($xmlWriter, $style);
|
|
|
|
|
$this->writeBorder($xmlWriter, $style);
|
2014-05-13 01:32:44 +07:00
|
|
|
|
2014-05-13 14:54:40 +07:00
|
|
|
$xmlWriter->endElement(); // w:tblPr
|
2014-05-08 19:25:29 +07:00
|
|
|
|
2014-05-13 14:54:40 +07:00
|
|
|
$this->writeShading($xmlWriter, $style);
|
2014-05-13 01:32:44 +07:00
|
|
|
|
2014-05-13 14:54:40 +07:00
|
|
|
// First row style
|
|
|
|
|
$firstRow = $style->getFirstRow();
|
2014-05-15 14:41:08 +07:00
|
|
|
if ($firstRow instanceof TableStyle) {
|
2014-05-13 14:54:40 +07:00
|
|
|
$this->writeFirstRow($xmlWriter, $firstRow);
|
2014-05-13 01:32:44 +07:00
|
|
|
}
|
2014-05-13 14:54:40 +07:00
|
|
|
}
|
2014-05-13 01:32:44 +07:00
|
|
|
|
2016-07-25 10:11:52 +02:00
|
|
|
/**
|
|
|
|
|
* Enable/Disable automatic resizing of the table
|
|
|
|
|
*
|
2018-02-13 23:29:53 +01:00
|
|
|
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
2016-07-25 10:11:52 +02:00
|
|
|
* @param string $layout autofit / fixed
|
|
|
|
|
*/
|
2018-02-14 00:39:37 +01:00
|
|
|
private function writeLayout(XMLWriter $xmlWriter, $layout)
|
2016-07-25 10:11:52 +02:00
|
|
|
{
|
|
|
|
|
$xmlWriter->startElement('w:tblLayout');
|
2018-02-14 00:39:37 +01:00
|
|
|
$xmlWriter->writeAttribute('w:type', $layout);
|
2016-07-25 10:11:52 +02:00
|
|
|
$xmlWriter->endElement(); // w:tblLayout
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 14:54:40 +07:00
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Write margin.
|
|
|
|
|
*
|
2016-01-23 19:16:34 +04:00
|
|
|
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
2014-07-03 15:40:24 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Style\Table $style
|
2014-05-13 14:54:40 +07:00
|
|
|
*/
|
|
|
|
|
private function writeMargin(XMLWriter $xmlWriter, TableStyle $style)
|
|
|
|
|
{
|
2014-05-15 01:13:22 +07:00
|
|
|
if ($style->hasMargin()) {
|
2014-05-13 14:54:40 +07:00
|
|
|
$xmlWriter->startElement('w:tblCellMar');
|
|
|
|
|
|
|
|
|
|
$styleWriter = new MarginBorder($xmlWriter);
|
|
|
|
|
$styleWriter->setSizes($style->getCellMargin());
|
|
|
|
|
$styleWriter->write();
|
|
|
|
|
|
|
|
|
|
$xmlWriter->endElement(); // w:tblCellMar
|
2014-04-26 11:14:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Write border.
|
|
|
|
|
*
|
2016-01-23 19:16:34 +04:00
|
|
|
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
2014-07-03 15:40:24 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Style\Table $style
|
2014-04-26 11:14:27 +07:00
|
|
|
*/
|
2014-05-13 14:54:40 +07:00
|
|
|
private function writeBorder(XMLWriter $xmlWriter, TableStyle $style)
|
2014-04-26 11:14:27 +07:00
|
|
|
{
|
2014-05-15 01:13:22 +07:00
|
|
|
if ($style->hasBorder()) {
|
2014-05-13 14:54:40 +07:00
|
|
|
$xmlWriter->startElement('w:tblBorders');
|
|
|
|
|
|
|
|
|
|
$styleWriter = new MarginBorder($xmlWriter);
|
|
|
|
|
$styleWriter->setSizes($style->getBorderSize());
|
|
|
|
|
$styleWriter->setColors($style->getBorderColor());
|
|
|
|
|
$styleWriter->write();
|
|
|
|
|
|
|
|
|
|
$xmlWriter->endElement(); // w:tblBorders
|
|
|
|
|
}
|
2014-04-26 11:14:27 +07:00
|
|
|
}
|
|
|
|
|
|
2018-02-18 02:10:10 +03:00
|
|
|
/**
|
|
|
|
|
* Writes a table width
|
|
|
|
|
*
|
|
|
|
|
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
|
|
|
|
* @param string $elementName
|
|
|
|
|
* @param string $unit
|
|
|
|
|
* @param int|float $width
|
|
|
|
|
*/
|
|
|
|
|
private function writeTblWidth(XMLWriter $xmlWriter, $elementName, $unit, $width = null)
|
|
|
|
|
{
|
|
|
|
|
if (null === $width) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$xmlWriter->startElement($elementName);
|
|
|
|
|
$xmlWriter->writeAttributeIf(null !== $width, 'w:w', $width);
|
|
|
|
|
$xmlWriter->writeAttribute('w:type', $unit);
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-26 11:14:27 +07:00
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Write row style.
|
|
|
|
|
*
|
2016-01-23 19:16:34 +04:00
|
|
|
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
2014-07-03 15:40:24 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Style\Table $style
|
2014-04-26 11:14:27 +07:00
|
|
|
*/
|
2014-05-13 14:54:40 +07:00
|
|
|
private function writeFirstRow(XMLWriter $xmlWriter, TableStyle $style)
|
2014-04-26 11:14:27 +07:00
|
|
|
{
|
2014-05-07 09:47:02 +07:00
|
|
|
$xmlWriter->startElement('w:tblStylePr');
|
|
|
|
|
$xmlWriter->writeAttribute('w:type', 'firstRow');
|
|
|
|
|
$xmlWriter->startElement('w:tcPr');
|
2014-05-13 14:54:40 +07:00
|
|
|
|
|
|
|
|
$this->writeBorder($xmlWriter, $style);
|
|
|
|
|
$this->writeShading($xmlWriter, $style);
|
|
|
|
|
|
|
|
|
|
$xmlWriter->endElement(); // w:tcPr
|
|
|
|
|
$xmlWriter->endElement(); // w:tblStylePr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Write shading.
|
|
|
|
|
*
|
2016-01-23 19:16:34 +04:00
|
|
|
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
2014-07-03 15:40:24 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Style\Table $style
|
2014-05-13 14:54:40 +07:00
|
|
|
*/
|
|
|
|
|
private function writeShading(XMLWriter $xmlWriter, TableStyle $style)
|
|
|
|
|
{
|
2015-10-10 19:06:23 +04:00
|
|
|
if (null !== $style->getShading()) {
|
2014-05-13 14:54:40 +07:00
|
|
|
$xmlWriter->startElement('w:tcPr');
|
|
|
|
|
|
2014-05-07 09:47:02 +07:00
|
|
|
$styleWriter = new Shading($xmlWriter, $style->getShading());
|
2014-05-02 09:45:16 +07:00
|
|
|
$styleWriter->write();
|
2014-04-26 11:14:27 +07:00
|
|
|
|
2014-05-13 14:54:40 +07:00
|
|
|
$xmlWriter->endElement();
|
2014-04-26 11:14:27 +07:00
|
|
|
}
|
2014-05-13 14:54:40 +07:00
|
|
|
}
|
2014-04-26 11:14:27 +07:00
|
|
|
|
2014-05-13 14:54:40 +07:00
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Set width.
|
2014-05-13 14:54:40 +07:00
|
|
|
*
|
|
|
|
|
* @param int $value
|
|
|
|
|
*/
|
|
|
|
|
public function setWidth($value = null)
|
|
|
|
|
{
|
|
|
|
|
$this->width = $value;
|
2014-04-26 11:14:27 +07:00
|
|
|
}
|
|
|
|
|
}
|