Skip to content

Commit 47669f5

Browse files
committed
Ability to create custom list PHPOffice#10 and to read list definition from DOCX
1 parent f837381 commit 47669f5

File tree

21 files changed

+1273
-135
lines changed

21 files changed

+1273
-135
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
44

55
## 0.9.2 - Not yet released
66

7-
This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. Word2007 reader capability is greatly enhanced. Endnote is introduced.
7+
This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`. Word2007 reader capability is greatly enhanced. Endnote is introduced. List numbering is now customizable.
88

99
### Features
1010

@@ -27,8 +27,9 @@ This release marked heavy refactorings on internal code structure with the creat
2727
- Object: Ability to add object in header, footer, textrun, and footnote - @ivanlanin GH-187
2828
- Media: Add `Media::resetElements()` to reset all media data - @juzi GH-19
2929
- General: Add `Style::resetStyles()`, `Footnote::resetElements()`, and `TOC::resetTitles()` - @ivanlanin GH-187
30-
- Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table - @ivanlanin
30+
- Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table, and list - @ivanlanin
3131
- Endnote: Ability to add endnotes - @ivanlanin
32+
- ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10
3233

3334
### Bugfixes
3435

samples/Sample_14_ListItem.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,52 @@
88
// Begin code
99
$section = $phpWord->addSection();
1010

11-
// Add listitem elements
12-
$section->addListItem('List Item 1', 0);
13-
$section->addListItem('List Item 2', 0);
14-
$section->addListItem('List Item 3', 0);
11+
// Style definition
12+
13+
$phpWord->addFontStyle('myOwnStyle', array('color'=>'FF0000'));
14+
$phpWord->addParagraphStyle('P-Style', array('spaceAfter'=>95));
15+
$phpWord->addNumberingStyle(
16+
'multilevel',
17+
array('type' => 'multilevel', 'levels' => array(
18+
array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360),
19+
array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720),
20+
)
21+
)
22+
);
23+
$predefinedMultilevel = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER_NESTED);
24+
25+
// Lists
26+
27+
$section->addText('Multilevel list.');
28+
$section->addListItem('List Item I', 0, null, 'multilevel');
29+
$section->addListItem('List Item I.a', 1, null, 'multilevel');
30+
$section->addListItem('List Item I.b', 1, null, 'multilevel');
31+
$section->addListItem('List Item II', 0, null, 'multilevel');
32+
$section->addListItem('List Item II.a', 1, null, 'multilevel');
33+
$section->addListItem('List Item III', 0, null, 'multilevel');
1534
$section->addTextBreak(2);
1635

17-
// Add listitem elements
18-
$section->addListItem('List Item 1', 0);
19-
$section->addListItem('List Item 1.1', 1);
20-
$section->addListItem('List Item 1.2', 1);
21-
$section->addListItem('List Item 1.3 (styled)', 1, array('bold'=>true));
22-
$section->addListItem('List Item 1.3.1', 2);
23-
$section->addListItem('List Item 1.3.2', 2);
36+
$section->addText('Basic simple bulleted list.');
37+
$section->addListItem('List Item 1');
38+
$section->addListItem('List Item 2');
39+
$section->addListItem('List Item 3');
2440
$section->addTextBreak(2);
2541

26-
// Add listitem elements
27-
$listStyle = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER);
28-
$section->addListItem('List Item 1', 0, null, $listStyle);
29-
$section->addListItem('List Item 2', 0, null, $listStyle);
30-
$section->addListItem('List Item 3', 0, null, $listStyle);
42+
$section->addText('Continue from multilevel list above.');
43+
$section->addListItem('List Item IV', 0, null, 'multilevel');
44+
$section->addListItem('List Item IV.a', 1, null, 'multilevel');
45+
$section->addTextBreak(2);
46+
47+
$section->addText('Multilevel predefined list.');
48+
$section->addListItem('List Item 1', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
49+
$section->addListItem('List Item 2', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
50+
$section->addListItem('List Item 3', 1, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
51+
$section->addListItem('List Item 4', 1, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
52+
$section->addListItem('List Item 5', 2, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
53+
$section->addListItem('List Item 6', 1, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
54+
$section->addListItem('List Item 7', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
3155
$section->addTextBreak(2);
3256

33-
// Add listitem elements
34-
$phpWord->addFontStyle('myOwnStyle', array('color'=>'FF0000'));
35-
$phpWord->addParagraphStyle('P-Style', array('spaceAfter'=>95));
36-
$listStyle = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER_NESTED);
37-
$section->addListItem('List Item 1', 0, 'myOwnStyle', $listStyle, 'P-Style');
38-
$section->addListItem('List Item 2', 0, 'myOwnStyle', $listStyle, 'P-Style');
39-
$section->addListItem('List Item 3', 1, 'myOwnStyle', $listStyle, 'P-Style');
40-
$section->addListItem('List Item 4', 1, 'myOwnStyle', $listStyle, 'P-Style');
41-
$section->addListItem('List Item 5', 2, 'myOwnStyle', $listStyle, 'P-Style');
42-
$section->addListItem('List Item 6', 1, 'myOwnStyle', $listStyle, 'P-Style');
43-
$section->addListItem('List Item 7', 0, 'myOwnStyle', $listStyle, 'P-Style');
44-
45-
// End code
4657

4758
// Save file
4859
$name = basename(__FILE__, '.php');

src/PhpWord/Element/AbstractElement.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,6 @@ protected function setStyle($styleObject, $styleValue = null, $returnObject = fa
469469
{
470470
if (!is_null($styleValue) && is_array($styleValue)) {
471471
foreach ($styleValue as $key => $value) {
472-
if (substr($key, 0, 1) == '_') {
473-
$key = substr($key, 1);
474-
}
475472
$styleObject->setStyleValue($key, $value);
476473
}
477474
$style = $styleObject;

src/PhpWord/Element/ListItem.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,21 @@ class ListItem extends AbstractElement
4444
*
4545
* @param string $text
4646
* @param int $depth
47-
* @param mixed $styleFont
48-
* @param mixed $styleList
49-
* @param mixed $styleParagraph
47+
* @param mixed $fontStyle
48+
* @param array|string|null $listStyle
49+
* @param mixed $paragraphStyle
5050
*/
51-
public function __construct($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null)
51+
public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
5252
{
53-
$this->textObject = new Text($text, $styleFont, $styleParagraph);
53+
$this->textObject = new Text($text, $fontStyle, $paragraphStyle);
5454
$this->depth = $depth;
55-
$this->style = $this->setStyle(new ListItemStyle(), $styleList, true);
55+
56+
// Version >= 0.9.2 will pass numbering style name. Older version will use old method
57+
if (!is_null($listStyle) && is_string($listStyle)) {
58+
$this->style = new ListItemStyle($listStyle);
59+
} else {
60+
$this->style = $this->setStyle(new ListItemStyle(), $listStyle, true);
61+
}
5662
}
5763

5864
/**

src/PhpWord/Endnotes.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace PhpOffice\PhpWord;
1111

12-
use PhpOffice\PhpWord\Media;
1312
use PhpOffice\PhpWord\Element\Endnote;
1413

1514
/**

src/PhpWord/PhpWord.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ public function addLinkStyle($styleName, $styles)
214214
Style::addLinkStyle($styleName, $styles);
215215
}
216216

217+
/**
218+
* Adds a numbering style
219+
*
220+
* @param string $styleName
221+
* @param mixed $styles
222+
*/
223+
public function addNumberingStyle($styleName, $styles)
224+
{
225+
Style::addNumberingStyle($styleName, $styles);
226+
}
227+
217228
/**
218229
* Get all sections
219230
*

0 commit comments

Comments
 (0)