Forum


Replies: 2   Views: 2451
Table issue when try to clone block
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by office_icsl  · 01-12-2017 - 12:18

I'm currently generating a Word Document from template and noticed wierd behaviour trying to use Clone Block, which includes table with some variables, and then replacing the variables with ReplaceTableVariables method with first match flag. All tables exclude the last one contain only first item from array, the last one has all items in it. I suppose it's some kind of bug, because by only one data structure, which compatible with replaceTableVariables (array of arrays) everything works fine

Posted by admin  · 01-12-2017 - 12:40

Hello,

We have done some tests and everything is replaced correctly. Please, could you send to contact[at]phpdocx.com the most simple script that illustrates your issue?, as we need to run it please avoid doing external connections (such as databases or web services) and also attach the template if any is used.

Regards.

P.S. : we have changed your username of the forum (it was an email) to avoid spam bots.

Posted by admin  · 04-12-2017 - 11:50

Hello,
 
Thank you very much for your files. You're right, there's a minor bug in the file that hasn't been detected until you have sent your files: a variable is not being initialized correctly. This issue only appears when there's more than one table with the same placeholder values.
 
Although it could be accomplished using DOCXPath or the MultiMerge classes without changing anything in the current version of the library, the dev team has generated a small patch that will be included in the next release of phpdocx.
 
As you are using the namespaces package (although the same line can be added to the classic package), please edit the Classes/Phpdocx/Create/CreateDocxFromTemplate.inc file and in line 886 add this line:
 
$tableCounter = 0;

Instead of this code:

foreach ($vars as $key => $rowValue) {
    foreach ($foundNodes as $node) {
        $domNode = dom_import_simplexml($node);
        if (!is_object($referenceNode) || !$domNode->parentNode->isSameNode($parentNode)) {
            $referenceNode = $domNode;
            $parentNode = $domNode->parentNode;
            $tableCounter++;
        }
This is the correct code:
foreach ($vars as $key => $rowValue) {
    $tableCounter = 0;
    foreach ($foundNodes as $node) {
        $domNode = dom_import_simplexml($node);
        if (!is_object($referenceNode) || !$domNode->parentNode->isSameNode($parentNode)) {
            $referenceNode = $domNode;
            $parentNode = $domNode->parentNode;
            $tableCounter++;
        }

After this small fix you us the cloneBlock with tables using the replaceTableVariables method.

Our apologize for any inconvenience. This change is being fully tested to be added in the next stable release of the library.

Regards.