Move `addTextBox` to `AbstractContainer` and add sample for textbox

This commit is contained in:
Ivan Lanin 2014-05-08 09:52:11 +07:00
parent c7a940cc4c
commit 6a0bfe3314
13 changed files with 91 additions and 509 deletions

View File

@ -52,7 +52,7 @@ script:
## PHP Copy/Paste Detector ## PHP Copy/Paste Detector
- php phpcpd.phar src/ tests/ --verbose - php phpcpd.phar src/ tests/ --verbose
## PHP Mess Detector ## PHP Mess Detector
- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php #- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php
## PHPLOC ## PHPLOC
#- php phploc.phar src/ #- php phploc.phar src/
## PHPUnit ## PHPUnit

View File

@ -10,6 +10,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
- Image: Ability to define relative and absolute positioning - @basjan GH-217 - Image: Ability to define relative and absolute positioning - @basjan GH-217
- Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219 - Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228
### Bugfixes ### Bugfixes

View File

@ -0,0 +1,22 @@
<?php
include_once 'Sample_Header.php';
// New Word Document
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$textbox = $section->addTextBox(array('align' => 'left', 'width' => 300, 'borderSize' => 1, 'borderColor' => '#FF0000'));
$textbox->addText('Text box content ');
$textbox->addText('with bold text', array('bold' => true));
$textbox->addText(', ');
$textbox->addLink('http://www.google.com', 'link');
$textbox->addText(', and image ');
$textbox->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18));
$textbox->addText('.');
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}

View File

