Update logic to support only one dynamic design class:

- New Design.php class that will act as master template
- PdfMaker->design() now accepts design object instead of string
- PdfMaker: Skip elements if no id|tag provided
- PdfMaker: 'content' property is now optional
- config/ninja.php now contains base_path for templates
- Refactored tests to be :green: ✔
- Removed PdfMakerDesignsTest since content is same for each template now
This commit is contained in:
Benjamin Beganović 2020-09-04 10:18:41 +02:00
parent 5b67a547d9
commit 50c37a8719
8 changed files with 286 additions and 1101 deletions

View File

@ -0,0 +1,227 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\PdfMaker;
use App\Services\PdfMaker\Designs\Utilities\BaseDesign;
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
use App\Utils\Traits\MakesInvoiceValues;
class Design extends BaseDesign
{
use MakesInvoiceValues, DesignHelpers;
/** @var App\Models\Invoice || @var App\Models\Quote */
public $entity;
/** Global state of the design, @var array */
public $context;
/** Type of entity => product||task */
public $type;
/** Design string */
public $design;
/** Construct options */
public $options;
const BOLD = 'bold.html';
const BUSINESS = 'business.html';
const CLEAN = 'clean.html';
const CREATIVE = 'creative.html';
const ELEGANT = 'elegant.html';
const HIPSTER = 'hipster.html';
const MODERN = 'modern.html';
const PLAIN = 'plain.html';
const PLAYFUL = 'playful.html';
public function __construct(string $design = null, array $options = [])
{
$this->design = $design;
$this->options = $options;
}
public function html(): ?string
{
$path = isset($this->options['custom_path'])
? $this->options['custom_path']
: config('ninja.designs.base_path');
return file_get_contents(
$path . $this->design
);
}
public function elements(array $context, string $type = 'product'): array
{
$this->context = $context;
$this->type = $type;
$this->setup();
return [
'company-details' => [
'id' => 'company-details',
'elements' => $this->companyDetails(),
],
'company-address' => [
'id' => 'company-address',
'elements' => $this->companyAddress(),
],
'client-details' => [
'id' => 'client-details',
'elements' => $this->clientDetails(),
],
'entity-details' => [
'id' => 'entity-details',
'elements' => $this->entityDetails(),
],
'product-table' => [
'id' => 'product-table',
'elements' => $this->productTable(),
],
'footer-elements' => [
'id' => 'footer',
'elements' => [
$this->sharedFooterElements(),
],
],
];
}
public function companyDetails()
{
$variables = $this->context['pdf_variables']['company_details'];
$elements = [];
foreach ($variables as $variable) {
$elements[] = ['element' => 'p', 'content' => $variable];
}
return $elements;
}
public function companyAddress(): array
{
$variables = $this->context['pdf_variables']['company_address'];
$elements = [];
foreach ($variables as $variable) {
$elements[] = ['element' => 'p', 'content' => $variable];
}
return $elements;
}
public function clientDetails(): array
{
$variables = $this->context['pdf_variables']['client_details'];
$elements = [];
foreach ($variables as $variable) {
$elements[] = ['element' => 'p', 'content' => $variable];
}
return $elements;
}
public function entityDetails(): array
{
$variables = $this->context['pdf_variables']['invoice_details'];
if ($this->entity instanceof \App\Models\Quote) {
$variables = $this->context['pdf_variables']['quote_details'];
}
$elements = [];
foreach ($variables as $variable) {
$elements[] = ['element' => 'tr', 'properties' => ['hidden' => $this->entityVariableCheck($variable)], 'elements' => [
['element' => 'th', 'content' => $variable . '_label'],
['element' => 'th', 'content' => $variable],
]];
}
return $elements;
}
public function productTable(): array
{
return [
['element' => 'thead', 'elements' => $this->buildTableHeader()],
['element' => 'tbody', 'elements' => $this->buildTableBody()],
['element' => 'tfoot', 'elements' => $this->tableFooter()],
];
}
public function buildTableHeader(): array
{
$this->processTaxColumns();
$elements = [];
foreach ($this->context['pdf_variables']["{$this->type}_columns"] as $column) {
$elements[] = ['element' => 'th', 'content' => $column . '_label'];
}
return $elements;
}
public function buildTableBody(): array
{
$elements = [];
$items = $this->transformLineItems($this->entity->line_items);
if (count($items) == 0) {
return [];
}
foreach ($items as $row) {
$element = ['element' => 'tr', 'elements' => []];
foreach ($this->context['pdf_variables']["{$this->type}_columns"] as $key => $cell) {
$element['elements'][] = ['element' => 'td', 'content' => $row[$cell]];
}
$elements[] = $element;
}
return $elements;
}
public function tableFooter()
{
$variables = $this->context['pdf_variables']['total_columns'];
$elements = [
['element' => 'tr', 'elements' => [
['element' => 'td', 'content' => '$entity.public_notes', 'properties' => ['colspan' => '100%']],
]],
];
foreach ($variables as $variable) {
['element' => 'tr', 'properties' => ['hidden' => 'false'], 'elements' => [
['element' => 'td', 'content' => $variable . '_label', 'properties' => ['colspan' => $this->calculateColspan(1)]],
['element' => 'td', 'content' => $variable],
]];
}
return $elements;
}
}

