From cd547927eaa6385cf1d2471d642e2d60677c193a Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 20 Jun 2014 22:24:59 +0700 Subject: [PATCH] New structured document tag (SDT) element --- samples/Sample_34_SDT.php | 17 +++ src/PhpWord/Element/AbstractContainer.php | 4 +- src/PhpWord/Element/SDT.php | 130 ++++++++++++++++++ src/PhpWord/Writer/Word2007/Element/SDT.php | 71 ++++++++++ .../Tests/Writer/Word2007/ElementTest.php | 2 +- 5 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 samples/Sample_34_SDT.php create mode 100644 src/PhpWord/Element/SDT.php create mode 100644 src/PhpWord/Writer/Word2007/Element/SDT.php diff --git a/samples/Sample_34_SDT.php b/samples/Sample_34_SDT.php new file mode 100644 index 00000000..fc1e7de6 --- /dev/null +++ b/samples/Sample_34_SDT.php @@ -0,0 +1,17 @@ +getProtection()->setEditing('forms'); + +$section = $phpWord->addSection(); + +$section->addSDT('comboBox')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2')); + +// Save file +echo write($phpWord, basename(__FILE__, '.php'), $writers); +if (!CLI) { + include_once 'Sample_Footer.php'; +} diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 3ec11390..afde4270 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -43,6 +43,7 @@ namespace PhpOffice\PhpWord\Element; * @method Shape addObject(string $type, mixed $style = null) * @method Chart addChart(string $type, array $categories, array $values, array $style = null) * @method FormField addFormField(string $type, mixed $fStyle = null, mixed $pStyle = null) + * @method SDT addSDT(string $type) * * @since 0.10.0 */ @@ -79,7 +80,7 @@ abstract class AbstractContainer extends AbstractElement $elements = array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', 'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line', 'Shape', - 'Title', 'TOC', 'PageBreak', 'Chart', 'FormField'); + 'Title', 'TOC', 'PageBreak', 'Chart', 'FormField', 'SDT'); $functions = array(); for ($i = 0; $i < count($elements); $i++) { $functions[$i] = 'add' . $elements[$i]; @@ -205,6 +206,7 @@ abstract class AbstractContainer extends AbstractElement 'TOC' => array('Section'), 'PageBreak' => array('Section'), 'Chart' => array('Section'), + 'SDT' => array('Section'), ); // Special condition, e.g. preservetext can only exists in cell when // the cell is located in header or footer diff --git a/src/PhpWord/Element/SDT.php b/src/PhpWord/Element/SDT.php new file mode 100644 index 00000000..477c50f3 --- /dev/null +++ b/src/PhpWord/Element/SDT.php @@ -0,0 +1,130 @@ +setType($type); + } + + /** + * Get type + * + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * Set type + * + * @param string $value + * @return self + */ + public function setType($value) + { + $enum = array('comboBox', 'dropDownList', 'date'); + $this->type = $this->setEnumVal($value, $enum, $this->type); + + return $this; + } + + /** + * Get value + * + * @return string|bool|int + */ + public function getValue() + { + return $this->value; + } + + /** + * Set value + * + * @param string|bool|int $value + * @return self + */ + public function setValue($value) + { + $this->value = $value; + + return $this; + } + + /** + * Get listItems + * + * @return array + */ + public function getListItems() + { + return $this->listItems; + } + + /** + * Set listItems + * + * @param array $value + * @return self + */ + public function setListItems($value) + { + $this->listItems = $value; + + return $this; + } +} diff --git a/src/PhpWord/Writer/Word2007/Element/SDT.php b/src/PhpWord/Writer/Word2007/Element/SDT.php new file mode 100644 index 00000000..d25eaf98 --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Element/SDT.php @@ -0,0 +1,71 @@ +getXmlWriter(); + $element = $this->getElement(); + if (!$element instanceof SDTElement) { + return; + } + $type = $element->getType(); + $listItems = $element->getListItems(); + + $this->startElementP(); + + $xmlWriter->startElement('w:sdt'); + + $xmlWriter->startElement('w:sdtPr'); + $xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999)); + $xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked'); + + $xmlWriter->startElement('w:placeholder'); + $xmlWriter->writeElementBlock('w:docPart', 'w:val', 'string'); + $xmlWriter->endElement(); // w:placeholder + + $xmlWriter->startElement("w:{$type}"); + foreach ($listItems as $key => $val) { + $xmlWriter->writeElementBlock('w:listItem', array('w:value' => $key, 'w:displayText' => $val)); + } + $xmlWriter->endElement(); // w:{$type} + + $xmlWriter->endElement(); // w:sdtPr + + $xmlWriter->startElement('w:sdtContent'); + $xmlWriter->endElement(); // w:sdtContent + + $xmlWriter->endElement(); // w:sdt + + $this->endElementP(); // w:p + } +} diff --git a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php index 6c10f3a5..752415a1 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php @@ -41,7 +41,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase $elements = array( 'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun', 'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC', - 'Field', 'Line', 'Shape', 'Chart' + 'Field', 'Line', 'Shape', 'Chart', 'FormField', 'SDT' ); foreach ($elements as $element) { $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element;