125 lines
3.7 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;
/**
* 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-07 09:47:02 +07:00
if (is_null($style = $this->getStyle())) {
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
2014-05-07 09:47:02 +07:00
$hasBorders = $style->hasBorders();
$hasMargins = $style->hasMargins();
2014-04-26 11:14:27 +07:00
if ($hasMargins || $hasBorders) {
2014-05-07 09:47:02 +07:00
$xmlWriter->startElement('w:tblPr');
2014-04-26 11:14:27 +07:00
if ($hasMargins) {
2014-05-07 09:47:02 +07:00
$mbWriter = new MarginBorder($xmlWriter);
$mbWriter->setSizes($style->getCellMargin());
2014-04-26 11:14:27 +07:00
2014-05-07 09:47:02 +07:00
$xmlWriter->startElement('w:tblCellMar');
2014-04-26 11:14:27 +07:00
$mbWriter->write();
2014-05-07 09:47:02 +07:00
$xmlWriter->endElement(); // w:tblCellMar
2014-04-26 11:14:27 +07:00
}
if ($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:tblBorders');
2014-04-26 11:14:27 +07:00
$mbWriter->write();
2014-05-07 09:47:02 +07:00
$xmlWriter->endElement(); // w:tblBorders
2014-04-26 11:14:27 +07:00
}
2014-05-07 09:47:02 +07:00
$xmlWriter->endElement(); // w:tblPr
2014-04-26 11:14:27 +07:00
}
2014-05-08 19:25:29 +07:00
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
}
}