Merge branch 'master' of https://github.com/Pyreweb/PHPWord into #189-pyreweb

This commit is contained in:
Ivan Lanin 2014-04-06 00:31:05 +07:00
commit a84171de42
5 changed files with 157 additions and 33 deletions

View File

@ -9,18 +9,34 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->createSection(); $section = $phpWord->createSection();
// Define the TOC font style // Define the TOC font style
$fontStyle = array('spaceAfter'=>60, 'size'=>12); $fontStyle = array('spaceAfter' => 60, 'size' => 12);
$fontStyle2 = array('size' => 10);
// Add title styles // Add title styles
$phpWord->addTitleStyle(1, array('size'=>20, 'color'=>'333333', 'bold'=>true)); $phpWord->addTitleStyle(1, array('size' => 20, 'color' => '333333', 'bold' => true));
$phpWord->addTitleStyle(2, array('size'=>16, 'color'=>'666666')); $phpWord->addTitleStyle(2, array('size' => 16, 'color' => '666666'));
$phpWord->addTitleStyle(3, array('size' => 14, 'italic' => true));
$phpWord->addTitleStyle(4, array('size' => 12));
// Add text elements // Add text elements
$section->addText('Table of contents:'); $section->addText('Table of contents 1');
$section->addTextBreak(2); $section->addTextBreak(2);
// Add TOC // Add TOC #1
$section->addTOC($fontStyle); $toc = $section->addTOC($fontStyle);
$section->addTextBreak(2);
// Filler
$section->addText('Text between TOC');
$section->addTextBreak(2);
// Add TOC #1
$section->addText('Table of contents 2');
$section->addTextBreak(2);
$toc2 = $section->addTOC($fontStyle2);
$toc2->setMinDepth(2);
$toc2->setMaxDepth(3);
// Add Titles // Add Titles
$section->addPageBreak(); $section->addPageBreak();
@ -41,6 +57,14 @@ $section->addText('And more text...');
$section->addTextBreak(2); $section->addTextBreak(2);
$section->addTitle('I am a Subtitle of Title 3', 2); $section->addTitle('I am a Subtitle of Title 3', 2);
$section->addText('Again and again, more text...'); $section->addText('Again and again, more text...');
$section->addTitle('Subtitle 3.1.1', 3);
$section->addText('Text');
$section->addTitle('Subtitle 3.1.1.1', 4);
$section->addText('Text');
$section->addTitle('Subtitle 3.1.1.2', 4);
$section->addText('Text');
$section->addTitle('Subtitle 3.1.2', 3);
$section->addText('Text');
echo date('H:i:s'), " Note: Please refresh TOC manually.", \EOL; echo date('H:i:s'), " Note: Please refresh TOC manually.", \EOL;
// End code // End code

View File

@ -276,11 +276,13 @@ class Section
* *
* @param mixed $styleFont * @param mixed $styleFont
* @param mixed $styleTOC * @param mixed $styleTOC
* @param int $minDepth
* @param int $maxDepth
* @return \PhpOffice\PhpWord\TOC * @return \PhpOffice\PhpWord\TOC
*/ */
public function addTOC($styleFont = null, $styleTOC = null) public function addTOC($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
{ {
$toc = new TOC($styleFont, $styleTOC); $toc = new TOC($styleFont, $styleTOC, $minDepth, $maxDepth);
$this->_elementCollection[] = $toc; $this->_elementCollection[] = $toc;
return $toc; return $toc;
} }

View File

@ -22,69 +22,87 @@ class TOC
* *
* @var array * @var array
*/ */
private static $_titles = array(); private static $titles = array();
/** /**
* TOC style * TOC style
* *
* @var TOCStyle * @var TOCStyle
*/ */
private static $_styleTOC; private static $tocStyle;
/** /**
* Font style * Font style
* *
* @var Font|array|string * @var Font|array|string
*/ */
private static $_styleFont; private static $fontStyle;
/** /**
* Title anchor * Title anchor
* *
* @var int * @var int
*/ */
private static $_anchor = 252634154; private static $anchor = 252634154;
/** /**
* Title bookmark * Title bookmark
* *
* @var int * @var int
*/ */
private static $_bookmarkId = 0; private static $bookmarkId = 0;
/**
* Min title depth to show
*
* @var int
*/
private $minDepth = 1;
/**
* Max title depth to show
*
* @var int
*/
private $maxDepth = 9;
/** /**
* Create a new Table-of-Contents Element * Create a new Table-of-Contents Element
* *
* @param mixed $styleFont * @param mixed $styleFont
* @param array $styleTOC * @param array $styleTOC
* @param int $minDepth
* @param int $maxDepth
*/ */
public function __construct($styleFont = null, $styleTOC = null) public function __construct($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
{ {
self::$_styleTOC = new TOCStyle(); self::$tocStyle = new TOCStyle();
if (!is_null($styleTOC) && is_array($styleTOC)) { if (!is_null($styleTOC) && is_array($styleTOC)) {
foreach ($styleTOC as $key => $value) { foreach ($styleTOC as $key => $value) {
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
self::$_styleTOC->setStyleValue($key, $value); self::$tocStyle->setStyleValue($key, $value);
} }
} }
if (!is_null($styleFont)) { if (!is_null($styleFont)) {
if (is_array($styleFont)) { if (is_array($styleFont)) {
self::$_styleFont = new Font(); self::$fontStyle = new Font();
foreach ($styleFont as $key => $value) { foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') { if (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
self::$_styleFont->setStyleValue($key, $value); self::$fontStyle->setStyleValue($key, $value);
} }
} else { } else {
self::$_styleFont = $styleFont; self::$fontStyle = $styleFont;
} }
} }
$this->minDepth = $minDepth;
$this->maxDepth = $maxDepth;
} }
/** /**
@ -96,8 +114,8 @@ class TOC
*/ */
public static function addTitle($text, $depth = 0) public static function addTitle($text, $depth = 0)
{ {
$anchor = '_Toc' . ++self::$_anchor; $anchor = '_Toc' . ++self::$anchor;
$bookmarkId = self::$_bookmarkId++; $bookmarkId = self::$bookmarkId++;
$title = array(); $title = array();
$title['text'] = $text; $title['text'] = $text;
@ -105,7 +123,7 @@ class TOC
$title['anchor'] = $anchor; $title['anchor'] = $anchor;
$title['bookmarkId'] = $bookmarkId; $title['bookmarkId'] = $bookmarkId;
self::$_titles[] = $title; self::$titles[] = $title;
return array($anchor, $bookmarkId); return array($anchor, $bookmarkId);
} }
@ -115,9 +133,20 @@ class TOC
* *
* @return array * @return array
*/ */
public static function getTitles() public function getTitles()
{ {
return self::$_titles; $titles = self::$titles;
foreach ($titles as $i => $title) {
if ($this->minDepth > $title['depth']) {
unset($titles[$i]);
}
if (($this->maxDepth != 0) && ($this->maxDepth < $title['depth'])) {
unset($titles[$i]);
}
}
$titles = array_merge(array(), $titles);
return $titles;
} }
/** /**
@ -127,7 +156,7 @@ class TOC
*/ */
public static function getStyleTOC() public static function getStyleTOC()
{ {
return self::$_styleTOC; return self::$tocStyle;
} }
/** /**
@ -137,6 +166,46 @@ class TOC
*/ */
public static function getStyleFont() public static function getStyleFont()
{ {
return self::$_styleFont; return self::$fontStyle;
}
/**
* Set max depth
*
* @param integer $value
*/
public function setMaxDepth($value)
{
$this->maxDepth = $value;
}
/**
* Get Max Depth
*
* @return int Max depth of titles
*/
public function getMaxDepth()
{
return $this->maxDepth;
}
/**
* Set min depth
*
* @param integer $value
*/
public function setMinDepth($value)
{
$this->minDepth = $value;
}
/**
* Get Min Depth
*
* @return int Min depth of titles
*/
public function getMinDepth()
{
return $this->minDepth;
} }
} }

