#156 Merge branch 'master' of https://github.com/ozilion/PHPWord
This commit is contained in:
commit
f0803efabb
|
|
@ -12,6 +12,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
|
|||
- Footnote: Ability to style footnote reference mark by using `FootnoteReference` style - @ivanlanin
|
||||
- Font: Add `bgColor` to font style to define background using HEX color - @jcarignan GH-168
|
||||
- Table: Add `exactHeight` to row style to define whether row height should be exact or atLeast - @jcarignan GH-168
|
||||
- Element: New `CheckBox` element for sections and table cells - @ozilion GH-156
|
||||
|
||||
### Bugfixes
|
||||
|
||||
|
|
|
|||
|
|
@ -357,3 +357,18 @@ The footnote reference number will be displayed with decimal number starting
|
|||
from 1. This number use ``FooterReference`` style which you can redefine by
|
||||
``addFontStyle`` method. Default value for this style is
|
||||
``array('superScript' => true)``;
|
||||
|
||||
Checkboxes
|
||||
----------
|
||||
|
||||
Checkbox elements can be added to sections or table cells by using
|
||||
``addCheckBox``.
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
$section->addCheckBox($name, $text, [$fontStyle], [$paragraphStyle])
|
||||
|
||||
- ``$name`` Name of the check box.
|
||||
- ``$text`` Text following the check box
|
||||
- ``$fontStyle`` See "Font style" section.
|
||||
- ``$paragraphStyle`` See "Paragraph style" section.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
include_once 'Sample_Header.php';
|
||||
|
||||
// New Word document
|
||||
echo date('H:i:s'), " Create new PhpWord object", \EOL;
|
||||
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||
|
||||
$section = $phpWord->createSection();
|
||||
$section->addText('Check box in section');
|
||||
$section->addCheckBox('chkBox1', 'Checkbox 1');
|
||||
$section->addText('Check box in table cell');
|
||||
$table = $section->addTable();
|
||||
$table->addRow();
|
||||
$cell = $table->addCell();
|
||||
$cell->addCheckBox('chkBox2', 'Checkbox 2');
|
||||
|
||||
// Save file
|
||||
$name = basename(__FILE__, '.php');
|
||||
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
|
||||
foreach ($writers as $writer => $extension) {
|
||||
echo date('H:i:s'), " Write to {$writer} format", \EOL;
|
||||
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer);
|
||||
$xmlWriter->save("{$name}.{$extension}");
|
||||
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
|
||||
}
|
||||
|
||||
include_once 'Sample_Footer.php';
|
||||
|
|
@ -23,6 +23,7 @@ use PhpOffice\PhpWord\Section\Text;
|
|||
use PhpOffice\PhpWord\Section\TextBreak;
|
||||
use PhpOffice\PhpWord\Section\TextRun;
|
||||
use PhpOffice\PhpWord\Section\Title;
|
||||
use PhpOffice\PhpWord\Section\CheckBox;
|
||||
use PhpOffice\PhpWord\Shared\String;
|
||||
|
||||
/**
|
||||
|
|
@ -116,7 +117,7 @@ class Section
|
|||
*/
|
||||
public function addText($text, $styleFont = null, $styleParagraph = null)
|
||||
{
|
||||
if (!String::IsUTF8($text)) {
|
||||
if (!String::isUTF8($text)) {
|
||||
$text = utf8_encode($text);
|
||||
}
|
||||
$text = new Text($text, $styleFont, $styleParagraph);
|
||||
|
|
@ -135,11 +136,11 @@ class Section
|
|||
*/
|
||||
public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null)
|
||||
{
|
||||
if (!String::IsUTF8($linkSrc)) {
|
||||
if (!String::isUTF8($linkSrc)) {
|
||||
$linkSrc = utf8_encode($linkSrc);
|
||||
}
|
||||
if (!is_null($linkName)) {
|
||||
if (!String::IsUTF8($linkName)) {
|
||||
if (!String::isUTF8($linkName)) {
|
||||
$linkName = utf8_encode($linkName);
|
||||
}
|
||||
}
|
||||
|
|
@ -414,4 +415,26 @@ class Section
|
|||
$this->_elementCollection[] = $footnote;
|
||||
return $footnote;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a CheckBox Element
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $text
|
||||
* @param mixed $style
|
||||
* @return PHPWord_Section_CheckBox
|
||||
*/
|
||||
public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null)
|
||||
{
|
||||
if (!String::isUTF8($name)) {
|
||||
$name = utf8_encode($name);
|
||||
}
|
||||
if (!String::isUTF8($text)) {
|
||||
$text = utf8_encode($text);
|
||||
}
|
||||
$element = new CheckBox($name, $text, $styleFont, $styleParagraph);
|
||||
$this->_elementCollection[] = $element;
|
||||
|
||||
return $element;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,187 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Section;
|
||||
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
|
||||
/**
|
||||
* Check box element
|
||||
*/
|
||||
class CheckBox
|
||||
{
|
||||
/**
|
||||
* Name content
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* Text content
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* Text style
|
||||
*
|
||||
* @var Font
|
||||
*/
|
||||
private $fontStyle;
|
||||
|
||||
/**
|
||||
* Paragraph style
|
||||
*
|
||||
* @var Paragraph
|
||||
*/
|
||||
private $paragraphStyle;
|
||||
|
||||
/**
|
||||
* Cell Element Collection
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_elementCollection = array();
|
||||
|
||||
/**
|
||||
* Create a new Text Element
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $text
|
||||
* @param Font $fontStyle
|
||||
* @param Paragraph $paragraphStyle
|
||||
*/
|
||||
public function __construct($name = null, $text = null, $fontStyle = null, $paragraphStyle = null)
|
||||
{
|
||||
$this->setName($name);
|
||||
$this->setText($text);
|
||||
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
|
||||
$this->setFontStyle($fontStyle, $paragraphStyle);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Text style
|
||||
*
|
||||
* @param Font $style
|
||||
* @param Paragraph $paragraphStyle
|
||||
* @return Font
|
||||
*/
|
||||
public function setFontStyle($style = null, $paragraphStyle = null)
|
||||
{
|
||||
if ($style instanceof Font) {
|
||||
$this->fontStyle = $style;
|
||||
$this->setParagraphStyle($paragraphStyle);
|
||||
} elseif (is_array($style)) {
|
||||
$this->fontStyle = new Font('text', $paragraphStyle);
|
||||
$this->fontStyle->setArrayStyle($style);
|
||||
} elseif (null === $style) {
|
||||
$this->fontStyle = new Font('text', $paragraphStyle);
|
||||
} else {
|
||||
$this->fontStyle = $style;
|
||||
$this->setParagraphStyle($paragraphStyle);
|
||||
}
|
||||
return $this->fontStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Text style
|
||||
*
|
||||
* @return Font
|
||||
*/
|
||||
public function getFontStyle()
|
||||
{
|
||||
return $this->fontStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Paragraph style
|
||||
*
|
||||
* @param Paragraph $style
|
||||
* @return Paragraph
|
||||
*/
|
||||
public function setParagraphStyle($style = null)
|
||||
{
|
||||
if (is_array($style)) {
|
||||
$this->paragraphStyle = new Paragraph;
|
||||
$this->paragraphStyle->setArrayStyle($style);
|
||||
} elseif ($style instanceof Paragraph) {
|
||||
$this->paragraphStyle = $style;
|
||||
} elseif (null === $style) {
|
||||
$this->paragraphStyle = new Paragraph;
|
||||
} else {
|
||||
$this->paragraphStyle = $style;
|
||||
}
|
||||
return $this->paragraphStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Paragraph style
|
||||
*
|
||||
* @return Paragraph
|
||||
*/
|
||||
public function getParagraphStyle()
|
||||
{
|
||||
return $this->paragraphStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return $this
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @return $this
|
||||
*/
|
||||
public function setText($text)
|
||||
{
|
||||
$this->text = $text;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Text content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getText()
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Elements
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getElements()
|
||||
{
|
||||
return $this->_elementCollection;
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,7 @@ use PhpOffice\PhpWord\Section\Object;
|
|||
use PhpOffice\PhpWord\Section\Text;
|
||||
use PhpOffice\PhpWord\Section\TextBreak;
|
||||
use PhpOffice\PhpWord\Section\TextRun;
|
||||
use PhpOffice\PhpWord\Section\CheckBox;
|
||||
use PhpOffice\PhpWord\Shared\String;
|
||||
|
||||
/**
|
||||
|
|
@ -290,6 +291,27 @@ class Cell
|
|||
return $textRun;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a CheckBox Element
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $text
|
||||
* @param mixed $style
|
||||
* @return PHPWord_Section_CheckBox
|
||||
*/
|
||||
public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null)
|
||||
{
|
||||
if (!String::isUTF8($name)) {
|
||||
$name = utf8_encode($name);
|
||||
}
|
||||
if (!String::isUTF8($text)) {
|
||||
$text = utf8_encode($text);
|
||||
}
|
||||
$text = new CheckBox($name, $text, $styleFont, $styleParagraph);
|
||||
$this->_elementCollection[] = $text;
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Elements
|
||||
*
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ use PhpOffice\PhpWord\Section\Text;
|
|||
use PhpOffice\PhpWord\Section\TextBreak;
|
||||
use PhpOffice\PhpWord\Section\TextRun;
|
||||
use PhpOffice\PhpWord\Section\Title;
|
||||
use PhpOffice\PhpWord\Section\CheckBox;
|
||||
use PhpOffice\PhpWord\Shared\String;
|
||||
use PhpOffice\PhpWord\Shared\XMLWriter;
|
||||
use PhpOffice\PhpWord\Style\Cell;
|
||||
|
|
@ -45,17 +46,17 @@ class Base extends WriterPart
|
|||
{
|
||||
$styleFont = $text->getFontStyle();
|
||||
|
||||
$SfIsObject = ($styleFont instanceof Font) ? true : false;
|
||||
$sfIsObject = ($styleFont instanceof Font) ? true : false;
|
||||
|
||||
if (!$withoutP) {
|
||||
$xmlWriter->startElement('w:p');
|
||||
|
||||
$styleParagraph = $text->getParagraphStyle();
|
||||
$SpIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
$spIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
|
||||
if ($SpIsObject) {
|
||||
if ($spIsObject) {
|
||||
$this->_writeParagraphStyle($xmlWriter, $styleParagraph);
|
||||
} elseif (!$SpIsObject && !is_null($styleParagraph)) {
|
||||
} elseif (!$spIsObject && !is_null($styleParagraph)) {
|
||||
$xmlWriter->startElement('w:pPr');
|
||||
$xmlWriter->startElement('w:pStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleParagraph);
|
||||
|
|
@ -69,9 +70,9 @@ class Base extends WriterPart
|
|||
|
||||
$xmlWriter->startElement('w:r');
|
||||
|
||||
if ($SfIsObject) {
|
||||
if ($sfIsObject) {
|
||||
$this->_writeTextStyle($xmlWriter, $styleFont);
|
||||
} elseif (!$SfIsObject && !is_null($styleFont)) {
|
||||
} elseif (!$sfIsObject && !is_null($styleFont)) {
|
||||
$xmlWriter->startElement('w:rPr');
|
||||
$xmlWriter->startElement('w:rStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleFont);
|
||||
|
|
@ -102,13 +103,13 @@ class Base extends WriterPart
|
|||
$elements = $textrun->getElements();
|
||||
$styleParagraph = $textrun->getParagraphStyle();
|
||||
|
||||
$SpIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
$spIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
|
||||
$xmlWriter->startElement('w:p');
|
||||
|
||||
if ($SpIsObject) {
|
||||
if ($spIsObject) {
|
||||
$this->_writeParagraphStyle($xmlWriter, $styleParagraph);
|
||||
} elseif (!$SpIsObject && !is_null($styleParagraph)) {
|
||||
} elseif (!$spIsObject && !is_null($styleParagraph)) {
|
||||
$xmlWriter->startElement('w:pPr');
|
||||
$xmlWriter->startElement('w:pStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleParagraph);
|
||||
|
|
@ -254,17 +255,17 @@ class Base extends WriterPart
|
|||
}
|
||||
|
||||
$styleFont = $link->getFontStyle();
|
||||
$SfIsObject = ($styleFont instanceof Font) ? true : false;
|
||||
$sfIsObject = ($styleFont instanceof Font) ? true : false;
|
||||
|
||||
if (!$withoutP) {
|
||||
$xmlWriter->startElement('w:p');
|
||||
|
||||
$styleParagraph = $link->getParagraphStyle();
|
||||
$SpIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
$spIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
|
||||
if ($SpIsObject) {
|
||||
if ($spIsObject) {
|
||||
$this->_writeParagraphStyle($xmlWriter, $styleParagraph);
|
||||
} elseif (!$SpIsObject && !is_null($styleParagraph)) {
|
||||
} elseif (!$spIsObject && !is_null($styleParagraph)) {
|
||||
$xmlWriter->startElement('w:pPr');
|
||||
$xmlWriter->startElement('w:pStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleParagraph);
|
||||
|
|
@ -278,9 +279,9 @@ class Base extends WriterPart
|
|||
$xmlWriter->writeAttribute('w:history', '1');
|
||||
|
||||
$xmlWriter->startElement('w:r');
|
||||
if ($SfIsObject) {
|
||||
if ($sfIsObject) {
|
||||
$this->_writeTextStyle($xmlWriter, $styleFont);
|
||||
} elseif (!$SfIsObject && !is_null($styleFont)) {
|
||||
} elseif (!$sfIsObject && !is_null($styleFont)) {
|
||||
$xmlWriter->startElement('w:rPr');
|
||||
$xmlWriter->startElement('w:rStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleFont);
|
||||
|
|
@ -312,8 +313,8 @@ class Base extends WriterPart
|
|||
$styleFont = $textrun->getFontStyle();
|
||||
$styleParagraph = $textrun->getParagraphStyle();
|
||||
|
||||
$SfIsObject = ($styleFont instanceof Font) ? true : false;
|
||||
$SpIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
$sfIsObject = ($styleFont instanceof Font) ? true : false;
|
||||
$spIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
|
||||
$arrText = $textrun->getText();
|
||||
if (!is_array($arrText)) {
|
||||
|
|
@ -322,9 +323,9 @@ class Base extends WriterPart
|
|||
|
||||
$xmlWriter->startElement('w:p');
|
||||
|
||||
if ($SpIsObject) {
|
||||
if ($spIsObject) {
|
||||
$this->_writeParagraphStyle($xmlWriter, $styleParagraph);
|
||||
} elseif (!$SpIsObject && !is_null($styleParagraph)) {
|
||||
} elseif (!$spIsObject && !is_null($styleParagraph)) {
|
||||
$xmlWriter->startElement('w:pPr');
|
||||
$xmlWriter->startElement('w:pStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleParagraph);
|
||||
|
|
@ -345,9 +346,9 @@ class Base extends WriterPart
|
|||
|
||||
$xmlWriter->startElement('w:r');
|
||||
|
||||
if ($SfIsObject) {
|
||||
if ($sfIsObject) {
|
||||
$this->_writeTextStyle($xmlWriter, $styleFont);
|
||||
} elseif (!$SfIsObject && !is_null($styleFont)) {
|
||||
} elseif (!$sfIsObject && !is_null($styleFont)) {
|
||||
$xmlWriter->startElement('w:rPr');
|
||||
$xmlWriter->startElement('w:rStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleFont);
|
||||
|
|
@ -378,9 +379,9 @@ class Base extends WriterPart
|
|||
|
||||
$xmlWriter->startElement('w:r');
|
||||
|
||||
if ($SfIsObject) {
|
||||
if ($sfIsObject) {
|
||||
$this->_writeTextStyle($xmlWriter, $styleFont);
|
||||
} elseif (!$SfIsObject && !is_null($styleFont)) {
|
||||
} elseif (!$sfIsObject && !is_null($styleFont)) {
|
||||
$xmlWriter->startElement('w:rPr');
|
||||
$xmlWriter->startElement('w:rStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleFont);
|
||||
|
|
@ -680,6 +681,8 @@ class Base extends WriterPart
|
|||
$this->_writeObject($xmlWriter, $element);
|
||||
} elseif ($element instanceof PreserveText) {
|
||||
$this->_writePreserveText($xmlWriter, $element);
|
||||
} elseif ($element instanceof CheckBox) {
|
||||
$this->_writeCheckBox($xmlWriter, $element);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -1214,4 +1217,102 @@ class Base extends WriterPart
|
|||
$xmlWriter->endElement(); // w:p
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write CheckBox
|
||||
*/
|
||||
protected function _writeCheckBox(XMLWriter $xmlWriter, CheckBox $checkbox, $withoutP = false, $checkState = false)
|
||||
{
|
||||
$count = 1;
|
||||
$_elements = $checkbox->getElements();
|
||||
if (count($_elements) > 1) {
|
||||
$count = $count + 1;
|
||||
}
|
||||
$name = htmlspecialchars($checkbox->getName());
|
||||
$name = String::controlCharacterPHP2OOXML($name);
|
||||
$text = htmlspecialchars($checkbox->getText());
|
||||
$text = String::controlCharacterPHP2OOXML($text);
|
||||
|
||||
$styleFont = $checkbox->getFontStyle();
|
||||
$sfIsObject = ($styleFont instanceof Font) ? true : false;
|
||||
if (!$withoutP) {
|
||||
$xmlWriter->startElement('w:p');
|
||||
$styleParagraph = $checkbox->getParagraphStyle();
|
||||
$spIsObject = ($styleParagraph instanceof Paragraph) ? true : false;
|
||||
if ($spIsObject) {
|
||||
$this->_writeParagraphStyle($xmlWriter, $styleParagraph);
|
||||
} elseif (!$spIsObject && !is_null($styleParagraph)) {
|
||||
$xmlWriter->startElement('w:pPr');
|
||||
$xmlWriter->startElement('w:pStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleParagraph);
|
||||
$xmlWriter->endElement();
|
||||
$xmlWriter->endElement();
|
||||
}
|
||||
}
|
||||
|
||||
$xmlWriter->startElement('w:r');
|
||||
$xmlWriter->startElement('w:fldChar');
|
||||
$xmlWriter->writeAttribute('w:fldCharType', 'begin');
|
||||
$xmlWriter->startElement('w:ffData');
|
||||
$xmlWriter->startElement('w:name');
|
||||
$xmlWriter->writeAttribute('w:val', $name);
|
||||
$xmlWriter->endElement(); //w:name
|
||||
$xmlWriter->writeAttribute('w:enabled', '');
|
||||
$xmlWriter->startElement('w:calcOnExit');
|
||||
$xmlWriter->writeAttribute('w:val', '0');
|
||||
$xmlWriter->endElement(); //w:calcOnExit
|
||||
$xmlWriter->startElement('w:checkBox');
|
||||
$xmlWriter->writeAttribute('w:sizeAuto', '');
|
||||
$xmlWriter->startElement('w:default');
|
||||
$xmlWriter->writeAttribute('w:val', ($checkState ? '1' : '0'));
|
||||
$xmlWriter->endElement(); //w:default
|
||||
$xmlWriter->endElement(); //w:checkBox
|
||||
$xmlWriter->endElement(); // w:ffData
|
||||
$xmlWriter->endElement(); // w:fldChar
|
||||
$xmlWriter->endElement(); // w:r
|
||||
|
||||
$xmlWriter->startElement('w:bookmarkStart');
|
||||
$xmlWriter->writeAttribute('w:name', $name);
|
||||
$xmlWriter->writeAttribute('w:id', $count);
|
||||
$xmlWriter->startElement('w:r');
|
||||
$xmlWriter->startElement('w:instrText');
|
||||
$xmlWriter->writeAttribute('xml:space', 'preserve');
|
||||
$xmlWriter->writeRaw(' FORMCHECKBOX ');
|
||||
$xmlWriter->endElement();// w:instrText
|
||||
$xmlWriter->endElement(); // w:r
|
||||
$xmlWriter->startElement('w:r');
|
||||
$xmlWriter->startElement('w:fldChar');
|
||||
$xmlWriter->writeAttribute('w:fldCharType', 'seperate');
|
||||
$xmlWriter->endElement();// w:fldChar
|
||||
$xmlWriter->endElement(); // w:r
|
||||
$xmlWriter->startElement('w:r');
|
||||
$xmlWriter->startElement('w:fldChar');
|
||||
$xmlWriter->writeAttribute('w:fldCharType', 'end');
|
||||
$xmlWriter->endElement();// w:fldChar
|
||||
$xmlWriter->endElement(); // w:r
|
||||
$xmlWriter->endElement(); // w:bookmarkStart
|
||||
$xmlWriter->startElement('w:bookmarkEnd');
|
||||
$xmlWriter->writeAttribute('w:id', $count);
|
||||
$xmlWriter->endElement();// w:bookmarkEnd
|
||||
|
||||
$xmlWriter->startElement('w:r');
|
||||
if ($sfIsObject) {
|
||||
$this->_writeTextStyle($xmlWriter, $styleFont);
|
||||
} elseif (!$sfIsObject && !is_null($styleFont)) {
|
||||
$xmlWriter->startElement('w:rPr');
|
||||
$xmlWriter->startElement('w:rStyle');
|
||||
$xmlWriter->writeAttribute('w:val', $styleFont);
|
||||
$xmlWriter->endElement();
|
||||
$xmlWriter->endElement();
|
||||
}
|
||||
$xmlWriter->startElement('w:t');
|
||||
$xmlWriter->writeAttribute('xml:space', 'preserve');
|
||||
$xmlWriter->writeRaw($text);
|
||||
$xmlWriter->endElement(); // w:t
|
||||
$xmlWriter->endElement(); // w:r
|
||||
|
||||
if (!$withoutP) {
|
||||
$xmlWriter->endElement(); // w:p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ use PhpOffice\PhpWord\Section\Text;
|
|||
use PhpOffice\PhpWord\Section\TextBreak;
|
||||
use PhpOffice\PhpWord\Section\TextRun;
|
||||
use PhpOffice\PhpWord\Section\Title;
|
||||
use PhpOffice\PhpWord\Section\CheckBox;
|
||||
use PhpOffice\PhpWord\Shared\XMLWriter;
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
|
|
@ -73,7 +74,6 @@ class Document extends Base
|
|||
$pSection++;
|
||||
|
||||
$_elements = $section->getElements();
|
||||
|
||||
foreach ($_elements as $element) {
|
||||
if ($element instanceof Text) {
|
||||
$this->_writeText($xmlWriter, $element);
|
||||
|
|
@ -99,6 +99,8 @@ class Document extends Base
|
|||
$this->_writeTOC($xmlWriter);
|
||||
} elseif ($element instanceof Footnote) {
|
||||
$this->_writeFootnote($xmlWriter, $element);
|
||||
} elseif ($element instanceof CheckBox) {
|
||||
$this->_writeCheckBox($xmlWriter, $element);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Tests\Section;
|
||||
|
||||
use PhpOffice\PhpWord\Section\CheckBox;
|
||||
|
||||
/**
|
||||
* Test class for PhpOffice\PhpWord\Section\CheckBox
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class CheckBoxTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Construct
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
$oCheckBox = new CheckBox();
|
||||
|
||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\CheckBox', $oCheckBox);
|
||||
$this->assertEquals(null, $oCheckBox->getText());
|
||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oCheckBox->getFontStyle());
|
||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oCheckBox->getParagraphStyle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name and text
|
||||
*/
|
||||
public function testCheckBox()
|
||||
{
|
||||
$oCheckBox = new CheckBox('chkBox', 'CheckBox');
|
||||
|
||||
$this->assertEquals($oCheckBox->getName(), 'chkBox');
|
||||
$this->assertEquals($oCheckBox->getText(), 'CheckBox');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font style
|
||||
*/
|
||||
public function testFont()
|
||||
{
|
||||
$oCheckBox = new CheckBox('chkBox', 'CheckBox', 'fontStyle');
|
||||
$this->assertEquals($oCheckBox->getFontStyle(), 'fontStyle');
|
||||
|
||||
$oCheckBox->setFontStyle(array('bold' => true, 'italic' => true, 'size' => 16));
|
||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oCheckBox->getFontStyle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paragraph style
|
||||
*/
|
||||
public function testParagraph()
|
||||
{
|
||||
$oCheckBox = new CheckBox('chkBox', 'CheckBox', 'fontStyle', 'paragraphStyle');
|
||||
$this->assertEquals($oCheckBox->getParagraphStyle(), 'paragraphStyle');
|
||||
|
||||
$oCheckBox->setParagraphStyle(array('align' => 'center', 'spaceAfter' => 100));
|
||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oCheckBox->getParagraphStyle());
|
||||
}
|
||||
}
|
||||
|
|
@ -202,6 +202,16 @@ class CellTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\TextRun', $element);
|
||||
}
|
||||
|
||||
public function testAddCheckBox()
|
||||
{
|
||||
$oCell = new Cell('section', 1);
|
||||
$element = $oCell->addCheckBox('check1', 'text');
|
||||
|
||||
$this->assertCount(1, $oCell->getElements());
|
||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\Section\\CheckBox', $element);
|
||||
}
|
||||
|
||||
|
||||
public function testGetElements()
|
||||
{
|
||||
$oCell = new Cell('section', 1);
|
||||
|
|
|
|||
|
|
@ -88,12 +88,13 @@ class SectionTest extends \PHPUnit_Framework_TestCase
|
|||
$section->addTitle(utf8_decode('ä'), 1);
|
||||
$section->createTextRun();
|
||||
$section->createFootnote();
|
||||
$section->addCheckBox('check1', utf8_decode('ä'));
|
||||
$section->addTOC();
|
||||
|
||||
$elementCollection = $section->getElements();
|
||||
$elementTypes = array('Text', 'Link', 'TextBreak', 'PageBreak',
|
||||
'Table', 'ListItem', 'Object', 'Image', 'Image',
|
||||
'Title', 'TextRun', 'Footnote');
|
||||
'Title', 'TextRun', 'Footnote', 'CheckBox');
|
||||
$i = 0;
|
||||
foreach ($elementTypes as $elementType) {
|
||||
$this->assertInstanceOf("PhpOffice\\PhpWord\\Section\\{$elementType}", $elementCollection[$i]);
|
||||
|
|
|
|||
|
|
@ -374,4 +374,23 @@ class BaseTest extends \PHPUnit_Framework_TestCase
|
|||
$element = "/w:document/w:body/w:p/w:r/w:fldChar";
|
||||
$this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType'));
|
||||
}
|
||||
|
||||
/**
|
||||
* covers ::_writeCheckbox
|
||||
*/
|
||||
public function testWriteCheckbox()
|
||||
{
|
||||
$rStyle = 'rStyle';
|
||||
$pStyle = 'pStyle';
|
||||
|
||||
$phpWord = new PhpWord();
|
||||
$phpWord->addFontStyle($rStyle, array('bold' => true));
|
||||
$phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
|
||||
$section = $phpWord->createSection();
|
||||
$section->addCheckbox('Check1', 'Test', $rStyle, $pStyle);
|
||||
$doc = TestHelperDOCX::getDocument($phpWord);
|
||||
|
||||
$element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name';
|
||||
$this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue