Section.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Element;
  18. use PhpOffice\PhpWord\ComplexType\FootnoteProperties;
  19. use PhpOffice\PhpWord\Style\Section as SectionStyle;
  20. class Section extends AbstractContainer
  21. {
  22. /**
  23. * @var string Container type
  24. */
  25. protected $container = 'Section';
  26. /**
  27. * Section style
  28. *
  29. * @var \PhpOffice\PhpWord\Style\Section
  30. */
  31. private $style;
  32. /**
  33. * Section headers, indexed from 1, not zero
  34. *
  35. * @var Header[]
  36. */
  37. private $headers = array();
  38. /**
  39. * Section footers, indexed from 1, not zero
  40. *
  41. * @var Footer[]
  42. */
  43. private $footers = array();
  44. /**
  45. * The properties for the footnote of this section
  46. *
  47. * @var FootnoteProperties
  48. */
  49. private $footnoteProperties;
  50. /**
  51. * Create new instance
  52. *
  53. * @param int $sectionCount
  54. * @param null|array|\PhpOffice\PhpWord\Style $style
  55. */
  56. public function __construct($sectionCount, $style = null)
  57. {
  58. $this->sectionId = $sectionCount;
  59. $this->setDocPart($this->container, $this->sectionId);
  60. if (null === $style) {
  61. $style = new SectionStyle();
  62. }
  63. $this->style = $this->setNewStyle(new SectionStyle(), $style);
  64. }
  65. /**
  66. * Set section style.
  67. *
  68. * @param array $style
  69. */
  70. public function setStyle($style = null)
  71. {
  72. if (!is_null($style) && is_array($style)) {
  73. $this->style->setStyleByArray($style);
  74. }
  75. }
  76. /**
  77. * Get section style
  78. *
  79. * @return \PhpOffice\PhpWord\Style\Section
  80. */
  81. public function getStyle()
  82. {
  83. return $this->style;
  84. }
  85. /**
  86. * Add header
  87. *
  88. * @since 0.10.0
  89. *
  90. * @param string $type
  91. *
  92. * @return Header
  93. */
  94. public function addHeader($type = Header::AUTO)
  95. {
  96. return $this->addHeaderFooter($type, true);
  97. }
  98. /**
  99. * Add footer
  100. *
  101. * @since 0.10.0
  102. *
  103. * @param string $type
  104. *
  105. * @return Footer
  106. */
  107. public function addFooter($type = Header::AUTO)
  108. {
  109. return $this->addHeaderFooter($type, false);
  110. }
  111. /**
  112. * Get header elements
  113. *
  114. * @return Header[]
  115. */
  116. public function getHeaders()
  117. {
  118. return $this->headers;
  119. }
  120. /**
  121. * Get footer elements
  122. *
  123. * @return Footer[]
  124. */
  125. public function getFooters()
  126. {
  127. return $this->footers;
  128. }
  129. /**
  130. * Get the footnote properties
  131. *
  132. * @return FootnoteProperties
  133. */
  134. public function getFootnoteProperties()
  135. {
  136. return $this->footnoteProperties;
  137. }
  138. /**
  139. * Get the footnote properties
  140. *
  141. * @deprecated Use the `getFootnoteProperties` method instead
  142. *
  143. * @return FootnoteProperties
  144. *
  145. * @codeCoverageIgnore
  146. */
  147. public function getFootnotePropoperties()
  148. {
  149. return $this->footnoteProperties;
  150. }
  151. /**
  152. * Set the footnote properties
  153. *
  154. * @param FootnoteProperties $footnoteProperties
  155. */
  156. public function setFootnoteProperties(FootnoteProperties $footnoteProperties = null)
  157. {
  158. $this->footnoteProperties = $footnoteProperties;
  159. }
  160. /**
  161. * Is there a header for this section that is for the first page only?
  162. *
  163. * If any of the Header instances have a type of Header::FIRST then this method returns true.
  164. * False otherwise.
  165. *
  166. * @return bool
  167. */
  168. public function hasDifferentFirstPage()
  169. {
  170. foreach ($this->headers as $header) {
  171. if ($header->getType() == Header::FIRST) {
  172. return true;
  173. }
  174. }
  175. foreach ($this->footers as $footer) {
  176. if ($footer->getType() == Header::FIRST) {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. /**
  183. * Add header/footer
  184. *
  185. * @since 0.10.0
  186. *
  187. * @param string $type
  188. * @param bool $header
  189. *
  190. * @throws \Exception
  191. *
  192. * @return Header|Footer
  193. */
  194. private function addHeaderFooter($type = Header::AUTO, $header = true)
  195. {
  196. $containerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' .
  197. ($header ? 'Header' : 'Footer');
  198. $collectionArray = $header ? 'headers' : 'footers';
  199. $collection = &$this->$collectionArray;
  200. if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {
  201. $index = count($collection);
  202. /** @var \PhpOffice\PhpWord\Element\AbstractContainer $container Type hint */
  203. $container = new $containerClass($this->sectionId, ++$index, $type);
  204. $container->setPhpWord($this->phpWord);
  205. $collection[$index] = $container;
  206. return $container;
  207. }
  208. throw new \Exception('Invalid header/footer type.');
  209. }
  210. /**
  211. * Set section style
  212. *
  213. * @deprecated 0.12.0
  214. *
  215. * @param array $settings
  216. *
  217. * @codeCoverageIgnore
  218. */
  219. public function setSettings($settings = null)
  220. {
  221. $this->setStyle($settings);
  222. }
  223. /**
  224. * Get section style
  225. *
  226. * @deprecated 0.12.0
  227. *
  228. * @return \PhpOffice\PhpWord\Style\Section
  229. *
  230. * @codeCoverageIgnore
  231. */
  232. public function getSettings()
  233. {
  234. return $this->getStyle();
  235. }
  236. /**
  237. * Create header
  238. *
  239. * @deprecated 0.10.0
  240. *
  241. * @return Header
  242. *
  243. * @codeCoverageIgnore
  244. */
  245. public function createHeader()
  246. {
  247. return $this->addHeader();
  248. }
  249. /**
  250. * Create footer
  251. *
  252. * @deprecated 0.10.0
  253. *
  254. * @return Footer
  255. *
  256. * @codeCoverageIgnore
  257. */
  258. public function createFooter()
  259. {
  260. return $this->addFooter();
  261. }
  262. /**
  263. * Get footer
  264. *
  265. * @deprecated 0.10.0
  266. *
  267. * @return Footer
  268. *
  269. * @codeCoverageIgnore
  270. */
  271. public function getFooter()
  272. {
  273. if (empty($this->footers)) {
  274. return null;
  275. }
  276. return $this->footers[1];
  277. }
  278. }