2012-05-06 18:25:40 +02: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.
|
2012-05-06 18:25:40 +02: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
|
2012-05-06 18:25:40 +02:00
|
|
|
*/
|
|
|
|
|
|
2014-04-08 00:23:49 +07:00
|
|
|
namespace PhpOffice\PhpWord\Element;
|
2014-03-31 01:13:02 +07:00
|
|
|
|
2014-05-04 00:57:44 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
|
|
|
|
use PhpOffice\PhpWord\Style\Section as SectionSettings;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Section
|
|
|
|
|
*/
|
2014-04-26 13:29:43 +07:00
|
|
|
class Section extends AbstractContainer
|
2013-12-16 06:40:30 -05:00
|
|
|
{
|
2014-05-11 22:54:51 +07:00
|
|
|
/**
|
|
|
|
|
* @var string Container type
|
|
|
|
|
*/
|
|
|
|
|
protected $container = 'Section';
|
|
|
|
|
|
2013-12-16 06:40:30 -05:00
|
|
|
/**
|
|
|
|
|
* Section settings
|
|
|
|
|
*
|
2014-04-16 17:22:30 +04:00
|
|
|
* @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)
|
|
|
|
|
{
|
2014-04-05 19:02:49 +07:00
|
|
|
$this->sectionId = $sectionCount;
|
|
|
|
|
$this->setDocPart($this->container, $this->sectionId);
|
2014-04-08 00:23:49 +07:00
|
|
|
$this->settings = new SectionSettings();
|
2014-03-11 16:27:42 +07:00
|
|
|
$this->setSettings($settings);
|
|
|
|
|
}
|
2013-12-16 06:40:30 -05:00
|
|
|
|
2014-03-11 16:27:42 +07:00
|
|
|
/**
|
2014-03-31 23:10:51 +07:00
|
|
|
* Set section settings
|
2014-03-11 16:27:42 +07:00
|
|
|
*
|
2014-03-31 23:10:51 +07:00
|
|
|
* @param array $settings
|
2014-03-11 16:27:42 +07:00
|
|
|
*/
|
|
|
|
|
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) {
|
2014-04-07 18:06:42 +07:00
|
|
|
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
|
|
|
|
|
*
|
2014-04-16 17:22:30 +04:00
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
2014-05-04 00:57:44 +07:00
|
|
|
/**
|
|
|
|
|
* Add a Title Element
|
|
|
|
|
*
|
|
|
|
|
* @param string $text
|
|
|
|
|
* @param int $depth
|
|
|
|
|
* @return \PhpOffice\PhpWord\Element\Title
|
|
|
|
|
*/
|
|
|
|
|
public function addTitle($text, $depth = 1)
|
|
|
|
|
{
|
2014-05-11 22:54:51 +07:00
|
|
|
return $this->addElement('Title', $text, $depth);
|
2014-05-04 00:57:44 +07:00
|
|
|
}
|
|
|
|
|
|
2013-12-16 06:40:30 -05:00
|
|
|
/**
|
|
|
|
|
* Add a PageBreak Element
|
|
|
|
|
*/
|
|
|
|
|
public function addPageBreak()
|
|
|
|
|
{
|
2014-05-11 22:54:51 +07:00
|
|
|
return $this->addElement('PageBreak');
|
2014-05-04 00:57:44 +07:00
|
|
|
}
|
|
|
|
|
|
2013-12-16 06:40:30 -05:00
|
|
|
/**
|
|
|
|
|
* Add a Table-of-Contents Element
|
|
|
|
|
*
|
2014-05-01 14:37:58 +07:00
|
|
|
* @param mixed $fontStyle
|
|
|
|
|
* @param mixed $tocStyle
|
2014-04-06 15:19:09 +07:00
|
|
|
* @param integer $minDepth
|
|
|
|
|
* @param integer $maxDepth
|
2014-04-25 10:33:45 +07:00
|
|
|
* @return \PhpOffice\PhpWord\Element\TOC
|
2013-12-16 06:40:30 -05:00
|
|
|
*/
|
2014-05-01 14:37:58 +07:00
|
|
|
public function addTOC($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
|
2013-12-16 06:40:30 -05:00
|
|
|
{
|
2014-05-11 22:54:51 +07:00
|
|
|
return $this->addElement('TOC', $fontStyle, $tocStyle, $minDepth, $maxDepth);
|
2013-12-16 06:40:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
2014-04-11 19:04:53 +07:00
|
|
|
* @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
|
2014-04-11 19:04:53 +07:00
|
|
|
* @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-15 12:56:40 +01:00
|
|
|
}
|
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?
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* 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
|
2014-04-10 11:06:39 +04:00
|
|
|
* @param boolean $header
|
2014-04-05 19:02:49 +07:00
|
|
|
* @return Header|Footer
|
2014-04-16 17:22:30 +04:00
|
|
|
* @throws \PhpOffice\PhpWord\Exception\Exception
|
2014-04-11 19:04:53 +07:00
|
|
|
* @since 0.10.0
|
2014-04-05 19:02:49 +07:00
|
|
|
*/
|
|
|
|
|
private function addHeaderFooter($type = Header::AUTO, $header = true)
|
|
|
|
|
{
|
2014-05-08 19:25:29 +07:00
|
|
|
$containerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' .
|
|
|
|
|
($header ? 'Header' : 'Footer');
|
2014-04-05 19:02:49 +07:00
|
|
|
$collectionArray = $header ? 'headers' : 'footers';
|
|
|
|
|
$collection = &$this->$collectionArray;
|
|
|
|
|
|
|
|
|
|
if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {
|
|
|
|
|
$index = count($collection);
|
|
|
|
|
$container = new $containerClass($this->sectionId, ++$index, $type);
|
2014-05-04 00:57:44 +07:00
|
|
|
$container->setPhpWord($this->phpWord);
|
|
|
|
|
|
2014-04-05 19:02:49 +07:00
|
|
|
$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
|
|
|
*
|
2014-04-05 00:08:00 +07:00
|
|
|
* @return Header
|
2014-04-11 19:04:53 +07:00
|
|
|
* @deprecated 0.10.0
|
2014-04-03 11:34:06 +07:00
|
|
|
* @codeCoverageIgnore
|
2013-12-16 06:40:30 -05:00
|
|
|
*/
|
2014-03-31 23:10:51 +07:00
|
|
|
public function createHeader()
|
2014-03-11 19:26:56 +07:00
|
|
|
{
|
2014-03-31 23:10:51 +07:00
|
|
|
return $this->addHeader();
|
2014-03-07 23:08:09 +01:00
|
|
|
}
|
2014-03-30 01:30:25 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-03-31 23:10:51 +07:00
|
|
|
* Create footer
|
2014-03-30 01:30:25 +07:00
|
|
|
*
|
2014-04-05 00:08:00 +07:00
|
|
|
* @return Footer
|
2014-04-11 19:04:53 +07:00
|
|
|
* @deprecated 0.10.0
|
2014-04-03 11:34:06 +07:00
|
|
|
* @codeCoverageIgnore
|
2014-03-30 01:30:25 +07:00
|
|
|
*/
|
2014-03-31 23:10:51 +07:00
|
|
|
public function createFooter()
|
2014-03-30 01:30:25 +07:00
|
|
|
{
|
2014-03-31 23:10:51 +07:00
|
|
|
return $this->addFooter();
|
2014-03-30 01:30:25 +07:00
|
|
|
}
|
2014-04-05 19:02:49 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get footer
|
|
|
|
|
*
|
|
|
|
|
* @return Footer
|
2014-04-11 19:04:53 +07:00
|
|
|
* @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];
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-23 17:37:26 +07:00
|
|
|
}
|