New structured document tag (SDT) element

This commit is contained in:
Ivan Lanin 2014-06-20 22:24:59 +07:00
parent c11df2e426
commit cd547927ea
5 changed files with 222 additions and 2 deletions

17
samples/Sample_34_SDT.php Normal file
View File

@ -0,0 +1,17 @@
<?php
include_once 'Sample_Header.php';
// New Word document
echo date('H:i:s'), " Create new PhpWord object", EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->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';
}

View File

@ -43,6 +43,7 @@ namespace PhpOffice\PhpWord\Element;
* @method Shape addObject(string $type, mixed $style = null) * @method Shape addObject(string $type, mixed $style = null)
* @method Chart addChart(string $type, array $categories, array $values, array $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 FormField addFormField(string $type, mixed $fStyle = null, mixed $pStyle = null)
* @method SDT addSDT(string $type)
* *
* @since 0.10.0 * @since 0.10.0
*/ */
@ -79,7 +80,7 @@ abstract class AbstractContainer extends AbstractElement
$elements = array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak', $elements = array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak',
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'Footnote', 'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'Footnote',
'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line', 'Shape', 'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line', 'Shape',
'Title', 'TOC', 'PageBreak', 'Chart', 'FormField'); 'Title', 'TOC', 'PageBreak', 'Chart', 'FormField', 'SDT');
$functions = array(); $functions = array();
for ($i = 0; $i < count($elements); $i++) { for ($i = 0; $i < count($elements); $i++) {
$functions[$i] = 'add' . $elements[$i]; $functions[$i] = 'add' . $elements[$i];
@ -205,6 +206,7 @@ abstract class AbstractContainer extends AbstractElement
'TOC' => array('Section'), 'TOC' => array('Section'),
'PageBreak' => array('Section'), 'PageBreak' => array('Section'),
'Chart' => array('Section'), 'Chart' => array('Section'),
'SDT' => array('Section'),
); );
// Special condition, e.g. preservetext can only exists in cell when // Special condition, e.g. preservetext can only exists in cell when
// the cell is located in header or footer // the cell is located in header or footer

130
src/PhpWord/Element/SDT.php Normal file
View File

@ -0,0 +1,130 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Element;
/**
* Structured document tag (SDT) element
*
* @since 0.12.0
*/
class SDT extends Text
{
/**
* Form field type: comboBox|dropDownList|date
*
* @var string
*/
private $type;
/**
* Value
*
* @var string|bool|int
*/
private $value;
/**
* CheckBox/DropDown list entries
*
* @var array
*/
private $listItems = array();
/**
* Create new instance
*
* @param string $type
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
{
$this->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;
}
}

View File

@ -0,0 +1,71 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Element\SDT as SDTElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Structured document tag element writer
*
* @since 0.12.0
* @link http://www.datypic.com/sc/ooxml/t-w_CT_SdtBlock.html
*/
class SDT extends Text
{
/**
* Write element
*/
public function write()
{
$xmlWriter = $this->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
}
}

View File

@ -41,7 +41,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase
$elements = array( $elements = array(
'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun', 'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun',
'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC', 'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC',
'Field', 'Line', 'Shape', 'Chart' 'Field', 'Line', 'Shape', 'Chart', 'FormField', 'SDT'
); );
foreach ($elements as $element) { foreach ($elements as $element) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element; $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element;