Skip to content

Commit a84171d

Browse files
committed
Merge branch 'master' of https://github.com/Pyreweb/PHPWord into #189-pyreweb
2 parents 51a8628 + b7fd623 commit a84171d

File tree

5 files changed

+157
-33
lines changed

5 files changed

+157
-33
lines changed

samples/Sample_17_TitleTOC.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,34 @@
99
$section = $phpWord->createSection();
1010

1111
// Define the TOC font style
12-
$fontStyle = array('spaceAfter'=>60, 'size'=>12);
12+
$fontStyle = array('spaceAfter' => 60, 'size' => 12);
13+
$fontStyle2 = array('size' => 10);
1314

1415
// Add title styles
15-
$phpWord->addTitleStyle(1, array('size'=>20, 'color'=>'333333', 'bold'=>true));
16-
$phpWord->addTitleStyle(2, array('size'=>16, 'color'=>'666666'));
16+
$phpWord->addTitleStyle(1, array('size' => 20, 'color' => '333333', 'bold' => true));
17+
$phpWord->addTitleStyle(2, array('size' => 16, 'color' => '666666'));
18+
$phpWord->addTitleStyle(3, array('size' => 14, 'italic' => true));
19+
$phpWord->addTitleStyle(4, array('size' => 12));
1720

1821
// Add text elements
19-
$section->addText('Table of contents:');
22+
$section->addText('Table of contents 1');
2023
$section->addTextBreak(2);
2124

22-
// Add TOC
23-
$section->addTOC($fontStyle);
25+
// Add TOC #1
26+
$toc = $section->addTOC($fontStyle);
27+
$section->addTextBreak(2);
28+
29+
// Filler
30+
$section->addText('Text between TOC');
31+
$section->addTextBreak(2);
32+
33+
// Add TOC #1
34+
$section->addText('Table of contents 2');
35+
$section->addTextBreak(2);
36+
$toc2 = $section->addTOC($fontStyle2);
37+
$toc2->setMinDepth(2);
38+
$toc2->setMaxDepth(3);
39+
2440

2541
// Add Titles
2642
$section->addPageBreak();
@@ -41,6 +57,14 @@
4157
$section->addTextBreak(2);
4258
$section->addTitle('I am a Subtitle of Title 3', 2);
4359
$section->addText('Again and again, more text...');
60+
$section->addTitle('Subtitle 3.1.1', 3);
61+
$section->addText('Text');
62+
$section->addTitle('Subtitle 3.1.1.1', 4);
63+
$section->addText('Text');
64+
$section->addTitle('Subtitle 3.1.1.2', 4);
65+
$section->addText('Text');
66+
$section->addTitle('Subtitle 3.1.2', 3);
67+
$section->addText('Text');
4468

4569
echo date('H:i:s'), " Note: Please refresh TOC manually.", \EOL;
4670
// End code

