Skip to content

Commit 86abe7e

Browse files
committed
GH-16 : Initial addition of basic footnote support
1 parent db52525 commit 86abe7e

File tree

12 files changed

+600
-2
lines changed

12 files changed

+600
-2
lines changed

Classes/PHPWord/Footnote.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2011 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 010 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version Beta 0.6.3, 08.07.2011
26+
*/
27+
28+
29+
/**
30+
* PHPWord_Footnote
31+
*
32+
* @category PHPWord
33+
* @package PHPWord
34+
* @copyright Copyright (c) 2011 PHPWord
35+
*/
36+
class PHPWord_Footnote {
37+
38+
/**
39+
* Footnote Elements
40+
*
41+
* @var array
42+
*/
43+
private static $_footnoteCollection = array();
44+
45+
/**
46+
* Footnote Link Elements
47+
*
48+
* @var array
49+
*/
50+
private static $_footnoteLink = array();
51+
52+
/**
53+
* Add new Footnote Element
54+
*
55+
* @param string $linkSrc
56+
* @param string $linkName
57+
*
58+
* @return mixed
59+
*/
60+
public static function addFootnoteElement(PHPWord_Section_Footnote $footnote) {
61+
$refID = self::countFootnoteElements() + 2;
62+
63+
self::$_footnoteCollection[] = $footnote;
64+
65+
return $refID;
66+
}
67+
68+
/**
69+
* Get Footnote Elements
70+
*
71+
* @return array
72+
*/
73+
public static function getFootnoteElements() {
74+
return self::$_footnoteCollection;
75+
}
76+
77+
/**
78+
* Get Footnote Elements Count
79+
*
80+
* @return int
81+
*/
82+
public static function countFootnoteElements() {
83+
return count(self::$_footnoteCollection);
84+
}
85+
86+
/**
87+
* Add new Footnote Link Element
88+
*
89+
* @param string $src
90+
* @param string $type
91+
*
92+
* @return mixed
93+
*/
94+
public static function addFootnoteLinkElement($linkSrc) {
95+
$rID = self::countFootnoteLinkElements() + 1;
96+
97+
$link = array();
98+
$link['target'] = $linkSrc;
99+
$link['rID'] = $rID;
100+
$link['type'] = 'hyperlink';
101+
102+
self::$_footnoteLink[] = $link;
103+
104+
return $rID;
105+
}
106+
107+
/**
108+
* Get Footnote Link Elements
109+
*
110+
* @return array
111+
*/
112+
public static function getFootnoteLinkElements() {
113+
return self::$_footnoteLink;
114+
}
115+
116+
/**
117+
* Get Footnote Link Elements Count
118+
*
119+
* @return int
120+
*/
121+
public static function countFootnoteLinkElements() {
122+
return count(self::$_footnoteLink);
123+
}
124+
125+
}

Classes/PHPWord/Section.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,18 @@ public function getFooter()
409409
{
410410
return $this->_footer;
411411
}
412+
413+
/**
414+
* Create a new Footnote Element
415+
*
416+
* @param string $text
417+
* @return PHPWord_Section_Footnote
418+
*/
419+
public function createFootnote($styleParagraph = null) {
420+
$footnote = new PHPWord_Section_Footnote($styleParagraph);
421+
$refID = PHPWord_Footnote::addFootnoteElement($footnote);
422+
$footnote->setReferenceId($refID);
423+
$this->_elementCollection[] = $footnote;
424+
return $footnote;
425+
}
412426
}

