From 1ab93e7e8a188ba9a984784b9c5c0a90d492ee06 Mon Sep 17 00:00:00 2001 From: sergeizelenyi Date: Mon, 23 Jan 2017 16:15:33 +0300 Subject: [PATCH 1/5] added functionality specified alias and tag --- src/PhpWord/Element/SDT.php | 46 +++++++++++++++++++++ src/PhpWord/Writer/Word2007/Element/SDT.php | 4 ++ 2 files changed, 50 insertions(+) diff --git a/src/PhpWord/Element/SDT.php b/src/PhpWord/Element/SDT.php index 58a477d9..68a79108 100644 --- a/src/PhpWord/Element/SDT.php +++ b/src/PhpWord/Element/SDT.php @@ -45,6 +45,20 @@ class SDT extends Text */ private $listItems = array(); + /** + * Alias + * + * @var string + */ + private $alias; + + /** + * Tag + * + * @var string + */ + private $tag; + /** * Create new instance * @@ -127,4 +141,36 @@ class SDT extends Text return $this; } + + /** + * @return string + */ + public function getTag() + { + return $this->tag; + } + + /** + * @param string $tag + */ + public function setTag($tag) + { + $this->tag = $tag; + } + + /** + * @return mixed + */ + public function getAlias() + { + return $this->alias; + } + + /** + * @param mixed $alias + */ + public function setAlias($alias) + { + $this->alias = $alias; + } } diff --git a/src/PhpWord/Writer/Word2007/Element/SDT.php b/src/PhpWord/Writer/Word2007/Element/SDT.php index 313bf7e0..b4b241aa 100644 --- a/src/PhpWord/Writer/Word2007/Element/SDT.php +++ b/src/PhpWord/Writer/Word2007/Element/SDT.php @@ -43,6 +43,8 @@ class SDT extends Text } $type = $element->getType(); $writeFormField = "write{$type}"; + $alias = $element->getAlias(); + $tag = $element->getTag(); $this->startElementP(); @@ -50,6 +52,8 @@ class SDT extends Text // Properties $xmlWriter->startElement('w:sdtPr'); + $xmlWriter->writeElementBlock('w:alias', 'w:val', $alias); + $xmlWriter->writeElementBlock('w:tag', 'w:val', $tag); $xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999)); $xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked'); $this->$writeFormField($xmlWriter, $element); From 8b960c79d5bb2ec1c24e4f97118edf574e457f54 Mon Sep 17 00:00:00 2001 From: sergeizelenyi Date: Mon, 23 Jan 2017 16:43:56 +0300 Subject: [PATCH 2/5] stylization code --- src/PhpWord/Element/SDT.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Element/SDT.php b/src/PhpWord/Element/SDT.php index 68a79108..89a14197 100644 --- a/src/PhpWord/Element/SDT.php +++ b/src/PhpWord/Element/SDT.php @@ -143,6 +143,8 @@ class SDT extends Text } /** + * Get tag + * * @return string */ public function getTag() @@ -151,15 +153,22 @@ class SDT extends Text } /** + * Set tag + * * @param string $tag + * @return self */ public function setTag($tag) { $this->tag = $tag; + + return $this; } /** - * @return mixed + * Get alias + * + * @return string */ public function getAlias() { @@ -167,10 +176,15 @@ class SDT extends Text } /** - * @param mixed $alias + * Set alias + * + * @param string $alias + * @return self */ public function setAlias($alias) { $this->alias = $alias; + + return $this; } } From 9ac9016f5071f70c05e59222c18168b1147d099a Mon Sep 17 00:00:00 2001 From: sergeizelenyi Date: Mon, 23 Jan 2017 16:15:33 +0300 Subject: [PATCH 3/5] added functionality specified alias and tag --- src/PhpWord/Element/SDT.php | 46 +++++++++++++++++++++ src/PhpWord/Writer/Word2007/Element/SDT.php | 4 ++ 2 files changed, 50 insertions(+) diff --git a/src/PhpWord/Element/SDT.php b/src/PhpWord/Element/SDT.php index 88ee7238..3e29410f 100644 --- a/src/PhpWord/Element/SDT.php +++ b/src/PhpWord/Element/SDT.php @@ -45,6 +45,20 @@ class SDT extends Text */ private $listItems = array(); + /** + * Alias + * + * @var string + */ + private $alias; + + /** + * Tag + * + * @var string + */ + private $tag; + /** * Create new instance * @@ -126,4 +140,36 @@ class SDT extends Text return $this; } + + /** + * @return string + */ + public function getTag() + { + return $this->tag; + } + + /** + * @param string $tag + */ + public function setTag($tag) + { + $this->tag = $tag; + } + + /** + * @return mixed + */ + public function getAlias() + { + return $this->alias; + } + + /** + * @param mixed $alias + */ + public function setAlias($alias) + { + $this->alias = $alias; + } } diff --git a/src/PhpWord/Writer/Word2007/Element/SDT.php b/src/PhpWord/Writer/Word2007/Element/SDT.php index e77f87e9..c6ea9f01 100644 --- a/src/PhpWord/Writer/Word2007/Element/SDT.php +++ b/src/PhpWord/Writer/Word2007/Element/SDT.php @@ -41,6 +41,8 @@ class SDT extends Text } $type = $element->getType(); $writeFormField = "write{$type}"; + $alias = $element->getAlias(); + $tag = $element->getTag(); $this->startElementP(); @@ -48,6 +50,8 @@ class SDT extends Text // Properties $xmlWriter->startElement('w:sdtPr'); + $xmlWriter->writeElementBlock('w:alias', 'w:val', $alias); + $xmlWriter->writeElementBlock('w:tag', 'w:val', $tag); $xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999)); $xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked'); $this->$writeFormField($xmlWriter, $element); From 9c1c544954833115d195d36688b9e86c7960a2ea Mon Sep 17 00:00:00 2001 From: sergeizelenyi Date: Mon, 23 Jan 2017 16:43:56 +0300 Subject: [PATCH 4/5] stylization code --- src/PhpWord/Element/SDT.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Element/SDT.php b/src/PhpWord/Element/SDT.php index 3e29410f..6d7f103f 100644 --- a/src/PhpWord/Element/SDT.php +++ b/src/PhpWord/Element/SDT.php @@ -142,6 +142,8 @@ class SDT extends Text } /** + * Get tag + * * @return string */ public function getTag() @@ -150,15 +152,22 @@ class SDT extends Text } /** + * Set tag + * * @param string $tag + * @return self */ public function setTag($tag) { $this->tag = $tag; + + return $this; } /** - * @return mixed + * Get alias + * + * @return string */ public function getAlias() { @@ -166,10 +175,15 @@ class SDT extends Text } /** - * @param mixed $alias + * Set alias + * + * @param string $alias + * @return self */ public function setAlias($alias) { $this->alias = $alias; + + return $this; } } From 1e9203adc9daa0fffe1693f19a8a57832fd227aa Mon Sep 17 00:00:00 2001 From: troosan Date: Mon, 6 Nov 2017 21:47:02 +0100 Subject: [PATCH 5/5] add unit tests --- src/PhpWord/Element/SDT.php | 2 +- src/PhpWord/Writer/Word2007/Element/SDT.php | 6 +++--- tests/PhpWord/Element/SDTTest.php | 6 ++++++ tests/PhpWord/Writer/Word2007/ElementTest.php | 14 +++++++++----- tests/PhpWord/_includes/XmlDocument.php | 18 ++++++++++++++++++ 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/PhpWord/Element/SDT.php b/src/PhpWord/Element/SDT.php index 6d7f103f..6d4207b7 100644 --- a/src/PhpWord/Element/SDT.php +++ b/src/PhpWord/Element/SDT.php @@ -143,7 +143,7 @@ class SDT extends Text /** * Get tag - * + * * @return string */ public function getTag() diff --git a/src/PhpWord/Writer/Word2007/Element/SDT.php b/src/PhpWord/Writer/Word2007/Element/SDT.php index c6ea9f01..908f9f49 100644 --- a/src/PhpWord/Writer/Word2007/Element/SDT.php +++ b/src/PhpWord/Writer/Word2007/Element/SDT.php @@ -50,10 +50,10 @@ class SDT extends Text // Properties $xmlWriter->startElement('w:sdtPr'); - $xmlWriter->writeElementBlock('w:alias', 'w:val', $alias); - $xmlWriter->writeElementBlock('w:tag', 'w:val', $tag); - $xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999)); + $xmlWriter->writeElementIf($alias != null, 'w:alias', 'w:val', $alias); $xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked'); + $xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999)); + $xmlWriter->writeElementIf($tag != null, 'w:tag', 'w:val', $tag); $this->$writeFormField($xmlWriter, $element); $xmlWriter->endElement(); // w:sdtPr diff --git a/tests/PhpWord/Element/SDTTest.php b/tests/PhpWord/Element/SDTTest.php index fc3682b6..537841a6 100644 --- a/tests/PhpWord/Element/SDTTest.php +++ b/tests/PhpWord/Element/SDTTest.php @@ -32,14 +32,20 @@ class SDTTest extends \PHPUnit_Framework_TestCase $types = array('comboBox', 'dropDownList', 'date'); $type = $types[rand(0, 2)]; $value = rand(0, 100); + $alias = 'alias'; + $tag = 'my_tag'; $object = new SDT($type); $object->setValue($value); $object->setListItems($types); + $object->setAlias($alias); + $object->setTag($tag); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\SDT', $object); $this->assertEquals($type, $object->getType()); $this->assertEquals($types, $object->getListItems()); $this->assertEquals($value, $object->getValue()); + $this->assertEquals($alias, $object->getAlias()); + $this->assertEquals($tag, $object->getTag()); } /** diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index 6186695b..0ebc7c57 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -269,13 +269,17 @@ class ElementTest extends \PHPUnit_Framework_TestCase $section->addSDT('comboBox'); $section->addSDT('dropDownList'); - $section->addSDT('date'); + $section->addSDT('date')->setAlias('date_alias')->setTag('my_tag'); $doc = TestHelperDOCX::getDocument($phpWord); - $path = '/w:document/w:body/w:p/w:sdt/w:sdtPr'; - $this->assertTrue($doc->elementExists($path . '/w:comboBox')); - $this->assertTrue($doc->elementExists($path . '/w:dropDownList')); - $this->assertTrue($doc->elementExists($path . '/w:date')); + $path = '/w:document/w:body/w:p'; + + $this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtPr/w:comboBox')); + $this->assertTrue($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:dropDownList')); + $this->assertFalse($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:alias')); + $this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:date')); + $this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:alias')); + $this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:tag')); } } diff --git a/tests/PhpWord/_includes/XmlDocument.php b/tests/PhpWord/_includes/XmlDocument.php index ef56ed15..eb335278 100644 --- a/tests/PhpWord/_includes/XmlDocument.php +++ b/tests/PhpWord/_includes/XmlDocument.php @@ -162,4 +162,22 @@ class XmlDocument return !($nodeList->length == 0); } + + /** + * Returns the xml, or part of it as a formatted string + * + * @param string $path + * @param string $file + * @return string + */ + public function printXml($path = '/w:document', $file = 'word/document.xml') + { + $newdoc = new \DOMDocument(); + $newdoc->formatOutput = true; + $newdoc->preserveWhiteSpace = false; + $node = $newdoc->importNode($this->getElement($path, $file), true); + $newdoc->appendChild($node); + + return $newdoc->saveXML($node); + } }