mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 13:12:50 +01:00
enable sorting between children elements
This commit is contained in:
parent
ceecddb8a2
commit
bfbe31b185
@ -192,4 +192,61 @@ class PdfMakerTest extends TestCase
|
||||
|
||||
$this->assertNotSame($output1, $output2);
|
||||
}
|
||||
|
||||
public function testOrderingElements()
|
||||
{
|
||||
$maker = new PdfMaker([
|
||||
'template' => [
|
||||
'header' => [
|
||||
'id' => 'header',
|
||||
'properties' => [],
|
||||
'elements' => [
|
||||
['element' => 'h1', 'content' => 'h1-element'],
|
||||
['element' => 'span', 'content' => 'span-element'],
|
||||
]
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$maker
|
||||
->design(Business::class)
|
||||
->build();
|
||||
|
||||
$node = $maker->getSectionNode('header');
|
||||
|
||||
$before = [];
|
||||
|
||||
foreach ($node->childNodes as $child) {
|
||||
$before[] = $child->nodeName;
|
||||
}
|
||||
|
||||
$this->assertEquals('h1', $before[1]);
|
||||
|
||||
$maker = new PdfMaker([
|
||||
'template' => [
|
||||
'header' => [
|
||||
'id' => 'header',
|
||||
'properties' => [],
|
||||
'elements' => [
|
||||
['element' => 'h1', 'content' => 'h1-element', 'order' => 1],
|
||||
['element' => 'span', 'content' => 'span-element', 'order' => 0],
|
||||
]
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$maker
|
||||
->design(Business::class)
|
||||
->build();
|
||||
|
||||
$node = $maker->getSectionNode('header');
|
||||
|
||||
$after = [];
|
||||
|
||||
foreach ($node->childNodes as $child) {
|
||||
$after[] = $child->nodeName;
|
||||
}
|
||||
|
||||
$this->assertEquals('span', $after[1]);
|
||||
}
|
||||
}
|
||||
|
@ -46,11 +46,32 @@ trait PdfMakerUtilities
|
||||
}
|
||||
|
||||
if (isset($element['elements'])) {
|
||||
$this->createElementContent($node, $element['elements']);
|
||||
$sorted = $this->processChildrenOrder($element['elements']);
|
||||
|
||||
$this->createElementContent($node, $sorted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function processChildrenOrder(array $children)
|
||||
{
|
||||
$processed = [];
|
||||
|
||||
foreach($children as $child) {
|
||||
if (!isset($child['order'])) {
|
||||
$child['order'] = 0;
|
||||
}
|
||||
|
||||
$processed[] = $child;
|
||||
}
|
||||
|
||||
usort($processed, function ($a, $b) {
|
||||
return $a['order'] <=> $b['order'];
|
||||
});
|
||||
|
||||
return $processed;
|
||||
}
|
||||
|
||||
public function updateElementProperty($element, string $attribute, string $value)
|
||||
{
|
||||
$element->setAttribute($attribute, $value);
|
||||
@ -65,6 +86,8 @@ trait PdfMakerUtilities
|
||||
public function createElementContent($element, $children)
|
||||
{
|
||||
foreach ($children as $child) {
|
||||
// info($child);
|
||||
|
||||
$_child = $this->document->createElement($child['element'], $child['content']);
|
||||
$element->appendChild($_child);
|
||||
|
||||
@ -73,7 +96,7 @@ trait PdfMakerUtilities
|
||||
$this->updateElementProperty($_child, $property, $value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isset($child['elements'])) {
|
||||
$this->createElementContent($_child, $child['elements']);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user