Skip to content

Commit 6a0bfe3

Browse files
committed
Move addTextBox to AbstractContainer and add sample for textbox
1 parent c7a940c commit 6a0bfe3

File tree

13 files changed

+91
-509
lines changed

13 files changed

+91
-509
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ script:
5252
## PHP Copy/Paste Detector
5353
- php phpcpd.phar src/ tests/ --verbose
5454
## PHP Mess Detector
55-
- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php
55+
#- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php
5656
## PHPLOC
5757
#- php phploc.phar src/
5858
## PHPUnit

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
1010

1111
- Image: Ability to define relative and absolute positioning - @basjan GH-217
1212
- Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219
13+
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228
1314

1415
### Bugfixes
1516

samples/Sample_25_TextBox.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// New Word Document
5+
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
6+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
8+
$section = $phpWord->addSection();
9+
$textbox = $section->addTextBox(array('align' => 'left', 'width' => 300, 'borderSize' => 1, 'borderColor' => '#FF0000'));
10+
$textbox->addText('Text box content ');
11+
$textbox->addText('with bold text', array('bold' => true));
12+
$textbox->addText(', ');
13+
$textbox->addLink('http://www.google.com', 'link');
14+
$textbox->addText(', and image ');
15+
$textbox->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18));
16+
$textbox->addText('.');
17+
18+
// Save file
19+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
20+
if (!CLI) {
21+
include_once 'Sample_Footer.php';
22+
}

src/PhpWord/Element/AbstractContainer.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = n
295295
return $element;
296296
}
297297

298+
/**
299+
* Add textbox element
300+
*
301+
* @param mixed $style
302+
* @return \PhpOffice\PhpWord\Element\TextBox
303+
*/
304+
public function addTextBox($style = null)
305+
{
306+
$this->checkValidity('TextBox');
307+
308+
$textbox = new TextBox($style);
309+
$textbox->setDocPart($this->getDocPart(), $this->getDocPartId());
310+
$this->addElement($textbox);
311+
312+
return $textbox;
313+
}
314+
298315
/**
299316
* Check if a method is allowed for the current container
300317
*
@@ -314,6 +331,7 @@ private function checkValidity($method)
314331
'TextRun' => array('section', 'header', 'footer', 'cell'),
315332
'ListItem' => array('section', 'header', 'footer', 'cell'),
316333
'CheckBox' => array('section', 'header', 'footer', 'cell'),
334+
'TextBox' => array('section', 'header', 'footer'),
317335
'Footnote' => array('section', 'textrun', 'cell'),
318336
'Endnote' => array('section', 'textrun', 'cell'),
319337
'PreserveText' => array('header', 'footer', 'cell'),
@@ -352,7 +370,7 @@ private function checkValidity($method)
352370
*/
353371
private function checkElementDocPart()
354372
{
355-
$isCellTextrun = in_array($this->container, array('cell', 'textrun'));
373+
$isCellTextrun = in_array($this->container, array('cell', 'textrun', 'textbox'));
356374
$docPart = $isCellTextrun ? $this->getDocPart() : $this->container;
357375
$docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId;
358376
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');

src/PhpWord/Element/AbstractElement.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
*/
2828
abstract class AbstractElement
2929
{
30-
30+
/**
31+
* PhpWord object
32+
*
33+
* @var \PhpOffice\PhpWord\PhpWord
34+
*/
3135
protected $phpWord;
3236

3337
/**

src/PhpWord/Element/Footer.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,6 @@ public function evenPage()
115115
return $this->type = self::EVEN;
116116
}
117117

118-
/**
119-
* Add textbox element
120-
*
121-
* @param mixed $style
122-
* @return \PhpOffice\PhpWord\Element\TextBox
123-
* @todo Merge with the same function on Section
124-
*/
125-
public function addTextBox($style = null)
126-
{
127-
$textbox = new TextBox($this->getDocPart(), $this->getDocPartId(), $style);
128-
$this->addElement($textbox);
129-
130-
return $textbox;
131-
}
132-
133118
/**
134119
* Add table element
135120
*

src/PhpWord/Element/Section.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,6 @@ public function addPageBreak()
117117
$this->addElement(new PageBreak());
118118
}
119119

120-
/**
121-
* Add textbox element
122-
*
123-
* @param mixed $style
124-
* @return \PhpOffice\PhpWord\Element\TextBox
125-
* @todo Merge with the same function on Footer
126-
*/
127-
public function addTextBox($style = null)
128-
{
129-
$textbox = new TextBox($this->getDocPart(), $this->getDocPartId(), $style);
130-
$this->addElement($textbox);
131-
132-
return $textbox;
133-
}
134-
135120
/**
136121
* Add table element
137122
*

src/PhpWord/Element/TextBox.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ class TextBox extends AbstractContainer
3030
* @var \PhpOffice\PhpWord\Style\TextBox
3131
*/
3232
private $style;
33-
33+
3434
/**
3535
* Create a new textbox
3636
*
3737
* @param string $docPart
3838
* @param integer $docPartId
3939
* @param mixed $style
4040
*/
41-
public function __construct($docPart, $docPartId, $style = null)
41+
public function __construct($style = null)
4242
{
4343
$this->container = 'textbox';
44-
$this->setDocPart($docPart, $docPartId);
4544
$this->style = $this->setStyle(new TextBoxStyle(), $style);
4645
}
4746

@@ -54,4 +53,4 @@ public function getStyle()
5453
{
5554
return $this->style;
5655
}
57-
}
56+
}

0 commit comments

Comments
 (0)