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

@ -10,17 +10,33 @@ $section = $phpWord->createSection();
// Define the TOC font style
$fontStyle = array('spaceAfter' => 60, 'size' => 12);
$fontStyle2 = array('size' => 10);
// Add title styles
$phpWord->addTitleStyle(1, array('size' => 20, 'color' => '333333', 'bold' => true));
$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
$section->addText('Table of contents:');
$section->addText('Table of contents 1');
$section->addTextBreak(2);
// Add TOC
$section->addTOC($fontStyle);
// Add TOC #1
$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
$section->addPageBreak();
@ -41,6 +57,14 @@ $section->addText('And more text...');
$section->addTextBreak(2);
$section->addTitle('I am a Subtitle of Title 3', 2);
$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;
// End code

View File

@ -276,11 +276,13 @@ class Section
*
* @param mixed $styleFont
* @param mixed $styleTOC
* @param int $minDepth
* @param int $maxDepth
* @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;
return $toc;
}

View File

@ -22,69 +22,87 @@ class TOC
*
* @var array
*/
private static $_titles = array();
private static $titles = array();
/**
* TOC style
*
* @var TOCStyle
*/
private static $_styleTOC;
private static $tocStyle;
/**
* Font style
*
* @var Font|array|string
*/
private static $_styleFont;
private static $fontStyle;
/**
* Title anchor
*
* @var int
*/
private static $_anchor = 252634154;
private static $anchor = 252634154;
/**
* Title bookmark
*
* @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
*
* @param mixed $styleFont
* @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)) {
foreach ($styleTOC as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
self::$_styleTOC->setStyleValue($key, $value);
self::$tocStyle->setStyleValue($key, $value);
}
}
if (!is_null($styleFont)) {
if (is_array($styleFont)) {
self::$_styleFont = new Font();
self::$fontStyle = new Font();
foreach ($styleFont as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
self::$_styleFont->setStyleValue($key, $value);
self::$fontStyle->setStyleValue($key, $value);
}
} 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)
{
$anchor = '_Toc' . ++self::$_anchor;
$bookmarkId = self::$_bookmarkId++;
$anchor = '_Toc' . ++self::$anchor;
$bookmarkId = self::$bookmarkId++;
$title = array();
$title['text'] = $text;
@ -105,7 +123,7 @@ class TOC
$title['anchor'] = $anchor;
$title['bookmarkId'] = $bookmarkId;
self::$_titles[] = $title;
self::$titles[] = $title;
return array($anchor, $bookmarkId);
}
@ -115,9 +133,20 @@ class TOC
*
* @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()
{
return self::$_styleTOC;
return self::$tocStyle;
}
/**
@ -137,6 +166,46 @@ class TOC
*/
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) {
$this->writeObject($xmlWriter, $element);
} elseif ($element instanceof TOC) {
$this->writeTOC($xmlWriter);
$this->writeTOC($xmlWriter, $element);
} elseif ($element instanceof Footnote) {
$this->writeFootnote($xmlWriter, $element);
} elseif ($element instanceof CheckBox) {
@ -289,13 +289,15 @@ class Document extends Base
* Write TOC element
*
* @param XMLWriter $xmlWriter
* @param TOC $toc
*/
private function writeTOC(XMLWriter $xmlWriter)
private function writeTOC(XMLWriter $xmlWriter, TOC $toc)
{
$titles = TOC::getTitles();
$styleFont = TOC::getStyleFont();
$styleTOC = TOC::getStyleTOC();
$titles = $toc->getTitles();
$styleFont = $toc->getStyleFont();
$styleTOC = $toc->getStyleTOC();
$maxDepth = $toc->getMaxDepth();
$minDepth = $toc->getMinDepth();
$fIndent = $styleTOC->getIndent();
$tabLeader = $styleTOC->getTabLeader();
$tabPos = $styleTOC->getTabPos();
@ -351,7 +353,7 @@ class Document extends Base
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:instrText');
$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();

View File

@ -79,4 +79,31 @@ class TOCTest extends \PHPUnit_Framework_TestCase
$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());
}
}