1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Generating template elements recursively

This commit is contained in:
Benjamin Beganović 2020-07-13 17:49:28 +02:00
parent 66d23cd816
commit 2be39a4756
3 changed files with 70 additions and 11 deletions

View File

@ -32,14 +32,9 @@ class PdfMaker
public function build()
{
$raw = $this->design->html();
foreach ($this->data['template'] as $element) {
foreach ($element['properties'] as $property => $value) {
$this->updateElementProperty($element['id'], $property, $value);
}
}
// $raw = $this->design->html();
$this->updateElementProperties($this->data['template']);
$this->updateVariables($this->data['variables']);
return $this;

View File

@ -108,4 +108,43 @@ class PdfMakerTest extends TestCase
$this->assertStringContainsString('Invoice Ninja', $maker->getCompiledHTML());
$this->assertStringContainsString('Invoice Ninja', $maker->getSection('header'));
}
public function testElementContentIsGenerated()
{
$state = [
'template' => [
'product-table' => [
'id' => 'product-table',
'properties' => [],
'elements' => [
['element' => 'thead', 'content' => '', 'elements' => [
['element' => 'th', 'content' => 'Company',],
['element' => 'th', 'content' => 'Contact'],
['element' => 'th', 'content' => 'Country'],
]],
['element' => 'tr', 'content' => '', 'elements' => [
['element' => 'td', 'content' => '$company'],
['element' => 'td', 'content' => '$email'],
['element' => 'td', 'content' => '$country'],
]],
],
],
],
'variables' => [
'$company' => 'Invoice Ninja',
'$email' => 'contact@invoiceninja.com',
'$country' => 'UK',
],
];
$maker = new PdfMaker($state);
$maker
->design(Business::class)
->build();
info($maker->getCompiledHTML());
$this->assertTrue(true);
}
}

View File

@ -34,6 +34,23 @@ trait PdfMakerUtilities
return $this->document->getElementById($selector);
}
public function updateElementProperties(array $elements)
{
foreach ($elements as $element) {
$node = $this->document->getElementById($element['id']);
if (isset($element['properties'])) {
foreach ($element['properties'] as $property => $value) {
$this->updateElementProperty($element['id'], $property, $value);
}
}
if (isset($element['elements'])) {
$this->createElementContent($node, $element['elements']);
}
}
}
public function updateElementProperty(string $element, string $attribute, string $value)
{
$element = $this->document->getElementById($element);
@ -44,11 +61,21 @@ trait PdfMakerUtilities
return $element;
}
info('Setting element property failed.');
return $element;
}
public function createElementContent($element, $children)
{
foreach ($children as $child) {
$_child = $this->document->createElement($child['element'], $child['content']);
$element->appendChild($_child);
if (isset($child['elements'])) {
$this->createElementContent($_child, $child['elements']);
}
}
}
public function updateVariables(array $variables)
{
$html = strtr($this->getCompiledHTML(), $variables);
@ -64,8 +91,6 @@ trait PdfMakerUtilities
$original = $element->nodeValue;
info([$variable => $value]);
$element->nodeValue = '';
$replaced = strtr($original, [$variable => $value]);