Skip to content

Commit 2d351c9

Browse files
committed
Merge branch 'sdt' into develop
2 parents a5c857f + b5a63c5 commit 2d351c9

File tree

6 files changed

+291
-2
lines changed

6 files changed

+291
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This release added form fields (textinput, checkbox, and dropdown), drawing shap
2020
- FormField: Ability to add textinput, checkbox, and dropdown form elements - @ivanlanin GH-266
2121
- Setting: Ability to define document protection (readOnly, comments, trackedChanges, forms) - @ivanlanin
2222
- Setting: Ability to remove [Compatibility Mode] text in the MS Word title bar - @ivanlanin
23+
- SDT: Ability to add structured document tag elements (comboBox, dropDownList, date) - @ivanlanin
2324

2425
### Bugfixes
2526

samples/Sample_34_SDT.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// New Word document
5+
echo date('H:i:s'), " Create new PhpWord object", EOL;
6+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
8+
$section = $phpWord->addSection();
9+
10+
$textrun = $section->addTextRun();
11+
$textrun->addText('Combobox: ');
12+
$textrun->addSDT('comboBox')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'));
13+
14+
$textrun = $section->addTextRun();
15+
$textrun->addText('Date: ');
16+
$textrun->addSDT('date');
17+
18+
$textrun = $section->addTextRun();
19+
$textrun->addText('Drop down list: ');
20+
$textrun->addSDT('dropDownList')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'));
21+
22+
// Save file
23+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
24+
if (!CLI) {
25+
include_once 'Sample_Footer.php';
26+
}

src/PhpWord/Element/AbstractContainer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* @method Shape addObject(string $type, mixed $style = null)
4444
* @method Chart addChart(string $type, array $categories, array $values, array $style = null)
4545
* @method FormField addFormField(string $type, mixed $fStyle = null, mixed $pStyle = null)
46+
* @method SDT addSDT(string $type)
4647
*
4748
* @since 0.10.0
4849
*/
@@ -79,7 +80,7 @@ public function __call($function, $args)
7980
$elements = array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak',
8081
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'Footnote',
8182
'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line', 'Shape',
82-
'Title', 'TOC', 'PageBreak', 'Chart', 'FormField');
83+
'Title', 'TOC', 'PageBreak', 'Chart', 'FormField', 'SDT');
8384
$functions = array();
8485
for ($i = 0; $i < count($elements); $i++) {
8586
$functions[$i] = 'add' . $elements[$i];
@@ -192,6 +193,7 @@ private function checkValidity($method)
192193
'Line' => $allContainers,
193194
'Shape' => $allContainers,
194195
'FormField' => $allContainers,
196+
'SDT' => $allContainers,
195197
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
196198
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
197199
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),

