CreateTable.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. /**
  3. * Create tables
  4. *
  5. * @category Phpdocx
  6. * @package elements
  7. * @copyright Copyright (c) 2009-2011 Narcea Producciones Multimedia S.L.
  8. * (http://www.2mdc.com)
  9. * @license LGPL
  10. * @version 1.0
  11. * @link http://www.phpdocx.com
  12. * @since File available since Release 1.0
  13. */
  14. include_once dirname(__FILE__) . '/CreateElement.inc';
  15. /**
  16. * Create tables
  17. *
  18. * @category Phpdocx
  19. * @package elements
  20. * @copyright Copyright (c) 2009-2011 Narcea Producciones Multimedia S.L.
  21. * (http://www.2mdc.com)
  22. * @license http://www.phpdocx.com/wp-content/themes/lightword/pro_license.php
  23. * @version 1.0
  24. * @link http://www.phpdocx.com
  25. * @since Class available since Release 1.0
  26. */
  27. class CreateTable extends CreateElement
  28. {
  29. /**
  30. * @access private
  31. * @var CreateTable
  32. * @static
  33. */
  34. private static $_instance = NULL;
  35. /**
  36. * Construct
  37. *
  38. * @access public
  39. */
  40. public function __construct()
  41. {
  42. }
  43. /**
  44. * Destruct
  45. *
  46. * @access public
  47. */
  48. public function __destruct()
  49. {
  50. }
  51. /**
  52. *
  53. * @access public
  54. * @return string
  55. */
  56. public function __toString()
  57. {
  58. $this->cleanTemplate();
  59. return $this->_xml;
  60. }
  61. /**
  62. *
  63. * @access public
  64. * @return CreateTable
  65. * @static
  66. */
  67. public static function getInstance()
  68. {
  69. if (self::$_instance == NULL) {
  70. self::$_instance = new CreateTable();
  71. }
  72. return self::$_instance;
  73. }
  74. /**
  75. * Create table
  76. *
  77. * @access public
  78. * @param array args[0]
  79. */
  80. public function createTable()
  81. {
  82. $this->_xml = '';
  83. $args = func_get_args();
  84. if (is_array($args[0])) {
  85. $this->generateTBL();
  86. $this->generateTBLPR();
  87. $this->generateTBLSTYLE();
  88. $this->generateTBLW();
  89. if (!empty($args[1]['border'])) {
  90. $this->generateTBLBORDERS();
  91. $this->generateTBLBOTTOM();
  92. $this->generateTBLLEFT();
  93. $this->generateTBLTOP();
  94. $this->generateTBLRIGHT();
  95. $this->generateTBLINSIDEH();
  96. $this->generateTBLINSIDEV();
  97. }
  98. $this->generateTBLLOOK();
  99. $this->generateTBLOVERLAP();
  100. $intLine = 0;
  101. foreach ($args[0] as $datDepth) {
  102. $this->generateTR();
  103. $intLine++;
  104. foreach ($datDepth as $cont) {
  105. $this->generateTC();
  106. $this->generateP();
  107. $this->generateR();
  108. if ($args[1]['font'] != '') {
  109. $this->generateRPR();
  110. $this->generateRFONTS($args[1]['font']);
  111. }
  112. $this->generateT($cont);
  113. }
  114. $this->cleanTemplateR();
  115. }
  116. }
  117. }
  118. /**
  119. * Add list
  120. *
  121. * @param string $list
  122. * @access protected
  123. */
  124. protected function addList($list)
  125. {
  126. $this->_xml = str_replace('__GENERATEP__', $list, $this->_xml);
  127. }
  128. /**
  129. * Generate w:gridcols
  130. *
  131. * @param string $w
  132. * @access protected
  133. */
  134. protected function generateGRIDCOLS($w)
  135. {
  136. $xml = '<' . CreateElement::NAMESPACEWORD . ':tblGrid ' .
  137. CreateElement::NAMESPACEWORD . ':w="' . $w . '"></' .
  138. CreateElement::NAMESPACEWORD . ':tblGrid>__GENERATEGRIDCOLS__';
  139. $this->_xml = str_replace('__GENERATEGRIDCOLS__', $xml, $this->_xml);
  140. }
  141. /**
  142. * Generate w:hmerge
  143. *
  144. * @access protected
  145. * @deprecated
  146. * @param string $val
  147. */
  148. protected function generateHMERGE($val = '')
  149. {
  150. }
  151. /**
  152. * Generate w:jc
  153. *
  154. * @param string $val
  155. * @access protected
  156. */
  157. protected function generateJC($val = '')
  158. {
  159. $xml = '<' . CreateElement::NAMESPACEWORD . ':jc ' .
  160. CreateElement::NAMESPACEWORD . ':val="' . $val . '"></' .
  161. CreateElement::NAMESPACEWORD . ':jc>';
  162. $this->_xml = str_replace('__GENERATEJC__', $xml, $this->_xml);
  163. }
  164. /**
  165. * Generate w:p
  166. *
  167. * @access protected
  168. */
  169. protected function generateP()
  170. {
  171. $xml = '<' . CreateElement::NAMESPACEWORD . ':p >__GENERATEP__</' .
  172. CreateElement::NAMESPACEWORD . ':p>';
  173. $this->_xml = str_replace('__GENERATETC__', $xml, $this->_xml);
  174. }
  175. /**
  176. * Generate w:shd
  177. *
  178. * @param string $val
  179. * @param string $color
  180. * @param string $fill
  181. * @param string $bgcolor
  182. * @param string $themeFill
  183. * @access protected
  184. */
  185. protected function generateSHD($val = 'horz-cross', $color = 'ff0000',
  186. $fill = '', $bgcolor = '', $themeFill = '')
  187. {
  188. $xmlAux = '<' . CreateElement::NAMESPACEWORD . ':shd w:val="' .
  189. $val . '"';
  190. if ($color != '') $xmlAux .= ' w:color="' . $color . '"';
  191. if ($fill != '') $xmlAux .= ' w:fill="' . $fill . '"';
  192. if ($bgcolor != '') $xmlAux .= ' wx:bgcolor="' . $bgcolor . '"';
  193. if ($themeFill != '') $xmlAux .= ' w:themeFill="' . $themeFill . '"';
  194. $xmlAux .= '></' . CreateElement::NAMESPACEWORD . ':shd>';
  195. $this->_xml = str_replace('__GENERATETCPR__', $xmlAux, $this->_xml);
  196. }
  197. /**
  198. * Generate w:tblborders
  199. *
  200. * @access protected
  201. */
  202. protected function generateTBLBORDERS()
  203. {
  204. $xml = '<' . CreateElement::NAMESPACEWORD .
  205. ':tblBorders>__GENERATETBLBORDER__</' .
  206. CreateElement::NAMESPACEWORD . ':tblBorders>';
  207. $this->_xml = str_replace('__GENERATETBLBORDERS__', $xml, $this->_xml);
  208. }
  209. /**
  210. * Generate w:tblbottom
  211. *
  212. * @param string $val
  213. * @param string $sz
  214. * @param string $space
  215. * @param string $color
  216. * @access protected
  217. */
  218. protected function generateTBLBOTTOM($val="single", $sz="4", $space='0',
  219. $color='auto')
  220. {
  221. $xml = '<' . CreateElement::NAMESPACEWORD .
  222. ':bottom ' . CreateElement::NAMESPACEWORD .
  223. ':val="' . $val . '" ' . CreateElement::NAMESPACEWORD .
  224. ':sz="' . $sz . '" ' . CreateElement::NAMESPACEWORD .
  225. ':space="' . $space . '" ' . CreateElement::NAMESPACEWORD .
  226. ':color="' . $color . '"></' . CreateElement::NAMESPACEWORD .
  227. ':bottom>__GENERATETBLBORDER__';
  228. $this->_xml = str_replace('__GENERATETBLBORDER__', $xml, $this->_xml);
  229. }
  230. /**
  231. * Generate w:tbl
  232. *
  233. * @access protected
  234. */
  235. protected function generateTBL()
  236. {
  237. $this->_xml = '<' . CreateElement::NAMESPACEWORD .
  238. ':tbl>__GENERATETBL__</' . CreateElement::NAMESPACEWORD .
  239. ':tbl>';
  240. }
  241. /**
  242. * Generate w:tblstyle
  243. *
  244. * @param string $strVal
  245. * @access protected
  246. */
  247. protected function generateTBLSTYLE($strVal = 'Tablaconcuadrcula')
  248. {
  249. $xml = '<' . CreateElement::NAMESPACEWORD .
  250. ':tblStyle ' . CreateElement::NAMESPACEWORD .
  251. ':val="' . $strVal . '"></' . CreateElement::NAMESPACEWORD .
  252. ':tblStyle>';
  253. $this->_xml = str_replace('__GENERATETBLSTYLE__', $xml, $this->_xml);
  254. }
  255. /**
  256. * Generate w:tblcellspacing
  257. *
  258. * @param string $w
  259. * @param string $type
  260. * @access protected
  261. */
  262. protected function generateTBLCELLSPACING($w = '', $type = '')
  263. {
  264. $xml = '<' . CreateElement::NAMESPACEWORD .
  265. ':tblCellSpacing ' . CreateElement::NAMESPACEWORD .
  266. ':w="' . $w . '" ' . CreateElement::NAMESPACEWORD .
  267. ':type="' . $type . '"></' . CreateElement::NAMESPACEWORD .
  268. ':tblCellSpacing>';
  269. $this->_xml = str_replace(
  270. '__GENERATETBLCELLSPACING__', $xml, $this->_xml
  271. );
  272. }
  273. /**
  274. * Generate w:tblgrid
  275. *
  276. * @access protected
  277. */
  278. protected function generateTBLGRID()
  279. {
  280. $xml = '<' . CreateElement::NAMESPACEWORD .
  281. ':tblGrid>__GENERATEGRIDCOLS__</' .
  282. CreateElement::NAMESPACEWORD .
  283. ':tblGrid>__GENERATETBL__';
  284. $this->_xml = str_replace('__GENERATETBL__', $xml, $this->_xml);
  285. }
  286. /**
  287. * Generate w:tblinsideh
  288. *
  289. * @param string $val
  290. * @param string $sz
  291. * @param string $space
  292. * @param string $color
  293. * @access protected
  294. */
  295. protected function generateTBLINSIDEH($val = "single", $sz = "4",
  296. $space = '0', $color = 'auto')
  297. {
  298. $xml = '<' . CreateElement::NAMESPACEWORD .
  299. ':insideH ' . CreateElement::NAMESPACEWORD .
  300. ':val="' . $val . '" ' . CreateElement::NAMESPACEWORD .
  301. ':sz="' . $sz . '" ' . CreateElement::NAMESPACEWORD .
  302. ':space="' . $space . '" ' . CreateElement::NAMESPACEWORD .
  303. ':color="' . $color . '"></' . CreateElement::NAMESPACEWORD .
  304. ':insideH>__GENERATETBLBORDER__';
  305. $this->_xml = str_replace('__GENERATETBLBORDER__', $xml, $this->_xml);
  306. }
  307. /**
  308. * Generate w:tblinsidev
  309. *
  310. * @param string $val
  311. * @param string $sz
  312. * @param string $space
  313. * @param string $color
  314. * @access protected
  315. */
  316. protected function generateTBLINSIDEV($val = "single", $sz= "4", $space='0',
  317. $color= 'auto')
  318. {
  319. $xml = '<' . CreateElement::NAMESPACEWORD . ':insideV ' .
  320. CreateElement::NAMESPACEWORD . ':val="' . $val . '" ' .
  321. CreateElement::NAMESPACEWORD . ':sz="' . $sz . '" ' .
  322. CreateElement::NAMESPACEWORD . ':space="' . $space . '" ' .
  323. CreateElement::NAMESPACEWORD . ':color="' . $color . '"></' .
  324. CreateElement::NAMESPACEWORD . ':insideV>__GENERATETBLBORDER__';
  325. $this->_xml = str_replace('__GENERATETBLBORDER__', $xml, $this->_xml);
  326. }
  327. /**
  328. * Generate w:tblleft
  329. *
  330. * @param string $val
  331. * @param string $sz
  332. * @param string $space
  333. * @param string $color
  334. * @access protected
  335. */
  336. protected function generateTBLLEFT($val = "single", $sz = "4", $space = '0',
  337. $color = 'auto')
  338. {
  339. $xml = '<' . CreateElement::NAMESPACEWORD . ':left ' .
  340. CreateElement::NAMESPACEWORD . ':val="' . $val . '" ' .
  341. CreateElement::NAMESPACEWORD . ':sz="' . $sz . '" ' .
  342. CreateElement::NAMESPACEWORD . ':space="' . $space . '" ' .
  343. CreateElement::NAMESPACEWORD . ':color="' . $color . '"></' .
  344. CreateElement::NAMESPACEWORD . ':left>__GENERATETBLBORDER__';
  345. $this->_xml = str_replace('__GENERATETBLBORDER__', $xml, $this->_xml);
  346. }
  347. /**
  348. * Generate w:tbllook
  349. *
  350. * @param string $val
  351. * @access protected
  352. */
  353. protected function generateTBLLOOK($val = '000001E0')
  354. {
  355. $xml = '<' . CreateElement::NAMESPACEWORD . ':tblLook ' .
  356. CreateElement::NAMESPACEWORD . ':val="' . $val . '"></' .
  357. CreateElement::NAMESPACEWORD . ':tblLook>';
  358. $this->_xml = str_replace('__GENERATETBLLOOK__', $xml, $this->_xml);
  359. }
  360. /**
  361. * Generate w:tbloverlap
  362. *
  363. * @param string $val
  364. * @access protected
  365. */
  366. protected function generateTBLOVERLAP($val = 'never')
  367. {
  368. $xml = '<' . CreateElement::NAMESPACEWORD . ':tblOverlap ' .
  369. CreateElement::NAMESPACEWORD . ':val="' . $val . '"></' .
  370. CreateElement::NAMESPACEWORD . ':tblOverlap>';
  371. $this->_xml = str_replace('__GENERATETBLOVERLAP__', $xml, $this->_xml);
  372. }
  373. /**
  374. * Generate w:tblpr
  375. *
  376. * @access protected
  377. */
  378. protected function generateTBLPR()
  379. {
  380. $xml = '<' . CreateElement::NAMESPACEWORD .
  381. ':tblPr>__GENERATETBLSTYLE____GENERA' .
  382. 'TEJC____GENERATETBLW____GENERATETBLL' .
  383. 'OOK____GENERATETBLOVERLAP____GENERATETBLCELL' .
  384. 'SPACING____GENERATETBLBORDERS____GENERATESHD__</' .
  385. CreateElement::NAMESPACEWORD . ':tblPr>__GENERATETBL__';
  386. $this->_xml = str_replace('__GENERATETBL__', $xml, $this->_xml);
  387. }
  388. /**
  389. * Generate w:tblright
  390. *
  391. * @param string $val
  392. * @param string $sz
  393. * @param string $space
  394. * @param string $color
  395. * @access protected
  396. */
  397. protected function generateTBLRIGHT($val = 'single', $sz = '4',
  398. $space = '0', $color = 'auto')
  399. {
  400. $xml = '<' . CreateElement::NAMESPACEWORD . ':right ' .
  401. CreateElement::NAMESPACEWORD . ':val="' . $val . '" ' .
  402. CreateElement::NAMESPACEWORD . ':sz="' . $sz . '" ' .
  403. CreateElement::NAMESPACEWORD . ':space="' . $space . '" ' .
  404. CreateElement::NAMESPACEWORD . ':color="' . $color . '"></' .
  405. CreateElement::NAMESPACEWORD . ':right>__GENERATETBLBORDER__';
  406. $this->_xml = str_replace('__GENERATETBLBORDER__', $xml, $this->_xml);
  407. }
  408. /**
  409. * Generate w:tbltop
  410. *
  411. * @param string $val
  412. * @param string $sz
  413. * @param string $space
  414. * @param string $color
  415. * @access protected
  416. */
  417. protected function generateTBLTOP($val = 'single', $sz = '4', $space = '0',
  418. $color = 'auto')
  419. {
  420. $xml = '<' . CreateElement::NAMESPACEWORD . ':top ' .
  421. CreateElement::NAMESPACEWORD . ':val="' . $val . '" ' .
  422. CreateElement::NAMESPACEWORD . ':sz="' . $sz . '" ' .
  423. CreateElement::NAMESPACEWORD . ':space="' . $space . '" ' .
  424. CreateElement::NAMESPACEWORD . ':color="' . $color . '"></' .
  425. CreateElement::NAMESPACEWORD . ':top>__GENERATETBLBORDER__';
  426. $this->_xml = str_replace('__GENERATETBLBORDER__', $xml, $this->_xml);
  427. }
  428. /**
  429. * Generate w:tblw
  430. *
  431. * @param string $type
  432. * @param string $w
  433. * @access protected
  434. */
  435. protected function generateTBLW($type = 'auto', $w = '0')
  436. {
  437. $xml = '<' . CreateElement::NAMESPACEWORD . ':tblW ' .
  438. CreateElement::NAMESPACEWORD . ':w="' . $w . '" ' .
  439. CreateElement::NAMESPACEWORD . ':type="' . $type . '"></' .
  440. CreateElement::NAMESPACEWORD . ':tblW>';
  441. $this->_xml = str_replace('__GENERATETBLW__', $xml, $this->_xml);
  442. }
  443. /**
  444. * Generate w:tc
  445. *
  446. * @access protected
  447. */
  448. protected function generateTC()
  449. {
  450. $xml = '<' . CreateElement::NAMESPACEWORD . ':tc >__GENERATETC__</' .
  451. CreateElement::NAMESPACEWORD . ':tc>__GENERATETR__';
  452. $this->_xml = str_replace('__GENERATETR__', $xml, $this->_xml);
  453. }
  454. /**
  455. * Generate w:tcpr
  456. *
  457. * @access protected
  458. */
  459. protected function generateTCPR()
  460. {
  461. $xml = '<' . CreateElement::NAMESPACEWORD .
  462. ':tcPr>__GENERATETCPR__</' . CreateElement::NAMESPACEWORD .
  463. ':tcPr>__GENERATETC__';
  464. $this->_xml = str_replace('__GENERATETC__', $xml, $this->_xml);
  465. }
  466. /**
  467. * Generate w:tcw
  468. *
  469. * @param string $w
  470. * @param string $type
  471. * @access protected
  472. */
  473. protected function generateTCW($w = '', $type = 'dxa')
  474. {
  475. $xml = '<' . CreateElement::NAMESPACEWORD . ':tcW ' .
  476. CreateElement::NAMESPACEWORD . ':w="' . $w . '" ' .
  477. CreateElement::NAMESPACEWORD . ':type="' . $type . '"></' .
  478. CreateElement::NAMESPACEWORD . ':tcW>__GENERATETCPR__';
  479. $this->_xml = str_replace('__GENERATETCPR__', $xml, $this->_xml);
  480. }
  481. /**
  482. * Generate w:tr
  483. *
  484. * @access protected
  485. */
  486. protected function generateTR()
  487. {
  488. $xml = '<' . CreateElement::NAMESPACEWORD .
  489. ':tr >__GENERATETRPR____GENERATETR__</' .
  490. CreateElement::NAMESPACEWORD . ':tr>__GENERATETBL__';
  491. $this->_xml = str_replace('__GENERATETBL__', $xml, $this->_xml);
  492. }
  493. /**
  494. * Generate w:trpr
  495. *
  496. * @access protected
  497. */
  498. protected function generateTRPR()
  499. {
  500. $xml = '<' . CreateElement::NAMESPACEWORD .
  501. ':trPr>__GENERATETRPR__</' . CreateElement::NAMESPACEWORD .
  502. ':trPr>';
  503. $this->_xml = str_replace('__GENERATETRPR__', $xml, $this->_xml);
  504. }
  505. /**
  506. * Generate w:vmerge
  507. *
  508. * @param <type> $val
  509. * @deprecated
  510. * @access protected
  511. */
  512. protected function generateVMERGE($val = '')
  513. {
  514. }
  515. /**
  516. * Clean template r token
  517. *
  518. * @access private
  519. */
  520. private function cleanTemplateR()
  521. {
  522. $this->_xml = preg_replace('/__GENERATETR__/', '', $this->_xml);
  523. }
  524. /**
  525. * Clean template trpr token
  526. *
  527. * @access private
  528. */
  529. private function cleanTemplateTRPR()
  530. {
  531. $this->_xml = preg_replace('/__GENERATETRPR__/', '', $this->_xml);
  532. }
  533. }