Skip to content

Commit f837381

Browse files
committed
Ability to add Endnotes
1 parent 3841867 commit f837381

25 files changed

+470
-205
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
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`.
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.
88

99
### Features
1010

@@ -28,6 +28,7 @@ This release marked heavy refactorings on internal code structure with the creat
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
3030
- Reader: Ability to read header, footer, footnotes, link, preservetext, textbreak, pagebreak, table - @ivanlanin
31+
- Endnote: Ability to add endnotes - @ivanlanin
3132

3233
### Bugfixes
3334

@@ -59,6 +60,7 @@ This release marked heavy refactorings on internal code structure with the creat
5960
- General: Give `Abstract` prefix and `Interface` suffix for all abstract classes and interfaces as per [PHP-FIG recommendation](https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md) - @ivanlanin GH-187
6061
- Style: New `Style\AbstractStyle` abstract class - @ivanlanin GH-187
6162
- Writer: New 'ODText\Base` class - @ivanlanin GH-187
63+
- General: Rename `Footnote` to `Footnotes` to reflect the nature of collection - @ivanlanin
6264

6365
## 0.9.1 - 27 Mar 2014
6466

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ With PHPWord, you can create DOCX, ODT, or RTF documents dynamically using your
2525
* Insert and format table with customized properties for each rows (e.g. repeat as header row) and cells (e.g. background color, rowspan, colspan)
2626
* Insert list items as bulleted, numbered, or multilevel
2727
* Insert hyperlinks
28+
* Insert footnotes and endnotes
2829
* Create document from templates
2930
* Use XSL 1.0 style sheets to transform main document part of OOXML template
3031
* ... and many more features on progress

docs/elements.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ the containers while the rows lists the elements.
3737
+-----+---------------+---------+--------+--------+------+----------+----------+
3838
| 14 | Footnote | v | \- | \- | v\*\*| v\*\* | \- |
3939
+-----+---------------+---------+--------+--------+------+----------+----------+
40-
| 15 | CheckBox | v | v | v | v | ? | ? |
40+
| 15 | Endnote | v | \- | \- | v\*\*| v\*\* | \- |
41+
+-----+---------------+---------+--------+--------+------+----------+----------+
42+
| 16 | CheckBox | v | v | v | v | ? | ? |
4143
+-----+---------------+---------+--------+--------+------+----------+----------+
4244

4345
Legend:
@@ -376,12 +378,13 @@ Options for ``$tocStyle``:
376378
twips.
377379
- ``indent`` The indent factor of the titles in twips.
378380

379-
Footnotes
380-
---------
381+
Footnotes & endnotes
382+
--------------------
381383

382-
You can create footnotes in texts or textruns, but it's recommended to
383-
use textrun to have better layout. You can use ``addText``, ``addLink``,
384-
and ``addTextBreak`` on a footnote.
384+
You can create footnotes with ``addFootnote`` and endnotes with ``addEndnote``
385+
in texts or textruns, but it's recommended to use textrun to have better layout.
386+
You can use ``addText``, ``addLink``, ``addTextBreak``, ``addImage``,
387+
``addObject`` on footnotes and endnotes.
385388

386389
On textrun:
387390

@@ -396,6 +399,8 @@ On textrun:
396399
$footnote->addTextBreak();
397400
$footnote->addText('And text break.');
398401
$textrun->addText('Trailing text.');
402+
$endnote = $textrun->addEndnote();
403+
$endnote->addText('Endnote put at the end');
399404
400405
On text:
401406

