From 88be3c962e1e29e560c46e476ce7300a0091f87c Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 28 Mar 2014 10:46:46 +0100 Subject: [PATCH 1/2] 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 --- src/PhpWord/Section.php | 6 ++- src/PhpWord/TOC.php | 51 ++++++++++++++++++++++-- src/PhpWord/Writer/Word2007/Document.php | 16 +++++--- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/PhpWord/Section.php b/src/PhpWord/Section.php index 082c7e8b..f1937420 100644 --- a/src/PhpWord/Section.php +++ b/src/PhpWord/Section.php @@ -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; } diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index 1445ee17..ef5ddba0 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -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; + } } diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 00f2b7fa..22cc8c0d 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -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(); From b7fd62312151011bd0383c0e6e2e1df67eaecd78 Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 31 Mar 2014 17:07:58 +0200 Subject: [PATCH 2/2] Re-indent with spaces TOC Depth filter Travis said spaces, not tab. Meh. --- src/PhpWord/TOC.php | 90 ++++++++++++------------ src/PhpWord/Writer/Word2007/Document.php | 6 +- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/PhpWord/TOC.php b/src/PhpWord/TOC.php index ef5ddba0..a17fb7f1 100644 --- a/src/PhpWord/TOC.php +++ b/src/PhpWord/TOC.php @@ -67,19 +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; + /** + * 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 @@ -114,9 +114,9 @@ class TOC self::$_styleFont = $styleFont; } } - - $this->_minDepth = $minDepth; - $this->_maxDepth = $maxDepth; + + $this->_minDepth = $minDepth; + $this->_maxDepth = $maxDepth; } /** @@ -150,17 +150,17 @@ class TOC public function getTitles() { $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; + 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; } /** @@ -182,22 +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; - } + + /** + * 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; + } } diff --git a/src/PhpWord/Writer/Word2007/Document.php b/src/PhpWord/Writer/Word2007/Document.php index 22cc8c0d..3ccc5f48 100644 --- a/src/PhpWord/Writer/Word2007/Document.php +++ b/src/PhpWord/Writer/Word2007/Document.php @@ -428,9 +428,9 @@ class Document extends Base $fIndent = $styleTOC->getIndent(); $tabLeader = $styleTOC->getTabLeader(); $tabPos = $styleTOC->getTabPos(); - - $maxDepth = $toc->getMaxDepth(); - $minDepth = $toc->getMinDepth(); + + $maxDepth = $toc->getMaxDepth(); + $minDepth = $toc->getMinDepth(); $isObject = ($styleFont instanceof Font) ? true : false;