Skip to content

Commit 645d237

Browse files
committed
Merge pull request #122 from ivanlanin/develop
Allow adding MemoryImage using URL
2 parents 8e17598 + d3c739e commit 645d237

32 files changed

+415
-282
lines changed

Classes/PHPWord/Media.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
8686
$file = null;
8787
if ($type === 'image') {
8888
$cImg++;
89+
//Detect if it's a memory image first by php ext and second by regex
8990
$isMemImage = false;
9091
if (stripos(strrev($src), strrev('.php')) === 0) {
9192
$isMemImage = true;
9293
}
93-
94+
if (!$isMemImage) {
95+
$isMemImage = (filter_var($src, FILTER_VALIDATE_URL) !== false);
96+
}
9497
$extension = '';
9598
if ($isMemImage) {
9699
$extension = $memoryImage->getImageExtension();

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,9 @@ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
751751

752752
/**
753753
* @param \PHPWord_Shared_XMLWriter $objWriter
754-
* @param \PHPWord_Section_Image $image
754+
* @param \PHPWord_Section_Image|\PHPWord_Section_MemoryImage $image
755755
*/
756-
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false)
756+
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image, $withoutP = false)
757757
{
758758
$rId = $image->getRelationId();
759759

Classes/PHPWord/Writer/Word2007/ContentTypes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ public function writeContentTypes($_imageTypes, $_objectTypes, $_cHdrs, $_cFtrs)
138138
'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml'
139139
);
140140

