TOC Depth filter function added

Add options to TOC to only show selected depth of titles ; ie pass 1,3
arguments to only show titles depth 1 to titles depth 3
Plus now you can have two+ TOC on your document, each different
This commit is contained in:
Louis 2014-03-28 10:46:46 +01:00
parent c6fc1d4e89
commit 88be3c962e
3 changed files with 62 additions and 11 deletions

View File

@ -304,11 +304,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

@ -67,6 +67,19 @@ class TOC
*/
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
@ -74,7 +87,7 @@ class TOC
* @param array $styleFont
* @param array $styleTOC
*/
public function __construct($styleFont = null, $styleTOC = null)
public function __construct($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
{
self::$_styleTOC = new \PhpOffice\PhpWord\Style\TOC();
@ -101,6 +114,9 @@ class TOC
self::$_styleFont = $styleFont;
}
}
$this->_minDepth = $minDepth;
$this->_maxDepth = $maxDepth;
}
/**
@ -131,9 +147,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;
}
/**
@ -155,4 +182,22 @@ class TOC
{
return self::$_styleFont;
}
/**
* Get Max Depth
*
* @return int Max depth of titles
*/
public function getMaxDepth() {
return $this->_maxDepth;
}
/**
* Get Min Depth
*
* @return int Min depth of titles
*/
public function getMinDepth() {
return $this->_minDepth;
}
}

View File

@ -112,7 +112,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->_writeFootnoteReference($xmlWriter, $element);
}
@ -417,16 +417,20 @@ class Document extends Base
* Write TOC element
*
* @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param PhpOffice\PhpWord\TOC $toc
*/
private function _writeTOC(XMLWriter $xmlWriter)
private function _writeTOC(XMLWriter $xmlWriter, TOC $toc)
{
$titles = TOC::getTitles();
$styleFont = TOC::getStyleFont();
$titles = $toc->getTitles();
$styleFont = $toc->getStyleFont();
$styleTOC = TOC::getStyleTOC();
$styleTOC = $toc->getStyleTOC();
$fIndent = $styleTOC->getIndent();
$tabLeader = $styleTOC->getTabLeader();
$tabPos = $styleTOC->getTabPos();
$maxDepth = $toc->getMaxDepth();
$minDepth = $toc->getMinDepth();
$isObject = ($styleFont instanceof Font) ? true : false;
@ -479,7 +483,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();