src/PhpWord/Element/SDT.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Element;
19+
20+
/**
21+
* Structured document tag (SDT) element
22+
*
23+
* @since 0.12.0
24+
*/
25+
class SDT extends Text
26+
{
27+
/**
28+
* Form field type: comboBox|dropDownList|date
29+
*
30+
* @var string
31+
*/
32+
private $type;
33+
34+
/**
35+
* Value
36+
*
37+
* @var string|bool|int
38+
*/
39+
private $value;
40+
41+
/**
42+
* CheckBox/DropDown list entries
43+
*
44+
* @var array
45+
*/
46+
private $listItems = array();
47+
48+
/**
49+
* Create new instance
50+
*
51+
* @param string $type
52+
* @param mixed $fontStyle
53+
* @param mixed $paragraphStyle
54+
* @return self
55+
*/
56+
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
57+
{
58+
$this->setType($type);
59+
}
60+
61+
/**
62+
* Get type
63+
*
64+
* @return string
65+
*/
66+
public function getType()
67+
{
68+
return $this->type;
69+
}
70+
71+
/**
72+
* Set type
73+
*
74+
* @param string $value
75+
* @return self
76+
*/
77+
public function setType($value)
78+
{
79+
$enum = array('comboBox', 'dropDownList', 'date');
80+
$this->type = $this->setEnumVal($value, $enum, $this->type);
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* Get value
87+
*
88+
* @return string|bool|int
89+
*/
90+
public function getValue()
91+
{
92+
return $this->value;
93+
}
94+
95+
/**
96+
* Set value
97+
*
98+
* @param string|bool|int $value
99+
* @return self
100+
*/
101+
public function setValue($value)
102+
{
103+
$this->value = $value;
104+
105+
return $this;
106+
}
107+
108+
/**
109+
* Get listItems
110+
*
111+
* @return array
112+
*/
113+
public function getListItems()
114+
{
115+
return $this->listItems;
116+
}
117+
118+
/**
119+
* Set listItems
120+
*
121+
* @param array $value
122+
* @return self
123+
*/
124+
public function setListItems($value)
125+
{
126+
$this->listItems = $value;
127+
128+
return $this;
129+
}
130+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
19+
20+
use PhpOffice\PhpWord\Element\SDT as SDTElement;
21+
use PhpOffice\PhpWord\Shared\XMLWriter;
22+
23+
/**
24+
* Structured document tag element writer
25+
*
26+
* @since 0.12.0
27+
* @link http://www.datypic.com/sc/ooxml/t-w_CT_SdtBlock.html
28+
*/
29+
class SDT extends Text
30+
{
31+
/**
32+
* Write element
33+
*/
34+
public function write()
35+
{
36+
$xmlWriter = $this->getXmlWriter();
37+
$element = $this->getElement();
38+
if (!$element instanceof SDTElement) {
39+
return;
40+
}
41+
$type = $element->getType();
42+
$writeFormField = "write{$type}";
43+
44+
$this->startElementP();
45+
46+
$xmlWriter->startElement('w:sdt');
47+
48+
// Properties
49+
$xmlWriter->startElement('w:sdtPr');
50+
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
51+
$xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked');
52+
$this->$writeFormField($xmlWriter, $element);
53+
$xmlWriter->endElement(); // w:sdtPr
54+
55+
// Content
56+
$xmlWriter->startElement('w:sdtContent');
57+
$xmlWriter->startElement('w:r');
58+
$xmlWriter->startElement('w:t');
59+
$xmlWriter->writeRaw('Pick value');
60+
$xmlWriter->endElement(); // w:t
61+
$xmlWriter->endElement(); // w:r
62+
$xmlWriter->endElement(); // w:sdtContent
63+
64+
$xmlWriter->endElement(); // w:sdt
65+
66+
$this->endElementP(); // w:p
67+
}
68+
69+
/**
70+
* Write combo box
71+
*
72+
* @link http://www.datypic.com/sc/ooxml/t-w_CT_SdtComboBox.html
73+
*/
74+
private function writeComboBox(XMLWriter $xmlWriter, SDTElement $element)
75+
{
76+
$type = $element->getType();
77+
$listItems = $element->getListItems();
78+
79+
$xmlWriter->startElement("w:{$type}");
80+
foreach ($listItems as $key => $val) {
81+
$xmlWriter->writeElementBlock('w:listItem', array('w:value' => $key, 'w:displayText' => $val));
82+
}
83+
$xmlWriter->endElement(); // w:{$type}
84+
}
85+
86+
/**
87+
* Write drop down list
88+
*
89+
* @link http://www.datypic.com/sc/ooxml/t-w_CT_SdtDropDownList.html
90+
*/
91+
private function writeDropDownList(XMLWriter $xmlWriter, SDTElement $element)
92+
{
93+
$this->writecomboBox($xmlWriter, $element);
94+
}
95+
96+
/**
97+
* Write date
98+
*
99+
* @link http://www.datypic.com/sc/ooxml/t-w_CT_SdtDate.html
100+
*/
101+
private function writeDate(XMLWriter $xmlWriter, SDTElement $element)
102+
{
103+
$xmlWriter->startElement("w:date");
104+
$xmlWriter->writeElementBlock('w:dateFormat', 'w:val', 'd/M/yyyy');
105+
$xmlWriter->writeElementBlock('w:lid', 'w:val', 'en-US');
106+
$xmlWriter->writeElementBlock('w:storeMappedDataAs', 'w:val', 'dateTime');
107+
$xmlWriter->writeElementBlock('w:calendar', 'w:val', 'gregorian');
108+
$xmlWriter->endElement(); // w:date
109+
}
110+
}

tests/PhpWord/Tests/Writer/Word2007/ElementTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testUnmatchedElements()
4141
$elements = array(
4242
'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun',
4343
'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC',
44-
'Field', 'Line', 'Shape', 'Chart'
44+
'Field', 'Line', 'Shape', 'Chart', 'FormField', 'SDT'
4545
);
4646
foreach ($elements as $element) {
4747
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element;
@@ -196,4 +196,24 @@ public function testFormFieldElements()
196196
$this->assertTrue($doc->elementExists($path . '/w:checkBox'));
197197
$this->assertTrue($doc->elementExists($path . '/w:ddList'));
198198
}
199+
200+
/**
201+
* Test SDT elements
202+
*/
203+
public function testSDTElements()
204+
{
205+
$phpWord = new PhpWord();
206+
$section = $phpWord->addSection();
207+
208+
$section->addSDT('comboBox');
209+
$section->addSDT('dropDownList');
210+
$section->addSDT('date');
211+
212+
$doc = TestHelperDOCX::getDocument($phpWord);
213+
214+
$path = "/w:document/w:body/w:p/w:sdt/w:sdtPr";
215+
$this->assertTrue($doc->elementExists($path . '/w:comboBox'));
216+
$this->assertTrue($doc->elementExists($path . '/w:dropDownList'));
217+
$this->assertTrue($doc->elementExists($path . '/w:date'));
218+
}
199219
}

0 commit comments

Comments
 (0)