PHPWord/src/PhpWord/Section.php

418 lines
10 KiB
PHP
Raw Normal View History

2012-05-06 18:25:40 +02:00
<?php
/**
* PHPWord
2012-05-06 18:25:40 +02:00
*
2014-03-27 23:55:06 +07:00
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
2012-05-06 18:25:40 +02:00
*/
namespace PhpOffice\PhpWord;
2014-03-25 17:53:10 +07:00
use PhpOffice\PhpWord\Exceptions\InvalidObjectException;
use PhpOffice\PhpWord\Section\Footer;
use PhpOffice\PhpWord\Section\Image;
use PhpOffice\PhpWord\Section\Header;
use PhpOffice\PhpWord\Section\Link;
use PhpOffice\PhpWord\Section\ListItem;
use PhpOffice\PhpWord\Section\Object;
use PhpOffice\PhpWord\Section\PageBreak;
use PhpOffice\PhpWord\Section\Settings;
use PhpOffice\PhpWord\Section\Table;
use PhpOffice\PhpWord\Section\Text;
use PhpOffice\PhpWord\Section\TextBreak;
use PhpOffice\PhpWord\Section\TextRun;
use PhpOffice\PhpWord\Section\Title;
use PhpOffice\PhpWord\Shared\String;
/**
* Section
*/
class Section
2013-12-16 06:40:30 -05:00
{
/**
* Section count
*
* @var int
*/
private $_sectionCount;
/**
* Section settings
*
* @var \PhpOffice\PhpWord\Section\Settings
2013-12-16 06:40:30 -05:00
*/
private $_settings;
/**
* Section Element Collection
*
* @var array
*/
private $_elementCollection = array();
/**
* Section Headers
*
* @var array
*/
private $_headers = array();
/**
* Section Footer
*
* @var \PhpOffice\PhpWord\Section\Footer
2013-12-16 06:40:30 -05:00
*/
private $_footer = null;
/**
* Create a new Section
*
* @param int $sectionCount
* @param mixed $settings
*/
public function __construct($sectionCount, $settings = null)
{
$this->_sectionCount = $sectionCount;
$this->_settings = new Settings();
$this->setSettings($settings);
}
2013-12-16 06:40:30 -05:00
/**
* Set Section Settings
*
* @param array $settings
*/
public function setSettings($settings = null)
{
2013-12-16 06:40:30 -05:00
if (!is_null($settings) && is_array($settings)) {
foreach ($settings as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_settings->setSettingValue($key, $value);
}
}
}
/**
* Get Section Settings
*
* @return \PhpOffice\PhpWord\Section\Settings
2013-12-16 06:40:30 -05:00
*/
public function getSettings()
{
return $this->_settings;
}
/**
* Add a Text Element
*
* @param string $text
* @param mixed $styleFont
* @param mixed $styleParagraph
* @return \PhpOffice\PhpWord\Section\Text
2013-12-16 06:40:30 -05:00
*/
public function addText($text, $styleFont = null, $styleParagraph = null)
{
if (!String::IsUTF8($text)) {
2013-12-16 06:40:30 -05:00
$text = utf8_encode($text);
}
$text = new Text($text, $styleFont, $styleParagraph);
2013-12-16 06:40:30 -05:00
$this->_elementCollection[] = $text;
return $text;
}
2013-12-16 06:40:30 -05:00
/**
* Add a Link Element
*
* @param string $linkSrc
* @param string $linkName
* @param mixed $styleFont
* @param mixed $styleParagraph
* @return \PhpOffice\PhpWord\Section\Link
2013-12-16 06:40:30 -05:00
*/
public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null)
{
if (!String::IsUTF8($linkSrc)) {
2013-12-16 06:40:30 -05:00
$linkSrc = utf8_encode($linkSrc);
}
if (!is_null($linkName)) {
if (!String::IsUTF8($linkName)) {
2013-12-16 06:40:30 -05:00
$linkName = utf8_encode($linkName);
}
}
$link = new Link($linkSrc, $linkName, $styleFont, $styleParagraph);
$rID = Media::addSectionLinkElement($linkSrc);
2013-12-16 06:40:30 -05:00
$link->setRelationId($rID);
$this->_elementCollection[] = $link;
return $link;
}
/**
* Add a TextBreak Element
*
* @param int $count
* @param null|string|array|\PhpOffice\PhpWord\Style\Font $fontStyle
* @param null|string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle
2013-12-16 06:40:30 -05:00
*/
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
2013-12-16 06:40:30 -05:00
{
for ($i = 1; $i <= $count; $i++) {
$this->_elementCollection[] = new TextBreak($fontStyle, $paragraphStyle);
2013-12-16 06:40:30 -05:00
}
}
/**
* Add a PageBreak Element
*/
public function addPageBreak()
{
$this->_elementCollection[] = new PageBreak();
}
2013-12-16 06:40:30 -05:00
/**
* Add a Table Element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Section\Table
2013-12-16 06:40:30 -05:00
*/
public function addTable($style = null)
{
$table = new Table('section', $this->_sectionCount, $style);
2013-12-16 06:40:30 -05:00
$this->_elementCollection[] = $table;
return $table;
}
/**
* Add a ListItem Element
*
* @param string $text
* @param int $depth
* @param mixed $styleFont
2012-05-06 18:25:40 +02:00
* @param mixed $styleList
2013-12-16 06:40:30 -05:00
* @param mixed $styleParagraph
* @return \PhpOffice\PhpWord\Section\ListItem
2013-12-16 06:40:30 -05:00
*/
public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null)
{
if (!String::isUTF8($text)) {
2013-12-16 06:40:30 -05:00
$text = utf8_encode($text);
}
$listItem = new ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
2013-12-16 06:40:30 -05:00
$this->_elementCollection[] = $listItem;
return $listItem;
}
/**
* Add a OLE-Object Element
*
2014-03-28 13:50:53 +07:00
* All exceptions should be handled by PhpOffice\PhpWord\Section\Object
*
2013-12-16 06:40:30 -05:00
* @param string $src
* @param mixed $style
* @return \PhpOffice\PhpWord\Section\Object
2013-12-16 06:40:30 -05:00
*/
public function addObject($src, $style = null)
{
$object = new Object($src, $style);
2013-12-16 06:40:30 -05:00
if (!is_null($object->getSource())) {
$inf = pathinfo($src);
$ext = $inf['extension'];
if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
$ext = substr($ext, 0, -1);
}
2014-03-28 13:50:53 +07:00
$icon = __DIR__ . "/_staticDocParts/_{$ext}.png";
$rIDimg = Media::addSectionMediaElement($icon, 'image', new Image($icon));
$data = Media::addSectionMediaElement($src, 'oleObject');
2013-12-16 06:40:30 -05:00
$rID = $data[0];
$objectId = $data[1];
$object->setRelationId($rID);
$object->setObjectId($objectId);
$object->setImageRelationId($rIDimg);
$this->_elementCollection[] = $object;
return $object;
2014-03-25 17:53:10 +07:00
} else {
2014-03-28 13:50:53 +07:00
throw new InvalidObjectException();
2013-12-16 06:40:30 -05:00
}
}
/**
2014-03-28 13:50:53 +07:00
* Add image element
*
* All exceptions should be handled by PhpOffice\PhpWord\Section\Image
2013-12-16 06:40:30 -05:00
*
* @param string $src
* @param mixed $style
* @return \PhpOffice\PhpWord\Section\Image
2013-12-16 06:40:30 -05:00
*/
public function addImage($src, $style = null)
{
$image = new Image($src, $style);
2014-03-28 13:50:53 +07:00
$rID = Media::addSectionMediaElement($src, 'image', $image);
$image->setRelationId($rID);
$this->_elementCollection[] = $image;
return $image;
2013-12-16 06:40:30 -05:00
}
/**
2014-03-28 13:50:53 +07:00
* Add memory image element
2013-12-16 06:40:30 -05:00
*
* @deprecated
2014-03-28 13:50:53 +07:00
*
* @param string $src
* @param mixed $style
2013-12-16 06:40:30 -05:00
*/
public function addMemoryImage($src, $style = null)
2013-12-16 06:40:30 -05:00
{
return $this->addImage($src, $style);
2013-12-16 06:40:30 -05:00
}
/**
* Add a Table-of-Contents Element
*
* @param mixed $styleFont
* @param mixed $styleTOC
* @return \PhpOffice\PhpWord\TOC
2013-12-16 06:40:30 -05:00
*/
public function addTOC($styleFont = null, $styleTOC = null)
{
$toc = new TOC($styleFont, $styleTOC);
2013-12-16 06:40:30 -05:00
$this->_elementCollection[] = $toc;
return $toc;
}
/**
* Add a Title Element
*
* @param string $text
* @param int $depth
* @return \PhpOffice\PhpWord\Section\Title
2013-12-16 06:40:30 -05:00
*/
public function addTitle($text, $depth = 1)
{
if (!String::isUTF8($text)) {
2013-12-16 06:40:30 -05:00
$text = utf8_encode($text);
}
$styles = Style::getStyles();
2013-12-16 06:40:30 -05:00
if (array_key_exists('Heading_' . $depth, $styles)) {
$style = 'Heading' . $depth;
} else {
$style = null;
}
$title = new Title($text, $depth, $style);
2013-12-16 06:40:30 -05:00
$data = TOC::addTitle($text, $depth);
2013-12-16 06:40:30 -05:00
$anchor = $data[0];
$bookmarkId = $data[1];
$title->setAnchor($anchor);
$title->setBookmarkId($bookmarkId);
$this->_elementCollection[] = $title;
return $title;
}
/**
* Create a new TextRun
*
* @param mixed $styleParagraph
* @return \PhpOffice\PhpWord\Section\TextRun
2013-12-16 06:40:30 -05:00
*/
public function createTextRun($styleParagraph = null)
{
$textRun = new TextRun($styleParagraph);
2013-12-16 06:40:30 -05:00
$this->_elementCollection[] = $textRun;
return $textRun;
}
/**
* Get all Elements
*
* @return array
*/
public function getElements()
{
return $this->_elementCollection;
}
/**
* Create a new Header
*
* @return \PhpOffice\PhpWord\Section\Header
2013-12-16 06:40:30 -05:00
*/
public function createHeader()
{
$header = new Header($this->_sectionCount);
2013-12-16 06:40:30 -05:00
$this->_headers[] = $header;
return $header;
}
2013-12-16 06:40:30 -05:00
/**
* Get Headers
*
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* Is there a header for this section that is for the first page only?
*
* If any of the Header instances have a type of Header::FIRST then this method returns true.
* False otherwise.
2013-12-16 06:40:30 -05:00
*
* @return Boolean
*/
public function hasDifferentFirstPage()
{
$value = array_filter($this->_headers, function (Header &$header) {
return $header->getType() == Header::FIRST;
2013-12-16 06:40:30 -05:00
});
return count($value) > 0;
}
/**
* Create a new Footer
*
* @return \PhpOffice\PhpWord\Section\Footer
2013-12-16 06:40:30 -05:00
*/
public function createFooter()
{
$footer = new Footer($this->_sectionCount);
2013-12-16 06:40:30 -05:00
$this->_footer = $footer;
return $footer;
}
/**
* Get footer element
*
* @return \PhpOffice\PhpWord\Section\Footer
2013-12-16 06:40:30 -05:00
*/
public function getFooter()
{
return $this->_footer;
}
/**
* Create a new Footnote Element
*
* @param mixed $styleParagraph
* @return \PhpOffice\PhpWord\Section\Footnote
*/
public function createFootnote($styleParagraph = null)
{
$footnote = new \PhpOffice\PhpWord\Section\Footnote($styleParagraph);
$refID = Footnote::addFootnoteElement($footnote);
$footnote->setReferenceId($refID);
$this->_elementCollection[] = $footnote;
return $footnote;
}
}