PHPWord/src/PhpWord/Element/Section.php

238 lines
5.2 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\Element;
2014-04-05 19:02:49 +07:00
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Style\Section as SectionSettings;
use PhpOffice\PhpWord\Element\PageBreak;
use PhpOffice\PhpWord\Element\TOC;
/**
* Section
*/
class Section extends AbstractElement
2013-12-16 06:40:30 -05:00
{
/**
* Section settings
*
* @var \PhpOffice\PhpWord\Style\Section
2013-12-16 06:40:30 -05:00
*/
2014-03-31 23:10:51 +07:00
private $settings;
2013-12-16 06:40:30 -05:00
/**
2014-04-05 19:02:49 +07:00
* Section headers, indexed from 1, not zero
2013-12-16 06:40:30 -05:00
*
2014-03-31 23:10:51 +07:00
* @var Header[]
2013-12-16 06:40:30 -05:00
*/
2014-03-31 23:10:51 +07:00
private $headers = array();
2013-12-16 06:40:30 -05:00
/**
2014-04-05 19:02:49 +07:00
* Section footers, indexed from 1, not zero
2013-12-16 06:40:30 -05:00
*
2014-04-05 19:02:49 +07:00
* @var Footer[]
2013-12-16 06:40:30 -05:00
*/
2014-04-05 19:02:49 +07:00
private $footers = array();
2013-12-16 06:40:30 -05:00
/**
2014-03-31 23:10:51 +07:00
* Create new instance
2013-12-16 06:40:30 -05:00
*
* @param int $sectionCount
2014-04-03 10:13:13 +07:00
* @param array $settings
2013-12-16 06:40:30 -05:00
*/
public function __construct($sectionCount, $settings = null)
{
$this->container = 'section';
2014-04-05 19:02:49 +07:00
$this->sectionId = $sectionCount;
$this->setDocPart($this->container, $this->sectionId);
$this->settings = new SectionSettings();
$this->setSettings($settings);
}
2013-12-16 06:40:30 -05:00
/**
2014-03-31 23:10:51 +07:00
* Set section settings
*
2014-03-31 23:10:51 +07:00
* @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 (is_null($value)) {
continue;
}
2014-03-31 23:10:51 +07:00
$this->settings->setSettingValue($key, $value);
2013-12-16 06:40:30 -05:00
}
}
}
/**
* Get Section Settings
*
* @return \PhpOffice\PhpWord\Style\Section
2013-12-16 06:40:30 -05:00
*/
public function getSettings()
{
2014-03-31 23:10:51 +07:00
return $this->settings;
2013-12-16 06:40:30 -05:00
}
/**
* Add a PageBreak Element
*/
public function addPageBreak()
{
2014-03-31 23:10:51 +07:00
$this->elements[] = new PageBreak();
2013-12-16 06:40:30 -05:00
}
/**
* Add a Table-of-Contents Element
*
* @param mixed $styleFont
* @param mixed $styleTOC
2014-04-06 15:19:09 +07:00
* @param integer $minDepth
* @param integer $maxDepth
* @return \PhpOffice\PhpWord\Element\TOC
2013-12-16 06:40:30 -05:00
*/
public function addTOC($styleFont = null, $styleTOC = null, $minDepth = 1, $maxDepth = 9)
2013-12-16 06:40:30 -05:00
{
$toc = new TOC($styleFont, $styleTOC, $minDepth, $maxDepth);
2014-03-31 23:10:51 +07:00
$this->elements[] = $toc;
2013-12-16 06:40:30 -05:00
return $toc;
}
/**
2014-03-31 23:10:51 +07:00
* Add header
2013-12-16 06:40:30 -05:00
*
2014-04-05 19:02:49 +07:00
* @param string $type
2014-03-31 23:10:51 +07:00
* @return Header
* @since 0.10.0
2013-12-16 06:40:30 -05:00
*/
2014-04-05 19:02:49 +07:00
public function addHeader($type = Header::AUTO)
2013-12-16 06:40:30 -05:00
{
2014-04-05 19:02:49 +07:00
return $this->addHeaderFooter($type, true);
2013-12-16 06:40:30 -05:00
}
/**
2014-03-31 23:10:51 +07:00
* Add footer
2013-12-16 06:40:30 -05:00
*
2014-04-05 19:02:49 +07:00
* @param string $type
2014-03-31 23:10:51 +07:00
* @return Footer
* @since 0.10.0
2013-12-16 06:40:30 -05:00
*/
2014-04-05 19:02:49 +07:00
public function addFooter($type = Header::AUTO)
2013-12-16 06:40:30 -05:00
{
2014-04-05 19:02:49 +07:00
return $this->addHeaderFooter($type, false);
2013-12-16 06:40:30 -05:00
}
/**
2014-04-05 19:02:49 +07:00
* Get header elements
2013-12-16 06:40:30 -05:00
*
2014-04-03 10:13:13 +07:00
* @return Header[]
2013-12-16 06:40:30 -05:00
*/
2014-03-31 23:10:51 +07:00
public function getHeaders()
2013-12-16 06:40:30 -05:00
{
2014-03-31 23:10:51 +07:00
return $this->headers;
}
2013-12-16 06:40:30 -05:00
/**
2014-04-05 19:02:49 +07:00
* Get footer elements
2013-12-16 06:40:30 -05:00
*
2014-04-05 19:02:49 +07:00
* @return Footer[]
2013-12-16 06:40:30 -05:00
*/
2014-04-05 19:02:49 +07:00
public function getFooters()
2013-12-16 06:40:30 -05:00
{
2014-04-05 19:02:49 +07:00
return $this->footers;
2013-12-16 06:40:30 -05:00
}
/**
* 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
*
2014-03-31 23:10:51 +07:00
* @return boolean
2013-12-16 06:40:30 -05:00
*/
public function hasDifferentFirstPage()
{
2014-04-05 19:02:49 +07:00
foreach ($this->headers as $header) {
if ($header->getType() == Header::FIRST) {
return true;
}
}
return false;
}
/**
* Add header/footer
*
* @param string $type
* @param boolean $header
2014-04-05 19:02:49 +07:00
* @return Header|Footer
* @throws \PhpOffice\PhpWord\Exception\Exception
* @since 0.10.0
2014-04-05 19:02:49 +07:00
*/
private function addHeaderFooter($type = Header::AUTO, $header = true)
{
$collectionArray = $header ? 'headers' : 'footers';
$containerClass = 'PhpOffice\\PhpWord\\Element\\';
2014-04-05 19:02:49 +07:00
$containerClass .= ($header ? 'Header' : 'Footer');
$collection = &$this->$collectionArray;
if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {
$index = count($collection);
$container = new $containerClass($this->sectionId, ++$index, $type);
$collection[$index] = $container;
return $container;
} else {
throw new Exception('Invalid header/footer type.');
}
2013-12-16 06:40:30 -05:00
}
/**
2014-03-31 23:10:51 +07:00
* Create header
2013-12-16 06:40:30 -05:00
*
* @return Header
* @deprecated 0.10.0
* @codeCoverageIgnore
2013-12-16 06:40:30 -05:00
*/
2014-03-31 23:10:51 +07:00
public function createHeader()
{
2014-03-31 23:10:51 +07:00
return $this->addHeader();
}
/**
2014-03-31 23:10:51 +07:00
* Create footer
*
* @return Footer
* @deprecated 0.10.0
* @codeCoverageIgnore
*/
2014-03-31 23:10:51 +07:00
public function createFooter()
{
2014-03-31 23:10:51 +07:00
return $this->addFooter();
}
2014-04-05 19:02:49 +07:00
/**
* Get footer
*
* @return Footer
* @deprecated 0.10.0
2014-04-05 19:02:49 +07:00
* @codeCoverageIgnore
*/
public function getFooter()
{
if (empty($this->footers)) {
return null;
} else {
return $this->footers[1];
}
}
}