Skip to content

Added ListItemRun element #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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