View File

@ -159,9 +159,9 @@ trait DesignHelpers
public function sharedFooterElements()
{
return ['element' => 'div', 'properties' => ['class' => 'flex items-center justify-between mt-10'], 'content' => '', 'elements' => [
['element' => 'img', 'content' => '', 'properties' => ['src' => '$contact.signature', 'class' => 'h-32']],
['element' => 'img', 'content' => '', 'properties' => ['src' => '$app_url/images/created-by-invoiceninja-new.png', 'class' => 'h-24', 'hidden' => $this->entity->user->account->isPaid() ? 'true' : 'false']],
return ['element' => 'div', 'properties' => ['style' => 'display: flex; justify-content: space-between'], 'elements' => [
['element' => 'img', 'properties' => ['src' => '$contact.signature', 'style' => 'height: 5rem;']],
['element' => 'img', 'properties' => ['src' => '$app_url/images/created-by-invoiceninja-new.png', 'style' => 'height: 5rem;', 'hidden' => $this->entity->user->account->isPaid() ? 'true' : 'false']],
]];
}
@ -177,16 +177,13 @@ trait DesignHelpers
}
if (is_null($this->entity->{$_variable})) {
// info("{$this->entity->id} $_variable is null!");
return true;
}
if (empty($this->entity->{$_variable})) {
// info("{$this->entity->id} $_variable is empty!");
return true;
}
// info("{$this->entity->id} $_variable ALL GOOD!!");
return false;
}
}

View File

@ -43,9 +43,9 @@ class PdfMaker
}
}
public function design(string $design)
public function design(Design $design)
{
$this->design = new $design();
$this->design = $design;
$this->initializeDomDocument();
@ -71,12 +71,12 @@ class PdfMaker
{
if ($final) {
$html = $this->document->saveXML();
$filtered = strtr($html, $this->filters);
return $filtered;
}
return $this->document->saveXML();
}
}

View File

@ -48,10 +48,17 @@ trait PdfMakerUtilities
public function updateElementProperties(array $elements)
{
foreach ($elements as $element) {
// if (!isset($element['tag']) || !isset($element['id']) || is_null($this->document->getElementById($element['id']))) {
// continue;
// }
if (isset($element['tag'])) {
$node = $this->document->getElementsByTagName($element['tag'])->item(0);
} else {
} elseif(!is_null($this->document->getElementById($element['id']))) {
$node = $this->document->getElementById($element['id']);
} else {
continue;
}
if (isset($element['properties'])) {
@ -109,7 +116,7 @@ trait PdfMakerUtilities
public function createElementContent($element, $children)
{
foreach ($children as $child) {
$_child = $this->document->createElement($child['element'], $child['content']);
$_child = $this->document->createElement($child['element'], isset($child['content']) ? $child['content'] : '');
$element->appendChild($_child);
if (isset($child['properties'])) {
@ -259,7 +266,7 @@ trait PdfMakerUtilities
}
if (
$header = $this->document->getElementById('header') &&
$header = $this->document->getElementById('header') &&
isset($this->data['options']['all_pages_header']) &&
$this->data['options']['all_pages_header']
) {

View File

@ -130,7 +130,6 @@ return [
'npm_path' => env('NPM_PATH', false)
],
'designs' => [
'base_path' => resource_path('views/pdf-designs'),
'templates' => ['bold', 'business', 'clean', 'creative', 'elegant', 'hipster', 'modern', 'plain', 'playful'],
'base_path' => resource_path('views/pdf-designs/new/'),
],
];

View File

@ -3,6 +3,7 @@
namespace Tests\Feature\PdfMaker;
use App\Models\Invoice;
use App\Services\PdfMaker\Design;
use App\Services\PdfMaker\Designs\Playful;
use App\Services\PdfMaker\PdfMaker;
use App\Utils\HtmlEngine;
@ -27,7 +28,10 @@ class ExampleIntegrationTest extends TestCase
$invitation = $invoice->invitations()->first();
$engine = new HtmlEngine(null, $invitation, 'invoice');
$design = new Playful();
$design = new Design(
Design::CLEAN
);
$state = [
'template' => $design->elements([
@ -41,12 +45,12 @@ class ExampleIntegrationTest extends TestCase
$maker = new PdfMaker($state);
$maker
->design(Playful::class)
->design($design)
->build();
// exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
// info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
namespace Tests\Feature\PdfMaker;
use App\Services\PdfMaker\Designs\Plain;
use App\Services\PdfMaker\Design;
use App\Services\PdfMaker\PdfMaker;
use Tests\TestCase;
@ -18,30 +18,35 @@ class PdfMakerTest extends TestCase
public function testDesignLoadsCorrectly()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($this->state);
$maker->design(ExampleDesign::class);
$maker->design($design);
$this->assertInstanceOf(ExampleDesign::class, $maker->design);
$this->assertInstanceOf(Design::class, $maker->design);
}
public function testHtmlDesignLoadsCorrectly()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($this->state);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
$this->assertStringContainsString('<!-- Business -->', $maker->getCompiledHTML());
$this->assertStringContainsString('Template: Example', $maker->getCompiledHTML());
}
public function testGetSectionUtility()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($this->state);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
$this->assertEquals('table', $maker->getSectionNode('product-table')->nodeName);
@ -59,12 +64,6 @@ class PdfMakerTest extends TestCase
'script' => 'console.log(1)',
],
],
'header' => [
'id' => 'header',
'properties' => [
'class' => 'header-class',
],
],
],
'variables' => [
'labels' => [],
@ -72,10 +71,11 @@ class PdfMakerTest extends TestCase
],
];
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
$this->assertStringContainsString('my-awesome-class', $maker->getSection('product-table', 'class'));
@ -85,36 +85,25 @@ class PdfMakerTest extends TestCase
public function testVariablesAreReplaced()
{
$state = [
'template' => [
'product-table' => [
'id' => 'product-table',
'properties' => [
'class' => 'my-awesome-class',
'style' => 'margin-top: 10px;',
'script' => 'console.log(1)',
],
],
'header' => [
'id' => 'header',
'properties' => [
'class' => 'header-class',
],
],
],
'variables' => [
'labels' => [],
'values' => [
'$title' => 'Invoice Ninja',
'$company.name' => 'Invoice Ninja',
],
],
];
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
$this->assertStringContainsString('Invoice Ninja', $maker->getCompiledHTML());
@ -123,7 +112,6 @@ class PdfMakerTest extends TestCase
public function testElementContentIsGenerated()
{
$state = [
'template' => [
'product-table' => [
@ -159,10 +147,11 @@ class PdfMakerTest extends TestCase
],
];
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
$compiled = 'contact@invoiceninja.com';
@ -172,6 +161,7 @@ class PdfMakerTest extends TestCase
public function testConditionalRenderingOfElements()
{
$design1 = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker1 = new PdfMaker([
'template' => [
@ -183,13 +173,14 @@ class PdfMakerTest extends TestCase
]);
$maker1
->design(ExampleDesign::class)
->design($design1)
->build();
$output1 = $maker1->getCompiledHTML();
$this->assertStringContainsString('<div id="header">$title</div>', $output1);
$this->assertStringContainsString('<div id="header">', $output1);
$design2 = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker2 = new PdfMaker([
'template' => [
'header' => [
@ -200,18 +191,19 @@ class PdfMakerTest extends TestCase
]);
$maker2
->design(ExampleDesign::class)
->design($design2)
->build();
$output2 = $maker2->getCompiledHTML();
$this->assertStringContainsString('<div id="header" hidden="true">$title</div>', $output2);
$this->assertStringContainsString('<div id="header" hidden="true">$company.name</div>', $output2);
$this->assertNotSame($output1, $output2);
}
public function testOrderingElements()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker([
'template' => [
@ -227,7 +219,7 @@ class PdfMakerTest extends TestCase
]);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
$node = $maker->getSectionNode('header');
@ -254,7 +246,7 @@ class PdfMakerTest extends TestCase
]);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
$node = $maker->getSectionNode('header');
@ -270,7 +262,6 @@ class PdfMakerTest extends TestCase
public function testGeneratingPdf()
{
$state = [
'template' => [
'header' => [
@ -319,10 +310,11 @@ class PdfMakerTest extends TestCase
]
];
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
$this->assertTrue(true);
@ -330,7 +322,7 @@ class PdfMakerTest extends TestCase
public function testGetSectionHTMLWorks()
{
$design = new ExampleDesign();
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$html = $design
->document()
@ -341,7 +333,7 @@ class PdfMakerTest extends TestCase
public function testWrapperHTMLWorks()
{
$design = new ExampleDesign();
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$state = [
'template' => [
@ -357,6 +349,7 @@ class PdfMakerTest extends TestCase
'values' => [],
],
'options' => [
'all_pages_header' => true,
'all_pages_footer' => true,
],
];
@ -364,7 +357,7 @@ class PdfMakerTest extends TestCase
$maker = new PdfMaker($state);
$maker
->design(ExampleDesign::class)
->design($design)
->build();
exec('echo "" > storage/logs/laravel.log');