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