No Description

Section.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Element;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. use PhpOffice\PhpWord\Style\Section as SectionStyle;
  20. /**
  21. * Section
  22. */
  23. class Section extends AbstractContainer
  24. {
  25. /**
  26. * @var string Container type
  27. */
  28. protected $container = 'Section';
  29. /**
  30. * Section style
  31. *
  32. * @var \PhpOffice\PhpWord\Style\Section
  33. */
  34. private $style;
  35. /**
  36. * Section headers, indexed from 1, not zero
  37. *
  38. * @var Header[]
  39. */
  40. private $headers = array();
  41. /**
  42. * Section footers, indexed from 1, not zero
  43. *
  44. * @var Footer[]
  45. */
  46. private $footers = array();
  47. /**
  48. * Create new instance
  49. *
  50. * @param int $sectionCount
  51. * @param array $style
  52. */
  53. public function __construct($sectionCount, $style = null)
  54. {
  55. $this->sectionId = $sectionCount;
  56. $this->setDocPart($this->container, $this->sectionId);
  57. $this->style = new SectionStyle();
  58. $this->setStyle($style);
  59. }
  60. /**
  61. * Set section style.
  62. *
  63. * @param array $style
  64. * @return void
  65. */
  66. public function setStyle($style = null)
  67. {
  68. if (!is_null($style) && is_array($style)) {
  69. $this->style->setStyleByArray($style);
  70. }
  71. }
  72. /**
  73. * Get section style
  74. *
  75. * @return \PhpOffice\PhpWord\Style\Section
  76. */
  77. public function getStyle()
  78. {
  79. return $this->style;
  80. }
  81. /**
  82. * Add header
  83. *
  84. * @param string $type
  85. * @return Header
  86. * @since 0.10.0
  87. */
  88. public function addHeader($type = Header::AUTO)
  89. {
  90. return $this->addHeaderFooter($type, true);
  91. }
  92. /**
  93. * Add footer
  94. *
  95. * @param string $type
  96. * @return Footer
  97. * @since 0.10.0
  98. */
  99. public function addFooter($type = Header::AUTO)
  100. {
  101. return $this->addHeaderFooter($type, false);
  102. }
  103. /**
  104. * Get header elements
  105. *
  106. * @return Header[]
  107. */
  108. public function getHeaders()
  109. {
  110. return $this->headers;
  111. }
  112. /**
  113. * Get footer elements
  114. *
  115. * @return Footer[]
  116. */
  117. public function getFooters()
  118. {
  119. return $this->footers;
  120. }
  121. /**
  122. * Is there a header for this section that is for the first page only?
  123. *
  124. * If any of the Header instances have a type of Header::FIRST then this method returns true.
  125. * False otherwise.
  126. *
  127. * @return boolean
  128. */
  129. public function hasDifferentFirstPage()
  130. {
  131. foreach ($this->headers as $header) {
  132. if ($header->getType() == Header::FIRST) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. /**
  139. * Add header/footer
  140. *
  141. * @param string $type
  142. * @param boolean $header
  143. * @return Header|Footer
  144. * @throws \PhpOffice\PhpWord\Exception\Exception
  145. * @since 0.10.0
  146. */
  147. private function addHeaderFooter($type = Header::AUTO, $header = true)
  148. {
  149. $containerClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' .
  150. ($header ? 'Header' : 'Footer');
  151. $collectionArray = $header ? 'headers' : 'footers';
  152. $collection = &$this->$collectionArray;
  153. if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {
  154. $index = count($collection);
  155. /** @var \PhpOffice\PhpWord\Element\AbstractContainer $container Type hint */
  156. $container = new $containerClass($this->sectionId, ++$index, $type);
  157. $container->setPhpWord($this->phpWord);
  158. $collection[$index] = $container;
  159. return $container;
  160. } else {
  161. throw new Exception('Invalid header/footer type.');
  162. }
  163. }
  164. /**
  165. * Set section style
  166. *
  167. * @param array $settings
  168. * @deprecated 0.12.0
  169. * @codeCoverageIgnore
  170. */
  171. public function setSettings($settings = null)
  172. {
  173. $this->setStyle($settings);
  174. }
  175. /**
  176. * Get section style
  177. *
  178. * @return \PhpOffice\PhpWord\Style\Section
  179. * @deprecated 0.12.0
  180. * @codeCoverageIgnore
  181. */
  182. public function getSettings()
  183. {
  184. return $this->getStyle();
  185. }
  186. /**
  187. * Create header
  188. *
  189. * @return Header
  190. * @deprecated 0.10.0
  191. * @codeCoverageIgnore
  192. */
  193. public function createHeader()
  194. {
  195. return $this->addHeader();
  196. }
  197. /**
  198. * Create footer
  199. *
  200. * @return Footer
  201. * @deprecated 0.10.0
  202. * @codeCoverageIgnore
  203. */
  204. public function createFooter()
  205. {
  206. return $this->addFooter();
  207. }
  208. /**
  209. * Get footer
  210. *
  211. * @return Footer
  212. * @deprecated 0.10.0
  213. * @codeCoverageIgnore
  214. */
  215. public function getFooter()
  216. {
  217. if (empty($this->footers)) {
  218. return null;
  219. } else {
  220. return $this->footers[1];
  221. }
  222. }
  223. }