Skip to content

List item change to depend on TextRun #232

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

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 26 additions & 18 deletions samples/Sample_14_ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,43 @@
// Lists

$section->addText('Multilevel list.');
$section->addListItem('List Item I', 0, null, 'multilevel');
$section->addListItem('List Item I.a', 1, null, 'multilevel');
$section->addListItem('List Item I.b', 1, null, 'multilevel');
$section->addListItem('List Item II', 0, null, 'multilevel');
$section->addListItem('List Item II.a', 1, null, 'multilevel');
$section->addListItem('List Item III', 0, null, 'multilevel');
$section->addListItem(0, null, 'multilevel')->addText('List Item I');
$section->addListItem(1, null, 'multilevel')->addText('List Item I.a');
$section->addListItem(1, null, 'multilevel')->addText('List Item I.b');
$section->addListItem(0, null, 'multilevel')->addText('List Item II');
$section->addListItem(1, null, 'multilevel')->addText('List Item II.a');
$section->addListItem(0, null, 'multilevel')->addText('List Item III');
$section->addTextBreak(2);

$section->addText('Basic simple bulleted list.');
$section->addListItem('List Item 1');
$section->addListItem('List Item 2');
$section->addListItem('List Item 3');
$section->addListItem()->addText('List Item 1');
$section->addListItem()->addText('List Item 2');
$section->addListItem()->addText('List Item 3');
$section->addTextBreak(2);

$section->addText('Continue from multilevel list above.');
$section->addListItem('List Item IV', 0, null, 'multilevel');
$section->addListItem('List Item IV.a', 1, null, 'multilevel');
$section->addListItem(0, null, 'multilevel')->addText('List Item IV');
$section->addListItem(1, null, 'multilevel')->addText('List Item IV.a');
$section->addTextBreak(2);

$section->addText('Multilevel predefined list.');
$section->addListItem('List Item 1', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
$section->addListItem('List Item 2', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
$section->addListItem('List Item 3', 1, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
$section->addListItem('List Item 4', 1, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
$section->addListItem('List Item 5', 2, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
$section->addListItem('List Item 6', 1, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
$section->addListItem('List Item 7', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
$section->addListItem(0, 'myOwnStyle', $predefinedMultilevel, 'P-Style')->addText('List Item 1');
$section->addListItem(0, 'myOwnStyle', $predefinedMultilevel, 'P-Style')->addText('List Item 2');
$section->addListItem(1, 'myOwnStyle', $predefinedMultilevel, 'P-Style')->addText('List Item 3');
$section->addListItem(1, 'myOwnStyle', $predefinedMultilevel, 'P-Style')->addText('List Item 4');
$section->addListItem(2, 'myOwnStyle', $predefinedMultilevel, 'P-Style')->addText('List Item 5');
$section->addListItem(1, 'myOwnStyle', $predefinedMultilevel, 'P-Style')->addText('List Item 6');
$section->addListItem(0, 'myOwnStyle', $predefinedMultilevel, 'P-Style')->addText('List Item 7');
$section->addTextBreak(2);

$section->addText('Listitems with inline formatting');
$listItemObject = $section->addListItem();
$listItemObject->addText('Testing');
$listItemObject->addText(' Strong', array('bold'=>true));
$listItemObject = $section->addListItem();
$listItemObject->addText('Testing 2');
$listItemObject->addText(' Italic and stron', array('bold'=>true, 'italic'=>true));

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
Expand Down
64 changes: 19 additions & 45 deletions src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,6 @@ public function countElements()
return count($this->elements);
}

/**
* Add generic element with style
*
* This is how all elements should be added with dependency injection: with
* just one simple $style. Currently this function supports TextRun, Table,
* and TextBox since all other elements have different arguments
*
* @todo Change the function name into something better?
*
* @param string $elementName
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\AbstractElement
*/
private function addGenericElement($elementName, $style)
{
$elementClass = __NAMESPACE__ . '\\' . $elementName;

$this->checkValidity($elementName);
$element = new $elementClass($style);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);

return $element;
}

/**
* Add text/preservetext element
*
Expand All @@ -106,7 +81,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', 'listitem'))) {
$paragraphStyle = null;
}

Expand All @@ -125,7 +100,13 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null, $eleme
*/
public function addTextRun($paragraphStyle = null)
{
return $this->addGenericElement('TextRun', $paragraphStyle);
$this->checkValidity('TextRun');

$element = new TextRun($paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);

return $element;
}

/**
Expand Down Expand Up @@ -194,29 +175,17 @@ public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = nu
* @param mixed $paragraphStyle
* @return \PhpOffice\PhpWord\Element\ListItem
*/
public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
public function addListItem($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
{
$this->checkValidity('ListItem');

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

return $element;
}

/**
* Add table element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
* @todo Merge with the same function on Footer
*/
public function addTable($style = null)
{
return $this->addGenericElement('Table', $style);
}

/**
* Add image element
*
Expand Down Expand Up @@ -333,7 +302,13 @@ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = n
*/
public function addTextBox($style = null)
{
return $this->addGenericElement('TextBox', $style);
$this->checkValidity('TextBox');

$textbox = new TextBox($style);
$textbox->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($textbox);

return $textbox;
}

/**
Expand All @@ -345,7 +320,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', 'listitem');
$validContainers = array(
'Text' => $allContainers,
'Link' => $allContainers,
Expand All @@ -354,7 +329,6 @@ private function checkValidity($method)
'Object' => $allContainers,
'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
'Table' => array('section', 'header', 'footer', 'textbox'),
'CheckBox' => array('section', 'header', 'footer', 'cell'),
'TextBox' => array('section', 'header', 'footer', 'cell'),
'Footnote' => array('section', 'textrun', 'cell'),
Expand Down Expand Up @@ -395,7 +369,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', 'listitem'));
$docPart = $inOtherPart ? $this->getDocPart() : $this->container;
$docPartId = $inOtherPart ? $this->getDocPartId() : $this->sectionId;
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');
Expand Down
17 changes: 5 additions & 12 deletions src/PhpWord/Element/ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

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

/**
* List item element
*/
class ListItem extends AbstractElement
class ListItem extends TextRun
{
/**
* ListItem Style
Expand All @@ -32,33 +33,24 @@ class ListItem extends AbstractElement
*/
private $style;

/**
* Textrun
*
* @var Text
*/
private $textObject;

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


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

// Version >= 0.10.0 will pass numbering style name. Older version will use old method
Expand All @@ -67,6 +59,7 @@ public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = n
} else {
$this->style = $this->setStyle(new ListItemStyle(), $listStyle, true);
}
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
}

/**
Expand Down
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', 'ListItem')) ? true : false;

// Loop through subelements
$subelements = $container->getElements();
Expand Down
13 changes: 5 additions & 8 deletions src/PhpWord/Writer/Word2007/Element/ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ public function write()
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();

$textObject = $element->getTextObject();

$styleWriter = new ParagraphStyleWriter($xmlWriter, $textObject->getParagraphStyle());
$styleWriter->setWithoutPPR(true);
$styleWriter->setIsInline(true);

$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');
Expand All @@ -56,8 +53,8 @@ public function write()

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

$elementWriter = new Text($xmlWriter, $textObject, true);
$elementWriter->write();
$containerWriter = new Container($xmlWriter, $element);
$containerWriter->write();

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