Table.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Style;
  18. use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
  19. use PhpOffice\PhpWord\SimpleType\Jc;
  20. use PhpOffice\PhpWord\SimpleType\JcTable;
  21. use PhpOffice\PhpWord\SimpleType\TblWidth;
  22. class Table extends Border
  23. {
  24. /**
  25. * @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO instead
  26. */
  27. const WIDTH_AUTO = 'auto'; // Automatically determined width
  28. /**
  29. * @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT instead
  30. */
  31. const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
  32. /**
  33. * @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP instead
  34. */
  35. const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip)
  36. //values for http://www.datypic.com/sc/ooxml/t-w_ST_TblLayoutType.html
  37. /**
  38. * AutoFit Table Layout
  39. *
  40. * @var string
  41. */
  42. const LAYOUT_AUTO = 'autofit';
  43. /**
  44. * Fixed Width Table Layout
  45. *
  46. * @var string
  47. */
  48. const LAYOUT_FIXED = 'fixed';
  49. /**
  50. * Is this a first row style?
  51. *
  52. * @var bool
  53. */
  54. private $isFirstRow = false;
  55. /**
  56. * Style for first row
  57. *
  58. * @var \PhpOffice\PhpWord\Style\Table
  59. */
  60. private $firstRowStyle;
  61. /**
  62. * Cell margin top
  63. *
  64. * @var int
  65. */
  66. private $cellMarginTop;
  67. /**
  68. * Cell margin left
  69. *
  70. * @var int
  71. */
  72. private $cellMarginLeft;
  73. /**
  74. * Cell margin right
  75. *
  76. * @var int
  77. */
  78. private $cellMarginRight;
  79. /**
  80. * Cell margin bottom
  81. *
  82. * @var int
  83. */
  84. private $cellMarginBottom;
  85. /**
  86. * Border size inside horizontal
  87. *
  88. * @var int
  89. */
  90. private $borderInsideHSize;
  91. /**
  92. * Border color inside horizontal
  93. *
  94. * @var string
  95. */
  96. private $borderInsideHColor;
  97. /**
  98. * Border size inside vertical
  99. *
  100. * @var int
  101. */
  102. private $borderInsideVSize;
  103. /**
  104. * Border color inside vertical
  105. *
  106. * @var string
  107. */
  108. private $borderInsideVColor;
  109. /**
  110. * Shading
  111. *
  112. * @var \PhpOffice\PhpWord\Style\Shading
  113. */
  114. private $shading;
  115. /**
  116. * @var string
  117. */
  118. private $alignment = '';
  119. /**
  120. * @var int|float Width value
  121. */
  122. private $width = 0;
  123. /**
  124. * @var string Width unit
  125. */
  126. private $unit = TblWidth::AUTO;
  127. /**
  128. * @var int|float cell spacing value
  129. */
  130. protected $cellSpacing = null;
  131. /**
  132. * @var string Table Layout
  133. */
  134. private $layout = self::LAYOUT_AUTO;
  135. /**
  136. * Position
  137. *
  138. * @var \PhpOffice\PhpWord\Style\TablePosition
  139. */
  140. private $position;
  141. /** @var TblWidthComplexType|null */
  142. private $indent;
  143. /**
  144. * The width of each column, computed based on the max cell width of each column
  145. *
  146. * @var int[]
  147. */
  148. private $columnWidths;
  149. /**
  150. * Visually Right to Left Table
  151. *
  152. * @see http://www.datypic.com/sc/ooxml/e-w_bidiVisual-1.html
  153. * @var bool
  154. */
  155. private $bidiVisual = false;
  156. /**
  157. * Create new table style
  158. *
  159. * @param mixed $tableStyle
  160. * @param mixed $firstRowStyle
  161. */
  162. public function __construct($tableStyle = null, $firstRowStyle = null)
  163. {
  164. // Clone first row from table style, but with certain properties disabled
  165. if ($firstRowStyle !== null && is_array($firstRowStyle)) {
  166. $this->firstRowStyle = clone $this;
  167. $this->firstRowStyle->isFirstRow = true;
  168. unset($this->firstRowStyle->firstRowStyle, $this->firstRowStyle->borderInsideHSize, $this->firstRowStyle->borderInsideHColor, $this->firstRowStyle->borderInsideVSize, $this->firstRowStyle->borderInsideVColor, $this->firstRowStyle->cellMarginTop, $this->firstRowStyle->cellMarginLeft, $this->firstRowStyle->cellMarginRight, $this->firstRowStyle->cellMarginBottom, $this->firstRowStyle->cellSpacing);
  169. $this->firstRowStyle->setStyleByArray($firstRowStyle);
  170. }
  171. if ($tableStyle !== null && is_array($tableStyle)) {
  172. $this->setStyleByArray($tableStyle);
  173. }
  174. }
  175. /**
  176. * @param float|int $cellSpacing
  177. */
  178. public function setCellSpacing($cellSpacing = null)
  179. {
  180. $this->cellSpacing = $cellSpacing;
  181. }
  182. /**
  183. * @return float|int
  184. */
  185. public function getCellSpacing()
  186. {
  187. return $this->cellSpacing;
  188. }
  189. /**
  190. * Set first row
  191. *
  192. * @return \PhpOffice\PhpWord\Style\Table
  193. */
  194. public function getFirstRow()
  195. {
  196. return $this->firstRowStyle;
  197. }
  198. /**
  199. * Get background
  200. *
  201. * @return string
  202. */
  203. public function getBgColor()
  204. {
  205. if ($this->shading !== null) {
  206. return $this->shading->getFill();
  207. }
  208. return null;
  209. }
  210. /**
  211. * Set background
  212. *
  213. * @param string $value
  214. * @return self
  215. */
  216. public function setBgColor($value = null)
  217. {
  218. $this->setShading(array('fill' => $value));
  219. return $this;
  220. }
  221. /**
  222. * Get TLRBHV Border Size
  223. *
  224. * @return int[]
  225. */
  226. public function getBorderSize()
  227. {
  228. return array(
  229. $this->getBorderTopSize(),
  230. $this->getBorderLeftSize(),
  231. $this->getBorderRightSize(),
  232. $this->getBorderBottomSize(),
  233. $this->getBorderInsideHSize(),
  234. $this->getBorderInsideVSize(),
  235. );
  236. }
  237. /**
  238. * Set TLRBHV Border Size
  239. *
  240. * @param int $value Border size in eighths of a point (1/8 point)
  241. * @return self
  242. */
  243. public function setBorderSize($value = null)
  244. {
  245. $this->setBorderTopSize($value);
  246. $this->setBorderLeftSize($value);
  247. $this->setBorderRightSize($value);
  248. $this->setBorderBottomSize($value);
  249. $this->setBorderInsideHSize($value);
  250. $this->setBorderInsideVSize($value);
  251. return $this;
  252. }
  253. /**
  254. * Get TLRBHV Border Color
  255. *
  256. * @return string[]
  257. */
  258. public function getBorderColor()
  259. {
  260. return array(
  261. $this->getBorderTopColor(),
  262. $this->getBorderLeftColor(),
  263. $this->getBorderRightColor(),
  264. $this->getBorderBottomColor(),
  265. $this->getBorderInsideHColor(),
  266. $this->getBorderInsideVColor(),
  267. );
  268. }
  269. /**
  270. * Set TLRBHV Border Color
  271. *
  272. * @param string $value
  273. * @return self
  274. */
  275. public function setBorderColor($value = null)
  276. {
  277. $this->setBorderTopColor($value);
  278. $this->setBorderLeftColor($value);
  279. $this->setBorderRightColor($value);
  280. $this->setBorderBottomColor($value);
  281. $this->setBorderInsideHColor($value);
  282. $this->setBorderInsideVColor($value);
  283. return $this;
  284. }
  285. /**
  286. * Get border size inside horizontal
  287. *
  288. * @return int
  289. */
  290. public function getBorderInsideHSize()
  291. {
  292. return $this->getTableOnlyProperty('borderInsideHSize');
  293. }
  294. /**
  295. * Set border size inside horizontal
  296. *
  297. * @param int $value
  298. * @return self
  299. */
  300. public function setBorderInsideHSize($value = null)
  301. {
  302. return $this->setTableOnlyProperty('borderInsideHSize', $value);
  303. }
  304. /**
  305. * Get border color inside horizontal
  306. *
  307. * @return string
  308. */
  309. public function getBorderInsideHColor()
  310. {
  311. return $this->getTableOnlyProperty('borderInsideHColor');
  312. }
  313. /**
  314. * Set border color inside horizontal
  315. *
  316. * @param string $value
  317. * @return self
  318. */
  319. public function setBorderInsideHColor($value = null)
  320. {
  321. return $this->setTableOnlyProperty('borderInsideHColor', $value, false);
  322. }
  323. /**
  324. * Get border size inside vertical
  325. *
  326. * @return int
  327. */
  328. public function getBorderInsideVSize()
  329. {
  330. return $this->getTableOnlyProperty('borderInsideVSize');
  331. }
  332. /**
  333. * Set border size inside vertical
  334. *
  335. * @param int $value
  336. * @return self
  337. */
  338. public function setBorderInsideVSize($value = null)
  339. {
  340. return $this->setTableOnlyProperty('borderInsideVSize', $value);
  341. }
  342. /**
  343. * Get border color inside vertical
  344. *
  345. * @return string
  346. */
  347. public function getBorderInsideVColor()
  348. {
  349. return $this->getTableOnlyProperty('borderInsideVColor');
  350. }
  351. /**
  352. * Set border color inside vertical
  353. *
  354. * @param string $value
  355. * @return self
  356. */
  357. public function setBorderInsideVColor($value = null)
  358. {
  359. return $this->setTableOnlyProperty('borderInsideVColor', $value, false);
  360. }
  361. /**
  362. * Get cell margin top
  363. *
  364. * @return int
  365. */
  366. public function getCellMarginTop()
  367. {
  368. return $this->getTableOnlyProperty('cellMarginTop');
  369. }
  370. /**
  371. * Set cell margin top
  372. *
  373. * @param int $value
  374. * @return self
  375. */
  376. public function setCellMarginTop($value = null)
  377. {
  378. return $this->setTableOnlyProperty('cellMarginTop', $value);
  379. }
  380. /**
  381. * Get cell margin left
  382. *
  383. * @return int
  384. */
  385. public function getCellMarginLeft()
  386. {
  387. return $this->getTableOnlyProperty('cellMarginLeft');
  388. }
  389. /**
  390. * Set cell margin left
  391. *
  392. * @param int $value
  393. * @return self
  394. */
  395. public function setCellMarginLeft($value = null)
  396. {
  397. return $this->setTableOnlyProperty('cellMarginLeft', $value);
  398. }
  399. /**
  400. * Get cell margin right
  401. *
  402. * @return int
  403. */
  404. public function getCellMarginRight()
  405. {
  406. return $this->getTableOnlyProperty('cellMarginRight');
  407. }
  408. /**
  409. * Set cell margin right
  410. *
  411. * @param int $value
  412. * @return self
  413. */
  414. public function setCellMarginRight($value = null)
  415. {
  416. return $this->setTableOnlyProperty('cellMarginRight', $value);
  417. }
  418. /**
  419. * Get cell margin bottom
  420. *
  421. * @return int
  422. */
  423. public function getCellMarginBottom()
  424. {
  425. return $this->getTableOnlyProperty('cellMarginBottom');
  426. }
  427. /**
  428. * Set cell margin bottom
  429. *
  430. * @param int $value
  431. * @return self
  432. */
  433. public function setCellMarginBottom($value = null)
  434. {
  435. return $this->setTableOnlyProperty('cellMarginBottom', $value);
  436. }
  437. /**
  438. * Get cell margin
  439. *
  440. * @return int[]
  441. */
  442. public function getCellMargin()
  443. {
  444. return array(
  445. $this->cellMarginTop,
  446. $this->cellMarginLeft,
  447. $this->cellMarginRight,
  448. $this->cellMarginBottom,
  449. );
  450. }
  451. /**
  452. * Set TLRB cell margin
  453. *
  454. * @param int $value Margin in twips
  455. * @return self
  456. */
  457. public function setCellMargin($value = null)
  458. {
  459. $this->setCellMarginTop($value);
  460. $this->setCellMarginLeft($value);
  461. $this->setCellMarginRight($value);
  462. $this->setCellMarginBottom($value);
  463. return $this;
  464. }
  465. /**
  466. * Check if any of the margin is not null
  467. *
  468. * @return bool
  469. */
  470. public function hasMargin()
  471. {
  472. $margins = $this->getCellMargin();
  473. return $margins !== array_filter($margins, 'is_null');
  474. }
  475. /**
  476. * Get shading
  477. *
  478. * @return \PhpOffice\PhpWord\Style\Shading
  479. */
  480. public function getShading()
  481. {
  482. return $this->shading;
  483. }
  484. /**
  485. * Set shading
  486. *
  487. * @param mixed $value
  488. * @return self
  489. */
  490. public function setShading($value = null)
  491. {
  492. $this->setObjectVal($value, 'Shading', $this->shading);
  493. return $this;
  494. }
  495. /**
  496. * @since 0.13.0
  497. *
  498. * @return string
  499. */
  500. public function getAlignment()
  501. {
  502. return $this->alignment;
  503. }
  504. /**
  505. * @since 0.13.0
  506. *
  507. * @param string $value
  508. *
  509. * @return self
  510. */
  511. public function setAlignment($value)
  512. {
  513. if (JcTable::isValid($value) || Jc::isValid($value)) {
  514. $this->alignment = $value;
  515. }
  516. return $this;
  517. }
  518. /**
  519. * @deprecated 0.13.0 Use the `getAlignment` method instead.
  520. *
  521. * @return string
  522. *
  523. * @codeCoverageIgnore
  524. */
  525. public function getAlign()
  526. {
  527. return $this->getAlignment();
  528. }
  529. /**
  530. * @deprecated 0.13.0 Use the `setAlignment` method instead.
  531. *
  532. * @param string $value
  533. *
  534. * @return self
  535. *
  536. * @codeCoverageIgnore
  537. */
  538. public function setAlign($value = null)
  539. {
  540. return $this->setAlignment($value);
  541. }
  542. /**
  543. * Get width
  544. *
  545. * @return int|float
  546. */
  547. public function getWidth()
  548. {
  549. return $this->width;
  550. }
  551. /**
  552. * Set width
  553. *
  554. * @param int|float $value
  555. * @return self
  556. */
  557. public function setWidth($value = null)
  558. {
  559. $this->width = $this->setNumericVal($value, $this->width);
  560. return $this;
  561. }
  562. /**
  563. * Get width unit
  564. *
  565. * @return string
  566. */
  567. public function getUnit()
  568. {
  569. return $this->unit;
  570. }
  571. /**
  572. * Set width unit
  573. *
  574. * @param string $value
  575. * @return self
  576. */
  577. public function setUnit($value = null)
  578. {
  579. TblWidth::validate($value);
  580. $this->unit = $value;
  581. return $this;
  582. }
  583. /**
  584. * Get layout
  585. *
  586. * @return string
  587. */
  588. public function getLayout()
  589. {
  590. return $this->layout;
  591. }
  592. /**
  593. * Set layout
  594. *
  595. * @param string $value
  596. * @return self
  597. */
  598. public function setLayout($value = null)
  599. {
  600. $enum = array(self::LAYOUT_AUTO, self::LAYOUT_FIXED);
  601. $this->layout = $this->setEnumVal($value, $enum, $this->layout);
  602. return $this;
  603. }
  604. /**
  605. * Get table style only property by checking if it's a firstRow
  606. *
  607. * This is necessary since firstRow style is cloned from table style but
  608. * without certain properties activated, e.g. margins
  609. *
  610. * @param string $property
  611. * @return int|string|null
  612. */
  613. private function getTableOnlyProperty($property)
  614. {
  615. if (false === $this->isFirstRow) {
  616. return $this->$property;
  617. }
  618. return null;
  619. }
  620. /**
  621. * Set table style only property by checking if it's a firstRow
  622. *
  623. * This is necessary since firstRow style is cloned from table style but
  624. * without certain properties activated, e.g. margins
  625. *
  626. * @param string $property
  627. * @param int|string $value
  628. * @param bool $isNumeric
  629. * @return self
  630. */
  631. private function setTableOnlyProperty($property, $value, $isNumeric = true)
  632. {
  633. if (false === $this->isFirstRow) {
  634. if (true === $isNumeric) {
  635. $this->$property = $this->setNumericVal($value, $this->$property);
  636. } else {
  637. $this->$property = $value;
  638. }
  639. }
  640. return $this;
  641. }
  642. /**
  643. * Get position
  644. *
  645. * @return \PhpOffice\PhpWord\Style\TablePosition
  646. */
  647. public function getPosition()
  648. {
  649. return $this->position;
  650. }
  651. /**
  652. * Set position
  653. *
  654. * @param mixed $value
  655. * @return self
  656. */
  657. public function setPosition($value = null)
  658. {
  659. $this->setObjectVal($value, 'TablePosition', $this->position);
  660. return $this;
  661. }
  662. /**
  663. * @return TblWidthComplexType
  664. */
  665. public function getIndent()
  666. {
  667. return $this->indent;
  668. }
  669. /**
  670. * @param TblWidthComplexType $indent
  671. * @return self
  672. * @see http://www.datypic.com/sc/ooxml/e-w_tblInd-1.html
  673. */
  674. public function setIndent(TblWidthComplexType $indent)
  675. {
  676. $this->indent = $indent;
  677. return $this;
  678. }
  679. /**
  680. * Get the columnWidths
  681. *
  682. * @return null|int[]
  683. */
  684. public function getColumnWidths()
  685. {
  686. return $this->columnWidths;
  687. }
  688. /**
  689. * The column widths
  690. *
  691. * @param int[] $value
  692. */
  693. public function setColumnWidths(array $value = null)
  694. {
  695. $this->columnWidths = $value;
  696. }
  697. /**
  698. * Get bidiVisual
  699. *
  700. * @return bool
  701. */
  702. public function isBidiVisual()
  703. {
  704. return $this->bidiVisual;
  705. }
  706. /**
  707. * Set bidiVisual
  708. *
  709. * @param bool $bidi
  710. * Set to true to visually present table as Right to Left
  711. * @return self
  712. */
  713. public function setBidiVisual($bidi)
  714. {
  715. $this->bidiVisual = $bidi;
  716. return $this;
  717. }
  718. }