src/PhpWord/Section.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,13 @@ public function addMemoryImage($src, $style = null)
276276
*
277277
* @param mixed $styleFont
278278
* @param mixed $styleTOC
279+
* @param int $minDepth
280+
* @param int $maxDepth
279281
* @return \PhpOffice\PhpWord\TOC
280282
*/
281-
public function addTOC($styleFont = null, $styleTOC = null)
283+
public function addTOC($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
282284
{
283-
$toc = new TOC($styleFont, $styleTOC);
285+
$toc = new TOC($styleFont, $styleTOC, $minDepth, $maxDepth);
284286
$this->_elementCollection[] = $toc;
285287
return $toc;
286288
}

src/PhpWord/TOC.php

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,69 +22,87 @@ class TOC
2222
*
2323
* @var array
2424
*/
25-
private static $_titles = array();
25+
private static $titles = array();
2626

2727
/**
2828
* TOC style
2929
*
3030
* @var TOCStyle
3131
*/
32-
private static $_styleTOC;
32+
private static $tocStyle;
3333

3434
/**
3535
* Font style
3636
*
3737
* @var Font|array|string
3838
*/
39-
private static $_styleFont;
39+
private static $fontStyle;
4040

4141
/**
4242
* Title anchor
4343
*
4444
* @var int
4545
*/
46-
private static $_anchor = 252634154;
46+
private static $anchor = 252634154;
4747

4848
/**
4949
* Title bookmark
5050
*
5151
* @var int
5252
*/
53-
private static $_bookmarkId = 0;
53+
private static $bookmarkId = 0;
5454

55+
/**
56+
* Min title depth to show
57+
*
58+
* @var int
59+
*/
60+
private $minDepth = 1;
61+
62+
/**
63+
* Max title depth to show
64+
*
65+
* @var int
66+
*/
67+
private $maxDepth = 9;
5568

5669
/**
5770
* Create a new Table-of-Contents Element
5871
*
5972
* @param mixed $styleFont
6073
* @param array $styleTOC
74+
* @param int $minDepth
75+
* @param int $maxDepth
6176
*/
62-
public function __construct($styleFont = null, $styleTOC = null)
77+
public function __construct($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
6378
{
64-
self::$_styleTOC = new TOCStyle();
79+
self::$tocStyle = new TOCStyle();
6580

6681
if (!is_null($styleTOC) && is_array($styleTOC)) {
6782
foreach ($styleTOC as $key => $value) {
6883
if (substr($key, 0, 1) != '_') {
6984
$key = '_' . $key;
7085
}
71-
self::$_styleTOC->setStyleValue($key, $value);
86+
self::$tocStyle->setStyleValue($key, $value);
7287
}
7388
}
7489

7590
if (!is_null($styleFont)) {
7691
if (is_array($styleFont)) {
77-
self::$_styleFont = new Font();
92+
self::$fontStyle = new Font();
7893
foreach ($styleFont as $key => $value) {
7994
if (substr($key, 0, 1) != '_') {
8095
$key = '_' . $key;
8196
}
82-
self::$_styleFont->setStyleValue($key, $value);
97+
self::$fontStyle->setStyleValue($key, $value);
8398
}
8499
} else {
85-
self::$_styleFont = $styleFont;
100+
self::$fontStyle = $styleFont;
86101
}
87102
}
103+
104+
$this->minDepth = $minDepth;
105+
$this->maxDepth = $maxDepth;
88106
}
89107

90108
/**
@@ -96,16 +114,16 @@ public function __construct($styleFont = null, $styleTOC = null)
96114
*/
97115
public static function addTitle($text, $depth = 0)
98116
{
99-
$anchor = '_Toc' . ++self::$_anchor;
100-
$bookmarkId = self::$_bookmarkId++;
117+
$anchor = '_Toc' . ++self::$anchor;
118+
$bookmarkId = self::$bookmarkId++;
101119

102120
$title = array();
103121
$title['text'] = $text;
104122
$title['depth'] = $depth;
105123
$title['anchor'] = $anchor;
106124
$title['bookmarkId'] = $bookmarkId;
107125

108-
self::$_titles[] = $title;
126+
self::$titles[] = $title;
109127

110128
return array($anchor, $bookmarkId);
111129
}
@@ -115,9 +133,20 @@ public static function addTitle($text, $depth = 0)
115133
*
116134
* @return array
117135
*/
118-
public static function getTitles()
136+
public function getTitles()
119137
{
120-
return self::$_titles;
138+
$titles = self::$titles;
139+
foreach ($titles as $i => $title) {
140+
if ($this->minDepth > $title['depth']) {
141+
unset($titles[$i]);
142+
}
143+
if (($this->maxDepth != 0) && ($this->maxDepth < $title['depth'])) {
144+
unset($titles[$i]);
145+
}
146+
}
147+
$titles = array_merge(array(), $titles);
148+
149+
return $titles;
121150
}
122151

123152
/**
@@ -127,7 +156,7 @@ public static function getTitles()
127156
*/
128157
public static function getStyleTOC()
129158
{
130-
return self::$_styleTOC;
159+
return self::$tocStyle;
131160
}
132161

133162
/**
@@ -137,6 +166,46 @@ public static function getStyleTOC()
137166
*/
138167
public static function getStyleFont()
139168
{
140-
return self::$_styleFont;
169+
return self::$fontStyle;
170+
}
171+
172+
/**
173+
* Set max depth
174+
*
175+
* @param integer $value
176+
*/
177+
public function setMaxDepth($value)
178+
{
179+
$this->maxDepth = $value;
180+
}
181+
182+
/**
183+
* Get Max Depth
184+
*
185+
* @return int Max depth of titles
186+
*/
187+
public function getMaxDepth()
188+
{
189+
return $this->maxDepth;
190+
}
191+
192+
/**
193+
* Set min depth
194+
*
195+
* @param integer $value
196+
*/
197+
public function setMinDepth($value)
198+
{
199+
$this->minDepth = $value;
200+
}
201+
202+
/**
203+
* Get Min Depth
204+
*
205+
* @return int Min depth of titles
206+
*/
207+
public function getMinDepth()
208+
{
209+
return $this->minDepth;
141210
}
142211
}

src/PhpWord/Writer/Word2007/Document.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function writeDocument(PhpWord $phpWord = null)
9292
} elseif ($element instanceof Object) {
9393
$this->writeObject($xmlWriter, $element);
9494
} elseif ($element instanceof TOC) {
95-
$this->writeTOC($xmlWriter);
95+
$this->writeTOC($xmlWriter, $element);
9696
} elseif ($element instanceof Footnote) {
9797
$this->writeFootnote($xmlWriter, $element);
9898
} elseif ($element instanceof CheckBox) {
@@ -289,13 +289,15 @@ private function writePageBreak(XMLWriter $xmlWriter)
289289
* Write TOC element
290290
*
291291
* @param XMLWriter $xmlWriter
292+
* @param TOC $toc
292293
*/
293-
private function writeTOC(XMLWriter $xmlWriter)
294+
private function writeTOC(XMLWriter $xmlWriter, TOC $toc)
294295
{
295-
$titles = TOC::getTitles();
296-
$styleFont = TOC::getStyleFont();
297-
298-
$styleTOC = TOC::getStyleTOC();
296+
$titles = $toc->getTitles();
297+
$styleFont = $toc->getStyleFont();
298+
$styleTOC = $toc->getStyleTOC();
299+
$maxDepth = $toc->getMaxDepth();
300+
$minDepth = $toc->getMinDepth();
299301
$fIndent = $styleTOC->getIndent();
300302
$tabLeader = $styleTOC->getTabLeader();
301303
$tabPos = $styleTOC->getTabPos();
@@ -351,7 +353,7 @@ private function writeTOC(XMLWriter $xmlWriter)
351353
$xmlWriter->startElement('w:r');
352354
$xmlWriter->startElement('w:instrText');
353355
$xmlWriter->writeAttribute('xml:space', 'preserve');
354-
$xmlWriter->writeRaw('TOC \o "1-9" \h \z \u');
356+
$xmlWriter->writeRaw('TOC \o "' . $minDepth . '-' . $maxDepth . '" \h \z \u');
355357
$xmlWriter->endElement();
356358
$xmlWriter->endElement();
357359

tests/PhpWord/Tests/TOCTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,31 @@ public function testAddAndGetTitle()
7979
$i++;
8080
}
8181
}
82+
83+
/**
84+
* Set/get minDepth and maxDepth
85+
*/
86+
public function testSetGetMinMaxDepth()
87+
{
88+
$toc = new TOC();
89+
$titles = array(
90+
'Heading 1' => 1,
91+
'Heading 2' => 2,
92+
'Heading 3' => 3,
93+
'Heading 4' => 4,
94+
);
95+
foreach ($titles as $text => $depth) {
96+
$toc->addTitle($text, $depth);
97+
}
98+
99+
$this->assertEquals(1, $toc->getMinDepth());
100+
$this->assertEquals(9, $toc->getMaxDepth());
101+
102+
$toc->setMinDepth(2);
103+
$toc->setMaxDepth(3);
104+
$toc->getTitles();
105+
106+
$this->assertEquals(2, $toc->getMinDepth());
107+
$this->assertEquals(3, $toc->getMaxDepth());
108+
}
82109
}

0 commit comments

Comments
 (0)