Skip to content

Commit b5a63c5

Browse files
committed
Elaborate SDT elements
1 parent b2daeed commit b5a63c5

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44
// New Word document
55
echo date('H:i:s'), " Create new PhpWord object", EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7-
$phpWord->getProtection()->setEditing('forms');
87

98
$section = $phpWord->addSection();
109

11-
$section->addSDT('comboBox')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'));
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'));
1221

1322
// Save file
1423
echo write($phpWord, basename(__FILE__, '.php'), $writers);

src/PhpWord/Element/AbstractContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ private function checkValidity($method)
193193
'Line' => $allContainers,
194194
'Shape' => $allContainers,
195195
'FormField' => $allContainers,
196+
'SDT' => $allContainers,
196197
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
197198
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
198199
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
@@ -206,7 +207,6 @@ private function checkValidity($method)
206207
'TOC' => array('Section'),
207208
'PageBreak' => array('Section'),
208209
'Chart' => array('Section'),
209-
'SDT' => array('Section'),
210210
);
211211
// Special condition, e.g. preservetext can only exists in cell when
212212
// the cell is located in header or footer

src/PhpWord/Writer/Word2007/Element/SDT.php

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,72 @@ public function write()
3939
return;
4040
}
4141
$type = $element->getType();
42-
$listItems = $element->getListItems();
42+
$writeFormField = "write{$type}";
4343

4444
$this->startElementP();
4545

4646
$xmlWriter->startElement('w:sdt');
4747

48+
// Properties
4849
$xmlWriter->startElement('w:sdtPr');
4950
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
5051
$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
5165

52-
$xmlWriter->startElement('w:placeholder');
53-
$xmlWriter->writeElementBlock('w:docPart', 'w:val', 'string');
54-
$xmlWriter->endElement(); // w:placeholder
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();
5578

5679
$xmlWriter->startElement("w:{$type}");
5780
foreach ($listItems as $key => $val) {
5881
$xmlWriter->writeElementBlock('w:listItem', array('w:value' => $key, 'w:displayText' => $val));
5982
}
6083
$xmlWriter->endElement(); // w:{$type}
84+
}
6185

62-
$xmlWriter->endElement(); // w:sdtPr
63-
64-
$xmlWriter->startElement('w:sdtContent');
65-
$xmlWriter->endElement(); // w:sdtContent
66-
67-
$xmlWriter->endElement(); // w:sdt
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+
}
6895

69-
$this->endElementP(); // w:p
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
70109
}
71110
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)