Skip to content

Commit 88be3c9

Browse files
author
Louis
committed
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
1 parent c6fc1d4 commit 88be3c9

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/PhpWord/Section.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,13 @@ public function addMemoryImage($src, $style = null)
304304
*
305305
* @param mixed $styleFont
306306
* @param mixed $styleTOC
307+
* @param int $minDepth
308+
* @param int $maxDepth
307309
* @return \PhpOffice\PhpWord\TOC
308310
*/
309-
public function addTOC($styleFont = null, $styleTOC = null)
311+
public function addTOC($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
310312
{
311-
$toc = new TOC($styleFont, $styleTOC);
313+
$toc = new TOC($styleFont, $styleTOC, $minDepth, $maxDepth);
312314
$this->_elementCollection[] = $toc;
313315
return $toc;
314316
}

src/PhpWord/TOC.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,27 @@ class TOC
6767
*/
6868
private static $_bookmarkId = 0;
6969

70+
/**
71+
* Min title depth to show
72+
*
73+
* @var int
74+
*/
75+
private $_minDepth = 1;
76+
77+
/**
78+
* Max title depth to show
79+
*
80+
* @var int
81+
*/
82+
private $_maxDepth = 9;
7083

7184
/**
7285
* Create a new Table-of-Contents Element
7386
*
7487
* @param array $styleFont
7588
* @param array $styleTOC
7689
*/
77-
public function __construct($styleFont = null, $styleTOC = null)
90+
public function __construct($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
7891
{
7992
self::$_styleTOC = new \PhpOffice\PhpWord\Style\TOC();
8093

@@ -101,6 +114,9 @@ public function __construct($styleFont = null, $styleTOC = null)
101114
self::$_styleFont = $styleFont;
102115
}
103116
}
117+
118+
$this->_minDepth = $minDepth;
119+
$this->_maxDepth = $maxDepth;
104120
}
105121

106122
/**
@@ -131,9 +147,20 @@ public static function addTitle($text, $depth = 0)
131147
*
132148
* @return array
133149
*/
134-
public static function getTitles()
150+
public function getTitles()
135151
{
136-
return self::$_titles;
152+
$titles = self::$_titles;
153+
foreach ($titles as $i=>$title) {
154+
if ($this->_minDepth > $title['depth']) {
155+
unset($titles[$i]);
156+
}
157+
if (($this->_maxDepth != 0) && ($this->_maxDepth < $title['depth'])) {
158+
unset($titles[$i]);
159+
}
160+
}
161+
$titles = array_merge(array(), $titles);
162+
163+
return $titles;
137164
}
138165

139166
/**
@@ -155,4 +182,22 @@ public static function getStyleFont()
155182
{
156183
return self::$_styleFont;
157184
}
185+
186+
/**
187+
* Get Max Depth
188+
*
189+
* @return int Max depth of titles
190+
*/
191+
public function getMaxDepth() {
192+
return $this->_maxDepth;
193+
}
194+
195+
/**
196+
* Get Min Depth
197+
*
198+
* @return int Min depth of titles
199+
*/
200+
public function getMinDepth() {
201+
return $this->_minDepth;
202+
}
158203
}

src/PhpWord/Writer/Word2007/Document.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function writeDocument(PhpWord $phpWord = null)
112112
} elseif ($element instanceof Object) {
113113
$this->_writeObject($xmlWriter, $element);
114114
} elseif ($element instanceof TOC) {
115-
$this->_writeTOC($xmlWriter);
115+
$this->_writeTOC($xmlWriter, $element);
116116
} elseif ($element instanceof Footnote) {
117117
$this->_writeFootnoteReference($xmlWriter, $element);
118118
}
@@ -417,16 +417,20 @@ protected function _writeObject(XMLWriter $xmlWriter, Object $object)
417417
* Write TOC element
418418
*
419419
* @param PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
420+
* @param PhpOffice\PhpWord\TOC $toc
420421
*/
421-
private function _writeTOC(XMLWriter $xmlWriter)
422+
private function _writeTOC(XMLWriter $xmlWriter, TOC $toc)
422423
{
423-
$titles = TOC::getTitles();
424-
$styleFont = TOC::getStyleFont();
424+
$titles = $toc->getTitles();
425+
$styleFont = $toc->getStyleFont();
425426

426-
$styleTOC = TOC::getStyleTOC();
427+
$styleTOC = $toc->getStyleTOC();
427428
$fIndent = $styleTOC->getIndent();
428429
$tabLeader = $styleTOC->getTabLeader();
429430
$tabPos = $styleTOC->getTabPos();
431+
432+
$maxDepth = $toc->getMaxDepth();
433+
$minDepth = $toc->getMinDepth();
430434

431435
$isObject = ($styleFont instanceof Font) ? true : false;
432436

@@ -479,7 +483,7 @@ private function _writeTOC(XMLWriter $xmlWriter)
479483
$xmlWriter->startElement('w:r');
480484
$xmlWriter->startElement('w:instrText');
481485
$xmlWriter->writeAttribute('xml:space', 'preserve');
482-
$xmlWriter->writeRaw('TOC \o "1-9" \h \z \u');
486+
$xmlWriter->writeRaw('TOC \o "'.$minDepth.'-'.$maxDepth.'" \h \z \u');
483487
$xmlWriter->endElement();
484488
$xmlWriter->endElement();
485489

0 commit comments

Comments
 (0)