From 258c9c6b9e24c872947a91ce769af5fa478d69c3 Mon Sep 17 00:00:00 2001 From: ozilion Date: Thu, 20 Mar 2014 16:27:19 +0200 Subject: [PATCH 1/2] 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. --- Classes/PHPWord/Section.php | 20 ++ Classes/PHPWord/Section/CheckBox.php | 198 +++++++++++++++++++ Classes/PHPWord/Section/Table/Cell.php | 20 ++ Classes/PHPWord/Writer/Word2007/Base.php | 114 +++++++++++ Classes/PHPWord/Writer/Word2007/Document.php | 12 +- 5 files changed, 359 insertions(+), 5 deletions(-) create mode 100644 Classes/PHPWord/Section/CheckBox.php diff --git a/Classes/PHPWord/Section.php b/Classes/PHPWord/Section.php index 07a17a31..f20cfd95 100755 --- a/Classes/PHPWord/Section.php +++ b/Classes/PHPWord/Section.php @@ -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 * diff --git a/Classes/PHPWord/Section/CheckBox.php b/Classes/PHPWord/Section/CheckBox.php new file mode 100644 index 00000000..af02356a --- /dev/null +++ b/Classes/PHPWord/Section/CheckBox.php @@ -0,0 +1,198 @@ +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; + } + +} diff --git a/Classes/PHPWord/Section/Table/Cell.php b/Classes/PHPWord/Section/Table/Cell.php index f8d6d340..1a0d1c6a 100755 --- a/Classes/PHPWord/Section/Table/Cell.php +++ b/Classes/PHPWord/Section/Table/Cell.php @@ -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 * diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index e9249120..9ae5602b 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -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) { diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index abdd92cc..b6764b40 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -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(); } From ab6503eb0dcd7305c5e9895f095672440b2d39ef Mon Sep 17 00:00:00 2001 From: ozilion Date: Thu, 27 Mar 2014 10:17:07 +0200 Subject: [PATCH 2/2] Test folder updated Test folder updated for new file CheckBoxText --- Classes/PHPWord/Section.php | 1 + Classes/PHPWord/Section/CheckBox.php | 5 +-- Classes/PHPWord/Section/Table/Cell.php | 1 + Classes/PHPWord/Section/Text.php | 2 +- Tests/PHPWord/AutoloaderTest.php | 1 + Tests/PHPWord/Section/CheckBoxTest.php | 42 ++++++++++++++++++++++ Tests/PHPWord/Section/Table/CellTest.php | 9 +++++ Tests/PHPWord/SectionTest.php | 2 ++ Tests/PHPWord/Writer/Word2007/BaseTest.php | 20 +++++++++++ 9 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 Tests/PHPWord/Section/CheckBoxTest.php diff --git a/Classes/PHPWord/Section.php b/Classes/PHPWord/Section.php index f20cfd95..e65267df 100755 --- a/Classes/PHPWord/Section.php +++ b/Classes/PHPWord/Section.php @@ -128,6 +128,7 @@ class PHPWord_Section /** * Add a CheckBox Element * + * @param string $name * @param string $text * @param mixed $style * @return PHPWord_Section_CheckBox diff --git a/Classes/PHPWord/Section/CheckBox.php b/Classes/PHPWord/Section/CheckBox.php index af02356a..03f8228a 100644 --- a/Classes/PHPWord/Section/CheckBox.php +++ b/Classes/PHPWord/Section/CheckBox.php @@ -68,6 +68,7 @@ class PHPWord_Section_CheckBox /** * Create a new Text Element * + * @param string $name * @param string $text * @param null|array|\PHPWord_Style_Font $fontStyle * @param null|array|\PHPWord_Style_Paragraph $paragraphStyle @@ -149,9 +150,9 @@ class PHPWord_Section_CheckBox * @param string $name * @return $this */ - public function setName($text) + public function setName($name) { - $this->name = $text; + $this->name = $name; return $this; } diff --git a/Classes/PHPWord/Section/Table/Cell.php b/Classes/PHPWord/Section/Table/Cell.php index 1a0d1c6a..e18dd829 100755 --- a/Classes/PHPWord/Section/Table/Cell.php +++ b/Classes/PHPWord/Section/Table/Cell.php @@ -116,6 +116,7 @@ class PHPWord_Section_Table_Cell /** * Add a CheckBox Element * + * @param string $name * @param string $text * @param mixed $style * @return PHPWord_Section_CheckBox diff --git a/Classes/PHPWord/Section/Text.php b/Classes/PHPWord/Section/Text.php index 8631b66e..2a96a2d9 100755 --- a/Classes/PHPWord/Section/Text.php +++ b/Classes/PHPWord/Section/Text.php @@ -139,7 +139,7 @@ class PHPWord_Section_Text $this->text = $text; return $this; } - + /** * Get Text content * diff --git a/Tests/PHPWord/AutoloaderTest.php b/Tests/PHPWord/AutoloaderTest.php index 03e16db0..b1899770 100644 --- a/Tests/PHPWord/AutoloaderTest.php +++ b/Tests/PHPWord/AutoloaderTest.php @@ -1,6 +1,7 @@ assertInstanceOf('PHPWord_Section_CheckBox', $oCheckBox); + $this->assertEquals(null, $oCheckBox->getText()); + $this->assertInstanceOf('PHPWord_Style_Font', $oCheckBox->getFontStyle()); + $this->assertInstanceOf('PHPWord_Style_Paragraph', $oCheckBox->getParagraphStyle()); + } + + public function testCheckBox() + { + $oCheckBox = new PHPWord_Section_CheckBox('CheckBox'); + + $this->assertEquals($oCheckBox->getText(), 'CheckBox'); + } + + public function testFont() + { + $oCheckBox = new PHPWord_Section_CheckBox('CheckBox', 'fontStyle'); + $this->assertEquals($oCheckBox->getFontStyle(), 'fontStyle'); + + $oCheckBox->setFontStyle(array('bold' => true, 'italic' => true, 'size' => 16)); + $this->assertInstanceOf('PHPWord_Style_Font', $oCheckBox->getFontStyle()); + } + + public function testParagraph() + { + $oCheckBox = new PHPWord_Section_CheckBox('CheckBox', 'fontStyle', 'paragraphStyle'); + $this->assertEquals($oCheckBox->getParagraphStyle(), 'paragraphStyle'); + + $oCheckBox->setParagraphStyle(array('align' => 'center', 'spaceAfter' => 100)); + $this->assertInstanceOf('PHPWord_Style_Paragraph', $oCheckBox->getParagraphStyle()); + } +} diff --git a/Tests/PHPWord/Section/Table/CellTest.php b/Tests/PHPWord/Section/Table/CellTest.php index 30a7fcdb..be2878f1 100644 --- a/Tests/PHPWord/Section/Table/CellTest.php +++ b/Tests/PHPWord/Section/Table/CellTest.php @@ -40,6 +40,15 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PHPWord_Section_Text', $element); } + public function testaddCheckBox() + { + $oCell = new PHPWord_Section_Table_Cell('section', 1); + $element = $oCell->addCheckBox('check1', 'text'); + + $this->assertCount(1, $oCell->getElements()); + $this->assertInstanceOf('PHPWord_Section_CheckBox', $element); + } + public function testAddTextNotUTF8() { $oCell = new PHPWord_Section_Table_Cell('section', 1); diff --git a/Tests/PHPWord/SectionTest.php b/Tests/PHPWord/SectionTest.php index ac540d06..1bcd108c 100644 --- a/Tests/PHPWord/SectionTest.php +++ b/Tests/PHPWord/SectionTest.php @@ -61,6 +61,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase /** * @covers PHPWord_Section::addText + * @covers PHPWord_Section::addCheckBox * @covers PHPWord_Section::addLink * @covers PHPWord_Section::addTextBreak * @covers PHPWord_Section::addPageBreak @@ -88,6 +89,7 @@ class SectionTest extends \PHPUnit_Framework_TestCase $section = new PHPWord_Section(0); $section->addText(utf8_decode('ä')); + $section->addCheckBox('check1', utf8_decode('ä')); $section->addLink(utf8_decode('http://äää.com'), utf8_decode('ä')); $section->addTextBreak(); $section->addPageBreak(); diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index fde6e30b..1ede88f4 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -43,6 +43,26 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals($pStyle, $doc->getElementAttribute($element, 'w:val')); } + /** + * 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 = $doc->getElement('/w:document/w:body/w:p/w:checkbox/w:r/w:t'); + + $this->assertEquals($expected, $element->nodeValue); + } + /** * covers ::_writeTextRun */