141+
// Footnotes
142+
$this->_writeOverrideContentType(
143+
$objWriter,
144+
'/word/footnotes.xml',
145+
'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml'
146+
);
147+
141148
for ($i = 1; $i <= $_cHdrs; $i++) {
142149
$this->_writeOverrideContentType(
143150
$objWriter,

Tests/PHPWord/SettingsTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPWord_Settings;
5+
6+
/**
7+
* Class TOCTest
8+
*
9+
* @package PHPWord\Tests
10+
* @covers PHPWord_Settings
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class SettingsTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @covers PHPWord_Settings::setCompatibility
17+
* @covers PHPWord_Settings::getCompatibility
18+
*/
19+
public function testGetSetCompatibility()
20+
{
21+
$this->assertTrue(PHPWord_Settings::getCompatibility());
22+
$this->assertTrue(PHPWord_Settings::setCompatibility(false));
23+
$this->assertFalse(PHPWord_Settings::getCompatibility());
24+
$this->assertFalse(PHPWord_Settings::setCompatibility('Non boolean'));
25+
}
26+
}

Tests/PHPWord/StyleTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace PHPWord\Tests;
33

4-
use PHPUnit_Framework_TestCase;
54
use PHPWord_Style;
65

76
/**
@@ -11,7 +10,7 @@
1110
* @covers PHPWord_Style
1211
* @runTestsInSeparateProcesses
1312
*/
14-
class StyleTest extends PHPUnit_Framework_TestCase
13+
class StyleTest extends \PHPUnit_Framework_TestCase
1514
{
1615
/**
1716
* @covers PHPWord_Style::addParagraphStyle

Tests/PHPWord/Writer/Word2007/BaseTest.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public function tearDown()
2121
TestHelperDOCX::clear();
2222
}
2323

24+
/**
25+
* covers ::_writeText
26+
*/
2427
public function testWriteText()
2528
{
2629
$rStyle = 'rStyle';
@@ -40,7 +43,7 @@ public function testWriteText()
4043
}
4144

4245
/**
43-
* Write text run
46+
* covers ::_writeTextRun
4447
*/
4548
public function testWriteTextRun()
4649
{
@@ -67,7 +70,7 @@ public function testWriteTextRun()
6770
}
6871

6972
/**
70-
* Write link
73+
* covers ::_writeLink
7174
*/
7275
public function testWriteLink()
7376
{
@@ -84,7 +87,7 @@ public function testWriteLink()
8487
}
8588

8689
/**
87-
* Write preserve text
90+
* covers ::_writePreserveText
8891
*/
8992
public function testWritePreserveText()
9093
{
@@ -102,7 +105,7 @@ public function testWritePreserveText()
102105
}
103106

104107
/**
105-
* Write paragraph style: Alignment
108+
* covers ::_writeParagraphStyle
106109
*/
107110
public function testWriteParagraphStyleAlign()
108111
{
@@ -118,7 +121,7 @@ public function testWriteParagraphStyleAlign()
118121
}
119122

120123
/**
121-
* Write paragraph style: Pagination
124+
* covers ::_writeParagraphStyle
122125
*/
123126
public function testWriteParagraphStylePagination()
124127
{
@@ -180,7 +183,7 @@ public function testWriteFontStyle()
180183
}
181184

182185
/**
183-
* Write table
186+
* covers ::_writeTableStyle
184187
*/
185188
public function testWriteTableStyle()
186189
{
@@ -238,7 +241,7 @@ public function testWriteTableStyle()
238241
}
239242

240243
/**
241-
* Write cell style
244+
* covers ::_writeCellStyle
242245
*/
243246
public function testWriteCellStyleCellGridSpan()
244247
{
@@ -265,7 +268,7 @@ public function testWriteCellStyleCellGridSpan()
265268
}
266269

267270
/**
268-
* Write image
271+
* covers ::_writeImage
269272
*/
270273
public function testWriteImagePosition()
271274
{
@@ -290,7 +293,27 @@ public function testWriteImagePosition()
290293
}
291294

292295
/**
293-
* Write title
296+
* covers ::_writeWatermark
297+
*/
298+
public function testWriteWatermark()
299+
{
300+
$imageSrc = join(
301+
DIRECTORY_SEPARATOR,
302+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'earth.jpg')
303+
);
304+
305+
$PHPWord = new PHPWord();
306+
$section = $PHPWord->createSection();
307+
$header = $section->createHeader();
308+
$header->addWatermark($imageSrc);
309+
$doc = TestHelperDOCX::getDocument($PHPWord);
310+
311+
$element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference");
312+
$this->assertStringStartsWith("rId", $element->getAttribute('r:id'));
313+
}
314+
315+
/**
316+
* covers ::_writeTitle
294317
*/
295318
public function testWriteTitle()
296319
{

Tests/PHPWord/Writer/Word2007/DocumentTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,56 @@ public function testWriteEndSectionPageNumbering()
3232

3333
$this->assertEquals(2, $element->getAttribute('w:start'));
3434
}
35+
36+
/**
37+
* covers ::_writeTOC
38+
* covers ::_writePageBreak
39+
* covers ::_writeListItem
40+
* covers ::_writeTitle
41+
* covers ::_writeObject
42+
*/
43+
public function testElements()
44+
{
45+
$objectSrc = join(
46+
DIRECTORY_SEPARATOR,
47+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'sheet.xls')
48+
);
49+
50+
$PHPWord = new PHPWord();
51+
$PHPWord->addTitleStyle(1, array('color' => '333333', 'bold'=>true));
52+
$PHPWord->addTitleStyle(2, array('color'=>'666666'));
53+
$section = $PHPWord->createSection();
54+
$section->addTOC();
55+
$section->addPageBreak();
56+
$section->addTitle('Title 1', 1);
57+
$section->addListItem('List Item 1', 0);
58+
$section->addListItem('List Item 2', 0);
59+
$section->addListItem('List Item 3', 0);
60+
$section = $PHPWord->createSection();
61+
$section->addTitle('Title 2', 2);
62+
$section->addObject($objectSrc);
63+
$doc = TestHelperDOCX::getDocument($PHPWord);
64+
65+
// TOC
66+
$element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:tabs/w:tab');
67+
$this->assertEquals('right', $element->getAttribute('w:val'));
68+
$this->assertEquals('dot', $element->getAttribute('w:leader'));
69+
$this->assertEquals(9062, $element->getAttribute('w:pos'));
70+
71+
// Page break
72+
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:br');
73+
$this->assertEquals('page', $element->getAttribute('w:type'));
74+
75+
// Title
76+
$element = $doc->getElement('/w:document/w:body/w:p[5]/w:pPr/w:pStyle');
77+
$this->assertEquals('Heading1', $element->getAttribute('w:val'));
78+
79+
// List item
80+
$element = $doc->getElement('/w:document/w:body/w:p[6]/w:pPr/w:numPr/w:numId');
81+
$this->assertEquals(3, $element->getAttribute('w:val'));
82+
83+
// Object
84+
$element = $doc->getElement('/w:document/w:body/w:p[11]/w:r/w:object/o:OLEObject');
85+
$this->assertEquals('Embed', $element->getAttribute('Type'));
86+
}
3587
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace PHPWord\Tests\Writer\Word2007;
3+
4+
use PHPWord;
5+
use PHPWord\Tests\TestHelperDOCX;
6+
7+
/**
8+
* Class PHPWord_Writer_Word2007_FootnotesTest
9+
* @package PHPWord\Tests
10+
* @runTestsInSeparateProcesses
11+
*/
12+
class FootnotesTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* Executed before each method of the class
16+
*/
17+
public function tearDown()
18+
{
19+
TestHelperDOCX::clear();
20+
}
21+
22+
public function testWriteFootnotes()
23+
{
24+
$PHPWord = new PHPWord();
25+
$section = $PHPWord->createSection();
26+
$section->addText('Text');
27+
$footnote = $section->createFootnote();
28+
$footnote->addText('Footnote');
29+
$footnote->addLink('http://google.com');
30+
$doc = TestHelperDOCX::getDocument($PHPWord);
31+
32+
$this->assertTrue($doc->elementExists("/w:document/w:body/w:p/w:r/w:footnoteReference"));
33+
}
34+
}