@ -295,6 +295,23 @@ abstract class AbstractContainer extends AbstractElement
return $element; return $element;
} }
/**
* Add textbox element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\TextBox
*/
public function addTextBox($style = null)
{
$this->checkValidity('TextBox');
$textbox = new TextBox($style);
$textbox->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($textbox);
return $textbox;
}
/** /**
* Check if a method is allowed for the current container * Check if a method is allowed for the current container
* *
@ -314,6 +331,7 @@ abstract class AbstractContainer extends AbstractElement
'TextRun' => array('section', 'header', 'footer', 'cell'), 'TextRun' => array('section', 'header', 'footer', 'cell'),
'ListItem' => array('section', 'header', 'footer', 'cell'), 'ListItem' => array('section', 'header', 'footer', 'cell'),
'CheckBox' => array('section', 'header', 'footer', 'cell'), 'CheckBox' => array('section', 'header', 'footer', 'cell'),
'TextBox' => array('section', 'header', 'footer'),
'Footnote' => array('section', 'textrun', 'cell'), 'Footnote' => array('section', 'textrun', 'cell'),
'Endnote' => array('section', 'textrun', 'cell'), 'Endnote' => array('section', 'textrun', 'cell'),
'PreserveText' => array('header', 'footer', 'cell'), 'PreserveText' => array('header', 'footer', 'cell'),
@ -352,7 +370,7 @@ abstract class AbstractContainer extends AbstractElement
*/ */
private function checkElementDocPart() private function checkElementDocPart()
{ {
$isCellTextrun = in_array($this->container, array('cell', 'textrun')); $isCellTextrun = in_array($this->container, array('cell', 'textrun', 'textbox'));
$docPart = $isCellTextrun ? $this->getDocPart() : $this->container; $docPart = $isCellTextrun ? $this->getDocPart() : $this->container;
$docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId;
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');

View File

@ -27,7 +27,11 @@ use PhpOffice\PhpWord\Style;
*/ */
abstract class AbstractElement abstract class AbstractElement
{ {
/**
* PhpWord object
*
* @var \PhpOffice\PhpWord\PhpWord
*/
protected $phpWord; protected $phpWord;
/** /**

View File

@ -115,21 +115,6 @@ class Footer extends AbstractContainer
return $this->type = self::EVEN; return $this->type = self::EVEN;
} }
/**
* Add textbox element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\TextBox
* @todo Merge with the same function on Section
*/
public function addTextBox($style = null)
{
$textbox = new TextBox($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($textbox);
return $textbox;
}
/** /**
* Add table element * Add table element
* *

View File

@ -117,21 +117,6 @@ class Section extends AbstractContainer
$this->addElement(new PageBreak()); $this->addElement(new PageBreak());
} }
/**
* Add textbox element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\TextBox
* @todo Merge with the same function on Footer
*/
public function addTextBox($style = null)
{
$textbox = new TextBox($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($textbox);
return $textbox;
}
/** /**
* Add table element * Add table element
* *

View File

@ -38,10 +38,9 @@ class TextBox extends AbstractContainer
* @param integer $docPartId * @param integer $docPartId
* @param mixed $style * @param mixed $style
*/ */
public function __construct($docPart, $docPartId, $style = null) public function __construct($style = null)
{ {
$this->container = 'textbox'; $this->container = 'textbox';
$this->setDocPart($docPart, $docPartId);
$this->style = $this->setStyle(new TextBoxStyle(), $style); $this->style = $this->setStyle(new TextBoxStyle(), $style);
} }

View File

@ -20,142 +20,8 @@ namespace PhpOffice\PhpWord\Style;
/** /**
* TextBox style * TextBox style
*/ */
class TextBox extends AbstractStyle class TextBox extends Image
{ {
/**
* Wrapping styles
*
* @const string
*/
const WRAPPING_STYLE_INLINE = 'inline';
const WRAPPING_STYLE_SQUARE = 'square';
const WRAPPING_STYLE_TIGHT = 'tight';
const WRAPPING_STYLE_BEHIND = 'behind';
const WRAPPING_STYLE_INFRONT = 'infront';
/**
* Horizontal alignment
*
* @const string
*/
const POSITION_HORIZONTAL_LEFT = 'left';
const POSITION_HORIZONTAL_CENTER = 'center';
const POSITION_HORIZONTAL_RIGHT = 'right';
/**
* Vertical alignment
*
* @const string
*/
const POSITION_VERTICAL_TOP = 'top';
const POSITION_VERTICAL_CENTER = 'center';
const POSITION_VERTICAL_BOTTOM = 'bottom';
const POSITION_VERTICAL_INSIDE = 'inside';
const POSITION_VERTICAL_OUTSIDE = 'outside';
/**
* Position relative to
*
* @const string
*/
const POSITION_RELATIVE_TO_MARGIN = 'margin';
const POSITION_RELATIVE_TO_PAGE = 'page';
const POSITION_RELATIVE_TO_COLUMN = 'column';
const POSITION_RELATIVE_TO_CHAR = 'char';
const POSITION_RELATIVE_TO_LMARGIN = 'left-margin-area';
const POSITION_RELATIVE_TO_RMARGIN = 'right-margin-area';
const POSITION_RELATIVE_TO_IMARGIN = 'inner-margin-area';
const POSITION_RELATIVE_TO_OMARGIN = 'outer-margin-area';
const POSITION_RELATIVE_TO_LINE = 'line';
const POSITION_RELATIVE_TO_TMARGIN = 'top-margin-area';
const POSITION_RELATIVE_TO_BMARGIN = 'bottom-margin-area';
/**
* Position type, relative/absolute
*
* @const string
*/
const POSITION_RELATIVE = 'relative';
const POSITION_ABSOLUTE = 'absolute';
/**
* TextBox width
*
* @var int
*/
private $width;
/**
* TextBox height
*
* @var int
*/
private $height;
/**
* Alignment
*
* @var string
*/
private $align;
/**
* Margin Top
*
* @var int
*/
private $marginTop;
/**
* Margin Left
*
* @var int
*/
private $marginLeft;
/**
* Wrapping style
*
* @var string
*/
private $wrappingStyle;
/**
* Positioning type (relative or absolute)
*
* @var string
*/
private $positioning;
/**
* Horizontal alignment
*
* @var string
*/
private $posHorizontal;
/**
* Horizontal Relation
*
* @var string
*/
private $posHorizontalRel;
/**
* Vertical alignment
*
* @var string
*/
private $posVertical;
/**
* Vertical Relation
*
* @var string
*/
private $posVerticalRel;
/** /**
* margin top * margin top
* *
@ -198,304 +64,6 @@ class TextBox extends AbstractStyle
*/ */
private $borderColor; private $borderColor;
/**
* Create new textbox style
*/
public function __construct()
{
$this->setWrappingStyle(self::WRAPPING_STYLE_INLINE);
$this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT);
$this->setPosHorizontalRel(self::POSITION_RELATIVE_TO_CHAR);
$this->setPosVertical(self::POSITION_VERTICAL_TOP);
$this->setPosVerticalRel(self::POSITION_RELATIVE_TO_LINE);
}
/**
* Get width
*/
public function getWidth()
{
return $this->width;
}
/**
* Set width
*
* @param int $value
*/
public function setWidth($value = null)
{
$this->width = $value;
}
/**
* Get height
*/
public function getHeight()
{
return $this->height;
}
/**
* Set height
*
* @param int $value
*/
public function setHeight($value = null)
{
$this->height = $value;
}
/**
* Get alignment
*/
public function getAlign()
{
return $this->align;
}
/**
* Set alignment
*
* @param string $value
*/
public function setAlign($value = null)
{
$this->align = $value;
}
/**
* Get Margin Top
*
* @return int
*/
public function getMarginTop()
{
return $this->marginTop;
}
/**
* Set Margin Top
*
* @param int $value
* @return self
*/
public function setMarginTop($value = null)
{
$this->marginTop = $value;
return $this;
}
/**
* Get Margin Left
*
* @return int
*/
public function getMarginLeft()
{
return $this->marginLeft;
}
/**
* Set Margin Left
*
* @param int $value
* @return self
*/
public function setMarginLeft($value = null)
{
$this->marginLeft = $value;
return $this;
}
/**
* Get wrapping style
*
* @return string
*/
public function getWrappingStyle()
{
return $this->wrappingStyle;
}
/**
* Set wrapping style
*
* @param string $wrappingStyle
* @throws \InvalidArgumentException
* @return self
*/
public function setWrappingStyle($wrappingStyle)
{
$enum = array(self::WRAPPING_STYLE_INLINE, self::WRAPPING_STYLE_INFRONT, self::WRAPPING_STYLE_BEHIND,
self::WRAPPING_STYLE_SQUARE, self::WRAPPING_STYLE_TIGHT);
if (in_array($wrappingStyle, $enum)) {
$this->wrappingStyle = $wrappingStyle;
} else {
throw new \InvalidArgumentException('Invalid wrapping style.');
}
return $this;
}
/**
* Get positioning type
*
* @return string
*/
public function getPositioning()
{
return $this->positioning;
}
/**
* Set positioning type
*
* @param string $positioning
* @throws \InvalidArgumentException
* @return self
*/
public function setPositioning($positioning)
{
$enum = array(self::POSITION_RELATIVE, self::POSITION_ABSOLUTE);
if (in_array($positioning, $enum)) {
$this->positioning = $positioning;
} else {
throw new \InvalidArgumentException('Invalid positioning.');
}
return $this;
}
/**
* Get horizontal alignment
*
* @return string
*/
public function getPosHorizontal()
{
return $this->posHorizontal;
}
/**
* Set horizontal alignment
*
* @param string $alignment
* @throws \InvalidArgumentException
* @return self
*/
public function setPosHorizontal($alignment)
{
$enum = array(self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER,
self::POSITION_HORIZONTAL_RIGHT);
if (in_array($alignment, $enum)) {
$this->posHorizontal = $alignment;
} else {
throw new \InvalidArgumentException('Invalid horizontal alignment.');
}
return $this;
}
/**
* Get vertical alignment
*
* @return string
*/
public function getPosVertical()
{
return $this->posVertical;
}
/**
* Set vertical alignment
*
* @param string $alignment
* @throws \InvalidArgumentException
* @return self
*/
public function setPosVertical($alignment)
{
$enum = array(self::POSITION_VERTICAL_TOP, self::POSITION_VERTICAL_CENTER,
self::POSITION_VERTICAL_BOTTOM, self::POSITION_VERTICAL_INSIDE, self::POSITION_VERTICAL_OUTSIDE);
if (in_array($alignment, $enum)) {
$this->posVertical = $alignment;
} else {
throw new \InvalidArgumentException('Invalid vertical alignment.');
}
return $this;
}
/**
* Get horizontal relation
*
* @return string
*/
public function getPosHorizontalRel()
{
return $this->posHorizontalRel;
}
/**
* Set horizontal relation
*
* @param string $relto
* @throws \InvalidArgumentException
* @return self
*/
public function setPosHorizontalRel($relto)
{
$enum = array(self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE,
self::POSITION_RELATIVE_TO_COLUMN, self::POSITION_RELATIVE_TO_CHAR,
self::POSITION_RELATIVE_TO_LMARGIN, self::POSITION_RELATIVE_TO_RMARGIN,
self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN);
if (in_array($relto, $enum)) {
$this->posHorizontalRel = $relto;
} else {
throw new \InvalidArgumentException('Invalid relative horizontal alignment.');
}
return $this;
}
/**
* Get vertical relation
*
* @return string
*/
public function getPosVerticalRel()
{
return $this->posVerticalRel;
}
/**
* Set vertical relation
*
* @param string $relto
* @throws \InvalidArgumentException
* @return self
*/
public function setPosVerticalRel($relto)
{
$enum = array(self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE,
self::POSITION_RELATIVE_TO_LINE,
self::POSITION_RELATIVE_TO_TMARGIN, self::POSITION_RELATIVE_TO_BMARGIN,
self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN);
if (in_array($relto, $enum)) {
$this->posVerticalRel = $relto;
} else {
throw new \InvalidArgumentException('Invalid relative vertical alignment.');
}
return $this;
}
/** /**
* Set margin top * Set margin top
* *

View File

@ -54,10 +54,12 @@ class TextBox extends Element
$this->xmlWriter->writeAttribute('type', '#_x0000_t0202'); $this->xmlWriter->writeAttribute('type', '#_x0000_t0202');
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->startElement('v:textbox'); $this->xmlWriter->startElement('v:textbox');
$margins=implode(', ',$tbxStyle->getInnerMargin()); $margins = implode(', ', $tbxStyle->getInnerMargin());
$this->xmlWriter->writeAttribute('inset', $margins); $this->xmlWriter->writeAttribute('inset', $margins);
$this->xmlWriter->startElement('w:txbxContent'); $this->xmlWriter->startElement('w:txbxContent');
$this->xmlWriter->startElement('w:p');
$this->parentWriter->writeContainerElements($this->xmlWriter, $this->element); $this->parentWriter->writeContainerElements($this->xmlWriter, $this->element);
$this->xmlWriter->endElement(); // w:p
$this->xmlWriter->endElement(); // w:txbxContent $this->xmlWriter->endElement(); // w:txbxContent
$this->xmlWriter->endElement(); // v: textbox $this->xmlWriter->endElement(); // v: textbox
$styleWriter->writeW10Wrap(); $styleWriter->writeW10Wrap();

View File

@ -110,7 +110,7 @@ abstract class AbstractPart
// Loop through elements // Loop through elements
$elements = $container->getElements(); $elements = $container->getElements();
$withoutP = in_array($containerName, array('TextRun', 'Footnote', 'Endnote')) ? true : false; $withoutP = in_array($containerName, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false;
if (count($elements) > 0) { if (count($elements) > 0) {
foreach ($elements as $element) { foreach ($elements as $element) {
if ($element instanceof AbstractElement) { if ($element instanceof AbstractElement) {

View File

@ -32,7 +32,7 @@ class TextBoxTest extends \PHPUnit_Framework_TestCase
*/ */
public function testConstruct() public function testConstruct()
{ {
$oTextBox = new TextBox('section', 1); $oTextBox = new TextBox();
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextBox', $oTextBox); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextBox', $oTextBox);
$this->assertEquals($oTextBox->getStyle(), null); $this->assertEquals($oTextBox->getStyle(), null);
@ -43,7 +43,7 @@ class TextBoxTest extends \PHPUnit_Framework_TestCase
*/ */
public function testStyleText() public function testStyleText()
{ {
$oTextBox = new TextBox('section', 1, 'textBoxStyle'); $oTextBox = new TextBox('textBoxStyle');
$this->assertEquals($oTextBox->getStyle(), 'textBoxStyle'); $this->assertEquals($oTextBox->getStyle(), 'textBoxStyle');
} }
@ -54,8 +54,6 @@ class TextBoxTest extends \PHPUnit_Framework_TestCase
public function testStyleArray() public function testStyleArray()
{ {
$oTextBox = new TextBox( $oTextBox = new TextBox(
'section',
1,
array( array(
'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(4.5), 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(4.5),
'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(17.5), 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(17.5),