Classes/PHPWord/Section/Footnote.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2011 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 010 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version Beta 0.6.3, 08.07.2011
26+
*/
27+
28+
29+
/**
30+
* PHPWord_Section_Footnote
31+
*
32+
* @category PHPWord
33+
* @package PHPWord_Section
34+
* @copyright Copyright (c) 2011 PHPWord
35+
*/
36+
class PHPWord_Section_Footnote {
37+
38+
/**
39+
* Paragraph style
40+
*
41+
* @var PHPWord_Style_Font
42+
*/
43+
private $_styleParagraph;
44+
45+
/**
46+
* Footnote Reference ID
47+
*
48+
* @var string
49+
*/
50+
private $_refId;
51+
52+
/**
53+
* Text collection
54+
*
55+
* @var array
56+
*/
57+
private $_elementCollection;
58+
59+
/**
60+
* Create a new Footnote Element
61+
*/
62+
public function __construct($styleParagraph = null) {
63+
$this->_elementCollection = array();
64+
65+
// Set paragraph style
66+
if(is_array($styleParagraph)) {
67+
$this->_styleParagraph = new PHPWord_Style_Paragraph();
68+
69+
foreach($styleParagraph as $key => $value) {
70+
if(substr($key, 0, 1) != '_') {
71+
$key = '_'.$key;
72+
}
73+
$this->_styleParagraph->setStyleValue($key, $value);
74+
}
75+
} else {
76+
$this->_styleParagraph = $styleParagraph;
77+
}
78+
}
79+
80+
81+
/**
82+
* Add a Text Element
83+
*
84+
* @var string $text
85+
* @var mixed $styleFont
86+
* @return PHPWord_Section_Text
87+
*/
88+
public function addText($text = null, $styleFont = null) {
89+
$givenText = $text;
90+
$text = new PHPWord_Section_Text($givenText, $styleFont);
91+
$this->_elementCollection[] = $text;
92+
return $text;
93+
}
94+
95+
/**
96+
* Add a Link Element
97+
*
98+
* @param string $linkSrc
99+
* @param string $linkName
100+
* @param mixed $styleFont
101+
* @return PHPWord_Section_Link
102+
*/
103+
public function addLink($linkSrc, $linkName = null, $styleFont = null) {
104+
105+
$link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont);
106+
$rID = PHPWord_Footnote::addFootnoteLinkElement($linkSrc);
107+
$link->setRelationId($rID);
108+
109+
$this->_elementCollection[] = $link;
110+
return $link;
111+
}
112+
113+
/**
114+
* Get Footnote content
115+
*
116+
* @return string
117+
*/
118+
public function getElements() {
119+
return $this->_elementCollection;
120+
}
121+
122+
/**
123+
* Get Paragraph style
124+
*
125+
* @return PHPWord_Style_Paragraph
126+
*/
127+
public function getParagraphStyle() {
128+
return $this->_styleParagraph;
129+
}
130+
131+
/**
132+
* Get Footnote Reference ID
133+
*
134+
* @return int
135+
*/
136+
public function getReferenceId() {
137+
return $this->_refId;
138+
}
139+
140+
/**
141+
* Set Footnote Reference ID
142+
*
143+
* @param int $refId
144+
*/
145+
public function setReferenceId($refId) {
146+
$this->_refId = $refId;
147+
}
148+
}

Classes/PHPWord/Section/TextRun.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ public function addImage($imageSrc, $style = null) {
130130
}
131131
}
132132

133+
/**
134+
* Create a new Footnote Element
135+
*
136+
* @param string $text
137+
* @return PHPWord_Section_Footnote
138+
*/
139+
public function createFootnote($styleParagraph = null) {
140+
$footnote = new PHPWord_Section_Footnote($styleParagraph);
141+
$refID = PHPWord_Footnote::addFootnoteElement($footnote);
142+
$footnote->setReferenceId($refID);
143+
$this->_elementCollection[] = $footnote;
144+
return $footnote;
145+
}
146+
133147
/**
134148
* Get TextRun content
135149
*

Classes/PHPWord/Writer/Word2007.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function __construct(PHPWord $PHPWord = null)
5252
$this->_writerParts['styles'] = new PHPWord_Writer_Word2007_Styles();
5353
$this->_writerParts['header'] = new PHPWord_Writer_Word2007_Header();
5454
$this->_writerParts['footer'] = new PHPWord_Writer_Word2007_Footer();
55+
$this->_writerParts['footnotes'] = new PHPWord_Writer_Word2007_Footnotes();
56+
$this->_writerParts['footnotesrels'] = new PHPWord_Writer_Word2007_FootnotesRels();
5557

5658
foreach ($this->_writerParts as $writer) {
5759
$writer->setParentWriter($this);
@@ -111,6 +113,11 @@ public function save($pFilename = null)
111113
}
112114
}
113115

116+
$footnoteLinks = array();
117+
$_footnoteElements = PHPWord_Footnote::getFootnoteLinkElements();
118+
foreach($_footnoteElements as $element) { // loop through footnote link elements
119+
$footnoteLinks[] = $element;
120+
}
114121

115122
$_cHdrs = 0;
116123
$_cFtrs = 0;
@@ -138,6 +145,16 @@ public function save($pFilename = null)
138145
}
139146
}
140147

148+
if (PHPWord_Footnote::countFootnoteElements() > 0) {
149+
$_allFootnotesCollection = PHPWord_Footnote::getFootnoteElements();
150+
$_footnoteFile = 'footnotes.xml';
151+
$sectionElements[] = array('target'=>$_footnoteFile, 'type'=>'footnotes', 'rID'=>++$rID);
152+
$objZip->addFromString('word/'.$_footnoteFile, $this->getWriterPart('footnotes')->writeFootnotes($_allFootnotesCollection));
153+
if (count($footnoteLinks) > 0) {
154+
$objZip->addFromString('word/_rels/footnotes.xml.rels', $this->getWriterPart('footnotesrels')->writeFootnotesRels($footnoteLinks));
155+
}
156+
}
157+
141158
// build docx file
142159
// Write dynamic files
143160
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->_imageTypes, $this->_objectTypes, $_cHdrs, $_cFtrs));

0 commit comments

Comments
 (0)