Tests/PHPWord/Writer/Word2007/StylesTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,31 @@ public function testWriteStyles()
2626
{
2727
$PHPWord = new PHPWord();
2828

29-
$defaultStyle = array('align' => 'both');
30-
$baseStyle = array('basedOn' => 'Normal');
31-
$newStyle = array('basedOn' => 'Base Style', 'next' => 'Normal');
32-
$PHPWord->setDefaultParagraphStyle($defaultStyle);
33-
$PHPWord->addParagraphStyle('Base Style', $baseStyle);
34-
$PHPWord->addParagraphStyle('New Style', $newStyle);
29+
$pStyle = array('align' => 'both');
30+
$pBase = array('basedOn' => 'Normal');
31+
$pNew = array('basedOn' => 'Base Style', 'next' => 'Normal');
32+
$rStyle = array('size' => 20);
33+
$tStyle = array(
34+
'bgColor' => 'FF0000',
35+
'cellMarginTop' => 120,
36+
'cellMarginBottom' => 120,
37+
'cellMarginLeft' => 120,
38+
'cellMarginRight' => 120,
39+
'borderTopSize' => 120,
40+
'borderBottomSize' => 120,
41+
'borderLeftSize' => 120,
42+
'borderRightSize' => 120,
43+
'borderInsideHSize' => 120,
44+
'borderInsideVSize' => 120,
45+
);
46+
$PHPWord->setDefaultParagraphStyle($pStyle);
47+
$PHPWord->addParagraphStyle('Base Style', $pBase);
48+
$PHPWord->addParagraphStyle('New Style', $pNew);
49+
$PHPWord->addFontStyle('New Style', $rStyle, $pStyle);
50+
$PHPWord->addTableStyle('Table Style', $tStyle, $tStyle);
51+
$PHPWord->addTitleStyle(1, $rStyle, $pStyle);
3552
$doc = TestHelperDOCX::getDocument($PHPWord);
53+
3654
$file = 'word/styles.xml';
3755

3856
// Normal style generated?

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Changes in branch for release 0.7.1 :
5151
- Feature: (ivanlanin) - Paragraph: setTabs() function
5252
- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF
5353
- Feature: (ivanlanin) - Reader: Initial effort for Word2007
54+
- Feature: (ivanlanin) - MemoryImage: Allow remote image when allow_url_open = on
5455
- QA: (Progi1984) - UnitTests
5556

5657
Changes in branch for release 0.7.0 :

samples/Sample_01_SimpleText.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
$section->addTextBreak();
4646

4747
// Image
48-
$section->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18));
48+
$section->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18));
4949

5050
// Save file
5151
$name = basename(__FILE__, '.php');

samples/Sample_04_Textrun.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
$textrun->addText(' Sample Link: ');
3333
$textrun->addLink('http://www.google.com', null, 'NLink');
3434
$textrun->addText(' Sample Image: ');
35-
$textrun->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18));
35+
$textrun->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18));
3636
$textrun->addText(' Here is some more text. ');
3737

3838
// Save file

0 commit comments

Comments
 (0)