From 258c9c6b9e24c872947a91ce769af5fa478d69c3 Mon Sep 17 00:00:00 2001 From: ozilion Date: Thu, 20 Mar 2014 16:27:19 +0200 Subject: [PATCH] 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(); }