Search code examples
laravelinvoicephpwordphpoffice

PhpWord TemplateProcessor clone table rows and insert information in them


I have a issue with the phpword tables.

Table structure

I have the following table and I want to clone the first table row and replace the information in it. So far I have no progress. I've used getVariables() method to get all the variables from the document and loop through them. I've checked if the value is array and If it is it belongs to the row. I have structured the data in the following way

Collection {#971 ▼
  #items: array:12 [▼
    "ticket_id" => array:1 [▼
      0 => 7
      0 => 6
    ]
    "ticket_number" => array:2 [▼
      0 => "157-12313121321"
      1 => null
    ]
    "price_offered_bgn" => array:2 [▼
      0 => 978.0
      1 => 196.0
    ]
    "ticket_is" => array:1 [▼
      0 => "Requested"
    ]
    "departure_date" => array:2 [▼
      0 => "2020-10-20 00:00:00"
      1 => "2020-01-29 00:00:00"
    ]
    "return_date" => array:2 [▼
      0 => "2020-10-29 00:00:00"
      1 => null
    ]
    "company_address" => array:1 [▼
      0 => "ADDRESS"
    ]
    "company_bulstat" => array:1 [▼
      0 => ""
    ]
    "company_dds_number" => array:1 [▼
      0 => "BG 104023232353"
    ]
    "mol" => array:1 [▼
      0 => "Gleichner"
    ]
    "first_name" => array:2 [▼
      0 => "Araceli"
      1 => "Francisca"
    ]
    "last_name" => array:2 [▼
      0 => "Gleichner"
      1 => "Schmitt"
    ]
  ]
}

After I tried to clone the variables and insert the values I came up with the following result

array:4 [▼
  0 => "TICKET_NUMBER"
  1 => "FIRST_NAME"
  2 => "LAST_NAME"
  3 => "DEPARTURE_DATE"
]
array:9 [▼
  0 => "FIRST_NAME#1"
  1 => "LAST_NAME#1"
  2 => "DEPARTURE_DATE#1"
  3 => "RETURN_DATE#1"
  4 => "TICKET_NUMBER#2"
  5 => "FIRST_NAME#2"
  6 => "LAST_NAME#2"
  7 => "DEPARTURE_DATE#2"
  8 => "RETURN_DATE#2"
]

And this error Can not clone row, template variable not found or variable contains markup. at TemplateProcessor->cloneRow('${FIRST_NAME}', 2)

I will be very thankful if you give me any ideas how I can clone this row and insert values in it.


Solution

  • Problem is solved. I've made the table structure like this

    +-----------+----------------+
    | ${row}    | ${Item}        |
    |           |                +
    |           | ${ItemInfo}    |
    +-----------+----------------+
    +-----------+----------------+
    | ${row#1}  | ${Item}        |
    |           |                +
    |           | ${ItemInfo}    |
    +-----------+----------------+
    

    I'm cloning the row with the cloneRow('ROW', 2) method PhpWord Docs this gives me 2 copies of ROW that I can work with and adds on each one of them #INDEX. This way i looped throught them and replaced the place holder with the actual value like this

     foreach ($fields as $key => $value) {
    $this->wordFile->setValue(strtoupper($key) . '#' . $index, $value);
    $this->wordFile->setValue('ROW#' . $index, $index);
    }
    

    the KEY variable is the field name and then i concatenate the #INDEX to it. Cloned rows start with index of 1 (#1, #2, #3 and so on...).