144 lines
4.2 KiB
PHP
Raw Normal View History

2014-04-26 11:14:27 +07: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.
2014-04-26 11:14:27 +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
2014-04-26 11:14:27 +07:00
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
2014-04-26 11:14:27 +07:00
/**
* Table style writer
*
* @since 0.10.0
*/
class Table extends AbstractStyle
{
/**
* Is full style
*
* @var bool
*/
private $isFullStyle = true;
/**
* Write style
*/
public function write()
{
2014-05-11 11:23:46 +07:00
$style = $this->getStyle();
if (!$style instanceof \PhpOffice\PhpWord\Style\Table) {
2014-04-26 11:14:27 +07:00
return;
}
2014-05-07 09:47:02 +07:00
$xmlWriter = $this->getXmlWriter();
2014-05-08 19:25:29 +07:00
// w:tblPr
2014-05-07 09:47:02 +07:00
$hasMargins = $style->hasMargins();
$hasBorders = $style->hasBorders();
$align = $style->getAlign();
$xmlWriter->startElement('w:tblPr');
$xmlWriter->startElement('w:tblW');
$xmlWriter->writeAttribute('w:w', $style->getWidth());
$xmlWriter->writeAttribute('w:type', $style->getUnit());
$xmlWriter->endElement(); // w:tblW
// Alignment
$styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $align)));
$styleWriter->write();
// Margins
if ($hasMargins) {
$mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($style->getCellMargin());
$xmlWriter->startElement('w:tblCellMar');
$mbWriter->write();
$xmlWriter->endElement(); // w:tblCellMar
2014-04-26 11:14:27 +07:00
}
2014-05-08 19:25:29 +07:00
// Borders
if ($hasBorders) {
$mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($style->getBorderSize());
$mbWriter->setColors($style->getBorderColor());
$xmlWriter->startElement('w:tblBorders');
$mbWriter->write();
$xmlWriter->endElement(); // w:tblBorders
}
$xmlWriter->endElement(); // w:tblPr
2014-04-26 11:14:27 +07:00
// Only write background color and first row for full style
if ($this->isFullStyle) {
// Background color
2014-05-07 09:47:02 +07:00
if (!is_null($style->getShading())) {
$xmlWriter->startElement('w:tcPr');
$styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write();
2014-05-07 09:47:02 +07:00
$xmlWriter->endElement();
2014-04-26 11:14:27 +07:00
}
// First Row
2014-05-07 09:47:02 +07:00
$firstRow = $style->getFirstRow();
2014-04-26 11:14:27 +07:00
if ($firstRow instanceof \PhpOffice\PhpWord\Style\Table) {
2014-05-07 09:47:02 +07:00
$this->writeFirstRow($firstRow);
2014-04-26 11:14:27 +07:00
}
}
}
/**
* Set is full style
*
* @param bool $value
*/
public function setIsFullStyle($value)
{
$this->isFullStyle = $value;
}
/**
* Write row style
*/
2014-05-07 09:47:02 +07:00
private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style)
2014-04-26 11:14:27 +07:00
{
2014-05-07 09:47:02 +07:00
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w:tblStylePr');
$xmlWriter->writeAttribute('w:type', 'firstRow');
$xmlWriter->startElement('w:tcPr');
if (!is_null($style->getShading())) {
2014-05-07 09:47:02 +07:00
$styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write();
2014-04-26 11:14:27 +07:00
}
// Borders
if ($style->hasBorders()) {
2014-05-07 09:47:02 +07:00
$mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($style->getBorderSize());
$mbWriter->setColors($style->getBorderColor());
2014-04-26 11:14:27 +07:00
2014-05-07 09:47:02 +07:00
$xmlWriter->startElement('w:tcBorders');
2014-04-26 11:14:27 +07:00
$mbWriter->write();
2014-05-07 09:47:02 +07:00
$xmlWriter->endElement(); // w:tcBorders
2014-04-26 11:14:27 +07:00
}
2014-05-07 09:47:02 +07:00
$xmlWriter->endElement(); // w:tcPr
$xmlWriter->endElement(); // w:tblStylePr
2014-04-26 11:14:27 +07:00
}
}