Section.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /**
  28. * PHPWord_Section
  29. *
  30. * @category PHPWord
  31. * @package PHPWord_Section
  32. * @copyright Copyright (c) 2011 PHPWord
  33. */
  34. class PHPWord_Section {
  35. /**
  36. * Section count
  37. *
  38. * @var int
  39. */
  40. private $_sectionCount;
  41. /**
  42. * Section settings
  43. *
  44. * @var PHPWord_Section_Settings
  45. */
  46. private $_settings;
  47. /**
  48. * Section Element Collection
  49. *
  50. * @var array
  51. */
  52. private $_elementCollection = array();
  53. /**
  54. * Section Header
  55. *
  56. * @var PHPWord_Section_Header
  57. */
  58. private $_header = null;
  59. /**
  60. * Section Footer
  61. *
  62. * @var PHPWord_Section_Footer
  63. */
  64. private $_footer = null;
  65. /**
  66. * Create a new Section
  67. *
  68. * @param int $sectionCount
  69. * @param mixed $settings
  70. */
  71. public function __construct($sectionCount, $settings = null) {
  72. $this->_sectionCount = $sectionCount;
  73. $this->_settings = new PHPWord_Section_Settings();
  74. if(!is_null($settings) && is_array($settings)) {
  75. foreach($settings as $key => $value) {
  76. if(substr($key, 0, 1) != '_') {
  77. $key = '_'.$key;
  78. }
  79. $this->_settings->setSettingValue($key, $value);
  80. }
  81. }
  82. }
  83. /**
  84. * Get Section Settings
  85. *
  86. * @return PHPWord_Section_Settings
  87. */
  88. public function getSettings() {
  89. return $this->_settings;
  90. }
  91. /**
  92. * Add a Text Element
  93. *
  94. * @param string $text
  95. * @param mixed $styleFont
  96. * @param mixed $styleParagraph
  97. * @return PHPWord_Section_Text
  98. */
  99. public function addText($text, $styleFont = null, $styleParagraph = null) {
  100. $givenText = utf8encode_dummy($text);
  101. $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph);
  102. $this->_elementCollection[] = $text;
  103. return $text;
  104. }
  105. /**
  106. * Add a Link Element
  107. *
  108. * @param string $linkSrc
  109. * @param string $linkName
  110. * @param mixed $styleFont
  111. * @param mixed $styleParagraph
  112. * @return PHPWord_Section_Link
  113. */
  114. public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) {
  115. $linkSrc = utf8encode_dummy($linkSrc);
  116. if(!is_null($linkName)) {
  117. $linkName = utf8encode_dummy($linkName);
  118. }
  119. $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph);
  120. $rID = PHPWord_Media::addSectionLinkElement($linkSrc);
  121. $link->setRelationId($rID);
  122. $this->_elementCollection[] = $link;
  123. return $link;
  124. }
  125. /**
  126. * Add a TextBreak Element
  127. *
  128. * @param int $count
  129. */
  130. public function addTextBreak($count = 1) {
  131. for($i=1; $i<=$count; $i++) {
  132. $this->_elementCollection[] = new PHPWord_Section_TextBreak();
  133. }
  134. }
  135. /**
  136. * Add a PageBreak Element
  137. */
  138. public function addPageBreak() {
  139. $this->_elementCollection[] = new PHPWord_Section_PageBreak();
  140. }
  141. /**
  142. * Add a Table Element
  143. *
  144. * @param mixed $style
  145. * @return PHPWord_Section_Table
  146. */
  147. public function addTable($style = null) {
  148. $table = new PHPWord_Section_Table('section', $this->_sectionCount, $style, $this->_settings);
  149. $this->_elementCollection[] = $table;
  150. return $table;
  151. }
  152. /**
  153. * Add a ListItem Element
  154. *
  155. * @param string $text
  156. * @param int $depth
  157. * @param mixed $styleFont
  158. * @param mixed $styleList
  159. * @param mixed $styleParagraph
  160. * @return PHPWord_Section_ListItem
  161. */
  162. public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) {
  163. $text = utf8encode_dummy($text);
  164. $listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
  165. $this->_elementCollection[] = $listItem;
  166. return $listItem;
  167. }
  168. /**
  169. * Add a OLE-Object Element
  170. *
  171. * @param string $src
  172. * @param mixed $style
  173. * @return PHPWord_Section_Object
  174. */
  175. public function addObject($src, $style = null) {
  176. $object = new PHPWord_Section_Object($src, $style);
  177. if(!is_null($object->getSource())) {
  178. $inf = pathinfo($src);
  179. $ext = $inf['extension'];
  180. if(strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
  181. $ext = substr($ext, 0, -1);
  182. }
  183. $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/';
  184. if(!file_exists($iconSrc.'_'.$ext.'.png')) {
  185. $iconSrc = $iconSrc.'_default.png';
  186. } else {
  187. $iconSrc .= '_'.$ext.'.png';
  188. }
  189. $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image');
  190. $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject');
  191. $rID = $data[0];
  192. $objectId = $data[1];
  193. $object->setRelationId($rID);
  194. $object->setObjectId($objectId);
  195. $object->setImageRelationId($rIDimg);
  196. $this->_elementCollection[] = $object;
  197. return $object;
  198. } else {
  199. trigger_error('Source does not exist or unsupported object type.');
  200. }
  201. }
  202. /**
  203. * Add an Image Element
  204. *
  205. * @param string $src
  206. * @param mixed $style
  207. * @return PHPWord_Section_Image
  208. */
  209. public function addImage($src, $style = null) {
  210. $image = new PHPWord_Section_Image($src, $style);
  211. if(!is_null($image->getSource())) {
  212. $rID = PHPWord_Media::addSectionMediaElement($src, 'image');
  213. $image->setRelationId($rID);
  214. $this->_elementCollection[] = $image;
  215. return $image;
  216. } else {
  217. trigger_error('Source does not exist or unsupported image type.');
  218. }
  219. }
  220. /**
  221. * Add a by PHP created Image Element
  222. *
  223. * @param string $link
  224. * @param mixed $style
  225. * @return PHPWord_Section_MemoryImage
  226. */
  227. public function addMemoryImage($link, $style = null) {
  228. $memoryImage = new PHPWord_Section_MemoryImage($link, $style);
  229. if(!is_null($memoryImage->getSource())) {
  230. $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage);
  231. $memoryImage->setRelationId($rID);
  232. $this->_elementCollection[] = $memoryImage;
  233. return $memoryImage;
  234. } else {
  235. trigger_error('Unsupported image type.');
  236. }
  237. }
  238. /**
  239. * Add a Table-of-Contents Element
  240. *
  241. * @param mixed $styleFont
  242. * @param mixed $styleTOC
  243. * @return PHPWord_TOC
  244. */
  245. public function addTOC($styleFont = null, $styleTOC = null) {
  246. $toc = new PHPWord_TOC($styleFont, $styleTOC);
  247. $this->_elementCollection[] = $toc;
  248. return $toc;
  249. }
  250. /**
  251. * Add a Title Element
  252. *
  253. * @param string $text
  254. * @param int $depth
  255. * @return PHPWord_Section_Title
  256. */
  257. public function addTitle($text, $depth = 1) {
  258. $text = utf8encode_dummy($text);
  259. $styles = PHPWord_Style::getStyles();
  260. if(array_key_exists('Heading_'.$depth, $styles)) {
  261. $style = 'Heading'.$depth;
  262. } else {
  263. $style = null;
  264. }
  265. $title = new PHPWord_Section_Title($text, $depth, $style);
  266. $data = PHPWord_TOC::addTitle($text, $depth);
  267. $anchor = $data[0];
  268. $bookmarkId = $data[1];
  269. $title->setAnchor($anchor);
  270. $title->setBookmarkId($bookmarkId);
  271. $this->_elementCollection[] = $title;
  272. return $title;
  273. }
  274. /**
  275. * Create a new TextRun
  276. *
  277. * @return PHPWord_Section_TextRun
  278. */
  279. public function createTextRun($styleParagraph = null) {
  280. $textRun = new PHPWord_Section_TextRun($styleParagraph);
  281. $this->_elementCollection[] = $textRun;
  282. return $textRun;
  283. }
  284. /**
  285. * Get all Elements
  286. *
  287. * @return array
  288. */
  289. public function getElements() {
  290. return $this->_elementCollection;
  291. }
  292. /**
  293. * Create a new Header
  294. *
  295. * @return PHPWord_Section_Header
  296. */
  297. public function createHeader() {
  298. $header = new PHPWord_Section_Header($this->_sectionCount, $this->_settings);
  299. $this->_header = $header;
  300. return $header;
  301. }
  302. /**
  303. * Get Header
  304. *
  305. * @return PHPWord_Section_Header
  306. */
  307. public function getHeader() {
  308. return $this->_header;
  309. }
  310. /**
  311. * Create a new Footer
  312. *
  313. * @return PHPWord_Section_Footer
  314. */
  315. public function createFooter() {
  316. $footer = new PHPWord_Section_Footer($this->_sectionCount, $this->_settings);
  317. $this->_footer = $footer;
  318. return $footer;
  319. }
  320. /**
  321. * Get Footer
  322. *
  323. * @return PHPWord_Section_Footer
  324. */
  325. public function getFooter() {
  326. return $this->_footer;
  327. }
  328. }
  329. ?>