docs/intro.rst

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Features
4848
rowspan, colspan)
4949
- Insert list items as bulleted, numbered, or multilevel
5050
- Insert hyperlinks
51+
- Insert footnotes and endnotes
5152
- Create document from templates
5253
- Use XSL 1.0 style sheets to transform main document part of OOXML
5354
template
@@ -90,8 +91,6 @@ Writers
9091
+ +-----------------------+--------+-------+-------+
9192
| | Image || | |
9293
+ +-----------------------+--------+-------+-------+
93-
| | MemoryImage || | |
94-
+ +-----------------------+--------+-------+-------+
9594
| | Object || | |
9695
+ +-----------------------+--------+-------+-------+
9796
| | Watermark || | |
@@ -103,6 +102,8 @@ Writers
103102
| | Footer || | |
104103
+ +-----------------------+--------+-------+-------+
105104
| | Footnote || | |
105+
+ +-----------------------+--------+-------+-------+
106+
| | Endnote || | |
106107
+-------------------------+-----------------------+--------+-------+-------+
107108
| **Graphs** | 2D basic graphs | | | |
108109
+ +-----------------------+--------+-------+-------+
@@ -126,45 +127,45 @@ Readers
126127
+-------------------------------------------------+--------+-------+-------+
127128
| Features | DOCX | ODT | RTF |
128129
+=========================+=======================+========+=======+=======+
129-
| **Document Properties** | Standard | | | |
130+
| **Document Properties** | Standard | | | |
130131
+ +-----------------------+--------+-------+-------+
131-
| | Extended | | | |
132+
| | Extended | | | |
132133
+ +-----------------------+--------+-------+-------+
133-
| | UserDefined | | | |
134+
| | UserDefined | | | |
134135
+-------------------------+-----------------------+--------+-------+-------+
135136
| **Element Type** | Text || | |
136137
+ +-----------------------+--------+-------+-------+
137138
| | Text Run || | |
138139
+ +-----------------------+--------+-------+-------+
139140
| | Title | | | |
140141
+ +-----------------------+--------+-------+-------+
141-
| | Link | | | |
142+
| | Link | | | |
142143
+ +-----------------------+--------+-------+-------+
143-
| | Preserve Text | | | |
144+
| | Preserve Text | | | |
144145
+ +-----------------------+--------+-------+-------+
145146
| | Text Break || | |
146147
+ +-----------------------+--------+-------+-------+
147-
| | Page Break | | | |
148+
| | Page Break | | | |
148149
+ +-----------------------+--------+-------+-------+
149150
| | List | | | |
150151
+ +-----------------------+--------+-------+-------+
151-
| | Table | | | |
152+
| | Table | | | |
152153
+ +-----------------------+--------+-------+-------+
153154
| | Image | | | |
154155
+ +-----------------------+--------+-------+-------+
155-
| | MemoryImage | | | |
156-
+ +-----------------------+--------+-------+-------+
157156
| | Object | | | |
158157
+ +-----------------------+--------+-------+-------+
159158
| | Watermark | | | |
160159
+ +-----------------------+--------+-------+-------+
161160
| | Table of Contents | | | |
162161
+ +-----------------------+--------+-------+-------+
163-
| | Header | | | |
162+
| | Header | | | |
164163
+ +-----------------------+--------+-------+-------+
165-
| | Footer | | | |
164+
| | Footer || | |
165+
+ +-----------------------+--------+-------+-------+
166+
| | Footnote || | |
166167
+ +-----------------------+--------+-------+-------+
167-
| | Footnote | | | |
168+
| | Endnote | | | |
168169
+-------------------------+-----------------------+--------+-------+-------+
169170
| **Graphs** | 2D basic graphs | | | |
170171
+ +-----------------------+--------+-------+-------+

src/PhpWord/Element/AbstractElement.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use PhpOffice\PhpWord\Media;
1414
use PhpOffice\PhpWord\Style;
1515
use PhpOffice\PhpWord\TOC;
16-
use PhpOffice\PhpWord\Footnote as FootnoteCollection;
16+
use PhpOffice\PhpWord\Footnotes;
17+
use PhpOffice\PhpWord\Endnotes;
1718
use PhpOffice\PhpWord\Shared\String;
1819
use PhpOffice\PhpWord\Element\Element;
1920
use PhpOffice\PhpWord\Element\Text;
@@ -27,6 +28,7 @@
2728
use PhpOffice\PhpWord\Element\Image;
2829
use PhpOffice\PhpWord\Element\Object;
2930
use PhpOffice\PhpWord\Element\Footnote as FootnoteElement;
31+
use PhpOffice\PhpWord\Element\Endnote;
3032
use PhpOffice\PhpWord\Element\CheckBox;
3133

3234
/**
@@ -37,7 +39,7 @@
3739
abstract class AbstractElement
3840
{
3941
/**
40-
* Container type section|header|footer|cell|textrun|footnote
42+
* Container type section|header|footer|cell|textrun|footnote|endnote
4143
*
4244
* @var string
4345
*/
@@ -99,7 +101,7 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null)
99101
$this->checkValidity('text');
100102

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

@@ -323,7 +325,7 @@ public function addFootnote($paragraphStyle = null)
323325
$this->checkValidity('footnote');
324326

325327
$footnote = new FootnoteElement($paragraphStyle);
326-
$rId = FootnoteCollection::addFootnoteElement($footnote);
328+
$rId = Footnotes::addElement($footnote);
327329

328330
$footnote->setDocPart('footnote', $this->getDocPartId());
329331
$footnote->setRelationId($rId);
@@ -332,6 +334,26 @@ public function addFootnote($paragraphStyle = null)
332334
return $footnote;
333335
}
334336

