Table.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Writer\RTF\Element;
  18. use PhpOffice\PhpWord\Element\Cell as CellElement;
  19. use PhpOffice\PhpWord\Element\Row as RowElement;
  20. use PhpOffice\PhpWord\Element\Table as TableElement;
  21. /**
  22. * Table element RTF writer
  23. *
  24. * @since 0.11.0
  25. */
  26. class Table extends AbstractElement
  27. {
  28. /**
  29. * Write element
  30. *
  31. * @return string
  32. */
  33. public function write()
  34. {
  35. if (!$this->element instanceof TableElement) {
  36. return '';
  37. }
  38. $element = $this->element;
  39. // No nesting table for now
  40. if ($element->getNestedLevel() >= 1) {
  41. return '';
  42. }
  43. $content = '';
  44. $rows = $element->getRows();
  45. $rowCount = count($rows);
  46. if ($rowCount > 0) {
  47. $content .= '\pard' . PHP_EOL;
  48. for ($i = 0; $i < $rowCount; $i++) {
  49. $content .= '\trowd ';
  50. $content .= $this->writeRowDef($rows[$i]);
  51. $content .= PHP_EOL;
  52. $content .= $this->writeRow($rows[$i]);
  53. $content .= '\row' . PHP_EOL;
  54. }
  55. $content .= '\pard' . PHP_EOL;
  56. }
  57. return $content;
  58. }
  59. /**
  60. * Write column
  61. *
  62. * @param \PhpOffice\PhpWord\Element\Row $row
  63. * @return string
  64. */
  65. private function writeRowDef(RowElement $row)
  66. {
  67. $content = '';
  68. $rightMargin = 0;
  69. foreach ($row->getCells() as $cell) {
  70. $width = $cell->getWidth();
  71. $vMerge = $this->getVMerge($cell->getStyle()->getVMerge());
  72. if ($width === null) {
  73. $width = 720; // Arbitrary default width
  74. }
  75. $rightMargin += $width;
  76. $content .= "{$vMerge}\cellx{$rightMargin} ";
  77. }
  78. return $content;
  79. }
  80. /**
  81. * Write row
  82. *
  83. * @param \PhpOffice\PhpWord\Element\Row $row
  84. * @return string
  85. */
  86. private function writeRow(RowElement $row)
  87. {
  88. $content = '';
  89. // Write cells
  90. foreach ($row->getCells() as $cell) {
  91. $content .= $this->writeCell($cell);
  92. }
  93. return $content;
  94. }
  95. /**
  96. * Write cell
  97. *
  98. * @param \PhpOffice\PhpWord\Element\Cell $cell
  99. * @return string
  100. */
  101. private function writeCell(CellElement $cell)
  102. {
  103. $content = '\intbl' . PHP_EOL;
  104. // Write content
  105. $writer = new Container($this->parentWriter, $cell);
  106. $content .= $writer->write();
  107. $content .= '\cell' . PHP_EOL;
  108. return $content;
  109. }
  110. /**
  111. * Get vertical merge style
  112. *
  113. * @param string $value
  114. * @return string
  115. * @todo Move to style
  116. */
  117. private function getVMerge($value)
  118. {
  119. $style = '';
  120. if ($value == 'restart') {
  121. $style = '\clvmgf';
  122. } elseif ($value == 'continue') {
  123. $style = '\clvmrg';
  124. }
  125. return $style;
  126. }
  127. }