Skip to content

Commit

Permalink
Merge branch '#234-#235-basjan' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlanin committed May 11, 2014
2 parents 17e2f02 + 8c9e511 commit 27d18fd
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.

- 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
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228 GH-229
- TextBox: Ability to add table inside textbox - @basjan GH-231
- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231
- ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235

### Bugfixes

- Header: All images added to the second header were assigned to the first header - @basjan GH-222
- Conversion: Fix conversion from cm to pixel, pixel to cm, and pixel to point - @basjan GH-233 GH-234

### Deprecated

Expand Down
12 changes: 12 additions & 0 deletions samples/Sample_14_ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@
$section->addListItem('List Item 7', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
$section->addTextBreak(2);

$section->addText('List with inline formatting.');
$listItemRun = $section->addListItemRun();
$listItemRun->addText('List item 1');
$listItemRun->addText(' in bold', array('bold'=>true));
$listItemRun = $section->addListItemRun();
$listItemRun->addText('List item 2');
$listItemRun->addText(' in italic', array('italic'=>true));
$listItemRun = $section->addListItemRun();
$listItemRun->addText('List item 3');
$listItemRun->addText(' underlined', array('underline'=>'dash'));
$section->addTextBreak(2);

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
Expand Down
27 changes: 24 additions & 3 deletions src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null, $eleme
$elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName;

// Reset paragraph style for footnote and textrun. They have their own
if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) {
if (in_array($this->container, array('textrun', 'footnote', 'endnote', 'listitemrun'))) {
$paragraphStyle = null;
}

Expand Down Expand Up @@ -205,6 +205,26 @@ public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = n
return $element;
}

/**
* Add listitemrun element
*
* @param int $depth
* @param mixed $fontStyle
* @param mixed $listStyle
* @param mixed $paragraphStyle
* @return \PhpOffice\PhpWord\Element\ListItemRun
*/
public function addListItemRun($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
{
$this->checkValidity('ListItemRun');

$element = new ListItemRun($depth, $fontStyle, $listStyle, $paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);

return $element;
}

/**
* Add table element
*
Expand Down Expand Up @@ -345,7 +365,7 @@ public function addTextBox($style = null)
private function checkValidity($method)
{
// Valid containers for each element
$allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote', 'textbox');
$allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote', 'textbox', 'listitemrun');
$validContainers = array(
'Text' => $allContainers,
'Link' => $allContainers,
Expand All @@ -354,6 +374,7 @@ private function checkValidity($method)
'Object' => $allContainers,
'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
'ListItemRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
'Table' => array('section', 'header', 'footer', 'textbox'),
'CheckBox' => array('section', 'header', 'footer', 'cell'),
'TextBox' => array('section', 'header', 'footer', 'cell'),
Expand Down Expand Up @@ -395,7 +416,7 @@ private function checkValidity($method)
*/
private function checkElementDocPart()
{
$inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox'));
$inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox', 'listitemrun'));
$docPart = $inOtherPart ? $this->getDocPart() : $this->container;
$docPartId = $inOtherPart ? $this->getDocPartId() : $this->sectionId;
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');
Expand Down
80 changes: 80 additions & 0 deletions src/PhpWord/Element/ListItemRun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
use PhpOffice\PhpWord\Style\Paragraph;

/**
* List item element
*/
class ListItemRun extends TextRun
{
/**
* ListItem Style
*
* @var \PhpOffice\PhpWord\Style\ListItem
*/
private $style;

/**
* ListItem Depth
*
* @var int
*/
private $depth;

/**
* Create a new ListItem
*
* @param int $depth
* @param mixed $fontStyle
* @param array|string|null $listStyle
* @param mixed $paragraphStyle
*/
public function __construct($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
{
$this->container = 'listitemrun';
$this->depth = $depth;

// Version >= 0.10.0 will pass numbering style name. Older version will use old method
if (!is_null($listStyle) && is_string($listStyle)) {
$this->style = new ListItemStyle($listStyle);
} else {
$this->style = $this->setStyle(new ListItemStyle(), $listStyle, true);
}
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
}

/**
* Get ListItem style
*/
public function getStyle()
{
return $this->style;
}

/**
* Get ListItem depth
*/
public function getDepth()
{
return $this->depth;
}
}
2 changes: 1 addition & 1 deletion src/PhpWord/Element/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TextRun extends AbstractContainer
*
* @var string|\PhpOffice\PhpWord\Style\Paragraph
*/
private $paragraphStyle;
protected $paragraphStyle;

/**
* Create new instance
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Shared/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function emuToPixels($value = 0)
*/
public static function pixelsToPoints($value = 0)
{
return $value * 0.67777777;
return $value * 0.75;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/Word2007/Element/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function write()
$xmlWriter = $this->getXmlWriter();
$container = $this->getElement();
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')) ? true : false;

// Loop through subelements
$subelements = $container->getElements();
Expand Down
61 changes: 61 additions & 0 deletions src/PhpWord/Writer/Word2007/Element/ListItemRun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\Word2007\Element;

use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;

/**
* ListItemRun element writer
*
* @since 0.10.0
*/
class ListItemRun extends AbstractElement
{
/**
* Write list item element
*/
public function write()
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();

$xmlWriter->startElement('w:p');

$xmlWriter->startElement('w:pPr');
$paragraphStyle = $element->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true);
$styleWriter->write();

$xmlWriter->startElement('w:numPr');
$xmlWriter->startElement('w:ilvl');
$xmlWriter->writeAttribute('w:val', $element->getDepth());
$xmlWriter->endElement(); // w:ilvl
$xmlWriter->startElement('w:numId');
$xmlWriter->writeAttribute('w:val', $element->getStyle()->getNumId());
$xmlWriter->endElement(); // w:numId
$xmlWriter->endElement(); // w:numPr

$xmlWriter->endElement(); // w:pPr

$containerWriter = new Container($xmlWriter, $element);
$containerWriter->write();

$xmlWriter->endElement(); // w:p
}
}
Loading

0 comments on commit 27d18fd

Please sign in to comment.