Cell.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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_Table_Cell
  29. *
  30. * @category PHPWord
  31. * @package PHPWord_Section_Table
  32. * @copyright Copyright (c) 2011 PHPWord
  33. */
  34. class PHPWord_Section_Table_Cell {
  35. /**
  36. * Cell Width
  37. *
  38. * @var int
  39. */
  40. private $_width = null;
  41. /**
  42. * Cell Style
  43. *
  44. * @var PHPWord_Style_Cell
  45. */
  46. private $_style;
  47. /**
  48. * Cell Element Collection
  49. *
  50. * @var array
  51. */
  52. private $_elementCollection = array();
  53. /**
  54. * Table holder
  55. *
  56. * @var string
  57. */
  58. private $_insideOf;
  59. /**
  60. * Section/Header/Footer count
  61. *
  62. * @var int
  63. */
  64. private $_pCount;
  65. /**
  66. * Create a new Table Cell
  67. *
  68. * @param string $insideOf
  69. * @param int $pCount
  70. * @param int $width
  71. * @param mixed $style
  72. */
  73. public function __construct($insideOf, $pCount, $width = null, $style = null) {
  74. $this->_insideOf = $insideOf;
  75. $this->_pCount = $pCount;
  76. $this->_width = $width;
  77. if(!is_null($style)) {
  78. if(is_array($style)) {
  79. $this->_style = new PHPWord_Style_Cell();
  80. foreach($style as $key => $value) {
  81. if(substr($key, 0, 1) != '_') {
  82. $key = '_'.$key;
  83. }
  84. $this->_style->setStyleValue($key, $value);
  85. }
  86. } else {
  87. $this->_style = $style;
  88. }
  89. }
  90. }
  91. /**
  92. * Add a Text Element
  93. *
  94. * @param string $text
  95. * @param mixed $style
  96. * @return PHPWord_Section_Text
  97. */
  98. public function addText($text, $styleFont = null, $styleParagraph = null) {
  99. //$text = utf8_encode($text);
  100. //$text = iconv('gbk', 'utf-8', $text);
  101. //$text = iconv('GB2312', 'utf-8//IGNORE', $text);
  102. $text = PHPWord_Media::GBKToUTF8($text);
  103. $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);
  104. $this->_elementCollection[] = $text;
  105. return $text;
  106. }
  107. /**
  108. * Add a Link Element
  109. *
  110. * @param string $linkSrc
  111. * @param string $linkName
  112. * @param mixed $style
  113. * @return PHPWord_Section_Link
  114. */
  115. public function addLink($linkSrc, $linkName = null, $style = null) {
  116. if($this->_insideOf == 'section') {
  117. //$linkSrc = utf8_encode($linkSrc);
  118. //$linkSrc = iconv('gbk', 'utf-8', $linkSrc);
  119. //$linkSrc = iconv('GB2312', 'utf-8//IGNORE', $linkSrc);
  120. $linkSrc = PHPWord_Media::GBKToUTF8($linkSrc);
  121. if(!is_null($linkName)) {
  122. //$linkName = utf8_encode($linkName);
  123. //$linkName = iconv('gbk', 'utf-8', $linkName);
  124. //$linkName = iconv('GB2312', 'utf-8//IGNORE', $linkName);
  125. $linkName = PHPWord_Media::GBKToUTF8($linkName);
  126. }
  127. $link = new PHPWord_Section_Link($linkSrc, $linkName, $style);
  128. $rID = PHPWord_Media::addSectionLinkElement($linkSrc);
  129. $link->setRelationId($rID);
  130. $this->_elementCollection[] = $link;
  131. return $link;
  132. } else {
  133. trigger_error('Unsupported Link header / footer reference');
  134. return false;
  135. }
  136. }
  137. /**
  138. * Add a TextBreak Element
  139. *
  140. * @param int $count
  141. */
  142. public function addTextBreak() {
  143. $this->_elementCollection[] = new PHPWord_Section_TextBreak();
  144. }
  145. /**
  146. * Add a ListItem Element
  147. *
  148. * @param string $text
  149. * @param int $depth
  150. * @param mixed $styleText
  151. * @param mixed $styleList
  152. * @return PHPWord_Section_ListItem
  153. */
  154. public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) {
  155. //$text = utf8_encode($text);
  156. //$text = iconv('gbk', 'utf-8', $text);
  157. //$text = iconv('GB2312', 'utf-8//IGNORE', $text);
  158. $text = PHPWord_Media::GBKToUTF8($text);
  159. $listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList);
  160. $this->_elementCollection[] = $listItem;
  161. return $listItem;
  162. }
  163. /**
  164. * Add a Image Element
  165. *
  166. * @param string $src
  167. * @param mixed $style
  168. * @return PHPWord_Section_Image
  169. */
  170. public function addImage($src, $style = null) {
  171. $image = new PHPWord_Section_Image($src, $style);
  172. if(!is_null($image->getSource())) {
  173. if($this->_insideOf == 'section') {
  174. $rID = PHPWord_Media::addSectionMediaElement($src, 'image');
  175. } elseif($this->_insideOf == 'header') {
  176. $rID = PHPWord_Media::addHeaderMediaElement($this->_pCount, $src);
  177. } elseif($this->_insideOf == 'footer') {
  178. $rID = PHPWord_Media::addFooterMediaElement($this->_pCount, $src);
  179. }
  180. $image->setRelationId($rID);
  181. $this->_elementCollection[] = $image;
  182. return $image;
  183. } else {
  184. trigger_error('Source does not exist or unsupported image type.');
  185. }
  186. }
  187. /**
  188. * Add a by PHP created Image Element
  189. *
  190. * @param string $link
  191. * @param mixed $style
  192. * @return PHPWord_Section_MemoryImage
  193. */
  194. public function addMemoryImage($link, $style = null) {
  195. $memoryImage = new PHPWord_Section_MemoryImage($link, $style);
  196. if(!is_null($memoryImage->getSource())) {
  197. if($this->_insideOf == 'section') {
  198. $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage);
  199. } elseif($this->_insideOf == 'header') {
  200. $rID = PHPWord_Media::addHeaderMediaElement($this->_pCount, $link, $memoryImage);
  201. } elseif($this->_insideOf == 'footer') {
  202. $rID = PHPWord_Media::addFooterMediaElement($this->_pCount, $link, $memoryImage);
  203. }
  204. $memoryImage->setRelationId($rID);
  205. $this->_elementCollection[] = $memoryImage;
  206. return $memoryImage;
  207. } else {
  208. trigger_error('Unsupported image type.');
  209. }
  210. }
  211. /**
  212. * Add a OLE-Object Element
  213. *
  214. * @param string $src
  215. * @param mixed $style
  216. * @return PHPWord_Section_Object
  217. */
  218. public function addObject($src, $style = null) {
  219. $object = new PHPWord_Section_Object($src, $style);
  220. if(!is_null($object->getSource())) {
  221. $inf = pathinfo($src);
  222. $ext = $inf['extension'];
  223. if(strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
  224. $ext = substr($ext, 0, -1);
  225. }
  226. $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/';
  227. if(!file_exists($iconSrc.'_'.$ext.'.png')) {
  228. $iconSrc = $iconSrc.'_default.png';
  229. } else {
  230. $iconSrc .= '_'.$ext.'.png';
  231. }
  232. $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image');
  233. $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject');
  234. $rID = $data[0];
  235. $objectId = $data[1];
  236. $object->setRelationId($rID);
  237. $object->setObjectId($objectId);
  238. $object->setImageRelationId($rIDimg);
  239. $this->_elementCollection[] = $object;
  240. return $object;
  241. } else {
  242. trigger_error('Source does not exist or unsupported object type.');
  243. }
  244. }
  245. /**
  246. * Add a PreserveText Element
  247. *
  248. * @param string $text
  249. * @param mixed $styleFont
  250. * @param mixed $styleParagraph
  251. * @return PHPWord_Section_Footer_PreserveText
  252. */
  253. public function addPreserveText($text, $styleFont = null, $styleParagraph = null) {
  254. if($this->_insideOf == 'footer' || $this->_insideOf == 'header') {
  255. //$text = utf8_encode($text);
  256. //$text = iconv('gbk', 'utf-8', $text);
  257. //$text = iconv('GB2312', 'utf-8//IGNORE', $text);
  258. $text = PHPWord_Media::GBKToUTF8($text);
  259. $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph);
  260. $this->_elementCollection[] = $ptext;
  261. return $ptext;
  262. } else {
  263. trigger_error('addPreserveText only supported in footer/header.');
  264. }
  265. }
  266. /**
  267. * Create a new TextRun
  268. *
  269. * @return PHPWord_Section_TextRun
  270. */
  271. public function createTextRun($styleParagraph = null) {
  272. $textRun = new PHPWord_Section_TextRun($styleParagraph);
  273. $this->_elementCollection[] = $textRun;
  274. return $textRun;
  275. }
  276. /**
  277. * Get all Elements
  278. *
  279. * @return array
  280. */
  281. public function getElements() {
  282. return $this->_elementCollection;
  283. }
  284. /**
  285. * Get Cell Style
  286. *
  287. * @return PHPWord_Style_Cell
  288. */
  289. public function getStyle() {
  290. return $this->_style;
  291. }
  292. /**
  293. * Get Cell width
  294. *
  295. * @return int
  296. */
  297. public function getWidth() {
  298. return $this->_width;
  299. }
  300. }
  301. ?>