Added addCheckBox() function

Created new file named CheckBox.php under Section folder. Added
addCheckBox() function to Section.php and \Table\Cell.php files and
_writeCheckbox(..) function to Base.php file.
This commit is contained in:
ozilion 2014-03-20 16:27:19 +02:00
parent 74566e3862
commit 258c9c6b9e
5 changed files with 359 additions and 5 deletions

View File

@ -125,6 +125,26 @@ class PHPWord_Section
return $text;
}
/**
* Add a CheckBox Element
*
* @param string $text
* @param mixed $style
* @return PHPWord_Section_CheckBox
*/
public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null)
{
if (!PHPWord_Shared_String::IsUTF8($name)) {
$name = utf8_encode($name);
}
if (!PHPWord_Shared_String::IsUTF8($text)) {
$text = utf8_encode($text);
}
$text = new PHPWord_Section_CheckBox($name, $text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $text;
return $text;
}
/**
* Add a Link Element
*

View File

@ -0,0 +1,198 @@
<?php
/**
* PHPWord
*
* Copyright (c) 2014 PHPWord
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPWord
* @package PHPWord
* @copyright Copyright (c) 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 0.8.0
*/
/**
* Class PHPWord_Section_CheckBox
*/
class PHPWord_Section_CheckBox
{
/**
* Name content
*
* @var string
*/
private $name;
/**
* Text content
*
* @var string
*/
private $text;
/**
* Text style
*
* @var PHPWord_Style_Font
*/
private $fontStyle;
/**
* Paragraph style
*
* @var PHPWord_Style_Paragraph
*/
private $paragraphStyle;
/**
* Cell Element Collection
*
* @var array
*/
private $_elementCollection = array();
/**
* Create a new Text Element
*
* @param string $text
* @param null|array|\PHPWord_Style_Font $fontStyle
* @param null|array|\PHPWord_Style_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);
}
/**
* Set Text style
*
* @param null|array|\PHPWord_Style_Font $style
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
* @return PHPWord_Style_Font
*/
public function setFontStyle($style = null, $paragraphStyle = null)
{
if ($style instanceof PHPWord_Style_Font) {
$this->fontStyle = $style;
$this->setParagraphStyle($paragraphStyle);
} elseif (is_array($style)) {
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
$this->fontStyle->setArrayStyle($style);
} elseif (null === $style) {
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
} else {
$this->fontStyle = $style;
$this->setParagraphStyle($paragraphStyle);
}
return $this->fontStyle;
}
/**
* Get Text style
*
* @return PHPWord_Style_Font
*/
public function getFontStyle()
{
return $this->fontStyle;
}
/**
* Set Paragraph style
*
* @param null|array|\PHPWord_Style_Paragraph $style
* @return null|\PHPWord_Style_Paragraph
*/
public function setParagraphStyle($style = null)
{
if (is_array($style)) {
$this->paragraphStyle = new PHPWord_Style_Paragraph;
$this->paragraphStyle->setArrayStyle($style);
} elseif ($style instanceof PHPWord_Style_Paragraph) {
$this->paragraphStyle = $style;
} elseif (null === $style) {
$this->paragraphStyle = new PHPWord_Style_Paragraph;
} else {
$this->paragraphStyle = $style;
}
return $this->paragraphStyle;
}
/**
* Get Paragraph style
*
* @return PHPWord_Style_Paragraph
*/
public function getParagraphStyle()
{
return $this->paragraphStyle;
}
/**
* @param string $name
* @return $this
*/
public function setName($text)
{
$this->name = $text;
return $this;
}
/**
* @param string $text
* @return $this
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get name content
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get Text content
*
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* Get all Elements
*
* @return array
*/
public function getElements()
{
return $this->_elementCollection;
}
}

View File

@ -113,6 +113,26 @@ class PHPWord_Section_Table_Cell
return $text;
}
/**
* Add a CheckBox Element
*
* @param string $text
* @param mixed $style
* @return PHPWord_Section_CheckBox
*/
public function addCheckBox($name, $text, $styleFont = null, $styleParagraph = null)
{
if (!PHPWord_Shared_String::IsUTF8($name)) {
$name = utf8_encode($name);
}
if (!PHPWord_Shared_String::IsUTF8($text)) {
$text = utf8_encode($text);
}
$text = new PHPWord_Section_CheckBox($name, $text, $styleFont, $styleParagraph);
$this->_elementCollection[] = $text;
return $text;
}
/**
* Add a Link Element
*

View File

@ -289,6 +289,118 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
/**
* Write CheckBox
*/
protected function _writeCheckbox(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_CheckBox $Checkbox, $withoutP = false, $checkState = false)
{
$cbCount = 1;
$_elements = $Checkbox->getElements();
if (count($_elements) > 1) {
$cbCount = $cbCount + 1;
}
$styleFont = $Checkbox->getFontStyle();
$SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
if (!$withoutP) {
$objWriter->startElement('w:p');
$styleParagraph = $Checkbox->getParagraphStyle();
$SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
if ($SpIsObject) {
$this->_writeParagraphStyle($objWriter, $styleParagraph);
} elseif (!$SpIsObject && !is_null($styleParagraph)) {
$objWriter->startElement('w:pPr');
$objWriter->startElement('w:pStyle');
$objWriter->writeAttribute('w:val', $styleParagraph);
$objWriter->endElement();
$objWriter->endElement();
}
}
$strName = htmlspecialchars($Checkbox->getName());
$strName = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strName);
$strText = htmlspecialchars($Checkbox->getText());
$strText = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strText);
$objWriter->startElement('w:r');
$objWriter->startElement('w:fldChar');
$objWriter->writeAttribute('w:fldCharType', 'begin');
$objWriter->startElement('w:ffData');
$objWriter->startElement('w:name');
$objWriter->writeAttribute('w:val', $strName);
$objWriter->endElement(); //w:name
$objWriter->writeAttribute('w:enabled', '');
$objWriter->startElement('w:calcOnExit');
$objWriter->writeAttribute('w:val', '0');
$objWriter->endElement(); //w:calcOnExit
$objWriter->startElement('w:checkBox');
$objWriter->writeAttribute('w:sizeAuto', '');
$objWriter->startElement('w:default');
if($checkState){
$objWriter->writeAttribute('w:val', '1');
}else{
$objWriter->writeAttribute('w:val', '0');
}
$objWriter->endElement(); //w:default
$objWriter->endElement(); //w:checkbox
$objWriter->endElement(); // w:ffData
$objWriter->endElement(); // w:fldChar
$objWriter->endElement(); // w:r
$objWriter->startElement('w:bookmarkStart');
$objWriter->writeAttribute('w:name', $strName);
$objWriter->writeAttribute('w:id', $cbCount);
$objWriter->startElement('w:r');
$objWriter->startElement('w:instrText');
$objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text
$objWriter->writeRaw(' FORMCHECKBOX ');
$objWriter->endElement();// w:instrText
$objWriter->endElement(); // w:r
$objWriter->startElement('w:r');
$objWriter->startElement('w:fldChar');
$objWriter->writeAttribute('w:fldCharType', 'seperate');
$objWriter->endElement();// w:fldChar
$objWriter->endElement(); // w:r
$objWriter->startElement('w:r');
$objWriter->startElement('w:fldChar');
$objWriter->writeAttribute('w:fldCharType', 'end');
$objWriter->endElement();// w:fldChar
$objWriter->endElement(); // w:r
$objWriter->endElement(); // w:bookmarkStart
$objWriter->startElement('w:bookmarkEnd');
$objWriter->writeAttribute('w:id', $cbCount);
$objWriter->endElement();// w:bookmarkEnd
$objWriter->startElement('w:r');
if ($SfIsObject) {
$this->_writeTextStyle($objWriter, $styleFont);
} elseif (!$SfIsObject && !is_null($styleFont)) {
$objWriter->startElement('w:rPr');
$objWriter->startElement('w:rStyle');
$objWriter->writeAttribute('w:val', $styleFont);
$objWriter->endElement();
$objWriter->endElement();
}
$objWriter->startElement('w:t');
$objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text
$objWriter->writeRaw($strText);
$objWriter->endElement();
$objWriter->endElement(); // w:r
if (!$withoutP) {
$objWriter->endElement(); // w:p
}
}
/**
* Write preserve text
*/
@ -610,6 +722,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$this->_writeTextRun($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_Link) {
$this->_writeLink($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_CheckBox) {
$this->_writeCheckbox($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_TextBreak) {
$this->_writeTextBreak($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_ListItem) {

View File

@ -76,6 +76,8 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base
$this->_writeTextRun($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_Link) {
$this->_writeLink($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_CheckBox) {
$this->_writeCheckbox($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_Title) {
$this->_writeTitle($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_TextBreak) {
@ -259,11 +261,11 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base
private function _writePageBreak(PHPWord_Shared_XMLWriter $objWriter = null)
{
$objWriter->startElement('w:p');
$objWriter->startElement('w:r');
$objWriter->startElement('w:br');
$objWriter->writeAttribute('w:type', 'page');
$objWriter->endElement();
$objWriter->endElement();
$objWriter->startElement('w:r');
$objWriter->startElement('w:br');
$objWriter->writeAttribute('w:type', 'page');
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
}