Skip to content

Commit 7297754

Browse files
committed
Merge pull request #42 from RLovelett/multiple-headers
Support Multiple headers
2 parents 95f16cc + 4fb1800 commit 7297754

File tree

5 files changed

+123
-16
lines changed

5 files changed

+123
-16
lines changed

Classes/PHPWord/Section.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ class PHPWord_Section {
5757
private $_elementCollection = array();
5858

5959
/**
60-
* Section Header
60+
* Section Headers
6161
*
62-
* @var PHPWord_Section_Header
62+
* @var array
6363
*/
64-
private $_header = null;
64+
private $_headers = array();
6565

6666
/**
6767
* Section Footer
@@ -345,17 +345,33 @@ public function getElements() {
345345
*/
346346
public function createHeader() {
347347
$header = new PHPWord_Section_Header($this->_sectionCount);
348-
$this->_header = $header;
348+
$this->_headers[] = $header;
349349
return $header;
350350
}
351351

352352
/**
353-
* Get Header
353+
* Get Headers
354354
*
355-
* @return PHPWord_Section_Header
355+
* @return array
356+
*/
357+
public function getHeaders() {
358+
return $this->_headers;
359+
}
360+
361+
/**
362+
* Is there a header for this section that is for the first page only?
363+
*
364+
* If any of the PHPWord_Section_Header instances have a type of
365+
* PHPWord_Section_Header::FIRST then this method returns true. False
366+
* otherwise.
367+
*
368+
* @return Boolean
356369
*/
357-
public function getHeader() {
358-
return $this->_header;
370+
public function hasDifferentFirstPage() {
371+
$value = array_filter($this->_headers, function(PHPWord_Section_Header &$header) {
372+
return $header->getType() == PHPWord_Section_Header::FIRST;
373+
});
374+
return count($value) > 0;
359375
}
360376

361377
/**

Classes/PHPWord/Section/Header.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ class PHPWord_Section_Header {
4848
* @var int
4949
*/
5050
private $_rId;
51+
52+
/**
53+
* Header type
54+
*
55+
* @var string
56+
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
57+
*/
58+
private $_type = PHPWord_Section_Header::AUTO;
59+
60+
/**
61+
* Even Numbered Pages Only
62+
* @var string
63+
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
64+
*/
65+
const EVEN = 'even';
66+
67+
/**
68+
* Default Header or Footer
69+
* @var string
70+
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
71+
*/
72+
const AUTO = 'default'; // Did not use DEFAULT because it is a PHP keyword
73+
74+
/**
75+
* First Page Only
76+
* @var string
77+
* @link http://www.schemacentral.com/sc/ooxml/a-w_type-4.html Header or Footer Type
78+
*/
79+
const FIRST = 'first';
5180

5281
/**
5382
* Header Element Collection
@@ -222,5 +251,34 @@ public function getElements() {
222251
public function getHeaderCount() {
223252
return $this->_headerCount;
224253
}
254+
255+
/**
256+
* Get Header Type
257+
*/
258+
public function getType() {
259+
return $this->_type;
260+
}
261+
262+
/**
263+
* Reset back to default
264+
*/
265+
public function resetType() {
266+
return $this->_type = PHPWord_Section_Header::AUTO;
267+
}
268+
269+
/**
270+
* First page only header
271+
*/
272+
public function firstPage() {
273+
return $this->_type = PHPWord_Section_Header::FIRST;
274+
}
275+
276+
/**
277+
* Even numbered Pages only
278+
*/
279+
public function evenPage() {
280+
return $this->_type = PHPWord_Section_Header::EVEN;
281+
}
282+
225283
}
226284
?>

Classes/PHPWord/Writer/Word2007.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,11 @@ public function save($pFilename = null) {
114114
$_sections = $this->_document->getSections();
115115

116116
foreach($_sections as $section) {
117-
$_header = $section->getHeader();
118-
if(!is_null($_header)) {
117+
$_headers = $section->getHeaders();
118+
foreach ($_headers as $index => &$_header) {
119119
$_cHdrs++;
120120
$_header->setRelationId(++$rID);
121-
$_headerCount = $_header->getHeaderCount();
122-
$_headerFile = 'header'.$_headerCount.'.xml';
121+
$_headerFile = 'header'.$_cHdrs.'.xml';
123122
$sectionElements[] = array('target'=>$_headerFile, 'type'=>'header', 'rID'=>$rID);
124123
$objZip->addFromString('word/'.$_headerFile, $this->getWriterPart('header')->writeHeader($_header));
125124
}

Classes/PHPWord/Writer/Word2007/Document.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
117117

118118
private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) {
119119
$_settings = $section->getSettings();
120-
$_header = $section->getHeader();
120+
$_headers = $section->getHeaders();
121121
$_footer = $section->getFooter();
122122
$pgSzW = $_settings->getPageSizeW();
123123
$pgSzH = $_settings->getPageSizeH();
@@ -132,14 +132,19 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
132132

133133
$objWriter->startElement('w:sectPr');
134134

135-
if(!is_null($_header)) {
135+
foreach ($_headers as &$_header) {
136136
$rId = $_header->getRelationId();
137137
$objWriter->startElement('w:headerReference');
138-
$objWriter->writeAttribute('w:type', 'default');
138+
$objWriter->writeAttribute('w:type', $_header->getType());
139139
$objWriter->writeAttribute('r:id', 'rId'.$rId);
140140
$objWriter->endElement();
141141
}
142142

143+
if($section->hasDifferentFirstPage()) {
144+
$objWriter->startElement('w:titlePg');
145+
$objWriter->endElement();
146+
}
147+
143148
if(!is_null($_footer)) {
144149
$rId = $_footer->getRelationId();
145150
$objWriter->startElement('w:footerReference');

samples/old/HeaderFooter.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
// New portrait section
88
$section = $PHPWord->createSection();
99

10-
// Add header
10+
// Add first page header
1111
$header = $section->createHeader();
12+
$header->firstPage();
1213
$table = $header->addTable();
1314
$table->addRow();
1415
$table->addCell(4500)->addText('This is the header.');
1516
$table->addCell(4500)->addImage('_earth.jpg', array('width'=>50, 'height'=>50, 'align'=>'right'));
1617

18+
// Add header for all other pages
19+
$subsequent = $section->createHeader();
20+
$subsequent->addText("Subsequent pages in Section 1 will Have this!");
21+
1722
// Add footer
1823
$footer = $section->createFooter();
1924
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align'=>'center'));
@@ -22,6 +27,30 @@
2227
$section->addTextBreak();
2328
$section->addText('Some text...');
2429

30+
// Create a second page
31+
$section->addPageBreak();
32+
33+
// Write some text
34+
$section->addTextBreak();
35+
$section->addText('Some text...');
36+
37+
// Create a third page
38+
$section->addPageBreak();
39+
40+
// Write some text
41+
$section->addTextBreak();
42+
$section->addText('Some text...');
43+
44+
// New portrait section
45+
$section2 = $PHPWord->createSection();
46+
47+
$sec2Header = $section2->createHeader();
48+
$sec2Header->addText("All pages in Section 2 will Have this!");
49+
50+
// Write some text
51+
$section2->addTextBreak();
52+
$section2->addText('Some text...');
53+
2554
// Save File
2655
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
2756
$objWriter->save('HeaderFooter.docx');

0 commit comments

Comments
 (0)