PHPWord/src/PhpWord/Element/Section.php

292 lines
6.2 KiB
PHP
Raw Normal View History

2012-05-06 18:25:40 +02:00
<?php
/**
* 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
*
* @see https://github.com/PHPOffice/PHPWord
2018-03-08 23:52:25 +01:00
* @copyright 2010-2018 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
*/
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\ComplexType\FootnoteProperties;
use PhpOffice\PhpWord\Style\Section as SectionStyle;
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 style
2013-12-16 06:40:30 -05:00
*
* @var \PhpOffice\PhpWord\Style\Section
2013-12-16 06:40:30 -05:00
*/
private $style;
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
/**
* The properties for the footnote of this section
*
* @var FootnoteProperties
*/
private $footnoteProperties;
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
* @param array $style
2013-12-16 06:40:30 -05:00
*/
public function __construct($sectionCount, $style = null)
2013-12-16 06:40:30 -05:00
{
2014-04-05 19:02:49 +07:00
$this->sectionId = $sectionCount;
$this->setDocPart($this->container, $this->sectionId);
$this->style = new SectionStyle();
$this->setStyle($style);
}
2013-12-16 06:40:30 -05:00
/**
* Set section style.
*
* @param array $style
*/
public function setStyle($style = null)
{
if (!is_null($style) && is_array($style)) {
$this->style->setStyleByArray($style);
2013-12-16 06:40:30 -05:00
}
}
/**
* Get section style
2013-12-16 06:40:30 -05:00
*
* @return \PhpOffice\PhpWord\Style\Section
2013-12-16 06:40:30 -05:00
*/
public function getStyle()
2013-12-16 06:40:30 -05:00
{
return $this->style;
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
*
* @since 0.10.0
*
2014-04-05 19:02:49 +07:00
* @param string $type
*
2014-03-31 23:10:51 +07:00
* @return Header
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
*
* @since 0.10.0
*
2014-04-05 19:02:49 +07:00
* @param string $type
*
2014-03-31 23:10:51 +07:00
* @return Footer
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
}
/**
* Get the footnote properties
*
2017-11-26 22:55:37 +01:00
* @return FootnoteProperties
*/
public function getFootnotePropoperties()
{
return $this->footnoteProperties;
}
/**
* Set the footnote properties
*
* @param FootnoteProperties $footnoteProperties
*/
public function setFootnoteProperties(FootnoteProperties $footnoteProperties = null)
{
$this->footnoteProperties = $footnoteProperties;
}
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
*
* @return bool
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;
}
}
2017-08-02 10:52:42 +02:00
foreach ($this->footers as $footer) {
if ($footer->getType() == Header::FIRST) {
return true;
}
}
2014-04-05 19:02:49 +07:00
return false;
}
/**
* Add header/footer
*
* @since 0.10.0
*
2014-04-05 19:02:49 +07:00
* @param string $type
* @param bool $header
*
* @throws \Exception
*
* @return Header|Footer
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;
2014-04-05 19:02:49 +07:00
if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {
$index = count($collection);
2014-05-19 00:14:14 +07:00
/** @var \PhpOffice\PhpWord\Element\AbstractContainer $container Type hint */
2014-04-05 19:02:49 +07:00
$container = new $containerClass($this->sectionId, ++$index, $type);
$container->setPhpWord($this->phpWord);
2014-04-05 19:02:49 +07:00
$collection[$index] = $container;
2014-04-05 19:02:49 +07:00
return $container;
}
throw new \Exception('Invalid header/footer type.');
2013-12-16 06:40:30 -05:00
}
/**
* Set section style
*
* @deprecated 0.12.0
*
* @param array $settings
*
* @codeCoverageIgnore
*/
public function setSettings($settings = null)
{
$this->setStyle($settings);
}
/**
* Get section style
*
* @deprecated 0.12.0
*
* @return \PhpOffice\PhpWord\Style\Section
*
* @codeCoverageIgnore
*/
public function getSettings()
{
return $this->getStyle();
}
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
*
* @deprecated 0.10.0
*
* @return Header
*
* @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
*
* @deprecated 0.10.0
*
* @return Footer
*
* @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
*
* @deprecated 0.10.0
*
* @return Footer
*
2014-04-05 19:02:49 +07:00
* @codeCoverageIgnore
*/
public function getFooter()
{
if (empty($this->footers)) {
return null;
}
return $this->footers[1];
2014-04-05 19:02:49 +07:00
}
}