Skip to content

Commit 079d08e

Browse files
author
Bas-Jan 't Jong
committed
Added Field Element
1 parent 9e99080 commit 079d08e

File tree

5 files changed

+225
-46
lines changed

5 files changed

+225
-46
lines changed

samples/Sample_27_Field.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// Begin code
9+
$section = $phpWord->addSection();
10+
11+
// Add Field elements
12+
// See Element/Field.php for all options
13+
$section->addField('DATE', array('dateformat'=>'d-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar'));
14+
$section->addField('PAGE', array('format'=>'ArabicDash'));
15+
$section->addField('NUMPAGES', array('format'=>'Arabic', 'numformat'=>'0,00'), array('PreserveFormat'));
16+
17+
// Save file
18+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
19+
if (!CLI) {
20+
include_once 'Sample_Footer.php';
21+
}

src/PhpWord/Element/AbstractContainer.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function addElement($elementName)
5656

5757
// Get arguments
5858
$args = func_get_args();
59-
$withoutP = in_array($this->container, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun'));
59+
$withoutP = in_array($this->container, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun', 'Field'));
6060
if ($withoutP && ($elementName == 'Text' || $elementName == 'PreserveText')) {
6161
$args[3] = null; // Remove paragraph style for texts in textrun
6262
}
@@ -147,14 +147,10 @@ public function addTextRun($paragraphStyle = null)
147147
* Add field element
148148
* @param
149149
*/
150-
public function addField($type = null, $properties = array(), $options = array()){
151-
//$this->checkValidity('Field');
152-
$elementDocPart = $this->checkElementDocPart();
153-
$element = new Field($type, $properties, $options);
154-
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
155-
$this->addElement($element);
150+
public function addField($type = null, $properties = array(), $options = array())
151+
{
152+
return $this->addElement('Field', $type, $properties, $options);
156153

157-
return $element;
158154
}
159155

160156
/**

src/PhpWord/Element/Field.php

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@
1515
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
1616
*/
1717

18-
/*
19-
* <w:fldSimple w:instr=" NUMWORDS \# "€ #.##0,00;(€ #.##0,00)" \* Arabic \* MERGEFORMAT ">
20-
<w:r>
21-
<w:rPr>
22-
<w:noProof/>
23-
</w:rPr>
24-
<w:t>5</w:t>
25-
</w:r>
26-
</w:fldSimple>
27-
28-
*/
29-
30-
3118
namespace PhpOffice\PhpWord\Element;
3219

3320
use PhpOffice\PhpWord\Shared\String;
@@ -37,22 +24,35 @@
3724
*/
3825
class Field extends AbstractElement
3926
{
40-
/** @const */
4127

42-
//self::$fieldsArray;
28+
29+
/**
30+
* Field properties and options. Depending on type, a field can have different properties
31+
* and options
32+
*
33+
* @var array
34+
*/
4335
protected $fieldsArray = array(
44-
'PAGE'=>array(
45-
'properties'=>array(
46-
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
47-
),
48-
'options'=>array()
36+
'PAGE'=>array(
37+
'properties'=>array(
38+
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
39+
),
40+
'options'=>array('PreserveFormat')
4941
),
5042
'NUMPAGES'=>array(
51-
'properties'=>array(
52-
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
53-
'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%')
54-
),
55-
'options'=>array()
43+
'properties'=>array(
44+
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
45+
'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%')
46+
),
47+
'options'=>array('PreserveFormat')
48+
),
49+
'DATE'=>array(
50+
'properties'=> array(
51+
'dateformat' =>array('d-M-yyyy', 'dddd d MMMM yyyy', 'd MMMM yyyy', 'd-M-yy', 'yyyy-MM-dd',
52+
'd-MMM-yy', 'd/M/yyyy', 'd MMM. yy', 'd/M/yy', 'MMM-yy', 'd-M-yyy H:mm', 'd-M-yyyy H:mm:ss',
53+
'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss')
54+
),
55+
'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat')
5656
)
5757
);
5858

@@ -128,15 +128,14 @@ public function getType()
128128
public function setProperties($properties = array())
129129
{
130130
if (is_array($properties)) {
131-
//CREATE FUNCTION, WHICH MATCHES SUBARRAY
132-
133-
if (array_key_exists($properties, $this->fieldsArray[$this->type])) {
134-
$this->properties=array_merge($this->properties, $properties);
135-
} else {
136-
throw new \InvalidArgumentException("Invalid property");
131+
foreach ($properties as $propkey => $propval) {
132+
if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) {
133+
throw new \InvalidArgumentException("Invalid property");
134+
}
137135
}
136+
$this->properties=array_merge($this->properties, $properties);
138137
}
139-
return self;
138+
return $this->properties;
140139
}
141140

142141
/**
@@ -158,13 +157,14 @@ public function getProperties()
158157
public function setOptions($options = array())
159158
{
160159
if (is_array($options)) {
161-
if (array_key_exists($options, self::$fieldsArray[$this->type])) {
162-
$this->options=array_merge($this->options, $options);
163-
} else {
164-
throw new \InvalidArgumentException("Invalid option");
160+
foreach ($options as $optionkey => $optionval) {
161+
if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) {
162+
throw new \InvalidArgumentException("Invalid option");
163+
}
165164
}
165+
$this->options=array_merge($this->options, $options);
166166
}
167-
return self;
167+
return $this->options;
168168
}
169169

170170
/**
@@ -176,5 +176,4 @@ public function getOptions()
176176
{
177177
return $this->options;
178178
}
179-
180179
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
/**
21+
* Field element writer
22+
*
23+
* @since 0.10.0
24+
*/
25+
class Field extends Text
26+
{
27+
/**
28+
* Write field element
29+
*/
30+
public function write()
31+
{
32+
$xmlWriter = $this->getXmlWriter();
33+
$element = $this->getElement();
34+
if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
35+
return;
36+
}
37+
38+
$instruction=' '.$element->getType().' ';
39+
$properties=$element->getProperties();
40+
foreach ($properties as $propkey => $propval) {
41+
switch ($propkey) {
42+
case 'format':
43+
$instruction.='\* '.$propval.' ';
44+
break;
45+
case 'numformat':
46+
$instruction.='\* '.$propval.' ';
47+
break;
48+
case 'dateformat':
49+
$instruction.='\@ "'.$propval.'" ';
50+
break;
51+
}
52+
}
53+
54+
$options=$element->getOptions();
55+
foreach ($options as $option) {
56+
switch ($option) {
57+
case 'PreserveFormat':
58+
$instruction.='\* MERGEFORMAT ';
59+
break;
60+
case 'LunarCalendar':
61+
$instruction.='\h ';
62+
break;
63+
case 'SakaEraCalendar':
64+
$instruction.='\s ';
65+
break;
66+
case 'LastUsedFormat':
67+
$instruction.='\l ';
68+
break;
69+
}
70+
}
71+
$this->writeOpeningWP();
72+
$xmlWriter->startElement('w:fldSimple');
73+
$xmlWriter->writeAttribute('w:instr', $instruction);
74+
$xmlWriter->startElement('w:r');
75+
$xmlWriter->startElement('w:rPr');
76+
$xmlWriter->startElement('w:noProof');
77+
$xmlWriter->endElement(); // w:noProof
78+
$xmlWriter->endElement(); // w:rPr
79+
80+
$xmlWriter->startElement('w:t');
81+
$xmlWriter->writeRaw('1');
82+
$xmlWriter->endElement(); // w:t
83+
$xmlWriter->endElement(); // w:r
84+
$xmlWriter->endElement(); // w:fldSimple
85+
86+
$this->writeClosingWP();
87+
}
88+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Tests\Element;
19+
20+
use PhpOffice\PhpWord\Element\Field;
21+
use PhpOffice\PhpWord\PhpWord;
22+
23+
/**
24+
* Test class for PhpOffice\PhpWord\Element\Field
25+
*
26+
* @runTestsInSeparateProcesses
27+
*/
28+
class FieldTest extends \PHPUnit_Framework_TestCase
29+
{
30+
/**
31+
* New instance
32+
*/
33+
public function testConstructNull()
34+
{
35+
$oField = new Field();
36+
37+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
38+
}
39+
40+
/**
41+
* New instance with type
42+
*/
43+
public function testConstructWithType()
44+
{
45+
$oField = new Field('DATE');
46+
47+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
48+
$this->assertEquals($oField->getType(), 'DATE');
49+
}
50+
51+
/**
52+
* New instance with type and properties
53+
*/
54+
public function testConstructWithTypeProperties()
55+
{
56+
$oField = new Field('DATE', array('dateformat'=>'d-M-yyyy'));
57+
58+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
59+
$this->assertEquals($oField->getType(), 'DATE');
60+
$this->assertEquals($oField->getProperties(), array('dateformat'=>'d-M-yyyy'));
61+
}
62+
63+
/**
64+
* New instance with type and properties and options
65+
*/
66+
public function testConstructWithTypePropertiesOptions()
67+
{
68+
$oField = new Field('DATE', array('dateformat'=>'d-M-yyyy'), array('SakaEraCalendar', 'PreserveFormat'));
69+
70+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
71+
$this->assertEquals($oField->getType(), 'DATE');
72+
$this->assertEquals($oField->getProperties(), array('dateformat'=>'d-M-yyyy'));
73+
$this->assertEquals($oField->getOptions(), array('SakaEraCalendar', 'PreserveFormat'));
74+
}
75+
}

0 commit comments

Comments
 (0)