Нема описа

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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\Style;
  18. /**
  19. * Table style
  20. */
  21. class Table extends Border
  22. {
  23. /**
  24. * @const string Table width units http://www.schemacentral.com/sc/ooxml/t-w_ST_TblWidth.html
  25. */
  26. const WIDTH_AUTO = 'auto'; // Automatically determined width
  27. const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
  28. const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip)
  29. /**
  30. * Is this a first row style?
  31. *
  32. * @var bool
  33. */
  34. private $isFirstRow = false;
  35. /**
  36. * Style for first row
  37. *
  38. * @var \PhpOffice\PhpWord\Style\Table
  39. */
  40. private $firstRowStyle;
  41. /**
  42. * Cell margin top
  43. *
  44. * @var int
  45. */
  46. private $cellMarginTop;
  47. /**
  48. * Cell margin left
  49. *
  50. * @var int
  51. */
  52. private $cellMarginLeft;
  53. /**
  54. * Cell margin right
  55. *
  56. * @var int
  57. */
  58. private $cellMarginRight;
  59. /**
  60. * Cell margin bottom
  61. *
  62. * @var int
  63. */
  64. private $cellMarginBottom;
  65. /**
  66. * Border size inside horizontal
  67. *
  68. * @var int
  69. */
  70. private $borderInsideHSize;
  71. /**
  72. * Border color inside horizontal
  73. *
  74. * @var string
  75. */
  76. private $borderInsideHColor;
  77. /**
  78. * Border size inside vertical
  79. *
  80. * @var int
  81. */
  82. private $borderInsideVSize;
  83. /**
  84. * Border color inside vertical
  85. *
  86. * @var string
  87. */
  88. private $borderInsideVColor;
  89. /**
  90. * Shading
  91. *
  92. * @var \PhpOffice\PhpWord\Style\Shading
  93. */
  94. private $shading;
  95. /**
  96. * @var \PhpOffice\PhpWord\Style\Alignment Alignment
  97. */
  98. private $alignment;
  99. /**
  100. * @var int|float Width value
  101. */
  102. private $width = 0;
  103. /**
  104. * @var string Width unit
  105. */
  106. private $unit = self::WIDTH_AUTO;
  107. /**
  108. * Create new table style
  109. *
  110. * @param mixed $tableStyle
  111. * @param mixed $firstRowStyle
  112. */
  113. public function __construct($tableStyle = null, $firstRowStyle = null)
  114. {
  115. $this->alignment = new Alignment();
  116. // Clone first row from table style, but with certain properties disabled
  117. if ($firstRowStyle !== null && is_array($firstRowStyle)) {
  118. $this->firstRowStyle = clone $this;
  119. $this->firstRowStyle->isFirstRow = true;
  120. unset($this->firstRowStyle->firstRowStyle);
  121. unset($this->firstRowStyle->borderInsideHSize);
  122. unset($this->firstRowStyle->borderInsideHColor);
  123. unset($this->firstRowStyle->borderInsideVSize);
  124. unset($this->firstRowStyle->borderInsideVColor);
  125. unset($this->firstRowStyle->cellMarginTop);
  126. unset($this->firstRowStyle->cellMarginLeft);
  127. unset($this->firstRowStyle->cellMarginRight);
  128. unset($this->firstRowStyle->cellMarginBottom);
  129. $this->firstRowStyle->setStyleByArray($firstRowStyle);
  130. }
  131. if ($tableStyle !== null && is_array($tableStyle)) {
  132. $this->setStyleByArray($tableStyle);
  133. }
  134. }
  135. /**
  136. * Set first row
  137. *
  138. * @return \PhpOffice\PhpWord\Style\Table
  139. */
  140. public function getFirstRow()
  141. {
  142. return $this->firstRowStyle;
  143. }
  144. /**
  145. * Get background
  146. *
  147. * @return string
  148. */
  149. public function getBgColor()
  150. {
  151. if ($this->shading !== null) {
  152. return $this->shading->getFill();
  153. }
  154. return null;
  155. }
  156. /**
  157. * Set background
  158. *
  159. * @param string $value
  160. * @return self
  161. */
  162. public function setBgColor($value = null)
  163. {
  164. $this->setShading(array('fill' => $value));
  165. return $this;
  166. }
  167. /**
  168. * Get TLRBHV Border Size
  169. *
  170. * @return integer[]
  171. */
  172. public function getBorderSize()
  173. {
  174. return array(
  175. $this->getBorderTopSize(),
  176. $this->getBorderLeftSize(),
  177. $this->getBorderRightSize(),
  178. $this->getBorderBottomSize(),
  179. $this->getBorderInsideHSize(),
  180. $this->getBorderInsideVSize(),
  181. );
  182. }
  183. /**
  184. * Set TLRBHV Border Size
  185. *
  186. * @param int $value Border size in eighths of a point (1/8 point)
  187. * @return self
  188. */
  189. public function setBorderSize($value = null)
  190. {
  191. $this->setBorderTopSize($value);
  192. $this->setBorderLeftSize($value);
  193. $this->setBorderRightSize($value);
  194. $this->setBorderBottomSize($value);
  195. $this->setBorderInsideHSize($value);
  196. $this->setBorderInsideVSize($value);
  197. return $this;
  198. }
  199. /**
  200. * Get TLRBHV Border Color
  201. *
  202. * @return string[]
  203. */
  204. public function getBorderColor()
  205. {
  206. return array(
  207. $this->getBorderTopColor(),
  208. $this->getBorderLeftColor(),
  209. $this->getBorderRightColor(),
  210. $this->getBorderBottomColor(),
  211. $this->getBorderInsideHColor(),
  212. $this->getBorderInsideVColor(),
  213. );
  214. }
  215. /**
  216. * Set TLRBHV Border Color
  217. *
  218. * @param string $value
  219. * @return self
  220. */
  221. public function setBorderColor($value = null)
  222. {
  223. $this->setBorderTopColor($value);
  224. $this->setBorderLeftColor($value);
  225. $this->setBorderRightColor($value);
  226. $this->setBorderBottomColor($value);
  227. $this->setBorderInsideHColor($value);
  228. $this->setBorderInsideVColor($value);
  229. return $this;
  230. }
  231. /**
  232. * Get border size inside horizontal
  233. *
  234. * @return int
  235. */
  236. public function getBorderInsideHSize()
  237. {
  238. return $this->getTableOnlyProperty('borderInsideHSize');
  239. }
  240. /**
  241. * Set border size inside horizontal
  242. *
  243. * @param int $value
  244. * @return self
  245. */
  246. public function setBorderInsideHSize($value = null)
  247. {
  248. return $this->setTableOnlyProperty('borderInsideHSize', $value);
  249. }
  250. /**
  251. * Get border color inside horizontal
  252. *
  253. * @return string
  254. */
  255. public function getBorderInsideHColor()
  256. {
  257. return $this->getTableOnlyProperty('borderInsideHColor');
  258. }
  259. /**
  260. * Set border color inside horizontal
  261. *
  262. * @param string $value
  263. * @return self
  264. */
  265. public function setBorderInsideHColor($value = null)
  266. {
  267. return $this->setTableOnlyProperty('borderInsideHColor', $value, false);
  268. }
  269. /**
  270. * Get border size inside vertical
  271. *
  272. * @return int
  273. */
  274. public function getBorderInsideVSize()
  275. {
  276. return $this->getTableOnlyProperty('borderInsideVSize');
  277. }
  278. /**
  279. * Set border size inside vertical
  280. *
  281. * @param int $value
  282. * @return self
  283. */
  284. public function setBorderInsideVSize($value = null)
  285. {
  286. return $this->setTableOnlyProperty('borderInsideVSize', $value);
  287. }
  288. /**
  289. * Get border color inside vertical
  290. *
  291. * @return string
  292. */
  293. public function getBorderInsideVColor()
  294. {
  295. return $this->getTableOnlyProperty('borderInsideVColor');
  296. }
  297. /**
  298. * Set border color inside vertical
  299. *
  300. * @param string $value
  301. * @return self
  302. */
  303. public function setBorderInsideVColor($value = null)
  304. {
  305. return $this->setTableOnlyProperty('borderInsideVColor', $value, false);
  306. }
  307. /**
  308. * Get cell margin top
  309. *
  310. * @return int
  311. */
  312. public function getCellMarginTop()
  313. {
  314. return $this->getTableOnlyProperty('cellMarginTop');
  315. }
  316. /**
  317. * Set cell margin top
  318. *
  319. * @param int $value
  320. * @return self
  321. */
  322. public function setCellMarginTop($value = null)
  323. {
  324. return $this->setTableOnlyProperty('cellMarginTop', $value);
  325. }
  326. /**
  327. * Get cell margin left
  328. *
  329. * @return int
  330. */
  331. public function getCellMarginLeft()
  332. {
  333. return $this->getTableOnlyProperty('cellMarginLeft');
  334. }
  335. /**
  336. * Set cell margin left
  337. *
  338. * @param int $value
  339. * @return self
  340. */
  341. public function setCellMarginLeft($value = null)
  342. {
  343. return $this->setTableOnlyProperty('cellMarginLeft', $value);
  344. }
  345. /**
  346. * Get cell margin right
  347. *
  348. * @return int
  349. */
  350. public function getCellMarginRight()
  351. {
  352. return $this->getTableOnlyProperty('cellMarginRight');
  353. }
  354. /**
  355. * Set cell margin right
  356. *
  357. * @param int $value
  358. * @return self
  359. */
  360. public function setCellMarginRight($value = null)
  361. {
  362. return $this->setTableOnlyProperty('cellMarginRight', $value);
  363. }
  364. /**
  365. * Get cell margin bottom
  366. *
  367. * @return int
  368. */
  369. public function getCellMarginBottom()
  370. {
  371. return $this->getTableOnlyProperty('cellMarginBottom');
  372. }
  373. /**
  374. * Set cell margin bottom
  375. *
  376. * @param int $value
  377. * @return self
  378. */
  379. public function setCellMarginBottom($value = null)
  380. {
  381. return $this->setTableOnlyProperty('cellMarginBottom', $value);
  382. }
  383. /**
  384. * Get cell margin
  385. *
  386. * @return integer[]
  387. */
  388. public function getCellMargin()
  389. {
  390. return array(
  391. $this->cellMarginTop,
  392. $this->cellMarginLeft,
  393. $this->cellMarginRight,
  394. $this->cellMarginBottom
  395. );
  396. }
  397. /**
  398. * Set TLRB cell margin
  399. *
  400. * @param int $value Margin in twips
  401. * @return self
  402. */
  403. public function setCellMargin($value = null)
  404. {
  405. $this->setCellMarginTop($value);
  406. $this->setCellMarginLeft($value);
  407. $this->setCellMarginRight($value);
  408. $this->setCellMarginBottom($value);
  409. return $this;
  410. }
  411. /**
  412. * Check if any of the margin is not null
  413. *
  414. * @return bool
  415. */
  416. public function hasMargin()
  417. {
  418. $margins = $this->getCellMargin();
  419. return $margins !== array_filter($margins, 'is_null');
  420. }
  421. /**
  422. * Get shading
  423. *
  424. * @return \PhpOffice\PhpWord\Style\Shading
  425. */
  426. public function getShading()
  427. {
  428. return $this->shading;
  429. }
  430. /**
  431. * Set shading
  432. *
  433. * @param mixed $value
  434. * @return self
  435. */
  436. public function setShading($value = null)
  437. {
  438. $this->setObjectVal($value, 'Shading', $this->shading);
  439. return $this;
  440. }
  441. /**
  442. * Get alignment
  443. *
  444. * @return string
  445. */
  446. public function getAlign()
  447. {
  448. return $this->alignment->getValue();
  449. }
  450. /**
  451. * Set alignment
  452. *
  453. * @param string $value
  454. * @return self
  455. */
  456. public function setAlign($value = null)
  457. {
  458. $this->alignment->setValue($value);
  459. return $this;
  460. }
  461. /**
  462. * Get width
  463. *
  464. * @return int|float
  465. */
  466. public function getWidth()
  467. {
  468. return $this->width;
  469. }
  470. /**
  471. * Set width
  472. *
  473. * @param int|float $value
  474. * @return self
  475. */
  476. public function setWidth($value = null)
  477. {
  478. $this->width = $this->setNumericVal($value, $this->width);
  479. return $this;
  480. }
  481. /**
  482. * Get width unit
  483. *
  484. * @return string
  485. */
  486. public function getUnit()
  487. {
  488. return $this->unit;
  489. }
  490. /**
  491. * Set width unit
  492. *
  493. * @param string $value
  494. * @return self
  495. */
  496. public function setUnit($value = null)
  497. {
  498. $enum = array(self::WIDTH_AUTO, self::WIDTH_PERCENT, self::WIDTH_TWIP);
  499. $this->unit = $this->setEnumVal($value, $enum, $this->unit);
  500. return $this;
  501. }
  502. /**
  503. * Get table style only property by checking if it's a firstRow
  504. *
  505. * This is necessary since firstRow style is cloned from table style but
  506. * without certain properties activated, e.g. margins
  507. *
  508. * @param string $property
  509. * @return int|string|null
  510. */
  511. private function getTableOnlyProperty($property)
  512. {
  513. if ($this->isFirstRow === false) {
  514. return $this->$property;
  515. }
  516. return null;
  517. }
  518. /**
  519. * Set table style only property by checking if it's a firstRow
  520. *
  521. * This is necessary since firstRow style is cloned from table style but
  522. * without certain properties activated, e.g. margins
  523. *
  524. * @param string $property
  525. * @param int|string $value
  526. * @param bool $isNumeric
  527. * @return self
  528. */
  529. private function setTableOnlyProperty($property, $value, $isNumeric = true)
  530. {
  531. if ($this->isFirstRow === false) {
  532. if ($isNumeric === true) {
  533. $this->$property = $this->setNumericVal($value, $this->$property);
  534. } else {
  535. $this->$property = $value;
  536. }
  537. }
  538. return $this;
  539. }
  540. }