1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 17:31:35 +02:00
invoiceninja/app/Services/Template/TemplateService.php

337 lines
10 KiB
PHP
Raw Normal View History

2023-09-22 08:14:25 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Template;
2023-09-22 15:31:12 +02:00
use App\Models\Task;
use App\Models\Quote;
use App\Models\Credit;
2023-09-22 08:14:25 +02:00
use App\Models\Design;
2023-09-22 15:31:12 +02:00
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Project;
2023-09-22 14:35:43 +02:00
use App\Utils\HtmlEngine;
2023-09-22 15:31:12 +02:00
use League\Fractal\Manager;
2023-09-25 05:19:08 +02:00
use App\Models\ClientContact;
2023-09-22 15:31:12 +02:00
use App\Models\PurchaseOrder;
2023-09-22 14:08:57 +02:00
use App\Utils\VendorHtmlEngine;
use App\Utils\PaymentHtmlEngine;
2023-09-22 14:35:43 +02:00
use Illuminate\Support\Collection;
2023-09-25 05:19:08 +02:00
use Twig\Extra\Intl\IntlExtension;
2023-09-22 15:31:12 +02:00
use App\Transformers\TaskTransformer;
use App\Transformers\QuoteTransformer;
use App\Transformers\CreditTransformer;
use App\Transformers\InvoiceTransformer;
use App\Transformers\PaymentTransformer;
use App\Transformers\ProjectTransformer;
use App\Transformers\PurchaseOrderTransformer;
use League\Fractal\Serializer\ArraySerializer;
2023-09-22 15:59:03 +02:00
use League\Fractal\Serializer\JsonApiSerializer;
2023-09-22 08:14:25 +02:00
class TemplateService
{
private \DomDocument $document;
private string $compiled_html = '';
public function __construct(public Design $template)
{
$this->template = $template;
$this->init();
}
/**
* Boot Dom Document
*
* @return self
*/
private function init(): self
{
$this->document = new \DOMDocument();
$this->document->validateOnParse = true;
return $this;
}
/**
* Iterate through all of the
* ninja nodes
*
* @param array $data - the payload to be passed into the template
* @return self
*/
2023-09-22 14:35:43 +02:00
public function build(array $data): self
2023-09-22 08:14:25 +02:00
{
$this->compose()
->parseNinjaBlocks($data)
2023-09-22 14:08:57 +02:00
->parseVariables($data);
2023-09-22 08:14:25 +02:00
return $this;
}
public function getHtml(): string
{
return $this->compiled_html;
}
/**
* Parses all Ninja tags in the document
*
* @param array $data
*
* @return self
*/
private function parseNinjaBlocks(array $data): self
{
$data = $this->preProcessDataBlocks($data);
$replacements = [];
2023-09-25 05:19:08 +02:00
nlog($data);
2023-09-22 08:14:25 +02:00
$contents = $this->document->getElementsByTagName('ninja');
foreach ($contents as $content) {
$template = $content->ownerDocument->saveHTML($content);
$loader = new \Twig\Loader\FilesystemLoader(storage_path());
$twig = new \Twig\Environment($loader);
$string_extension = new \Twig\Extension\StringLoaderExtension();
$twig->addExtension($string_extension);
2023-09-25 05:19:08 +02:00
$twig->addExtension(new IntlExtension());
2023-09-22 08:14:25 +02:00
$template = $twig->createTemplate(html_entity_decode($template));
$template = $template->render($data);
2023-09-25 05:19:08 +02:00
nlog($template);
2023-09-22 08:14:25 +02:00
$f = $this->document->createDocumentFragment();
$f->appendXML($template);
$replacements[] = $f;
}
foreach($contents as $key => $content) {
$content->parentNode->replaceChild($replacements[$key], $content);
}
$this->save();
return $this;
}
/**
* Parses all variables in the document
2023-09-22 14:35:43 +02:00
* @param array $data
2023-09-22 08:14:25 +02:00
* @return self
*/
2023-09-22 14:35:43 +02:00
private function parseVariables(array $data): self
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:35:43 +02:00
$variables = $this->resolveHtmlEngine($data);
$html = $this->getHtml();
2023-09-22 08:14:25 +02:00
2023-09-22 14:08:57 +02:00
foreach($variables as $key => $variable) {
2023-09-22 14:35:43 +02:00
$html = strtr($html, $variable['labels']);
2023-09-22 14:08:57 +02:00
$html = strtr($html, $variable['values']);
}
2023-09-22 08:14:25 +02:00
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$this->save();
return $this;
}
/**
* Saves the document and updates the compiled string.
*
* @return self
*/
private function save(): self
{
$this->compiled_html = str_replace('%24', '$', $this->document->saveHTML());
return $this;
}
/**
* compose
*
* @return self
*/
private function compose(): self
{
$html = '';
$html .= $this->template->design->includes;
$html .= $this->template->design->header;
$html .= $this->template->design->body;
$html .= $this->template->design->footer;
@$this->document->loadHTML($html);
return $this;
}
/**
* Resolves the labels and values needed to replace the string
* holders in the template.
*
* @return array
*/
2023-09-22 14:08:57 +02:00
private function resolveHtmlEngine(array $data): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:35:43 +02:00
return collect($data)->map(function ($value, $key) {
$processed = [];
2023-09-22 14:08:57 +02:00
match ($key) {
2023-09-22 14:35:43 +02:00
'invoices' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'quotes' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'credits' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'payments' => $processed = (new PaymentHtmlEngine($value->first(), $value->first()->client->contacts()->first()))->generateLabelsAndValues(),
'tasks' => $processed = [],
'projects' => $processed = [],
'purchase_orders' => $processed = (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
2023-09-22 14:08:57 +02:00
};
return $processed;
})->toArray();
2023-09-22 08:14:25 +02:00
}
private function preProcessDataBlocks($data): array
{
2023-09-22 14:35:43 +02:00
return collect($data)->map(function ($value, $key){
2023-09-22 08:14:25 +02:00
2023-09-22 14:35:43 +02:00
$processed = [];
2023-09-22 08:14:25 +02:00
match ($key) {
2023-09-22 14:35:43 +02:00
'invoices' => $processed = $this->processInvoices($value),
'quotes' => $processed = $this->processQuotes($value),
'credits' => $processed = $this->processCredits($value),
'payments' => $processed = $this->processPayments($value),
'tasks' => $processed = $this->processTasks($value),
'projects' => $processed = $this->processProjects($value),
'purchase_orders' => $processed = $this->processPurchaseOrders($value),
2023-09-22 08:14:25 +02:00
};
return $processed;
})->toArray();
}
2023-09-22 15:31:12 +02:00
private function processInvoices($invoices): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new InvoiceTransformer();
2023-09-25 05:19:08 +02:00
$it->setDefaultIncludes(['client','payments']);
2023-09-22 15:31:12 +02:00
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->parseIncludes(['client','payments','payments.type']);
$resource = new \League\Fractal\Resource\Collection($invoices, $it, null);
$invoices = $manager->createData($resource)->toArray();
// nlog($invoices);
foreach($invoices['data'] as $key => $invoice)
{
$invoices['data'][$key]['client'] = $invoice['client']['data'] ?? [];
$invoices['data'][$key]['client']['contacts'] = $invoice['client']['data']['contacts']['data'] ?? [];
$invoices['data'][$key]['payments'] = $invoice['payments']['data'] ?? [];
if($invoice['payments']['data'] ?? false) {
foreach($invoice['payments']['data'] as $keyx => $payment) {
$invoices['data'][$key]['payments'][$keyx]['paymentables']= $payment['paymentables']['data'] ?? [];
}
}
}
return $invoices['data'];
2023-09-22 08:14:25 +02:00
}
2023-09-25 05:19:08 +02:00
private function processQuotes($quotes): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new QuoteTransformer();
$it->setDefaultIncludes(['client']);
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->setSerializer(new ArraySerializer());
2023-09-22 15:31:12 +02:00
$resource = new \League\Fractal\Resource\Collection($quotes, $it, Quote::class);
$i = $manager->createData($resource)->toArray();
2023-09-25 05:19:08 +02:00
$i['client']['contacts'] = $i['client']['contacts'][ClientContact::class];
return $i[Quote::class];
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-09-25 05:19:08 +02:00
private function processCredits($credits): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new CreditTransformer();
$it->setDefaultIncludes(['client']);
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->setSerializer(new ArraySerializer());
2023-09-22 15:31:12 +02:00
$resource = new \League\Fractal\Resource\Collection($credits, $it, Credit::class);
$i = $manager->createData($resource)->toArray();
2023-09-25 05:19:08 +02:00
return $i[Credit::class];
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-09-25 05:19:08 +02:00
private function processPayments($payments): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new PaymentTransformer();
$it->setDefaultIncludes(['client','invoices','paymentables']);
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->setSerializer(new ArraySerializer());
2023-09-22 15:31:12 +02:00
$resource = new \League\Fractal\Resource\Collection($payments, $it, Payment::class);
$i = $manager->createData($resource)->toArray();
2023-09-25 05:19:08 +02:00
return $i[Payment::class];
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-09-25 05:19:08 +02:00
private function processTasks($tasks): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new TaskTransformer();
$it->setDefaultIncludes(['client','tasks','project','invoice']);
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->setSerializer(new ArraySerializer());
2023-09-22 15:31:12 +02:00
$resource = new \League\Fractal\Resource\Collection($tasks, $it, Task::class);
$i = $manager->createData($resource)->toArray();
2023-09-25 05:19:08 +02:00
return $i[Task::class];
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-09-25 05:19:08 +02:00
private function processProjects($projects): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:08:57 +02:00
2023-09-22 15:31:12 +02:00
$it = new ProjectTransformer();
$it->setDefaultIncludes(['client','tasks']);
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->setSerializer(new ArraySerializer());
2023-09-22 15:31:12 +02:00
$resource = new \League\Fractal\Resource\Collection($projects, $it, Project::class);
$i = $manager->createData($resource)->toArray();
2023-09-25 05:19:08 +02:00
return $i[Project::class];
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
private function processPurchaseOrders($purchase_orders): array
{
2023-09-22 15:31:12 +02:00
$it = new PurchaseOrderTransformer();
$it->setDefaultIncludes(['vendor','expense']);
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->setSerializer(new ArraySerializer());
2023-09-22 15:31:12 +02:00
$resource = new \League\Fractal\Resource\Collection($purchase_orders, $it, PurchaseOrder::class);
$i = $manager->createData($resource)->toArray();
2023-09-25 05:19:08 +02:00
return $i[PurchaseOrder::class];
2023-09-22 08:14:25 +02:00
}
}