Section.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 = utf8_encode($text);
  101. //$givenText = iconv('gbk', 'utf-8', $text);
  102. //$givenText = iconv('GB2312', 'utf-8//IGNORE', $text);
  103. $givenText = PHPWord_Media::GBKToUTF8($text);
  104. $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph);
  105. $this->_elementCollection[] = $text;
  106. return $text;
  107. }
  108. /**
  109. * Add a Link Element
  110. *
  111. * @param string $linkSrc
  112. * @param string $linkName
  113. * @param mixed $styleFont
  114. * @param mixed $styleParagraph
  115. * @return PHPWord_Section_Link
  116. */
  117. public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) {
  118. //$linkSrc = utf8_encode($linkSrc);
  119. //$linkSrc = iconv('gbk', 'utf-8', $linkSrc);
  120. //$linkSrc = iconv('GB2312', 'utf-8//IGNORE', $linkSrc);
  121. $linkSrc = PHPWord_Media::GBKToUTF8($linkSrc);
  122. if(!is_null($linkName)) {
  123. //$linkName = utf8_encode($linkName);
  124. //$linkName = iconv('gbk', 'utf-8', $linkName);
  125. //$linkName = iconv('GB2312', 'utf-8//IGNORE', $linkName);
  126. $linkName = PHPWord_Media::GBKToUTF8($linkName);
  127. }
  128. $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph);
  129. $rID = PHPWord_Media::addSectionLinkElement($linkSrc);
  130. $link->setRelationId($rID);
  131. $this->_elementCollection[] = $link;
  132. return $link;
  133. }
  134. /**
  135. * Add a TextBreak Element
  136. *
  137. * @param int $count
  138. */
  139. public function addTextBreak($count = 1) {
  140. for($i=1; $i<=$count; $i++) {
  141. $this->_elementCollection[] = new PHPWord_Section_TextBreak();
  142. }
  143. }
  144. /**
  145. * Add a PageBreak Element
  146. */
  147. public function addPageBreak() {
  148. $this->_elementCollection[] = new PHPWord_Section_PageBreak();
  149. }
  150. /**
  151. * Add a Table Element
  152. *
  153. * @param mixed $style
  154. * @return PHPWord_Section_Table
  155. */
  156. public function addTable($style = null) {
  157. $table = new PHPWord_Section_Table('section', $this->_sectionCount, $style);
  158. $this->_elementCollection[] = $table;
  159. return $table;
  160. }
  161. /**
  162. * Add a ListItem Element
  163. *
  164. * @param string $text
  165. * @param int $depth
  166. * @param mixed $styleFont
  167. * @param mixed $styleList
  168. * @param mixed $styleParagraph
  169. * @return PHPWord_Section_ListItem
  170. */
  171. public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) {
  172. //$text = utf8_encode($text);
  173. //$text = iconv('gbk', 'utf-8', $text);
  174. //$text = iconv('GB2312', 'utf-8//IGNORE', $text);
  175. $text = PHPWord_Media::GBKToUTF8($text);
  176. $listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph);
  177. $this->_elementCollection[] = $listItem;
  178. return $listItem;
  179. }
  180. /**
  181. * Add a OLE-Object Element
  182. *
  183. * @param string $src
  184. * @param mixed $style
  185. * @return PHPWord_Section_Object
  186. */
  187. public function addObject($src, $style = null) {
  188. $object = new PHPWord_Section_Object($src, $style);
  189. if(!is_null($object->getSource())) {
  190. $inf = pathinfo($src);
  191. $ext = $inf['extension'];
  192. if(strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
  193. $ext = substr($ext, 0, -1);
  194. }
  195. $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/';
  196. if(!file_exists($iconSrc.'_'.$ext.'.png')) {
  197. $iconSrc = $iconSrc.'_default.png';
  198. } else {
  199. $iconSrc .= '_'.$ext.'.png';
  200. }
  201. $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image');
  202. $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject');
  203. $rID = $data[0];
  204. $objectId = $data[1];
  205. $object->setRelationId($rID);
  206. $object->setObjectId($objectId);
  207. $object->setImageRelationId($rIDimg);
  208. $this->_elementCollection[] = $object;
  209. return $object;
  210. } else {
  211. trigger_error('Source does not exist or unsupported object type.');
  212. }
  213. }
  214. /**
  215. * Add a Image Element
  216. *
  217. * @param string $src
  218. * @param mixed $style
  219. * @return PHPWord_Section_Image
  220. */
  221. public function addImage($src, $style = null) {
  222. $image = new PHPWord_Section_Image($src, $style);
  223. if(!is_null($image->getSource())) {
  224. $rID = PHPWord_Media::addSectionMediaElement($src, 'image');
  225. $image->setRelationId($rID);
  226. $this->_elementCollection[] = $image;
  227. return $image;
  228. } else {
  229. trigger_error('Source does not exist or unsupported image type.');
  230. }
  231. }
  232. /**
  233. * Add a by PHP created Image Element
  234. *
  235. * @param string $link
  236. * @param mixed $style
  237. * @return PHPWord_Section_MemoryImage
  238. */
  239. public function addMemoryImage($link, $style = null) {
  240. $memoryImage = new PHPWord_Section_MemoryImage($link, $style);
  241. if(!is_null($memoryImage->getSource())) {
  242. $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage);
  243. $memoryImage->setRelationId($rID);
  244. $this->_elementCollection[] = $memoryImage;
  245. return $memoryImage;
  246. } else {
  247. trigger_error('Unsupported image type.');
  248. }
  249. }
  250. /**
  251. * Add a Table-of-Contents Element
  252. *
  253. * @param mixed $styleFont
  254. * @param mixed $styleTOC
  255. * @return PHPWord_TOC
  256. */
  257. public function addTOC($styleFont = null, $styleTOC = null) {
  258. $toc = new PHPWord_TOC($styleFont, $styleTOC);
  259. $this->_elementCollection[] = $toc;
  260. return $toc;
  261. }
  262. /**
  263. * Add a Title Element
  264. *
  265. * @param string $text
  266. * @param int $depth
  267. * @return PHPWord_Section_Title
  268. */
  269. public function addTitle($text, $depth = 1) {
  270. //$text = utf8_encode($text);
  271. //$text = iconv('gbk', 'utf-8', $text);
  272. //$text = iconv('GB2312', 'utf-8//IGNORE', $text);
  273. $text = PHPWord_Media::GBKToUTF8($text);
  274. $styles = PHPWord_Style::getStyles();
  275. if(array_key_exists('Heading_'.$depth, $styles)) {
  276. $style = 'Heading'.$depth;
  277. } else {
  278. $style = null;
  279. }
  280. $title = new PHPWord_Section_Title($text, $depth, $style);
  281. $data = PHPWord_TOC::addTitle($text, $depth);
  282. $anchor = $data[0];
  283. $bookmarkId = $data[1];
  284. $title->setAnchor($anchor);
  285. $title->setBookmarkId($bookmarkId);
  286. $this->_elementCollection[] = $title;
  287. return $title;
  288. }
  289. /**
  290. * Create a new TextRun
  291. *
  292. * @return PHPWord_Section_TextRun
  293. */
  294. public function createTextRun($styleParagraph = null) {
  295. $textRun = new PHPWord_Section_TextRun($styleParagraph);
  296. $this->_elementCollection[] = $textRun;
  297. return $textRun;
  298. }
  299. /**
  300. * Get all Elements
  301. *
  302. * @return array
  303. */
  304. public function getElements() {
  305. return $this->_elementCollection;
  306. }
  307. /**
  308. * Create a new Header
  309. *
  310. * @return PHPWord_Section_Header
  311. */
  312. public function createHeader() {
  313. $header = new PHPWord_Section_Header($this->_sectionCount);
  314. $this->_header = $header;
  315. return $header;
  316. }
  317. /**
  318. * Get Header
  319. *
  320. * @return PHPWord_Section_Header
  321. */
  322. public function getHeader() {
  323. return $this->_header;
  324. }
  325. /**
  326. * Create a new Footer
  327. *
  328. * @return PHPWord_Section_Footer
  329. */
  330. public function createFooter() {
  331. $footer = new PHPWord_Section_Footer($this->_sectionCount);
  332. $this->_footer = $footer;
  333. return $footer;
  334. }
  335. /**
  336. * Get Footer
  337. *
  338. * @return PHPWord_Section_Footer
  339. */
  340. public function getFooter() {
  341. return $this->_footer;
  342. }
  343. }
  344. ?>