Skip to content

Ability to add custom numbering style #198

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 4 commits into from
Apr 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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers

## 0.9.2 - Not yet released

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.
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.

### Features

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

### Bugfixes

Expand Down
38 changes: 38 additions & 0 deletions docs/elements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,14 @@ Lists

To add a list item use the function ``addListItem``.

Basic usage:

.. code-block:: php

$section->addListItem($text, [$depth], [$fontStyle], [$listStyle], [$paragraphStyle]);

Parameters:

- ``$text`` Text that appears in the document.
- ``$depth`` Depth of list item.
- ``$fontStyle`` See "Font style" section.
Expand All @@ -219,6 +223,40 @@ To add a list item use the function ``addListItem``.
PHPWord\_Style\_ListItem.
- ``$paragraphStyle`` See "Paragraph style" section.

Advanced usage:

You can also create your own numbering style by changing the ``$listStyle`` parameter
with the name of your numbering style.

.. code-block:: php

$phpWord->addNumberingStyle(
'multilevel',
array('type' => 'multilevel', 'levels' => array(
array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360),
array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720),
)
)
);
$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');

Level styles:

- ``start`` Starting value
- ``format`` Numbering format bullet|decimal|upperRoman|lowerRoman|upperLetter|lowerLetter
- ``restart`` Restart numbering level symbol
- ``suffix`` Content between numbering symbol and paragraph text tab|space|nothing
- ``text`` Numbering level text e.g. %1 for nonbullet or bullet character
- ``align`` Numbering symbol align left|center|right|both
- ``left`` See paragraph style
- ``hanging`` See paragraph style
- ``tabPos`` See paragraph style
- ``font`` Font name
- ``hint`` See font style

Tables
------

Expand Down
4 changes: 2 additions & 2 deletions docs/general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ XML Writer compatibility
~~~~~~~~~~~~~~~~~~~~~~~~

This option sets
```XMLWriter::setIndent`` <http://www.php.net/manual/en/function.xmlwriter-set-indent.php>`__
`XMLWriter::setIndent <http://www.php.net/manual/en/function.xmlwriter-set-indent.php>`__
and
```XMLWriter::setIndentString`` <http://www.php.net/manual/en/function.xmlwriter-set-indent-string.php>`__.
`XMLWriter::setIndentString <http://www.php.net/manual/en/function.xmlwriter-set-indent-string.php>`__.
The default value of this option is ``true`` (compatible), which is
`required for OpenOffice <https://github.com/PHPOffice/PHPWord/issues/103>`__ to
render OOXML document correctly. You can set this option to ``false``
Expand Down
69 changes: 40 additions & 29 deletions samples/Sample_14_ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,52 @@
// Begin code
$section = $phpWord->addSection();

// Add listitem elements
$section->addListItem('List Item 1', 0);
$section->addListItem('List Item 2', 0);
$section->addListItem('List Item 3', 0);
// Style definition

$phpWord->addFontStyle('myOwnStyle', array('color'=>'FF0000'));
$phpWord->addParagraphStyle('P-Style', array('spaceAfter'=>95));
$phpWord->addNumberingStyle(
'multilevel',
array('type' => 'multilevel', 'levels' => array(
array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360),
array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720),
)
)
);
$predefinedMultilevel = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER_NESTED);

// 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->addTextBreak(2);

// Add listitem elements
$section->addListItem('List Item 1', 0);
$section->addListItem('List Item 1.1', 1);
$section->addListItem('List Item 1.2', 1);
$section->addListItem('List Item 1.3 (styled)', 1, array('bold'=>true));
$section->addListItem('List Item 1.3.1', 2);
$section->addListItem('List Item 1.3.2', 2);
$section->addText('Basic simple bulleted list.');
$section->addListItem('List Item 1');
$section->addListItem('List Item 2');
$section->addListItem('List Item 3');
$section->addTextBreak(2);

// Add listitem elements
$listStyle = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER);
$section->addListItem('List Item 1', 0, null, $listStyle);
$section->addListItem('List Item 2', 0, null, $listStyle);
$section->addListItem('List Item 3', 0, null, $listStyle);
$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->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->addTextBreak(2);

// Add listitem elements
$phpWord->addFontStyle('myOwnStyle', array('color'=>'FF0000'));
$phpWord->addParagraphStyle('P-Style', array('spaceAfter'=>95));
$listStyle = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER_NESTED);
$section->addListItem('List Item 1', 0, 'myOwnStyle', $listStyle, 'P-Style');
$section->addListItem('List Item 2', 0, 'myOwnStyle', $listStyle, 'P-Style');
$section->addListItem('List Item 3', 1, 'myOwnStyle', $listStyle, 'P-Style');
$section->addListItem('List Item 4', 1, 'myOwnStyle', $listStyle, 'P-Style');
$section->addListItem('List Item 5', 2, 'myOwnStyle', $listStyle, 'P-Style');
$section->addListItem('List Item 6', 1, 'myOwnStyle', $listStyle, 'P-Style');
$section->addListItem('List Item 7', 0, 'myOwnStyle', $listStyle, 'P-Style');

// End code

// Save file
$name = basename(__FILE__, '.php');
Expand Down
3 changes: 0 additions & 3 deletions src/PhpWord/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,6 @@ protected function setStyle($styleObject, $styleValue = null, $returnObject = fa
{
if (!is_null($styleValue) && is_array($styleValue)) {
foreach ($styleValue as $key => $value) {
if (substr($key, 0, 1) == '_') {
$key = substr($key, 1);
}
$styleObject->setStyleValue($key, $value);
}
$style = $styleObject;
Expand Down
18 changes: 12 additions & 6 deletions src/PhpWord/Element/ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ class ListItem extends AbstractElement
*
* @param string $text
* @param int $depth
* @param mixed $styleFont
* @param mixed $styleList
* @param mixed $styleParagraph
* @param mixed $fontStyle
* @param array|string|null $listStyle
* @param mixed $paragraphStyle
*/
public function __construct($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null)
public function __construct($text, $depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
{
$this->textObject = new Text($text, $styleFont, $styleParagraph);
$this->textObject = new Text($text, $fontStyle, $paragraphStyle);
$this->depth = $depth;
$this->style = $this->setStyle(new ListItemStyle(), $styleList, true);

// Version >= 0.9.2 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);
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/PhpWord/Endnotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace PhpOffice\PhpWord;

use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Element\Endnote;

/**
Expand Down
11 changes: 11 additions & 0 deletions src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ public function addLinkStyle($styleName, $styles)
Style::addLinkStyle($styleName, $styles);
}

/**
* Adds a numbering style
*
* @param string $styleName
* @param mixed $styles
*/
public function addNumberingStyle($styleName, $styles)
{
Style::addNumberingStyle($styleName, $styles);
}

/**
* Get all sections
*
Expand Down
Loading