337+
/**
338+
* Add endnote element
339+
*
340+
* @param mixed $paragraphStyle
341+
* @return Endnote
342+
*/
343+
public function addEndnote($paragraphStyle = null)
344+
{
345+
$this->checkValidity('endnote');
346+
347+
$endnote = new Endnote($paragraphStyle);
348+
$rId = Endnotes::addElement($endnote);
349+
350+
$endnote->setDocPart('endnote', $this->getDocPartId());
351+
$endnote->setRelationId($rId);
352+
$this->elements[] = $endnote;
353+
354+
return $endnote;
355+
}
356+
335357
/**
336358
* Add a CheckBox Element
337359
*
@@ -469,7 +491,7 @@ protected function setStyle($styleObject, $styleValue = null, $returnObject = fa
469491
private function checkValidity($method)
470492
{
471493
// Valid containers for each element
472-
$allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote');
494+
$allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote');
473495
$validContainers = array(
474496
'text' => $allContainers,
475497
'link' => $allContainers,
@@ -481,6 +503,7 @@ private function checkValidity($method)
481503
'checkbox' => array('section', 'header', 'footer', 'cell'),
482504
'table' => array('section', 'header', 'footer'),
483505
'footnote' => array('section', 'textrun', 'cell'),
506+
'endnote' => array('section', 'textrun', 'cell'),
484507
'preservetext' => array('header', 'footer', 'cell'),
485508
'title' => array('section'),
486509
);
@@ -489,6 +512,7 @@ private function checkValidity($method)
489512
$validContainerInContainers = array(
490513
'preservetext' => array(array('cell'), array('header', 'footer')),
491514
'footnote' => array(array('cell', 'textrun'), array('section')),
515+
'endnote' => array(array('cell', 'textrun'), array('section')),
492516
);
493517

494518
// Check if a method is valid for current container

src/PhpWord/Element/Endnote.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Element;
11+
12+
use PhpOffice\PhpWord\Style\Paragraph;
13+
14+
/**
15+
* Endnote element
16+
*
17+
* @since 0.9.2
18+
*/
19+
class Endnote extends Footnote
20+
{
21+
/**
22+
* Create new instance
23+
*
24+
* @param string|array|Paragraph $paragraphStyle
25+
*/
26+
public function __construct($paragraphStyle = null)
27+
{
28+
$this->container = 'endnote';
29+
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
30+
}
31+
}

src/PhpWord/Element/Footnote.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Footnote extends AbstractElement
2121
*
2222
* @var string|Paragraph
2323
*/
24-
private $paragraphStyle;
24+
protected $paragraphStyle;
2525

2626
/**
2727
* Create new instance

src/PhpWord/Endnotes.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord;
11+
12+
use PhpOffice\PhpWord\Media;
13+
use PhpOffice\PhpWord\Element\Endnote;
14+
15+
/**
16+
* Endnote collection
17+
*
18+
* @since 0.9.2
19+
*/
20+
class Endnotes
21+
{
22+
/**
23+
* Elements
24+
*
25+
* @var array
26+
*/
27+
private static $elements = array();
28+
29+
/**
30+
* Add new element
31+
*
32+
* @param Endnote $element
33+
* @return integer Reference ID
34+
*/
35+
public static function addElement($element)
36+
{
37+
$rId = self::countElements() + 1;
38+
self::$elements[$rId] = $element;
39+
40+
return $rId;
41+
}
42+
43+
/**
44+
* Set element
45+
*
46+
* @param integer $index
47+
* @param Endnote $element
48+
*/
49+
public static function setElement($index, $element)
50+
{
51+
if (array_key_exists($index, self::$elements)) {
52+
self::$elements[$index] = $element;
53+
}
54+
}
55+
56+
/**
57+
* Get element by index
58+
*
59+
* @param integer $index
60+
* @return Endnote
61+
*/
62+
public static function getElement($index)
63+
{
64+
if (array_key_exists($index, self::$elements)) {
65+
return self::$elements[$index];
66+
} else {
67+
return null;
68+
}
69+
}
70+
71+
/**
72+
* Get elements
73+
*
74+
* @return array
75+
*/
76+
public static function getElements()
77+
{
78+
return self::$elements;
79+
}
80+
81+
/**
82+
* Get element count
83+
*
84+
* @return integer
85+
*/
86+
public static function countElements()
87+
{
88+
return count(self::$elements);
89+
}
90+
91+
/**
92+
* Reset elements
93+
*/
94+
public static function resetElements()
95+
{
96+
self::$elements = array();
97+
}
98+
}

0 commit comments

Comments
 (0)