View File

@ -92,7 +92,7 @@ class Document extends Base
} elseif ($element instanceof Object) { } elseif ($element instanceof Object) {
$this->writeObject($xmlWriter, $element); $this->writeObject($xmlWriter, $element);
} elseif ($element instanceof TOC) { } elseif ($element instanceof TOC) {
$this->writeTOC($xmlWriter); $this->writeTOC($xmlWriter, $element);
} elseif ($element instanceof Footnote) { } elseif ($element instanceof Footnote) {
$this->writeFootnote($xmlWriter, $element); $this->writeFootnote($xmlWriter, $element);
} elseif ($element instanceof CheckBox) { } elseif ($element instanceof CheckBox) {
@ -289,13 +289,15 @@ class Document extends Base
* Write TOC element * Write TOC element
* *
* @param XMLWriter $xmlWriter * @param XMLWriter $xmlWriter
* @param TOC $toc
*/ */
private function writeTOC(XMLWriter $xmlWriter) private function writeTOC(XMLWriter $xmlWriter, TOC $toc)
{ {
$titles = TOC::getTitles(); $titles = $toc->getTitles();
$styleFont = TOC::getStyleFont(); $styleFont = $toc->getStyleFont();
$styleTOC = $toc->getStyleTOC();
$styleTOC = TOC::getStyleTOC(); $maxDepth = $toc->getMaxDepth();
$minDepth = $toc->getMinDepth();
$fIndent = $styleTOC->getIndent(); $fIndent = $styleTOC->getIndent();
$tabLeader = $styleTOC->getTabLeader(); $tabLeader = $styleTOC->getTabLeader();
$tabPos = $styleTOC->getTabPos(); $tabPos = $styleTOC->getTabPos();
@ -351,7 +353,7 @@ class Document extends Base
$xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:instrText'); $xmlWriter->startElement('w:instrText');
$xmlWriter->writeAttribute('xml:space', 'preserve'); $xmlWriter->writeAttribute('xml:space', 'preserve');
$xmlWriter->writeRaw('TOC \o "1-9" \h \z \u'); $xmlWriter->writeRaw('TOC \o "' . $minDepth . '-' . $maxDepth . '" \h \z \u');
$xmlWriter->endElement(); $xmlWriter->endElement();
$xmlWriter->endElement(); $xmlWriter->endElement();

View File

@ -79,4 +79,31 @@ class TOCTest extends \PHPUnit_Framework_TestCase
$i++; $i++;
} }
} }
/**
* Set/get minDepth and maxDepth
*/
public function testSetGetMinMaxDepth()
{
$toc = new TOC();
$titles = array(
'Heading 1' => 1,
'Heading 2' => 2,
'Heading 3' => 3,
'Heading 4' => 4,
);
foreach ($titles as $text => $depth) {
$toc->addTitle($text, $depth);
}
$this->assertEquals(1, $toc->getMinDepth());
$this->assertEquals(9, $toc->getMaxDepth());
$toc->setMinDepth(2);
$toc->setMaxDepth(3);
$toc->getTitles();
$this->assertEquals(2, $toc->getMinDepth());
$this->assertEquals(3, $toc->getMaxDepth());
}
} }