Skip to content
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

initial addition of basic footnote support #16

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
38 changes: 38 additions & 0 deletions src/Examples/Footnote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
require_once '../PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Add style definitions
$PHPWord->addParagraphStyle('pStyle', array('spacing'=>100));
$PHPWord->addFontStyle('BoldText', array('bold'=>true));
$PHPWord->addFontStyle('ColoredText', array('color'=>'FF8080'));
$PHPWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE));

// Add text elements
$textrun = $section->createTextRun('pStyle');
$textrun->addText('This is some lead text in a paragraph with a following footnote. ','pStyle');

$footnote = $textrun->createFootnote();
$footnote->addText('Just like a textrun a footnote can contain native text and link elements.');
$footnote->addText(' No break is placed after adding an element.', 'BoldText');
$footnote->addText(' All elements are placed inside a paragraph.', 'ColoredText');
$footnote->addText(' The best search engine: ');
$footnote->addLink('http://www.google.com', null, 'NLink');
$footnote->addText('. Also not bad: ');
$footnote->addLink('http://www.bing.com', null, 'NLink');

$textrun->addText('The trailing text in the paragraph.');

$section->addText('You can also create the footnote directly from the section making it wrap in a paragraph like the footnote below this paragraph. But is is best used from within a textrun.');
$footnote = $section->createFootnote();
$footnote->addText('The reference for this is wrapped in its own line');

// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Footnote.docx');
?>
126 changes: 126 additions & 0 deletions src/PHPWord/Footnote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* PHPWord
*
* Copyright (c) 2011 PHPWord
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPWord
* @package PHPWord
* @copyright Copyright (c) 010 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version Beta 0.6.3, 08.07.2011
*/


/**
* PHPWord_Footnotes
*
* @category PHPWord
* @package PHPWord
* @copyright Copyright (c) 2011 PHPWord
*/
class PHPWord_Footnote {

/**
* Footnote Elements
*
* @var array
*/
private static $_footnoteCollection = array();

/**
* Footnote Link Elements
*
* @var array
*/
private static $_footnoteLink = array();

/**
* Add new Footnote Element
*
* @param string $linkSrc
* @param string $linkName
*
* @return mixed
*/
public static function addFootnoteElement(PHPWord_Section_Footnote $footnote) {
$refID = self::countFootnoteElements() + 2;

self::$_footnoteCollection[] = $footnote;

return $refID;
}

/**
* Get Footnote Elements
*
* @return array
*/
public static function getFootnoteElements() {
return self::$_footnoteCollection;
}

/**
* Get Footnote Elements Count
*
* @return int
*/
public static function countFootnoteElements() {
return count(self::$_footnoteCollection);
}

/**
* Add new Footnote Link Element
*
* @param string $src
* @param string $type
*
* @return mixed
*/
public static function addFootnoteLinkElement($linkSrc) {
$rID = self::countFootnoteLinkElements() + 1;

$link = array();
$link['target'] = $linkSrc;
$link['rID'] = $rID;
$link['type'] = 'hyperlink';

self::$_footnoteLink[] = $link;

return $rID;
}

/**
* Get Footnote Link Elements
*
* @return array
*/
public static function getFootnoteLinkElements() {
return self::$_footnoteLink;
}

/**
* Get Footnote Link Elements Count
*
* @return int
*/
public static function countFootnoteLinkElements() {
return count(self::$_footnoteLink);
}

}
?>
17 changes: 15 additions & 2 deletions src/PHPWord/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ class PHPWord_Section {
* @var PHPWord_Section_Footer
*/
private $_footer = null;



/**
* Create a new Section
*
Expand Down Expand Up @@ -367,5 +366,19 @@ public function createFooter() {
public function getFooter() {
return $this->_footer;
}

/**
* Create a new Footnote Element
*
* @param string $text
* @return PHPWord_Section_Footnote
*/
public function createFootnote($styleParagraph = null) {
$footnote = new PHPWord_Section_Footnote($styleParagraph);
$refID = PHPWord_Footnote::addFootnoteElement($footnote);
$footnote->setReferenceId($refID);
$this->_elementCollection[] = $footnote;
return $footnote;
}
}
?>
151 changes: 151 additions & 0 deletions src/PHPWord/Section/Footnote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* PHPWord
*
* Copyright (c) 2011 PHPWord
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPWord
* @package PHPWord
* @copyright Copyright (c) 010 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version Beta 0.6.3, 08.07.2011
*/


/**
* PHPWord_Section_Footnote
*
* @category PHPWord
* @package PHPWord_Section
* @copyright Copyright (c) 2011 PHPWord
*/
class PHPWord_Section_Footnote {

/**
* Paragraph style
*
* @var PHPWord_Style_Font
*/
private $_styleParagraph;

/**
* Footnote Reference ID
*
* @var string
*/
private $_refId;

/**
* Text collection
*
* @var array
*/
private $_elementCollection;

/**
* Create a new Footnote Element
*/
public function __construct($styleParagraph = null) {
$this->_elementCollection = array();

// Set paragraph style
if(is_array($styleParagraph)) {
$this->_styleParagraph = new PHPWord_Style_Paragraph();

foreach($styleParagraph as $key => $value) {
if(substr($key, 0, 1) != '_') {
$key = '_'.$key;
}
$this->_styleParagraph->setStyleValue($key, $value);
}
} else {
$this->_styleParagraph = $styleParagraph;
}
}


/**
* Add a Text Element
*
* @var string $text
* @var mixed $styleFont
* @return PHPWord_Section_Text
*/
public function addText($text = null, $styleFont = null) {
$givenText = $text;
$text = new PHPWord_Section_Text($givenText, $styleFont);
$this->_elementCollection[] = $text;
return $text;
}

/**
* Add a Link Element
*
* @param string $linkSrc
* @param string $linkName
* @param mixed $styleFont
* @return PHPWord_Section_Link
*/
public function addLink($linkSrc, $linkName = null, $styleFont = null) {

$link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont);
$rID = PHPWord_Footnote::addFootnoteLinkElement($linkSrc);
$link->setRelationId($rID);

$this->_elementCollection[] = $link;
return $link;
}

/**
* Get Footnote content
*
* @return string
*/
public function getElements() {
return $this->_elementCollection;
}

/**
* Get Paragraph style
*
* @return PHPWord_Style_Paragraph
*/
public function getParagraphStyle() {
return $this->_styleParagraph;
}

/**
* Get Footnote Reference ID
*
* @return int
*/
public function getReferenceId() {
return $this->_refId;
}

/**
* Set Footnote Reference ID
*
* @param int $refId
*/
public function setReferenceId($refId) {
$this->_refId = $refId;
}


}
?>
14 changes: 14 additions & 0 deletions src/PHPWord/Section/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public function addLink($linkSrc, $linkName = null, $styleFont = null) {
return $link;
}

/**
* Create a new Footnote Element
*
* @param string $text
* @return PHPWord_Section_Footnote
*/
public function createFootnote($styleParagraph = null) {
$footnote = new PHPWord_Section_Footnote($styleParagraph);
$refID = PHPWord_Footnote::addFootnoteElement($footnote);
$footnote->setReferenceId($refID);
$this->_elementCollection[] = $footnote;
return $footnote;
}

/**
* Get TextRun content
*
Expand Down
Loading