188 lines
3.6 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\Container;
use PhpOffice\PhpWord\TOC;
use PhpOffice\PhpWord\Container\Footer;
use PhpOffice\PhpWord\Container\Header;
2014-03-31 23:10:51 +07:00
use PhpOffice\PhpWord\Container\Settings;
use PhpOffice\PhpWord\Element\PageBreak;
/**
* Section
*/
2014-03-31 23:10:51 +07:00
class Section extends Container
2013-12-16 06:40:30 -05:00
{
/**
* Section settings
*
2014-03-31 23:10:51 +07:00
* @var Settings
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-03-31 23:10:51 +07:00
* Section headers
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-03-31 23:10:51 +07:00
* Section footer
2013-12-16 06:40:30 -05:00
*
2014-03-31 23:10:51 +07:00
* @var Footer
2013-12-16 06:40:30 -05:00
*/
2014-03-31 23:10:51 +07:00
private $footer = null;
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';
$this->containerId = $sectionCount;
2014-03-31 23:10:51 +07:00
$this->settings = new Settings();
$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 (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
2014-03-31 23:10:51 +07:00
$this->settings->setSettingValue($key, $value);
2013-12-16 06:40:30 -05:00
}
}
}
/**
* Get Section Settings
*
2014-03-31 23:10:51 +07:00
* @return Settings
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-03-31 23:10:51 +07:00
* @return TOC
2013-12-16 06:40:30 -05:00
*/
public function addTOC($styleFont = null, $styleTOC = null)
{
$toc = new TOC($styleFont, $styleTOC);
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-03-31 23:10:51 +07:00
* @return Header
2013-12-16 06:40:30 -05:00
*/
2014-03-31 23:10:51 +07:00
public function addHeader()
2013-12-16 06:40:30 -05:00
{
$header = new Header($this->containerId);
2014-03-31 23:10:51 +07:00
$this->headers[] = $header;
return $header;
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-03-31 23:10:51 +07:00
* @return Footer
2013-12-16 06:40:30 -05:00
*/
2014-03-31 23:10:51 +07:00
public function addFooter()
2013-12-16 06:40:30 -05:00
{
$footer = new Footer($this->containerId);
2014-03-31 23:10:51 +07:00
$this->footer = $footer;
return $footer;
2013-12-16 06:40:30 -05:00
}
/**
2014-03-31 23:10:51 +07:00
* Get Headers
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-03-31 23:10:51 +07:00
* Get footer element
2013-12-16 06:40:30 -05:00
*
2014-03-31 23:10:51 +07:00
* @return Footer
2013-12-16 06:40:30 -05:00
*/
2014-03-31 23:10:51 +07:00
public function getFooter()
2013-12-16 06:40:30 -05:00
{
2014-03-31 23:10:51 +07:00
return $this->footer;
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-03-31 23:10:51 +07:00
$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;
}
/**
2014-03-31 23:10:51 +07:00
* Create header
2013-12-16 06:40:30 -05:00
*
2014-03-31 23:10:51 +07:00
* @deprecated 0.9.2
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
*
2014-03-31 23:10:51 +07:00
* @deprecated 0.9.2
*/
2014-03-31 23:10:51 +07:00
public function createFooter()
{
2014-03-31 23:10:51 +07:00
return $this->addFooter();
}
}