Final fix that satisfies both MS Word and OpenOffice

This commit is contained in:
Ivan Lanin 2014-03-14 15:26:17 +07:00
parent d87fa2aa8b
commit c8a1e2e0eb
4 changed files with 28 additions and 17 deletions

View File

@ -422,9 +422,6 @@ class PHPWord_Section
*/ */
public function getFooter() public function getFooter()
{ {
if (!($this->_footer instanceOf PHPWord_Section_Footer)) {
$this->_footer = new PHPWord_Section_Footer($this->_sectionCount);
}
return $this->_footer; return $this->_footer;
} }

View File

@ -128,6 +128,7 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter
$rID = PHPWord_Media::countSectionMediaElements() + 6; $rID = PHPWord_Media::countSectionMediaElements() + 6;
$_sections = $this->_document->getSections(); $_sections = $this->_document->getSections();
$footers = array();
foreach ($_sections as $section) { foreach ($_sections as $section) {
$_headers = $section->getHeaders(); $_headers = $section->getHeaders();
foreach ($_headers as $index => &$_header) { foreach ($_headers as $index => &$_header) {
@ -139,12 +140,14 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter
} }
$_footer = $section->getFooter(); $_footer = $section->getFooter();
$_cFtrs++; $footers[++$_cFtrs] = $_footer;
$_footer->setRelationId(++$rID); if (!is_null($_footer)) {
$_footerCount = $_footer->getFooterCount(); $_footer->setRelationId(++$rID);
$_footerFile = 'footer' . $_footerCount . '.xml'; $_footerCount = $_footer->getFooterCount();
$sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID); $_footerFile = 'footer' . $_footerCount . '.xml';
$objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer)); $sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID);
$objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer));
}
} }
if (PHPWord_Footnote::countFootnoteElements() > 0) { if (PHPWord_Footnote::countFootnoteElements() > 0) {
@ -159,7 +162,15 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter
// build docx file // build docx file
// Write dynamic files // Write dynamic files
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->_imageTypes, $this->_objectTypes, $_cHdrs, $_cFtrs)); $objZip->addFromString(
'[Content_Types].xml',
$this->getWriterPart('contenttypes')->writeContentTypes(
$this->_imageTypes,
$this->_objectTypes,
$_cHdrs,
$footers
)
);
$objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document)); $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document));
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document)); $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document));
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document)); $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document));

View File

@ -31,7 +31,7 @@
class PHPWord_Writer_Word2007_ContentTypes extends PHPWord_Writer_Word2007_WriterPart class PHPWord_Writer_Word2007_ContentTypes extends PHPWord_Writer_Word2007_WriterPart
{ {
public function writeContentTypes($_imageTypes, $_objectTypes, $_cHdrs, $_cFtrs) public function writeContentTypes($_imageTypes, $_objectTypes, $_cHdrs, $footers)
{ {
// Create XML writer // Create XML writer
$objWriter = null; $objWriter = null;
@ -153,12 +153,14 @@ class PHPWord_Writer_Word2007_ContentTypes extends PHPWord_Writer_Word2007_Write
); );
} }
for ($i = 1; $i <= $_cFtrs; $i++) { for ($i = 1; $i <= count($footers); $i++) {
$this->_writeOverrideContentType( if (!is_null($footers[$i])) {
$objWriter, $this->_writeOverrideContentType(
'/word/footer' . $i . '.xml', $objWriter,
'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' '/word/footer' . $i . '.xml',
); 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml'
);
}
} }

View File

@ -52,6 +52,7 @@ Changes in branch for release 0.7.1 :
- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF - Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF
- Feature: (ivanlanin) - Reader: Initial effort for Word2007 - Feature: (ivanlanin) - Reader: Initial effort for Word2007
- Feature: (ivanlanin) - MemoryImage: Allow remote image when allow_url_open = on - Feature: (ivanlanin) - MemoryImage: Allow remote image when allow_url_open = on
- Bugfix: (ivanlanin) - Footnote: Corrupt DOCX reported by MS Word when sections > 1 and not every sections have footnote
- QA: (Progi1984) - UnitTests - QA: (Progi1984) - UnitTests
Changes in branch for release 0.7.0 : Changes in branch for release 0.7.0 :