From ded651d947f9f83c0c3bc6072542c4b75998a15e Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 11 May 2014 17:30:23 +0700 Subject: [PATCH] #149: Ability to add table inside a cell (nested table) --- CHANGELOG.md | 1 + samples/Sample_09_Tables.php | 10 +++++++++ src/PhpWord/Element/AbstractContainer.php | 2 +- .../Writer/Word2007/Element/Container.php | 22 +++++++++++-------- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a337da69..9656a3ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3. - TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228 - TextBox: Ability to add table inside textbox - @basjan GH-231 - HTML: Ability to add elements to PHPWord object via html - @basjan GH-231 +- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149 ### Bugfixes diff --git a/samples/Sample_09_Tables.php b/samples/Sample_09_Tables.php index 5b4b1300..5f77f8db 100644 --- a/samples/Sample_09_Tables.php +++ b/samples/Sample_09_Tables.php @@ -84,6 +84,16 @@ $table->addCell(2000, $cellVCentered)->addText('C', null, $cellHCentered); $table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered); $table->addCell(null, $cellRowContinue); +// 4. Nested table + +$section->addTextBreak(2); +$section->addText('Nested table', $header); + +$cell = $section->addTable()->addRow()->addCell(); +$cell->addText('This cell contains nested table.'); +$innerCell = $cell->addTable()->addRow()->addCell(); +$innerCell->addText('Inside nested table'); + // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); if (!CLI) { diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 431a3066..8989007a 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -354,7 +354,7 @@ abstract class AbstractContainer extends AbstractElement 'Object' => $allContainers, 'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'), 'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'), - 'Table' => array('section', 'header', 'footer', 'textbox'), + 'Table' => array('section', 'header', 'footer', 'cell', 'textbox'), 'CheckBox' => array('section', 'header', 'footer', 'cell'), 'TextBox' => array('section', 'header', 'footer', 'cell'), 'Footnote' => array('section', 'textrun', 'cell'), diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php index 384fbd5b..cca79cca 100644 --- a/src/PhpWord/Writer/Word2007/Element/Container.php +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -43,19 +43,23 @@ class Container extends AbstractElement $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; - // Loop through subelements - $subelements = $container->getElements(); - if (count($subelements) > 0) { - foreach ($subelements as $subelement) { - $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, get_class($subelement)); + // Loop through elements + $elements = $container->getElements(); + $elementClass = ''; + if (count($elements) > 0) { + foreach ($elements as $element) { + $elementClass = get_class($element); + $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass); if (class_exists($writerClass)) { - $writer = new $writerClass($xmlWriter, $subelement, $withoutP); + $writer = new $writerClass($xmlWriter, $element, $withoutP); $writer->write(); } } - } else { - // Special case for Cell: They have to contain a TextBreak at least - if ($containerClass == 'Cell') { + } + + // Special case for Cell: They have to contain a w:p element at the end + if ($containerClass == 'Cell') { + if ($elementClass == '' || $elementClass == 'PhpOffice\\PhpWord\\Element\\Table') { $writerClass = "{$this->namespace}\\TextBreak"; $writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP); $writer->write();