From d52d9b8985d763f227c59ae95353ab26658ca422 Mon Sep 17 00:00:00 2001 From: antoine Date: Thu, 27 Oct 2016 23:12:26 +0200 Subject: [PATCH 01/20] Add support for XE and INDEX fields --- samples/Sample_27_Field.php | 12 ++++- src/PhpWord/Element/Field.php | 52 ++++++++++++++++++- src/PhpWord/Writer/Word2007/Element/Field.php | 42 +++++++++++++-- tests/PhpWord/Element/FieldTest.php | 14 +++++ 4 files changed, 111 insertions(+), 9 deletions(-) diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php index 57747895..7e2b968e 100644 --- a/samples/Sample_27_Field.php +++ b/samples/Sample_27_Field.php @@ -14,10 +14,18 @@ $section->addText('Date field:'); $section->addField('DATE', array('dateformat' => 'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat')); $section->addText('Page field:'); -$section->addField('PAGE', array('format' => 'ArabicDash')); +$section->addField('PAGE', array('format' => 'Arabic')); $section->addText('Number of pages field:'); -$section->addField('NUMPAGES', array('format' => 'Arabic', 'numformat' => '0,00'), array('PreserveFormat')); +$section->addField('NUMPAGES', array('numformat' => '0,00', 'format' => 'Arabic'), array('PreserveFormat')); + +$textrun = $section->addTextRun(); +$textrun->addText('An index field is '); +$textrun->addField('XE', array(), array('Bold'), 'FieldValue'); +$textrun->addText('here:'); + +$section->addText('The actual index:'); +$section->addField('INDEX', array(), array('PreserveFormat')); $textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER)); $textrun->addText('This is the date of lunar calendar '); diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 48dc1d2e..b5dec19a 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -40,7 +40,8 @@ class Field extends AbstractElement ), 'NUMPAGES'=>array( 'properties'=>array( - 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), + 'format' => array('Arabic', 'ArabicDash', 'CardText', 'DollarText', 'Ordinal', 'OrdText', + 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN', 'Caps', 'FirstCap', 'Lower', 'Upper'), 'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%') ), 'options'=>array('PreserveFormat') @@ -52,6 +53,14 @@ class Field extends AbstractElement 'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss') ), 'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat') + ), + 'XE'=>array( + 'properties' => array(), + 'options' => array('Bold', 'Italic') + ), + 'INDEX'=>array( + 'properties' => array(), + 'options'=>array('PreserveFormat') ) ); @@ -62,6 +71,13 @@ class Field extends AbstractElement */ protected $type; + /** + * Field text + * + * @var string + */ + protected $text; + /** * Field properties * @@ -83,11 +99,12 @@ class Field extends AbstractElement * @param array $properties * @param array $options */ - public function __construct($type = null, $properties = array(), $options = array()) + public function __construct($type = null, $properties = array(), $options = array(), $text = null) { $this->setType($type); $this->setProperties($properties); $this->setOptions($options); + $this->setText($text); } /** @@ -184,4 +201,35 @@ class Field extends AbstractElement { return $this->options; } + + /** + * Set Field text + * + * @param string $text + * + * @return string + * + * @throws \InvalidArgumentException + */ + public function setText($text) + { + if (isset($text)) { + if (is_string($text)) { + $this->text = $text; + } else { + throw new \InvalidArgumentException("Invalid text"); + } + } + return $this->text; + } + + /** + * Get Field text + * + * @return string + */ + public function getText() + { + return $this->text; + } } diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index ae4c66ba..ad8032c7 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -38,13 +38,18 @@ class Field extends Text } $instruction = ' ' . $element->getType() . ' '; + if ($element->getText() != null) { + $instruction .= '"' . $element->getText() . '" '; + } $properties = $element->getProperties(); foreach ($properties as $propkey => $propval) { switch ($propkey) { case 'format': - case 'numformat': $instruction .= '\* ' . $propval . ' '; break; + case 'numformat': + $instruction .= '\# ' . $propval . ' '; + break; case 'dateformat': $instruction .= '\@ "' . $propval . '" '; break; @@ -66,22 +71,49 @@ class Field extends Text case 'LastUsedFormat': $instruction .= '\l '; break; + case 'Bold': + $instruction .= '\b '; + break; + case 'Italic': + $instruction .= '\i '; + break; } } $this->startElementP(); - $xmlWriter->startElement('w:fldSimple'); - $xmlWriter->writeAttribute('w:instr', $instruction); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->text($instruction); + $xmlWriter->endElement(); // w:instrText + $xmlWriter->endElement(); // w:r + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'separate'); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:rPr'); $xmlWriter->startElement('w:noProof'); $xmlWriter->endElement(); // w:noProof $xmlWriter->endElement(); // w:rPr - $xmlWriter->writeElement('w:t', '1'); $xmlWriter->endElement(); // w:r - $xmlWriter->endElement(); // w:fldSimple + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r $this->endElementP(); // w:p } diff --git a/tests/PhpWord/Element/FieldTest.php b/tests/PhpWord/Element/FieldTest.php index b9afad1f..a37722e5 100644 --- a/tests/PhpWord/Element/FieldTest.php +++ b/tests/PhpWord/Element/FieldTest.php @@ -70,6 +70,20 @@ class FieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('SakaEraCalendar', 'PreserveFormat'), $oField->getOptions()); } + /** + * New instance with type and properties and options and text + */ + public function testConstructWithTypePropertiesOptionsText() + { + $oField = new Field('XE', array(), array('Bold'), 'FieldValue'); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); + $this->assertEquals('XE', $oField->getType()); + $this->assertEquals(array(), $oField->getProperties()); + $this->assertEquals(array('Bold'), $oField->getOptions()); + $this->assertEquals('FieldValue', $oField->getText()); + } + /** * Test setType exception * From 0953065b8c2d20c1b49f62dac7920dba91cf14cd Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 28 Oct 2016 00:20:47 +0200 Subject: [PATCH 02/20] improve code coverage --- tests/PhpWord/Element/FieldTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/PhpWord/Element/FieldTest.php b/tests/PhpWord/Element/FieldTest.php index a37722e5..d4e47e62 100644 --- a/tests/PhpWord/Element/FieldTest.php +++ b/tests/PhpWord/Element/FieldTest.php @@ -119,4 +119,16 @@ class FieldTest extends \PHPUnit_Framework_TestCase $object = new Field('PAGE'); $object->setOptions(array('foo' => 'bar')); } + + /** + * Test setText exception + * + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Invalid text + */ + public function testSetTextException() + { + $object = new Field('XE'); + $object->setText(array()); + } } From e7229fc15c12858a6f6bba4750f96e1332e7b1b9 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 28 Oct 2016 22:27:05 +0200 Subject: [PATCH 03/20] make FontStyle based on paragraph if it set --- src/PhpWord/Style/Font.php | 2 +- src/PhpWord/Writer/Word2007/Part/Styles.php | 7 +++- .../Writer/Word2007/Part/StylesTest.php | 40 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index b625e3b8..9056844a 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -725,7 +725,7 @@ class Font extends AbstractStyle } /** - * Set shading + * Set Paragraph * * @param mixed $value * @return self diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 7bcb8d92..9ad87510 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -170,6 +170,9 @@ class Styles extends AbstractPart $xmlWriter->startElement('w:link'); $xmlWriter->writeAttribute('w:val', $styleLink); $xmlWriter->endElement(); + } else if (!is_null($paragraphStyle)) { + // if type is 'paragraph' it should have a styleId + $xmlWriter->writeAttribute('w:styleId', $styleName); } // Style name @@ -178,7 +181,9 @@ class Styles extends AbstractPart $xmlWriter->endElement(); // Parent style - $xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal'); + if (!is_null($paragraphStyle)) { + $xmlWriter->writeElementBlock('w:basedOn', 'w:val', $paragraphStyle->getStyleName()); + } // w:pPr if (!is_null($paragraphStyle)) { diff --git a/tests/PhpWord/Writer/Word2007/Part/StylesTest.php b/tests/PhpWord/Writer/Word2007/Part/StylesTest.php index f40387a1..df20ab3b 100644 --- a/tests/PhpWord/Writer/Word2007/Part/StylesTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/StylesTest.php @@ -18,7 +18,10 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; +use PhpOffice\PhpWord\Style\Font; +use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWord\Writer\Word2007; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Styles @@ -74,4 +77,41 @@ class StylesTest extends \PHPUnit_Framework_TestCase $element = $doc->getElement($path, $file); $this->assertEquals('Normal', $element->getAttribute('w:val')); } + + public function testFontStyleBasedOn() + { + $phpWord = new PhpWord(); + + $baseParagraphStyle = new Paragraph(); + $baseParagraphStyle->setAlignment(Jc::CENTER); + $baseParagraphStyle = $phpWord->addParagraphStyle('BaseStyle', $baseParagraphStyle); + + $childFont = new Font(); + $childFont->setParagraph($baseParagraphStyle); + $childFont->setSize(16); + $childFont = $phpWord->addFontStyle('ChildFontStyle', $childFont); + + $otherFont = new Font(); + $otherFont->setSize(20); + $otherFont = $phpWord->addFontStyle('OtherFontStyle', $otherFont); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/styles.xml'; + + // Normal style generated? + $path = '/w:styles/w:style[@w:styleId="BaseStyle"]/w:name'; + $element = $doc->getElement($path, $file); + $this->assertEquals('BaseStyle', $element->getAttribute('w:val')); + + // Font style with paragraph should have it's base style set to that paragraphs style name + $path = '/w:styles/w:style[w:name/@w:val="ChildFontStyle"]/w:basedOn'; + $element = $doc->getElement($path, $file); + $this->assertEquals('BaseStyle', $element->getAttribute('w:val')); + + // Font style without paragraph should not have a base style set + $path = '/w:styles/w:style[w:name/@w:val="OtherFontStyle"]/w:basedOn'; + $element = $doc->getElement($path, $file); + $this->assertNull($element); + } } From 77ed1565c3d058c4ad6f3b655efdba90a0ebf0d8 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 28 Oct 2016 22:27:05 +0200 Subject: [PATCH 04/20] make FontStyle based on paragraph if it set --- src/PhpWord/Style/Font.php | 2 +- src/PhpWord/Writer/Word2007/Part/Styles.php | 7 +++- .../Writer/Word2007/Part/StylesTest.php | 40 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index b625e3b8..9056844a 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -725,7 +725,7 @@ class Font extends AbstractStyle } /** - * Set shading + * Set Paragraph * * @param mixed $value * @return self diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 7bcb8d92..9ad87510 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -170,6 +170,9 @@ class Styles extends AbstractPart $xmlWriter->startElement('w:link'); $xmlWriter->writeAttribute('w:val', $styleLink); $xmlWriter->endElement(); + } else if (!is_null($paragraphStyle)) { + // if type is 'paragraph' it should have a styleId + $xmlWriter->writeAttribute('w:styleId', $styleName); } // Style name @@ -178,7 +181,9 @@ class Styles extends AbstractPart $xmlWriter->endElement(); // Parent style - $xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal'); + if (!is_null($paragraphStyle)) { + $xmlWriter->writeElementBlock('w:basedOn', 'w:val', $paragraphStyle->getStyleName()); + } // w:pPr if (!is_null($paragraphStyle)) { diff --git a/tests/PhpWord/Writer/Word2007/Part/StylesTest.php b/tests/PhpWord/Writer/Word2007/Part/StylesTest.php index f40387a1..df20ab3b 100644 --- a/tests/PhpWord/Writer/Word2007/Part/StylesTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/StylesTest.php @@ -18,7 +18,10 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; +use PhpOffice\PhpWord\Style\Font; +use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWord\Writer\Word2007; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Styles @@ -74,4 +77,41 @@ class StylesTest extends \PHPUnit_Framework_TestCase $element = $doc->getElement($path, $file); $this->assertEquals('Normal', $element->getAttribute('w:val')); } + + public function testFontStyleBasedOn() + { + $phpWord = new PhpWord(); + + $baseParagraphStyle = new Paragraph(); + $baseParagraphStyle->setAlignment(Jc::CENTER); + $baseParagraphStyle = $phpWord->addParagraphStyle('BaseStyle', $baseParagraphStyle); + + $childFont = new Font(); + $childFont->setParagraph($baseParagraphStyle); + $childFont->setSize(16); + $childFont = $phpWord->addFontStyle('ChildFontStyle', $childFont); + + $otherFont = new Font(); + $otherFont->setSize(20); + $otherFont = $phpWord->addFontStyle('OtherFontStyle', $otherFont); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/styles.xml'; + + // Normal style generated? + $path = '/w:styles/w:style[@w:styleId="BaseStyle"]/w:name'; + $element = $doc->getElement($path, $file); + $this->assertEquals('BaseStyle', $element->getAttribute('w:val')); + + // Font style with paragraph should have it's base style set to that paragraphs style name + $path = '/w:styles/w:style[w:name/@w:val="ChildFontStyle"]/w:basedOn'; + $element = $doc->getElement($path, $file); + $this->assertEquals('BaseStyle', $element->getAttribute('w:val')); + + // Font style without paragraph should not have a base style set + $path = '/w:styles/w:style[w:name/@w:val="OtherFontStyle"]/w:basedOn'; + $element = $doc->getElement($path, $file); + $this->assertNull($element); + } } From 18474c56c42516c22a49399f912da3932742ed70 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 28 Oct 2016 23:27:24 +0200 Subject: [PATCH 05/20] replace tab with spaces --- src/PhpWord/Writer/Word2007/Part/Styles.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 9ad87510..637c60ff 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -171,7 +171,7 @@ class Styles extends AbstractPart $xmlWriter->writeAttribute('w:val', $styleLink); $xmlWriter->endElement(); } else if (!is_null($paragraphStyle)) { - // if type is 'paragraph' it should have a styleId + // if type is 'paragraph' it should have a styleId $xmlWriter->writeAttribute('w:styleId', $styleName); } From c50e3c581a47a79ae1f3de8ec3df9847f0352f55 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 28 Oct 2016 23:27:24 +0200 Subject: [PATCH 06/20] replace tab with spaces --- src/PhpWord/Writer/Word2007/Part/Styles.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 9ad87510..637c60ff 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -171,7 +171,7 @@ class Styles extends AbstractPart $xmlWriter->writeAttribute('w:val', $styleLink); $xmlWriter->endElement(); } else if (!is_null($paragraphStyle)) { - // if type is 'paragraph' it should have a styleId + // if type is 'paragraph' it should have a styleId $xmlWriter->writeAttribute('w:styleId', $styleName); } From 46eebe2136babf70cba9508ae6d2e047743b66a8 Mon Sep 17 00:00:00 2001 From: antoine Date: Tue, 8 Nov 2016 23:22:22 +0100 Subject: [PATCH 07/20] add support for Image creation from string image data --- src/PhpWord/Element/Image.php | 31 +++++++++++++++-- tests/PhpWord/Element/ImageTest.php | 53 +++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index bf2b03d6..d7e5a665 100644 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -35,6 +35,7 @@ class Image extends AbstractElement const SOURCE_LOCAL = 'local'; // Local images const SOURCE_GD = 'gd'; // Generated using GD const SOURCE_ARCHIVE = 'archive'; // Image in archives zip://$archive#$image + const SOURCE_STRING = 'string'; // Image from string /** * Image source @@ -379,6 +380,8 @@ class Image extends AbstractElement // Check image data if ($this->sourceType == self::SOURCE_ARCHIVE) { $imageData = $this->getArchiveImageSize($source); + } else if ($this->sourceType == self::SOURCE_STRING) { + $imageData = $this->getStringImageSize($source); } else { $imageData = @getimagesize($source); } @@ -416,9 +419,15 @@ class Image extends AbstractElement } elseif (strpos($source, 'zip://') !== false) { $this->memoryImage = false; $this->sourceType = self::SOURCE_ARCHIVE; + } elseif (filter_var($source, FILTER_VALIDATE_URL) !== false) { + $this->memoryImage = true; + $this->sourceType = self::SOURCE_GD; + } elseif (@file_exists($source)) { + $this->memoryImage = false; + $this->sourceType = self::SOURCE_LOCAL; } else { - $this->memoryImage = (filter_var($source, FILTER_VALIDATE_URL) !== false); - $this->sourceType = $this->memoryImage ? self::SOURCE_GD : self::SOURCE_LOCAL; + $this->memoryImage = true; + $this->sourceType = self::SOURCE_STRING; } } @@ -460,6 +469,24 @@ class Image extends AbstractElement return $imageData; } + /** + * get image size from string + * + * @param string $source + * + * @codeCoverageIgnore this method is just a replacement for getimagesizefromstring which exists only as of PHP 5.4 + */ + private function getStringImageSize($source) + { + if (!function_exists('getimagesizefromstring')) { + $uri = 'data://application/octet-stream;base64,' . base64_encode($source); + return @getimagesize($uri); + } else { + return @getimagesizefromstring($source); + } + return false; + } + /** * Set image functions and extensions. * diff --git a/tests/PhpWord/Element/ImageTest.php b/tests/PhpWord/Element/ImageTest.php index c463b0c4..9ef9694b 100644 --- a/tests/PhpWord/Element/ImageTest.php +++ b/tests/PhpWord/Element/ImageTest.php @@ -156,4 +156,57 @@ class ImageTest extends \PHPUnit_Framework_TestCase $image = new Image("zip://{$archiveFile}#{$imageFile}"); $this->assertEquals('image/jpeg', $image->getImageType()); } + + /** + * Test getting image as string + */ + public function testImageAsStringFromFile() + { + $image = new Image(__DIR__ . '/../_files/images/earth.jpg'); + + $this->assertNotNull($image->getImageStringData()); + $this->assertNotNull($image->getImageStringData(true)); + } + + /** + * Test getting image from zip as string + */ + public function testImageAsStringFromZip() + { + $archiveFile = __DIR__ . '/../_files/documents/reader.docx'; + $imageFile = 'word/media/image1.jpeg'; + $image = new Image("zip://{$archiveFile}#{$imageFile}"); + + $this->assertNotNull($image->getImageStringData()); + $this->assertNotNull($image->getImageStringData(true)); + } + + /** + * Test construct from string + */ + public function testConstructFromString() + { + $source = file_get_contents(__DIR__ . '/../_files/images/earth.jpg'); + + $image = new Image($source); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image); + $this->assertEquals($source, $image->getSource()); + $this->assertEquals(md5($source), $image->getMediaId()); + $this->assertEquals('image/jpeg', $image->getImageType()); + $this->assertEquals('jpg', $image->getImageExtension()); + $this->assertEquals('imagecreatefromjpeg', $image->getImageCreateFunction()); + $this->assertEquals('imagejpeg', $image->getImageFunction()); + $this->assertTrue($image->isMemImage()); + } + + /** + * Test invalid string image + * + * @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException + */ + public function testInvalidImageString() + { + $object = new Image('this_is-a_non_valid_image'); + $object->getSource(); + } } From d23409f687ec864ba6c489923be124ce29efb8e0 Mon Sep 17 00:00:00 2001 From: antoine Date: Sat, 28 Jan 2017 11:39:43 +0100 Subject: [PATCH 08/20] Add possibility to show/hide spelling and grammatical errors --- docs/general.rst | 13 +++++ src/PhpWord/Settings.php | 52 +++++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Settings.php | 4 ++ tests/PhpWord/SettingsTest.php | 14 +++++ .../Writer/Word2007/Part/SettingsTest.php | 38 ++++++++++++++ 5 files changed, 121 insertions(+) diff --git a/docs/general.rst b/docs/general.rst index 27d0448a..9b7eb54b 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -183,3 +183,16 @@ points to twips. $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); // 2 cm right margin $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); + +Language +-------- + +You can hide spelling errors: + +.. code-block:: php + \PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true); + +And hide grammatical errors: + +.. code-block:: php + \PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true); diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index e049c46e..b1ef2759 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -119,6 +119,18 @@ class Settings */ private static $defaultFontSize = self::DEFAULT_FONT_SIZE; + /** + * Hide spelling errors + * @var boolean + */ + private static $spellingErrorsHidden = false; + + /** + * Hide grammatical errors + * @var boolean + */ + private static $grammaticalErrorsHidden = false; + /** * The user defined temporary directory. * @@ -393,6 +405,46 @@ class Settings return false; } + /** + * Are spelling errors hidden + * + * @return boolean + */ + public static function isSpellingErrorsHidden() + { + return self::$spellingErrorsHidden; + } + + /** + * Hide spelling errors + * + * @param boolean $spellingErrorsHidden + */ + public static function setSpellingErrorsHidden($spellingErrorsHidden) + { + self::$spellingErrorsHidden = $spellingErrorsHidden; + } + + /** + * Are grammatical errors hidden + * + * @return boolean + */ + public static function isGrammaticalErrorsHidden() + { + return self::$grammaticalErrorsHidden; + } + + /** + * Hide grammatical errors + * + * @param boolean $grammaticalErrorsHidden + */ + public static function setGrammaticalErrorsHidden($grammaticalErrorsHidden) + { + self::$grammaticalErrorsHidden = $grammaticalErrorsHidden; + } + /** * Load setting from phpword.yml or phpword.yml.dist * diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index d881e13a..f13381d2 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; +use PhpOffice\PhpWord\Settings as DocumentSettings; + /** * Word2007 settings part writer: word/settings.xml * @@ -104,6 +106,8 @@ class Settings extends AbstractPart 'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')), 'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')), 'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')), + 'w:hideSpellingErrors' => array('@attributes' => array('w:val' => DocumentSettings::isSpellingErrorsHidden() ? 'true' : 'false')), + 'w:hideGrammaticalErrors' => array('@attributes' => array('w:val' => DocumentSettings::isGrammaticalErrorsHidden() ? 'true' : 'false')), 'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')), 'w:listSeparator' => array('@attributes' => array('w:val' => ';')), 'w:compat' => array(), diff --git a/tests/PhpWord/SettingsTest.php b/tests/PhpWord/SettingsTest.php index f5ac3ed6..0d007b74 100644 --- a/tests/PhpWord/SettingsTest.php +++ b/tests/PhpWord/SettingsTest.php @@ -114,6 +114,20 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertFalse(Settings::setDefaultFontSize(null)); } + /** + * Test set/get spelling and grammar + */ + public function testSetGetSpellingGrammar() + { + $this->assertFalse(Settings::isSpellingErrorsHidden()); + Settings::setSpellingErrorsHidden(true); + $this->assertTrue(Settings::isSpellingErrorsHidden()); + + $this->assertFalse(Settings::isGrammaticalErrorsHidden()); + Settings::setGrammaticalErrorsHidden(true); + $this->assertTrue(Settings::isGrammaticalErrorsHidden()); + } + /** * Test load config */ diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index 6ed23e44..7ae06e14 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWord\Settings; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings @@ -66,4 +67,41 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertTrue($doc->elementExists($path, $file)); $this->assertEquals($phpWord->getCompatibility()->getOoxmlVersion(), 15); } + + /** + * Test language + */ + public function testLanguage() + { + $phpWord = new PhpWord(); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:themeFontLang'; + $this->assertTrue($doc->elementExists($path, $file)); + $element = $doc->getElement($path, $file); + + $this->assertEquals('en-US', $element->getAttribute('w:val')); + } + + /** + * Test spelling + */ + public function testSpelling() + { + $phpWord = new PhpWord(); + Settings::setSpellingErrorsHidden(true); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:hideSpellingErrors'; + $this->assertTrue($doc->elementExists($path, $file)); + $element = $doc->getElement($path, $file); + + $this->assertEquals('true', $element->getAttribute('w:val')); + } } From 517c432ee68bd079e85542a8f6d8bb9a30a33758 Mon Sep 17 00:00:00 2001 From: antoine Date: Sun, 29 Jan 2017 13:16:54 +0100 Subject: [PATCH 09/20] fix image loading over https --- samples/Sample_13_Images.php | 6 ++++ src/PhpWord/Element/Image.php | 43 ++++++++++++++++------------- tests/PhpWord/Element/ImageTest.php | 12 ++++++-- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/samples/Sample_13_Images.php b/samples/Sample_13_Images.php index a3c2af23..1e6943db 100644 --- a/samples/Sample_13_Images.php +++ b/samples/Sample_13_Images.php @@ -20,6 +20,12 @@ $source = 'http://php.net/images/logos/php-med-trans-light.gif'; $section->addText("Remote image from: {$source}"); $section->addImage($source); +// Image from string +$source = 'resources/_mars.jpg'; +$fileContent = file_get_contents($source); +$section->addText("Image from string"); +$section->addImage($fileContent); + //Wrapping style $text = str_repeat('Hello World! ', 15); $wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight'); diff --git a/src/PhpWord/Element/Image.php b/src/PhpWord/Element/Image.php index d7e5a665..b8b5cca1 100644 --- a/src/PhpWord/Element/Image.php +++ b/src/PhpWord/Element/Image.php @@ -340,6 +340,8 @@ class Image extends AbstractElement call_user_func($this->imageFunc, $imageResource); $imageBinary = ob_get_contents(); ob_end_clean(); + } elseif ($this->sourceType == self::SOURCE_STRING) { + $imageBinary = $this->source; } else { $fileHandle = fopen($actualSource, 'rb', false); if ($fileHandle !== false) { @@ -366,33 +368,31 @@ class Image extends AbstractElement /** * Check memory image, supported type, image functions, and proportional width/height. * - * @param string $source - * * @return void * * @throws \PhpOffice\PhpWord\Exception\InvalidImageException * @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException */ - private function checkImage($source) + private function checkImage() { - $this->setSourceType($source); + $this->setSourceType(); // Check image data if ($this->sourceType == self::SOURCE_ARCHIVE) { - $imageData = $this->getArchiveImageSize($source); + $imageData = $this->getArchiveImageSize($this->source); } else if ($this->sourceType == self::SOURCE_STRING) { - $imageData = $this->getStringImageSize($source); + $imageData = $this->getStringImageSize($this->source); } else { - $imageData = @getimagesize($source); + $imageData = @getimagesize($this->source); } if (!is_array($imageData)) { - throw new InvalidImageException(sprintf('Invalid image: %s', $source)); + throw new InvalidImageException(sprintf('Invalid image: %s', $this->source)); } list($actualWidth, $actualHeight, $imageType) = $imageData; // Check image type support $supportedTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG); - if ($this->sourceType != self::SOURCE_GD) { + if ($this->sourceType != self::SOURCE_GD && $this->sourceType != self::SOURCE_STRING) { $supportedTypes = array_merge($supportedTypes, array(IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)); } if (!in_array($imageType, $supportedTypes)) { @@ -408,21 +408,26 @@ class Image extends AbstractElement /** * Set source type. * - * @param string $source * @return void */ - private function setSourceType($source) + private function setSourceType() { - if (stripos(strrev($source), strrev('.php')) === 0) { + if (stripos(strrev($this->source), strrev('.php')) === 0) { $this->memoryImage = true; $this->sourceType = self::SOURCE_GD; - } elseif (strpos($source, 'zip://') !== false) { + } elseif (strpos($this->source, 'zip://') !== false) { $this->memoryImage = false; $this->sourceType = self::SOURCE_ARCHIVE; - } elseif (filter_var($source, FILTER_VALIDATE_URL) !== false) { + } elseif (filter_var($this->source, FILTER_VALIDATE_URL) !== false) { $this->memoryImage = true; - $this->sourceType = self::SOURCE_GD; - } elseif (@file_exists($source)) { + if (strpos($this->source, 'https') === 0) { + $fileContent = file_get_contents($this->source); + $this->source = $fileContent; + $this->sourceType = self::SOURCE_STRING; + } else { + $this->sourceType = self::SOURCE_GD; + } + } elseif (@file_exists($this->source)) { $this->memoryImage = false; $this->sourceType = self::SOURCE_LOCAL; } else { @@ -496,18 +501,18 @@ class Image extends AbstractElement { switch ($this->imageType) { case 'image/png': - $this->imageCreateFunc = 'imagecreatefrompng'; + $this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefrompng'; $this->imageFunc = 'imagepng'; $this->imageExtension = 'png'; break; case 'image/gif': - $this->imageCreateFunc = 'imagecreatefromgif'; + $this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromgif'; $this->imageFunc = 'imagegif'; $this->imageExtension = 'gif'; break; case 'image/jpeg': case 'image/jpg': - $this->imageCreateFunc = 'imagecreatefromjpeg'; + $this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromjpeg'; $this->imageFunc = 'imagejpeg'; $this->imageExtension = 'jpg'; break; diff --git a/tests/PhpWord/Element/ImageTest.php b/tests/PhpWord/Element/ImageTest.php index 9ef9694b..974e868c 100644 --- a/tests/PhpWord/Element/ImageTest.php +++ b/tests/PhpWord/Element/ImageTest.php @@ -131,7 +131,15 @@ class ImageTest extends \PHPUnit_Framework_TestCase */ public function testUnsupportedImage() { - $object = new Image('http://samples.libav.org/image-samples/RACECAR.BMP'); + //disable ssl verification, never do this in real application, you should pass the certiciate instead!!! + $arrContextOptions = array( + "ssl" => array( + "verify_peer" => false, + "verify_peer_name" => false, + ), + ); + stream_context_set_default($arrContextOptions); + $object = new Image('https://samples.libav.org/image-samples/RACECAR.BMP'); $object->getSource(); } @@ -194,7 +202,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertEquals(md5($source), $image->getMediaId()); $this->assertEquals('image/jpeg', $image->getImageType()); $this->assertEquals('jpg', $image->getImageExtension()); - $this->assertEquals('imagecreatefromjpeg', $image->getImageCreateFunction()); + $this->assertEquals('imagecreatefromstring', $image->getImageCreateFunction()); $this->assertEquals('imagejpeg', $image->getImageFunction()); $this->assertTrue($image->isMemImage()); } From 759794d746a9cc0ee0e9f3275795feac56c9a55d Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 12 May 2017 00:45:21 +0200 Subject: [PATCH 10/20] fix twig dependency so build can succeed --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index c49eb9cd..faa17c1b 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,7 @@ "require-dev": { "phpunit/phpunit": "3.7.*", "phpdocumentor/phpdocumentor":"2.*", + "twig/twig":"1.27", "squizlabs/php_codesniffer": "1.*", "phpmd/phpmd": "2.*", "phploc/phploc": "2.*", From 0e0817c9727160f8243ff04cc742e8200ffaddf8 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 12 May 2017 00:45:31 +0200 Subject: [PATCH 11/20] ignore the build directory --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e5deb643..90940594 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ Thumbs.db Desktop.ini .idea _build +/build phpunit.xml composer.lock composer.phar @@ -16,4 +17,4 @@ vendor /.settings phpword.ini /.buildpath -/.project \ No newline at end of file +/.project From 1cfb62de0db71f7c6805c43f7d3ed93688a69f5f Mon Sep 17 00:00:00 2001 From: antoine Date: Mon, 15 May 2017 22:49:02 +0200 Subject: [PATCH 12/20] Add support for comments --- docs/elements.rst | 25 +++- docs/intro.rst | 5 + samples/Sample_37_Comments.php | 63 +++++++++ src/PhpWord/Collection/Comments.php | 27 ++++ src/PhpWord/Element/AbstractContainer.php | 9 +- src/PhpWord/Element/AbstractElement.php | 65 +++++++++- src/PhpWord/Element/Comment.php | 122 ++++++++++++++++++ src/PhpWord/Element/TrackChange.php | 77 +++++++++++ src/PhpWord/PhpWord.php | 6 +- src/PhpWord/Writer/Word2007.php | 26 ++++ .../Word2007/Element/AbstractElement.php | 53 ++++++++ src/PhpWord/Writer/Word2007/Element/Chart.php | 1 + src/PhpWord/Writer/Word2007/Element/Image.php | 1 + src/PhpWord/Writer/Word2007/Element/Line.php | 1 + .../Writer/Word2007/Element/Object.php | 1 + src/PhpWord/Writer/Word2007/Element/Shape.php | 1 + .../Writer/Word2007/Element/TextBox.php | 1 + src/PhpWord/Writer/Word2007/Part/Comments.php | 103 +++++++++++++++ .../Writer/Word2007/Part/ContentTypes.php | 1 + tests/PhpWord/Element/CommentTest.php | 83 ++++++++++++ .../Writer/Word2007/Part/CommentsTest.php | 61 +++++++++ 21 files changed, 724 insertions(+), 8 deletions(-) create mode 100644 samples/Sample_37_Comments.php create mode 100644 src/PhpWord/Collection/Comments.php create mode 100644 src/PhpWord/Element/Comment.php create mode 100644 src/PhpWord/Element/TrackChange.php create mode 100644 src/PhpWord/Writer/Word2007/Part/Comments.php create mode 100644 tests/PhpWord/Element/CommentTest.php create mode 100644 tests/PhpWord/Writer/Word2007/Part/CommentsTest.php diff --git a/docs/elements.rst b/docs/elements.rst index d68ee035..3d7667b2 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -379,4 +379,27 @@ Available line style attributes: - ``endArrow``. End type of arrow: block, open, classic, diamond, oval. - ``width``. Line-object width in pt. - ``height``. Line-object height in pt. -- ``flip``. Flip the line element: true, false. \ No newline at end of file +- ``flip``. Flip the line element: true, false. + +Comments +--------- + +Comments can be added to a document by using ``addComment``. +The comment can contain formatted text. Once the comment has been added, it can be linked to any to any element. + +.. code-block:: php + + // first create a comment + $comment= new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials'); + $comment->addText('Test', array('bold' => true)); + + // add it to the document + $phpWord->addComment($comment); + + $textrun = $section->addTextRun(); + $textrun->addText('This '); + $text = $textrun->addText('is'); + // link the comment to the text you just created + $text->setCommentStart($comment); + +If no end is set for a comment using the ``setCommentEnd``, the comment will be ended automatically at the end of the element it is started on. \ No newline at end of file diff --git a/docs/intro.rst b/docs/intro.rst index d1c791cf..d88cd626 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -49,6 +49,7 @@ Features - Insert drawing shapes (arc, curve, line, polyline, rect, oval) - Insert charts (pie, doughnut, bar, line, area, scatter, radar) - Insert form fields (textinput, checkbox, and dropdown) +- Insert comments - Create document from templates - Use XSL 1.0 style sheets to transform headers, main document part, and footers of an OOXML template - ... and many more features on progress @@ -102,6 +103,8 @@ Writers +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Endnote | ✓ | | | ✓ | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ +| | Comments | ✓ | | | | | ++---------------------------+----------------------+--------+-------+-------+--------+-------+ | **Graphs** | 2D basic graphs | ✓ | | | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | 2D advanced graphs | | | | | | @@ -161,6 +164,8 @@ Readers +---------------------------+----------------------+--------+-------+-------+-------+-------+ | | Endnote | ✓ | | | | | +---------------------------+----------------------+--------+-------+-------+-------+-------+ +| | Comments | | | | | | ++---------------------------+----------------------+--------+-------+-------+-------+-------+ | **Graphs** | 2D basic graphs | | | | | | +---------------------------+----------------------+--------+-------+-------+-------+-------+ | | 2D advanced graphs | | | | | | diff --git a/samples/Sample_37_Comments.php b/samples/Sample_37_Comments.php new file mode 100644 index 00000000..91d353a9 --- /dev/null +++ b/samples/Sample_37_Comments.php @@ -0,0 +1,63 @@ +addText('Test', array('bold' => true)); +$phpWord->addComment($comment); + +$section = $phpWord->addSection(); + +$textrun = $section->addTextRun(); +$textrun->addText('This '); +$text = $textrun->addText('is'); +$text->setCommentStart($comment); +$textrun->addText(' a test'); + +$section->addTextBreak(2); + +// Let's create a comment that we will link to a start element and an end element +$commentWithStartAndEnd = new \PhpOffice\PhpWord\Element\Comment('Foo Bar', new \DateTime(), ''); +$commentWithStartAndEnd->addText('A comment with a start and an end'); +$phpWord->addComment($commentWithStartAndEnd); + +$textrunWithEnd = $section->addTextRun(); +$textrunWithEnd->addText('This '); +$textToStartOn = $textrunWithEnd->addText('is', array('bold' => true)); +$textToStartOn->setCommentStart($commentWithStartAndEnd); +$textrunWithEnd->addText(' another', array('italic' => true)); +$textToEndOn = $textrunWithEnd->addText(' test'); +$textToEndOn->setCommentEnd($commentWithStartAndEnd); + +$section->addTextBreak(2); + +// Let's add a comment on an image +$commentOnImage = new \PhpOffice\PhpWord\Element\Comment('Mr Smart', new \DateTime(), ''); +$imageComment = $commentOnImage->addTextRun(); +$imageComment->addText('Hey, Mars does look '); +$imageComment->addText('red', array('color' => 'FF0000')); +$phpWord->addComment($commentOnImage); +$image = $section->addImage('resources/_mars.jpg'); +$image->setCommentStart($commentOnImage); + +$section->addTextBreak(2); + +// We can also do things the other way round, link the comment to the element +$anotherText = $section->addText("another text"); + +$comment1 = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials'); +$comment1->addText('Test', array('bold' => true)); +$comment1->setStartElement($anotherText); +$comment1->setEndElement($anotherText); +$phpWord->addComment($comment1); + + +// Save file +echo write($phpWord, basename(__FILE__, '.php'), $writers); +if (!CLI) { + include_once 'Sample_Footer.php'; +} diff --git a/src/PhpWord/Collection/Comments.php b/src/PhpWord/Collection/Comments.php new file mode 100644 index 00000000..e0383814 --- /dev/null +++ b/src/PhpWord/Collection/Comments.php @@ -0,0 +1,27 @@ + $generalContainers, 'FormField' => $generalContainers, 'SDT' => $generalContainers, - 'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), + 'TrackChange' => $generalContainers, + 'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox', 'TrackChange'), 'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), 'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), 'Table' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index b0ed8ae2..b84211b9 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -108,12 +108,26 @@ abstract class AbstractElement protected $mediaRelation = false; /** - * Is part of collection; true for Title, Footnote, Endnote, and Chart + * Is part of collection; true for Title, Footnote, Endnote, Chart, and Comment * * @var bool */ protected $collectionRelation = false; + /** + * The start position for the linked comment + * + * @var Comment + */ + protected $commentStart; + + /** + * The end position for the linked comment + * + * @var Comment + */ + protected $commentEnd; + /** * Get PhpWord * @@ -265,6 +279,55 @@ abstract class AbstractElement return $this->nestedLevel; } + /** + * Get comment start + * + * @return Comment + */ + public function getCommentStart() + { + return $this->commentStart; + } + + /** + * Set comment start + * + * @param Comment $value + */ + public function setCommentStart(Comment $value) + { + if ($this instanceof Comment) { + throw new \InvalidArgumentException("Cannot set a Comment on a Comment"); + } + $this->commentStart = $value; + $this->commentStart->setStartElement($this); + } + + /** + * Get comment end + * + * @return Comment + */ + public function getCommentEnd() + { + return $this->commentEnd; + } + + /** + * Set comment end + * + * @param Comment $value + * @return void + */ + public function setCommentEnd(Comment $value) + { + if ($this instanceof Comment) { + throw new \InvalidArgumentException("Cannot set a Comment on a Comment"); + } + $this->commentEnd = $value; + $this->commentEnd->setEndElement($this); + } + /** * Set parent container * diff --git a/src/PhpWord/Element/Comment.php b/src/PhpWord/Element/Comment.php new file mode 100644 index 00000000..df2e4d4d --- /dev/null +++ b/src/PhpWord/Element/Comment.php @@ -0,0 +1,122 @@ +initials = $initials; + return $this; + } + + /** + * Get Initials + * + * @return string + */ + public function getInitials() + { + return $this->initials; + } + + /** + * Sets the element where this comment starts + * + * @param \PhpOffice\PhpWord\Element\AbstractElement $value + */ + public function setStartElement(AbstractElement $value) + { + $this->startElement = $value; + if ($value->getCommentStart() == null) { + $value->setCommentStart($this); + } + } + + /** + * Get the element where this comment starts + * + * @return \PhpOffice\PhpWord\Element\AbstractElement + */ + public function getStartElement() + { + return $this->startElement; + } + + /** + * Sets the element where this comment ends + * + * @param \PhpOffice\PhpWord\Element\AbstractElement $value + */ + public function setEndElement(AbstractElement $value) + { + $this->endElement = $value; + if ($value->getCommentEnd() == null) { + $value->setCommentEnd($this); + } + } + + /** + * Get the element where this comment ends + * + * @return \PhpOffice\PhpWord\Element\AbstractElement + */ + public function getEndElement() + { + return $this->endElement; + } +} diff --git a/src/PhpWord/Element/TrackChange.php b/src/PhpWord/Element/TrackChange.php new file mode 100644 index 00000000..782e6f35 --- /dev/null +++ b/src/PhpWord/Element/TrackChange.php @@ -0,0 +1,77 @@ +author = $author; + $this->date = $date; + return $this; + } + + /** + * Get TrackChange Author + * + * @return string + */ + public function getAuthor() + { + return $this->author; + } + + /** + * Get TrackChange Date + * + * @return \DateTime + */ + public function getDate() + { + return $this->date; + } +} diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 0fa76b2f..38895e4f 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -27,11 +27,13 @@ use PhpOffice\PhpWord\Exception\Exception; * @method Collection\Footnotes getFootnotes() * @method Collection\Endnotes getEndnotes() * @method Collection\Charts getCharts() + * @method Collection\Comments getComments() * @method int addBookmark(Element\Bookmark $bookmark) * @method int addTitle(Element\Title $title) * @method int addFootnote(Element\Footnote $footnote) * @method int addEndnote(Element\Endnote $endnote) * @method int addChart(Element\Chart $chart) + * @method int addComment(Element\Comment $comment) * * @method Style\Paragraph addParagraphStyle(string $styleName, array $styles) * @method Style\Font addFontStyle(string $styleName, mixed $fontStyle, mixed $paragraphStyle = null) @@ -84,7 +86,7 @@ class PhpWord public function __construct() { // Collection - $collections = array('Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts'); + $collections = array('Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts', 'Comments'); foreach ($collections as $collection) { $class = 'PhpOffice\\PhpWord\\Collection\\' . $collection; $this->collections[$collection] = new $class(); @@ -118,7 +120,7 @@ class PhpWord $addCollection = array(); $addStyle = array(); - $collections = array('Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart'); + $collections = array('Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart', 'Comment'); foreach ($collections as $collection) { $getCollection[] = strtolower("get{$collection}s"); $addCollection[] = strtolower("add{$collection}"); diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 8e10f5f6..bb7b521f 100644 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -60,6 +60,7 @@ class Word2007 extends AbstractWriter implements WriterInterface 'DocPropsCustom' => 'docProps/custom.xml', 'RelsDocument' => 'word/_rels/document.xml.rels', 'Document' => 'word/document.xml', + 'Comments' => 'word/comments.xml', 'Styles' => 'word/styles.xml', 'Numbering' => 'word/numbering.xml', 'Settings' => 'word/settings.xml', @@ -129,6 +130,7 @@ class Word2007 extends AbstractWriter implements WriterInterface $this->addNotes($zip, $rId, 'footnote'); $this->addNotes($zip, $rId, 'endnote'); + $this->addComments($zip, $rId); $this->addChart($zip, $rId); // Write parts @@ -255,6 +257,30 @@ class Word2007 extends AbstractWriter implements WriterInterface } } + /** + * Add comments + * + * @param \PhpOffice\PhpWord\Shared\ZipArchive $zip + * @param integer &$rId + * @return void + */ + private function addComments(ZipArchive $zip, &$rId) + { + $phpWord = $this->getPhpWord(); + $collection = $phpWord->getComments(); + $partName = "comments"; + + // Add comment relations and contents + /** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collection Type hint */ + if ($collection->countItems() > 0) { + $this->relationships[] = array('target' => "{$partName}.xml", 'type' => $partName, 'rID' => ++$rId); + + // Write content file, e.g. word/comments.xml + $writerPart = $this->getWriterPart($partName)->setElements($collection->getItems()); + $zip->addFromString("word/{$partName}.xml", $writerPart->write()); + } + } + /** * Add chart. * diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index f5a454d2..474d40ce 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -103,6 +103,7 @@ abstract class AbstractElement $this->writeParagraphStyle(); } } + $this->writeCommentRangeStart(); } /** @@ -112,11 +113,63 @@ abstract class AbstractElement */ protected function endElementP() { + $this->writeCommentRangeEnd(); if (!$this->withoutP) { $this->xmlWriter->endElement(); // w:p } } + /** + * Writes the w:commentRangeStart DOM element + * + * @return void + */ + protected function writeCommentRangeStart() + { + if ($this->element->getCommentStart() != null) { + $comment = $this->element->getCommentStart(); + //only set the ID if it is not yet set, otherwise it will overwrite it + if ($comment->getElementId() == null) { + $comment->setElementId(); + } + + $this->xmlWriter->writeElementBlock('w:commentRangeStart', array('w:id' => $comment->getElementId())); + + } + } + + /** + * Writes the w:commentRangeEnd DOM element + * + * @return void + */ + protected function writeCommentRangeEnd() + { + if ($this->element->getCommentEnd() != null) { + $comment = $this->element->getCommentEnd(); + //only set the ID if it is not yet set, otherwise it will overwrite it + if ($comment->getElementId() == null) { + $comment->setElementId(); + } + + $this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId())); + $this->xmlWriter->startElement('w:r'); + $this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId())); + $this->xmlWriter->endElement(); + } elseif ($this->element->getCommentStart() != null && $this->element->getCommentStart()->getEndElement() == null) { + $comment = $this->element->getCommentStart(); + //only set the ID if it is not yet set, otherwise it will overwrite it + if ($comment->getElementId() == null) { + $comment->setElementId(); + } + + $this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId())); + $this->xmlWriter->startElement('w:r'); + $this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId())); + $this->xmlWriter->endElement(); + } + } + /** * Write ending. * diff --git a/src/PhpWord/Writer/Word2007/Element/Chart.php b/src/PhpWord/Writer/Word2007/Element/Chart.php index 12602532..ecdde362 100644 --- a/src/PhpWord/Writer/Word2007/Element/Chart.php +++ b/src/PhpWord/Writer/Word2007/Element/Chart.php @@ -45,6 +45,7 @@ class Chart extends AbstractElement if (!$this->withoutP) { $xmlWriter->startElement('w:p'); } + $this->writeCommentRangeStart(); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:drawing'); diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index 914c78ea..edf32739 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -63,6 +63,7 @@ class Image extends AbstractElement $xmlWriter->startElement('w:p'); $styleWriter->writeAlignment(); } + $this->writeCommentRangeStart(); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:pict'); diff --git a/src/PhpWord/Writer/Word2007/Element/Line.php b/src/PhpWord/Writer/Word2007/Element/Line.php index ade91fb8..ebc5d395 100644 --- a/src/PhpWord/Writer/Word2007/Element/Line.php +++ b/src/PhpWord/Writer/Word2007/Element/Line.php @@ -48,6 +48,7 @@ class Line extends AbstractElement $xmlWriter->startElement('w:p'); $styleWriter->writeAlignment(); } + $this->writeCommentRangeStart(); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:pict'); diff --git a/src/PhpWord/Writer/Word2007/Element/Object.php b/src/PhpWord/Writer/Word2007/Element/Object.php index 4fdf6fed..fc0532cd 100644 --- a/src/PhpWord/Writer/Word2007/Element/Object.php +++ b/src/PhpWord/Writer/Word2007/Element/Object.php @@ -51,6 +51,7 @@ class Object extends AbstractElement $xmlWriter->startElement('w:p'); $styleWriter->writeAlignment(); } + $this->writeCommentRangeStart(); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:object'); diff --git a/src/PhpWord/Writer/Word2007/Element/Shape.php b/src/PhpWord/Writer/Word2007/Element/Shape.php index f282c4a5..a589af6c 100644 --- a/src/PhpWord/Writer/Word2007/Element/Shape.php +++ b/src/PhpWord/Writer/Word2007/Element/Shape.php @@ -55,6 +55,7 @@ class Shape extends AbstractElement if (!$this->withoutP) { $xmlWriter->startElement('w:p'); } + $this->writeCommentRangeStart(); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:pict'); diff --git a/src/PhpWord/Writer/Word2007/Element/TextBox.php b/src/PhpWord/Writer/Word2007/Element/TextBox.php index 3c4f48c2..e83fe0c9 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBox.php @@ -45,6 +45,7 @@ class TextBox extends Image $xmlWriter->startElement('w:p'); $styleWriter->writeAlignment(); } + $this->writeCommentRangeStart(); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:pict'); diff --git a/src/PhpWord/Writer/Word2007/Part/Comments.php b/src/PhpWord/Writer/Word2007/Part/Comments.php new file mode 100644 index 00000000..73314785 --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Part/Comments.php @@ -0,0 +1,103 @@ +getXmlWriter(); + + $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); + $xmlWriter->startElement('w:comments'); + $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006'); + $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office'); + $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); + $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math'); + $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml'); + $xmlWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'); + $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word'); + $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); + $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); + + if ($this->elements !== null) { + foreach ($this->elements as $element) { + if ($element instanceof Comment) { + $this->writeComment($xmlWriter, $element); + } + } + } + + $xmlWriter->endElement(); // w:comments + + return $xmlWriter->getData(); + } + + /** + * Write comment item. + * + * @param \PhpOffice\Common\XMLWriter $xmlWriter + * @param \PhpOffice\PhpWord\Element\Comment $comment + * @return void + */ + protected function writeComment(XMLWriter $xmlWriter, Comment $comment) + { + $xmlWriter->startElement('w:comment'); + $xmlWriter->writeAttribute('w:id', $comment->getElementId()); + $xmlWriter->writeAttribute('w:author', $comment->getAuthor()); + $xmlWriter->writeAttribute('w:date', $comment->getDate()->format($this->dateFormat)); + $xmlWriter->writeAttribute('w:initials', $comment->getInitials()); + + $containerWriter = new Container($xmlWriter, $comment); + $containerWriter->write(); + + $xmlWriter->endElement(); // w:comment + } + + /** + * Set element + * + * @param \PhpOffice\PhpWord\Collection\Comments $elements + * @return self + */ + public function setElements($elements) + { + $this->elements = $elements; + + return $this; + } +} diff --git a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php index 1c81f343..7a03243e 100644 --- a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php @@ -49,6 +49,7 @@ class ContentTypes extends AbstractPart '/word/theme/theme1.xml' => $openXMLPrefix . 'officedocument.theme+xml', '/word/webSettings.xml' => $wordMLPrefix . 'webSettings+xml', '/word/fontTable.xml' => $wordMLPrefix . 'fontTable+xml', + '/word/comments.xml' => $wordMLPrefix . 'comments+xml', ); $defaults = $contentTypes['default']; diff --git a/tests/PhpWord/Element/CommentTest.php b/tests/PhpWord/Element/CommentTest.php new file mode 100644 index 00000000..db9ec902 --- /dev/null +++ b/tests/PhpWord/Element/CommentTest.php @@ -0,0 +1,83 @@ +setStartElement($oText); + $oComment->setEndElement($oText); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Comment', $oComment); + $this->assertEquals($author, $oComment->getAuthor()); + $this->assertEquals($date, $oComment->getDate()); + $this->assertEquals($initials, $oComment->getInitials()); + $this->assertEquals($oText, $oComment->getStartElement()); + $this->assertEquals($oText, $oComment->getEndElement()); + } + + /** + * Add text + */ + public function testAddText() + { + $oComment = new Comment('Test User', new \DateTime(), 'my_initials'); + $element = $oComment->addText('text'); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element); + $this->assertCount(1, $oComment->getElements()); + $this->assertEquals('text', $element->getText()); + } + + /** + * Get elements + */ + public function testGetElements() + { + $oComment = new Comment('Test User', new \DateTime(), 'my_initials'); + + $this->assertInternalType('array', $oComment->getElements()); + } + + /** + * Set/get relation Id + */ + public function testRelationId() + { + $oComment = new Comment('Test User', new \DateTime(), 'my_initials'); + + $iVal = rand(1, 1000); + $oComment->setRelationId($iVal); + $this->assertEquals($iVal, $oComment->getRelationId()); + } +} diff --git a/tests/PhpWord/Writer/Word2007/Part/CommentsTest.php b/tests/PhpWord/Writer/Word2007/Part/CommentsTest.php new file mode 100644 index 00000000..aac4b15b --- /dev/null +++ b/tests/PhpWord/Writer/Word2007/Part/CommentsTest.php @@ -0,0 +1,61 @@ +addText('Test'); + + $phpWord = new PhpWord(); + $phpWord->addComment($comment); + $doc = TestHelperDOCX::getDocument($phpWord); + + $path = '/w:comments/w:comment'; + $file = 'word/comments.xml'; + + $this->assertTrue($doc->elementExists($path, $file)); + + $element = $doc->getElement($path, $file); + $this->assertNotNull($element->getAttribute('w:id')); + $this->assertEquals("Authors name", $element->getAttribute('w:author')); + $this->assertEquals("my_initials", $element->getAttribute('w:initials')); + } +} From 09e669f9e9de9bcc7b60b8c3224b6c058a69166b Mon Sep 17 00:00:00 2001 From: antoine Date: Tue, 16 May 2017 02:38:15 +0200 Subject: [PATCH 13/20] basedOn not correctly set if FontStyle is based on other FontStyle --- src/PhpWord/Writer/Word2007/Part/Styles.php | 6 +++- .../Writer/Word2007/Part/StylesTest.php | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index 637c60ff..01b84c08 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -182,7 +182,11 @@ class Styles extends AbstractPart // Parent style if (!is_null($paragraphStyle)) { - $xmlWriter->writeElementBlock('w:basedOn', 'w:val', $paragraphStyle->getStyleName()); + if ($paragraphStyle->getStyleName() != null) { + $xmlWriter->writeElementBlock('w:basedOn', 'w:val', $paragraphStyle->getStyleName()); + } elseif ($paragraphStyle->getBasedOn() != null) { + $xmlWriter->writeElementBlock('w:basedOn', 'w:val', $paragraphStyle->getBasedOn()); + } } // w:pPr diff --git a/tests/PhpWord/Writer/Word2007/Part/StylesTest.php b/tests/PhpWord/Writer/Word2007/Part/StylesTest.php index df20ab3b..9153fe65 100644 --- a/tests/PhpWord/Writer/Word2007/Part/StylesTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/StylesTest.php @@ -114,4 +114,32 @@ class StylesTest extends \PHPUnit_Framework_TestCase $element = $doc->getElement($path, $file); $this->assertNull($element); } + + function testFontStyleBasedOnOtherFontStyle() { + $phpWord = new PhpWord(); + + $styleGenerationP = new Paragraph(); + $styleGenerationP->setAlignment(Jc::BOTH); + + $styleGeneration = new Font(); + $styleGeneration->setParagraph($styleGenerationP); + $styleGeneration->setSize(9.5); + $phpWord->addFontStyle('Generation', $styleGeneration); + + $styleGenerationEteinteP = new Paragraph(); + $styleGenerationEteinteP->setBasedOn('Generation'); + + $styleGenerationEteinte = new Font(); + $styleGenerationEteinte->setParagraph($styleGenerationEteinteP); + $styleGenerationEteinte->setSize(8.5); + $phpWord->addFontStyle('GeneratEteinte', $styleGenerationEteinte); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/styles.xml'; + + $path = '/w:styles/w:style[@w:styleId="GeneratEteinte"]/w:basedOn'; + $element = $doc->getElement($path, $file); + $this->assertEquals('Generation', $element->getAttribute('w:val')); + } } From 68dd3fd576de0d7eafb50176d2db4d199fa03628 Mon Sep 17 00:00:00 2001 From: antoine Date: Wed, 24 May 2017 00:18:17 +0200 Subject: [PATCH 14/20] rename attribute to match generated XML node name --- src/PhpWord/Element/AbstractElement.php | 24 +++++++++---------- src/PhpWord/Element/Comment.php | 8 +++---- .../Word2007/Element/AbstractElement.php | 12 +++++----- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index b84211b9..8ff64194 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -119,14 +119,14 @@ abstract class AbstractElement * * @var Comment */ - protected $commentStart; + protected $commentRangeStart; /** * The end position for the linked comment * * @var Comment */ - protected $commentEnd; + protected $commentRangeEnd; /** * Get PhpWord @@ -284,9 +284,9 @@ abstract class AbstractElement * * @return Comment */ - public function getCommentStart() + public function getCommentRangeStart() { - return $this->commentStart; + return $this->commentRangeStart; } /** @@ -294,13 +294,13 @@ abstract class AbstractElement * * @param Comment $value */ - public function setCommentStart(Comment $value) + public function setCommentRangeStart(Comment $value) { if ($this instanceof Comment) { throw new \InvalidArgumentException("Cannot set a Comment on a Comment"); } - $this->commentStart = $value; - $this->commentStart->setStartElement($this); + $this->commentRangeStart= $value; + $this->commentRangeStart->setStartElement($this); } /** @@ -308,9 +308,9 @@ abstract class AbstractElement * * @return Comment */ - public function getCommentEnd() + public function getCommentRangeEnd() { - return $this->commentEnd; + return $this->commentRangeEnd; } /** @@ -319,13 +319,13 @@ abstract class AbstractElement * @param Comment $value * @return void */ - public function setCommentEnd(Comment $value) + public function setCommentRangeEnd(Comment $value) { if ($this instanceof Comment) { throw new \InvalidArgumentException("Cannot set a Comment on a Comment"); } - $this->commentEnd = $value; - $this->commentEnd->setEndElement($this); + $this->commentRangeEnd= $value; + $this->commentRangeEnd->setEndElement($this); } /** diff --git a/src/PhpWord/Element/Comment.php b/src/PhpWord/Element/Comment.php index df2e4d4d..def9d5a8 100644 --- a/src/PhpWord/Element/Comment.php +++ b/src/PhpWord/Element/Comment.php @@ -82,8 +82,8 @@ class Comment extends TrackChange public function setStartElement(AbstractElement $value) { $this->startElement = $value; - if ($value->getCommentStart() == null) { - $value->setCommentStart($this); + if ($value->getCommentRangeStart() == null) { + $value->setCommentRangeStart($this); } } @@ -105,8 +105,8 @@ class Comment extends TrackChange public function setEndElement(AbstractElement $value) { $this->endElement = $value; - if ($value->getCommentEnd() == null) { - $value->setCommentEnd($this); + if ($value->getCommentRangeEnd() == null) { + $value->setCommentRangeEnd($this); } } diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index 474d40ce..79877b10 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -126,8 +126,8 @@ abstract class AbstractElement */ protected function writeCommentRangeStart() { - if ($this->element->getCommentStart() != null) { - $comment = $this->element->getCommentStart(); + if ($this->element->getCommentRangeStart() != null) { + $comment = $this->element->getCommentRangeStart(); //only set the ID if it is not yet set, otherwise it will overwrite it if ($comment->getElementId() == null) { $comment->setElementId(); @@ -145,8 +145,8 @@ abstract class AbstractElement */ protected function writeCommentRangeEnd() { - if ($this->element->getCommentEnd() != null) { - $comment = $this->element->getCommentEnd(); + if ($this->element->getCommentRangeEnd() != null) { + $comment = $this->element->getCommentRangeEnd(); //only set the ID if it is not yet set, otherwise it will overwrite it if ($comment->getElementId() == null) { $comment->setElementId(); @@ -156,8 +156,8 @@ abstract class AbstractElement $this->xmlWriter->startElement('w:r'); $this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId())); $this->xmlWriter->endElement(); - } elseif ($this->element->getCommentStart() != null && $this->element->getCommentStart()->getEndElement() == null) { - $comment = $this->element->getCommentStart(); + } elseif ($this->element->getCommentRangeStart() != null && $this->element->getCommentRangeStart()->getEndElement() == null) { + $comment = $this->element->getCommentRangeStart(); //only set the ID if it is not yet set, otherwise it will overwrite it if ($comment->getElementId() == null) { $comment->setElementId(); From f21f79761aaa991a28fb700caddabb80649c4627 Mon Sep 17 00:00:00 2001 From: antoine Date: Thu, 25 May 2017 22:04:23 +0200 Subject: [PATCH 15/20] fix sample --- samples/Sample_37_Comments.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/Sample_37_Comments.php b/samples/Sample_37_Comments.php index 91d353a9..670e914b 100644 --- a/samples/Sample_37_Comments.php +++ b/samples/Sample_37_Comments.php @@ -15,7 +15,7 @@ $section = $phpWord->addSection(); $textrun = $section->addTextRun(); $textrun->addText('This '); $text = $textrun->addText('is'); -$text->setCommentStart($comment); +$text->setCommentRangeStart($comment); $textrun->addText(' a test'); $section->addTextBreak(2); @@ -28,10 +28,10 @@ $phpWord->addComment($commentWithStartAndEnd); $textrunWithEnd = $section->addTextRun(); $textrunWithEnd->addText('This '); $textToStartOn = $textrunWithEnd->addText('is', array('bold' => true)); -$textToStartOn->setCommentStart($commentWithStartAndEnd); +$textToStartOn->setCommentRangeStart($commentWithStartAndEnd); $textrunWithEnd->addText(' another', array('italic' => true)); $textToEndOn = $textrunWithEnd->addText(' test'); -$textToEndOn->setCommentEnd($commentWithStartAndEnd); +$textToEndOn->setCommentRangeEnd($commentWithStartAndEnd); $section->addTextBreak(2); @@ -42,7 +42,7 @@ $imageComment->addText('Hey, Mars does look '); $imageComment->addText('red', array('color' => 'FF0000')); $phpWord->addComment($commentOnImage); $image = $section->addImage('resources/_mars.jpg'); -$image->setCommentStart($commentOnImage); +$image->setCommentRangeStart($commentOnImage); $section->addTextBreak(2); From 6a84b8ed267eb210a97d3efa92a752b73c7b0fb8 Mon Sep 17 00:00:00 2001 From: antoine Date: Wed, 7 Jun 2017 02:26:59 +0200 Subject: [PATCH 16/20] Add possiblity to add not defined options --- src/PhpWord/Element/Field.php | 10 +++++----- src/PhpWord/Writer/Word2007/Element/Field.php | 4 +++- tests/PhpWord/Element/FieldTest.php | 14 ++++++++++++-- tests/PhpWord/Writer/Word2007/ElementTest.php | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index b5dec19a..bdfdcb40 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -60,7 +60,7 @@ class Field extends AbstractElement ), 'INDEX'=>array( 'properties' => array(), - 'options'=>array('PreserveFormat') + 'options' => array('PreserveFormat') ) ); @@ -122,7 +122,7 @@ class Field extends AbstractElement if (isset($this->fieldsArray[$type])) { $this->type = $type; } else { - throw new \InvalidArgumentException("Invalid type"); + throw new \InvalidArgumentException("Invalid type '$type'"); } } return $this->type; @@ -152,7 +152,7 @@ class Field extends AbstractElement if (is_array($properties)) { foreach (array_keys($properties) as $propkey) { if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) { - throw new \InvalidArgumentException("Invalid property"); + throw new \InvalidArgumentException("Invalid property '$propkey'"); } } $this->properties = array_merge($this->properties, $properties); @@ -183,8 +183,8 @@ class Field extends AbstractElement { if (is_array($options)) { foreach (array_keys($options) as $optionkey) { - if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey]))) { - throw new \InvalidArgumentException("Invalid option"); + if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') { + throw new \InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options'])); } } $this->options = array_merge($this->options, $options); diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index ad8032c7..802803db 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -77,6 +77,8 @@ class Field extends Text case 'Italic': $instruction .= '\i '; break; + default: + $instruction .= $option .' '; } } @@ -106,7 +108,7 @@ class Field extends Text $xmlWriter->startElement('w:noProof'); $xmlWriter->endElement(); // w:noProof $xmlWriter->endElement(); // w:rPr - $xmlWriter->writeElement('w:t', '1'); + $xmlWriter->writeElement('w:t', $element->getText() == null ? '1' : $element->getText()); $xmlWriter->endElement(); // w:r $xmlWriter->startElement('w:r'); diff --git a/tests/PhpWord/Element/FieldTest.php b/tests/PhpWord/Element/FieldTest.php index d4e47e62..415aaa0b 100644 --- a/tests/PhpWord/Element/FieldTest.php +++ b/tests/PhpWord/Element/FieldTest.php @@ -75,15 +75,25 @@ class FieldTest extends \PHPUnit_Framework_TestCase */ public function testConstructWithTypePropertiesOptionsText() { - $oField = new Field('XE', array(), array('Bold'), 'FieldValue'); + $oField = new Field('XE', array(), array('Bold', 'Italic'), 'FieldValue'); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); $this->assertEquals('XE', $oField->getType()); $this->assertEquals(array(), $oField->getProperties()); - $this->assertEquals(array('Bold'), $oField->getOptions()); + $this->assertEquals(array('Bold', 'Italic'), $oField->getOptions()); $this->assertEquals('FieldValue', $oField->getText()); } + public function testConstructWithOptionValue() + { + $oField = new Field('INDEX', array(), array('\\c "3" \\h "A"')); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); + $this->assertEquals('INDEX', $oField->getType()); + $this->assertEquals(array(), $oField->getProperties()); + $this->assertEquals(array('\\c "3" \\h "A"'), $oField->getOptions()); + } + /** * Test setType exception * diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index 027ba86a..7208449d 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -192,6 +192,24 @@ class ElementTest extends \PHPUnit_Framework_TestCase } } + public function testFieldElement() + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $section->addField('INDEX', [], ['\\c "3"']); + $section->addField('XE', [], ['Bold', 'Italic'], 'Index Entry'); + $section->addField('DATE', ['dateformat' => 'd-M-yyyy'], ['PreserveFormat', 'LastUsedFormat']); + $section->addField('DATE', [], ['LunarCalendar']); + $section->addField('DATE', [], ['SakaEraCalendar']); + $section->addField('NUMPAGES', ['format' => 'roman', 'numformat' => '0,00'], ['SakaEraCalendar']); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:p/w:r/w:instrText'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals(' INDEX \\c "3" ', $doc->getElement($element)->textContent); + } + /** * Test form fields */ From 8aabf812f8bd27ba49e251d904f4953b6b91cca5 Mon Sep 17 00:00:00 2001 From: antoine Date: Sun, 11 Jun 2017 12:56:59 +0200 Subject: [PATCH 17/20] update XE field to support formatted text + add documentation --- docs/elements.rst | 30 +++- samples/Sample_27_Field.php | 16 ++- src/PhpWord/Element/AbstractContainer.php | 2 +- src/PhpWord/Element/Field.php | 11 +- src/PhpWord/Writer/Word2007/Element/Field.php | 129 +++++++++++------- tests/PhpWord/Element/FieldTest.php | 17 +++ tests/PhpWord/Writer/Word2007/ElementTest.php | 28 ++++ 7 files changed, 177 insertions(+), 56 deletions(-) diff --git a/docs/elements.rst b/docs/elements.rst index d68ee035..b753ddf7 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -358,7 +358,35 @@ To be completed Fields ------ -To be completed +Currently the following fields are supported: + +- PAGE +- NUMPAGES +- DATE +- XE +- INDEX + +.. code-block:: php + + $section->addField($fieldType, [$properties], [$options], [$fieldText]) + +See ``\PhpOffice\PhpWord\Element\Field`` for list of properties and options available for each field type. +Options which are not specifically defined can be added. Those must start with a ``\``. + +For instance for the INDEX field, you can do the following (See `Index Field for list of available options `_ ): + +.. code-block:: php + + //the $fieldText can be either a simple string + $fieldText = 'The index value'; + + //or a 'TextRun', to be able to format the text you want in the index + $fieldText = new TextRun(); + $fieldText->addText('My '); + $fieldText->addText('bold index', ['bold' => true]); + $fieldText->addText(' entry'); + + $section->addField('INDEX', array(), array('\\e " " \\h "A" \\c "3"'), $fieldText); Line ------ diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php index 7e2b968e..b5be12ca 100644 --- a/samples/Sample_27_Field.php +++ b/samples/Sample_27_Field.php @@ -1,4 +1,6 @@ addField('NUMPAGES', array('numformat' => '0,00', 'format' => 'Arabic' $textrun = $section->addTextRun(); $textrun->addText('An index field is '); -$textrun->addField('XE', array(), array('Bold'), 'FieldValue'); +$textrun->addField('XE', array(), array('Italic'), 'My first index'); +$textrun->addText('here:'); + +$indexEntryText = new TextRun(); +$indexEntryText->addText('My '); +$indexEntryText->addText('bold index', ['bold' => true]); +$indexEntryText->addText(' entry'); + +$textrun = $section->addTextRun(); +$textrun->addText('A complex index field is '); +$textrun->addField('XE', array(), array('Bold'), $indexEntryText); $textrun->addText('here:'); $section->addText('The actual index:'); -$section->addField('INDEX', array(), array('PreserveFormat')); +$section->addField('INDEX', array(), array('\\e " "'), 'right click to update the index'); $textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER)); $textrun->addText('This is the date of lunar calendar '); diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index a3872e0b..0107006f 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -39,7 +39,7 @@ namespace PhpOffice\PhpWord\Element; * @method Image addImage(string $source, mixed $style = null, bool $isWatermark = false) * @method Object addObject(string $source, mixed $style = null) * @method TextBox addTextBox(mixed $style = null) - * @method Field addField(string $type = null, array $properties = array(), array $options = array()) + * @method Field addField(string $type = null, array $properties = array(), array $options = array(), mixed $text = null) * @method Line addLine(mixed $lineStyle = null) * @method Shape addShape(string $type, mixed $style = null) * @method Chart addChart(string $type, array $categories, array $values, array $style = null) diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index bdfdcb40..380d7a92 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -74,7 +74,7 @@ class Field extends AbstractElement /** * Field text * - * @var string + * @var TextRun | string */ protected $text; @@ -98,6 +98,7 @@ class Field extends AbstractElement * @param string $type * @param array $properties * @param array $options + * @param TextRun | string $text */ public function __construct($type = null, $properties = array(), $options = array(), $text = null) { @@ -205,16 +206,16 @@ class Field extends AbstractElement /** * Set Field text * - * @param string $text + * @param string | TextRun $text * - * @return string + * @return string | TextRun * * @throws \InvalidArgumentException */ public function setText($text) { if (isset($text)) { - if (is_string($text)) { + if (is_string($text) || $text instanceof TextRun) { $this->text = $text; } else { throw new \InvalidArgumentException("Invalid text"); @@ -226,7 +227,7 @@ class Field extends AbstractElement /** * Get Field text * - * @return string + * @return string | TextRun */ public function getText() { diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index 802803db..a79683e2 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; +use PhpOffice\PhpWord\Element\TextRun; + /** * Field element writer * @@ -37,51 +39,6 @@ class Field extends Text return; } - $instruction = ' ' . $element->getType() . ' '; - if ($element->getText() != null) { - $instruction .= '"' . $element->getText() . '" '; - } - $properties = $element->getProperties(); - foreach ($properties as $propkey => $propval) { - switch ($propkey) { - case 'format': - $instruction .= '\* ' . $propval . ' '; - break; - case 'numformat': - $instruction .= '\# ' . $propval . ' '; - break; - case 'dateformat': - $instruction .= '\@ "' . $propval . '" '; - break; - } - } - - $options = $element->getOptions(); - foreach ($options as $option) { - switch ($option) { - case 'PreserveFormat': - $instruction .= '\* MERGEFORMAT '; - break; - case 'LunarCalendar': - $instruction .= '\h '; - break; - case 'SakaEraCalendar': - $instruction .= '\s '; - break; - case 'LastUsedFormat': - $instruction .= '\l '; - break; - case 'Bold': - $instruction .= '\b '; - break; - case 'Italic': - $instruction .= '\i '; - break; - default: - $instruction .= $option .' '; - } - } - $this->startElementP(); $xmlWriter->startElement('w:r'); @@ -90,6 +47,17 @@ class Field extends Text $xmlWriter->endElement(); // w:fldChar $xmlWriter->endElement(); // w:r + $instruction = ' ' . $element->getType() . ' '; + if ($element->getText() != null) { + if (is_string($element->getText())) { + $instruction .= '"' . $element->getText() . '" '; + $instruction .= $this->buildPropertiesAndOptions($element); + } else { + $instruction .= '"'; + } + } else { + $instruction .= $this->buildPropertiesAndOptions($element); + } $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:instrText'); $xmlWriter->writeAttribute('xml:space', 'preserve'); @@ -97,6 +65,27 @@ class Field extends Text $xmlWriter->endElement(); // w:instrText $xmlWriter->endElement(); // w:r + if ($element->getText() != null) { + if ($element->getText() instanceof TextRun) { + + $containerWriter = new Container($xmlWriter, $element->getText(), true); + $containerWriter->write(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->text('"' . $this->buildPropertiesAndOptions($element)); + $xmlWriter->endElement(); // w:instrText + $xmlWriter->endElement(); // w:r + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->text(' '); + $xmlWriter->endElement(); // w:instrText + $xmlWriter->endElement(); // w:r + } + } + $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:fldChar'); $xmlWriter->writeAttribute('w:fldCharType', 'separate'); @@ -108,9 +97,9 @@ class Field extends Text $xmlWriter->startElement('w:noProof'); $xmlWriter->endElement(); // w:noProof $xmlWriter->endElement(); // w:rPr - $xmlWriter->writeElement('w:t', $element->getText() == null ? '1' : $element->getText()); + $xmlWriter->writeElement('w:t', $element->getText() != null && is_string($element->getText()) ? $element->getText() : '1'); $xmlWriter->endElement(); // w:r - + $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:fldChar'); $xmlWriter->writeAttribute('w:fldCharType', 'end'); @@ -119,4 +108,50 @@ class Field extends Text $this->endElementP(); // w:p } + + private function buildPropertiesAndOptions(\PhpOffice\PhpWord\Element\Field $element) + { + $propertiesAndOptions = ''; + $properties = $element->getProperties(); + foreach ($properties as $propkey => $propval) { + switch ($propkey) { + case 'format': + $propertiesAndOptions.= '\* ' . $propval . ' '; + break; + case 'numformat': + $propertiesAndOptions.= '\# ' . $propval . ' '; + break; + case 'dateformat': + $propertiesAndOptions.= '\@ "' . $propval . '" '; + break; + } + } + + $options = $element->getOptions(); + foreach ($options as $option) { + switch ($option) { + case 'PreserveFormat': + $propertiesAndOptions.= '\* MERGEFORMAT '; + break; + case 'LunarCalendar': + $propertiesAndOptions.= '\h '; + break; + case 'SakaEraCalendar': + $propertiesAndOptions.= '\s '; + break; + case 'LastUsedFormat': + $propertiesAndOptions.= '\l '; + break; + case 'Bold': + $propertiesAndOptions.= '\b '; + break; + case 'Italic': + $propertiesAndOptions.= '\i '; + break; + default: + $propertiesAndOptions.= $option .' '; + } + } + return $propertiesAndOptions; + } } diff --git a/tests/PhpWord/Element/FieldTest.php b/tests/PhpWord/Element/FieldTest.php index 415aaa0b..6f5ebbbf 100644 --- a/tests/PhpWord/Element/FieldTest.php +++ b/tests/PhpWord/Element/FieldTest.php @@ -84,6 +84,23 @@ class FieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals('FieldValue', $oField->getText()); } + /** + * New instance with type and properties and options and text as TextRun + */ + public function testConstructWithTypePropertiesOptionsTextAsTextRun() + { + $textRun = new TextRun(); + $textRun->addText('test string'); + + $oField = new Field('XE', array(), array('Bold', 'Italic'), $textRun); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); + $this->assertEquals('XE', $oField->getType()); + $this->assertEquals(array(), $oField->getProperties()); + $this->assertEquals(array('Bold', 'Italic'), $oField->getOptions()); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oField->getText()); + } + public function testConstructWithOptionValue() { $oField = new Field('INDEX', array(), array('\\c "3" \\h "A"')); diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index 7208449d..5a04c25b 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWord\Element\TextRun; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Element subnamespace @@ -210,6 +211,33 @@ class ElementTest extends \PHPUnit_Framework_TestCase $this->assertEquals(' INDEX \\c "3" ', $doc->getElement($element)->textContent); } + public function testFieldElementWithComplexText() + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $text = new TextRun(); + $text->addText('test string', array('bold' => true)); + + $section->addField('XE', [], ['Bold', 'Italic'], $text); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:p/w:r[2]/w:instrText'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals(' XE "', $doc->getElement($element)->textContent); + + $element = '/w:document/w:body/w:p/w:r[3]/w:rPr/w:b'; + $this->assertTrue($doc->elementExists($element)); + + $element = '/w:document/w:body/w:p/w:r[3]/w:t'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals('test string', $doc->getElement($element)->textContent); + + $element = '/w:document/w:body/w:p/w:r[4]/w:instrText'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals('"\\b \\i ', $doc->getElement($element)->textContent); + } + /** * Test form fields */ From 64f06e111492c4dfe764df9d289b8b91435cf1a6 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 16 Jun 2017 17:55:24 +0200 Subject: [PATCH 18/20] use fully qualified name --- src/PhpWord/Writer/Word2007/Element/Field.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index a79683e2..5f5ae094 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Element\TextRun; - /** * Field element writer * @@ -66,7 +64,7 @@ class Field extends Text $xmlWriter->endElement(); // w:r if ($element->getText() != null) { - if ($element->getText() instanceof TextRun) { + if ($element->getText() instanceof PhpOffice\PhpWord\Element\TextRun) { $containerWriter = new Container($xmlWriter, $element->getText(), true); $containerWriter->write(); From 11d816f94ddff1799e2275185f45d1c3bce06b93 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 16 Jun 2017 22:39:20 +0200 Subject: [PATCH 19/20] php 5.3 compatibility --- tests/PhpWord/Writer/Word2007/ElementTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index 5a04c25b..b3c7b197 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -198,12 +198,12 @@ class ElementTest extends \PHPUnit_Framework_TestCase $phpWord = new PhpWord(); $section = $phpWord->addSection(); - $section->addField('INDEX', [], ['\\c "3"']); - $section->addField('XE', [], ['Bold', 'Italic'], 'Index Entry'); - $section->addField('DATE', ['dateformat' => 'd-M-yyyy'], ['PreserveFormat', 'LastUsedFormat']); - $section->addField('DATE', [], ['LunarCalendar']); - $section->addField('DATE', [], ['SakaEraCalendar']); - $section->addField('NUMPAGES', ['format' => 'roman', 'numformat' => '0,00'], ['SakaEraCalendar']); + $section->addField('INDEX', array(), array('\\c "3"')); + $section->addField('XE', array(), array('Bold', 'Italic'), 'Index Entry'); + $section->addField('DATE', array('dateformat' => 'd-M-yyyy'), array('PreserveFormat', 'LastUsedFormat')); + $section->addField('DATE', array(), array('LunarCalendar')); + $section->addField('DATE', array(), array('SakaEraCalendar')); + $section->addField('NUMPAGES', array('format' => 'roman', 'numformat' => '0,00'), array('SakaEraCalendar')); $doc = TestHelperDOCX::getDocument($phpWord); $element = '/w:document/w:body/w:p/w:r/w:instrText'; @@ -219,7 +219,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase $text = new TextRun(); $text->addText('test string', array('bold' => true)); - $section->addField('XE', [], ['Bold', 'Italic'], $text); + $section->addField('XE', array(), array('Bold', 'Italic'), $text); $doc = TestHelperDOCX::getDocument($phpWord); $element = '/w:document/w:body/w:p/w:r[2]/w:instrText'; From 2b4b200e42d324e1b839c073632c2d458543a541 Mon Sep 17 00:00:00 2001 From: antoine Date: Sat, 17 Jun 2017 00:34:02 +0200 Subject: [PATCH 20/20] fix namespace --- src/PhpWord/Writer/Word2007/Element/Field.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index 5f5ae094..9fc45b21 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -64,7 +64,7 @@ class Field extends Text $xmlWriter->endElement(); // w:r if ($element->getText() != null) { - if ($element->getText() instanceof PhpOffice\PhpWord\Element\TextRun) { + if ($element->getText() instanceof \PhpOffice\PhpWord\Element\TextRun) { $containerWriter = new Container($xmlWriter, $element->getText(), true); $containerWriter->write();