2014-03-22 10:06:08 +04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
* word processing documents.
|
|
|
|
|
*
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2014-03-22 10:06:08 +04:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
|
2014-04-30 08:54:38 +07:00
|
|
|
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-04-10 11:47:26 +04:00
|
|
|
use PhpOffice\PhpWord\Element\Section;
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\Shared\XMLWriter;
|
2014-05-07 09:47:02 +07:00
|
|
|
use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
|
2014-05-01 18:37:34 +07:00
|
|
|
use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
2014-05-06 08:50:55 +07:00
|
|
|
* Word2007 document part writer: word/document.xml
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-04-30 08:54:38 +07:00
|
|
|
class Document extends AbstractPart
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
2014-05-06 08:50:55 +07:00
|
|
|
* Write part
|
2014-03-24 00:26:10 +07:00
|
|
|
*
|
2014-04-10 11:47:26 +04:00
|
|
|
* @return string
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-05-06 01:45:13 +07:00
|
|
|
public function write()
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2014-05-06 08:50:55 +07:00
|
|
|
$phpWord = $this->getParentWriter()->getPhpWord();
|
2014-03-30 16:31:01 +07:00
|
|
|
$xmlWriter = $this->getXmlWriter();
|
2014-05-06 08:50:55 +07:00
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
$sections = $phpWord->getSections();
|
|
|
|
|
$sectionCount = count($sections);
|
|
|
|
|
$currentSection = 0;
|
2014-05-06 08:50:55 +07:00
|
|
|
$drawingSchema = 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing';
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
|
|
|
|
|
$xmlWriter->startElement('w:document');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
|
2014-05-06 08:50:55 +07:00
|
|
|
$xmlWriter->writeAttribute('xmlns:wp', $drawingSchema);
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
|
|
|
|
|
|
|
|
|
|
$xmlWriter->startElement('w:body');
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
if ($sectionCount > 0) {
|
2014-04-03 06:54:45 +07:00
|
|
|
foreach ($sections as $section) {
|
2014-05-01 18:37:34 +07:00
|
|
|
$currentSection++;
|
2014-05-07 09:47:02 +07:00
|
|
|
|
|
|
|
|
$containerWriter = new Container($xmlWriter, $section);
|
|
|
|
|
$containerWriter->write();
|
|
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
if ($currentSection == $sectionCount) {
|
|
|
|
|
$this->writeSectionSettings($xmlWriter, $section);
|
2014-03-22 10:06:08 +04:00
|
|
|
} else {
|
2014-03-30 18:51:25 +07:00
|
|
|
$this->writeSection($xmlWriter, $section);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 22:13:00 +07:00
|
|
|
$xmlWriter->endElement(); // w:body
|
|
|
|
|
$xmlWriter->endElement(); // w:document
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
return $xmlWriter->getData();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Write begin section
|
|
|
|
|
*
|
2014-04-10 11:47:26 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
|
|
|
|
* @param \PhpOffice\PhpWord\Element\Section $section
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-03-30 18:51:25 +07:00
|
|
|
private function writeSection(XMLWriter $xmlWriter, Section $section)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
|
|
|
|
$xmlWriter->startElement('w:p');
|
|
|
|
|
$xmlWriter->startElement('w:pPr');
|
2014-05-01 18:37:34 +07:00
|
|
|
$this->writeSectionSettings($xmlWriter, $section);
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Write end section
|
|
|
|
|
*
|
2014-04-10 11:47:26 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
|
|
|
|
* @param \PhpOffice\PhpWord\Element\Section $section
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-05-01 18:37:34 +07:00
|
|
|
private function writeSectionSettings(XMLWriter $xmlWriter, Section $section)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
|
|
|
|
$xmlWriter->startElement('w:sectPr');
|
|
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
// Header reference
|
2014-05-01 18:37:34 +07:00
|
|
|
foreach ($section->getHeaders() as $header) {
|
2014-04-03 06:54:45 +07:00
|
|
|
$rId = $header->getRelationId();
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->startElement('w:headerReference');
|
2014-04-03 06:54:45 +07:00
|
|
|
$xmlWriter->writeAttribute('w:type', $header->getType());
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
2014-05-01 18:37:34 +07:00
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
// Footer reference
|
2014-05-01 18:37:34 +07:00
|
|
|
foreach ($section->getFooters() as $footer) {
|
2014-04-03 06:54:45 +07:00
|
|
|
$rId = $footer->getRelationId();
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->startElement('w:footerReference');
|
2014-04-05 19:02:49 +07:00
|
|
|
$xmlWriter->writeAttribute('w:type', $footer->getType());
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
2014-05-01 18:37:34 +07:00
|
|
|
|
2014-04-05 19:02:49 +07:00
|
|
|
// Different first page
|
|
|
|
|
if ($section->hasDifferentFirstPage()) {
|
|
|
|
|
$xmlWriter->startElement('w:titlePg');
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
// Section settings
|
2014-06-08 16:44:46 +07:00
|
|
|
$styleWriter = new SectionStyleWriter($xmlWriter, $section->getStyle());
|
2014-05-01 18:37:34 +07:00
|
|
|
$styleWriter->write();
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
$xmlWriter->endElement(); // w:sectPr
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
}
|