2020-07-09 16:05:17 +02:00
|
|
|
<?php
|
|
|
|
|
2020-07-22 14:30:55 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-22 14:30:55 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-22 14:30:55 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-22 14:30:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\PdfMaker;
|
2020-07-09 16:05:17 +02:00
|
|
|
|
|
|
|
use DOMDocument;
|
|
|
|
use DOMXPath;
|
|
|
|
|
|
|
|
trait PdfMakerUtilities
|
|
|
|
{
|
|
|
|
private function initializeDomDocument()
|
|
|
|
{
|
|
|
|
$document = new DOMDocument();
|
|
|
|
|
|
|
|
$document->validateOnParse = true;
|
2020-11-03 16:18:07 +01:00
|
|
|
@$document->loadHTML(mb_convert_encoding($this->design->html(), 'HTML-ENTITIES', 'UTF-8'));
|
2020-07-13 14:16:18 +02:00
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
$this->document = $document;
|
|
|
|
$this->xpath = new DOMXPath($document);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSection(string $selector, string $section = null)
|
|
|
|
{
|
|
|
|
$element = $this->document->getElementById($selector);
|
|
|
|
|
|
|
|
if ($section) {
|
|
|
|
return $element->getAttribute($section);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $element->nodeValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSectionNode(string $selector)
|
|
|
|
{
|
|
|
|
return $this->document->getElementById($selector);
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
public function updateElementProperties(array $elements)
|
|
|
|
{
|
|
|
|
foreach ($elements as $element) {
|
2020-08-25 10:51:49 +02:00
|
|
|
if (isset($element['tag'])) {
|
|
|
|
$node = $this->document->getElementsByTagName($element['tag'])->item(0);
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (! is_null($this->document->getElementById($element['id']))) {
|
2020-08-25 10:51:49 +02:00
|
|
|
$node = $this->document->getElementById($element['id']);
|
2020-09-04 10:18:41 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
2020-08-25 10:51:49 +02:00
|
|
|
}
|
2020-07-13 17:49:28 +02:00
|
|
|
|
|
|
|
if (isset($element['properties'])) {
|
|
|
|
foreach ($element['properties'] as $property => $value) {
|
2020-07-14 13:50:00 +02:00
|
|
|
$this->updateElementProperty($node, $property, $value);
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($element['elements'])) {
|
2021-05-17 13:28:33 +02:00
|
|
|
$this->createElementContent($node, $element['elements']);
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 07:24:02 +01:00
|
|
|
public function updateElementProperty($element, string $attribute, ?string $value)
|
2020-07-09 16:05:17 +02:00
|
|
|
{
|
2020-08-10 16:43:25 +02:00
|
|
|
// We have exception for "hidden" property.
|
2020-09-06 11:38:10 +02:00
|
|
|
// hidden="true" or hidden="false" will both hide the element,
|
2020-08-10 16:43:25 +02:00
|
|
|
// that's why we have to create an exception here for this rule.
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($attribute == 'hidden' && ($value == false || $value == 'false')) {
|
2020-08-10 16:43:25 +02:00
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
$element->setAttribute($attribute, $value);
|
|
|
|
|
|
|
|
if ($element->getAttribute($attribute) === $value) {
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
public function createElementContent($element, $children)
|
|
|
|
{
|
|
|
|
foreach ($children as $child) {
|
2020-10-09 15:05:57 +02:00
|
|
|
$contains_html = false;
|
2021-01-11 13:34:54 +01:00
|
|
|
|
2021-08-03 11:56:50 +02:00
|
|
|
if ($child['element'] !== 'script') {
|
2022-02-07 02:31:14 +01:00
|
|
|
if (array_key_exists('process_markdown', $this->data) && array_key_exists('content', $child) && $this->data['process_markdown']) {
|
2023-03-19 10:30:28 +01:00
|
|
|
$child['content'] = str_replace('<br>', "\r", ($child['content'] ?? ''));
|
2024-06-28 07:16:08 +02:00
|
|
|
$child['content'] = $this->commonmark->convert($child['content'] ?? ''); //@phpstan-ignore-line
|
2021-08-09 14:24:50 +02:00
|
|
|
}
|
2021-08-03 11:56:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 23:51:39 +01:00
|
|
|
if (isset($child['content'])) {
|
|
|
|
if (isset($child['is_empty']) && $child['is_empty'] === true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:35:07 +02:00
|
|
|
$contains_html = preg_match('#(?<=<)\w+(?=[^<]*?>)#', $child['content'], $m) != 0;
|
2020-10-09 15:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($contains_html) {
|
2021-07-29 11:35:07 +02:00
|
|
|
// If the element contains the HTML, we gonna display it as is. Backend is going to
|
|
|
|
// encode it for us, preventing any errors on the processing stage.
|
2021-04-09 13:06:14 +02:00
|
|
|
// Later, we decode this using Javascript so it looks like it's normal HTML being injected.
|
2021-07-29 11:35:07 +02:00
|
|
|
// To get all elements that need frontend decoding, we use 'data-state' property.
|
2020-10-09 15:05:57 +02:00
|
|
|
|
|
|
|
$_child = $this->document->createElement($child['element'], '');
|
2021-04-09 13:06:14 +02:00
|
|
|
$_child->setAttribute('data-state', 'encoded-html');
|
2021-04-09 13:11:28 +02:00
|
|
|
$_child->nodeValue = htmlspecialchars($child['content']);
|
2020-10-09 15:05:57 +02:00
|
|
|
} else {
|
|
|
|
// .. in case string doesn't contain any HTML, we'll just return
|
|
|
|
// raw $content.
|
|
|
|
|
2020-11-16 00:49:44 +01:00
|
|
|
$_child = $this->document->createElement($child['element'], isset($child['content']) ? htmlspecialchars($child['content']) : '');
|
2020-10-09 15:05:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
$element->appendChild($_child);
|
2020-07-14 13:50:00 +02:00
|
|
|
|
|
|
|
if (isset($child['properties'])) {
|
|
|
|
foreach ($child['properties'] as $property => $value) {
|
|
|
|
$this->updateElementProperty($_child, $property, $value);
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 14:35:27 +02:00
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
if (isset($child['elements'])) {
|
2021-05-17 13:28:33 +02:00
|
|
|
$this->createElementContent($_child, $child['elements']);
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:16:18 +02:00
|
|
|
public function updateVariables(array $variables)
|
|
|
|
{
|
2020-07-29 13:37:05 +02:00
|
|
|
$html = strtr($this->getCompiledHTML(), $variables['labels']);
|
2020-08-10 16:43:25 +02:00
|
|
|
|
2020-07-30 16:43:57 +02:00
|
|
|
$html = strtr($html, $variables['values']);
|
2020-07-13 14:16:18 +02:00
|
|
|
|
2020-10-17 02:24:02 +02:00
|
|
|
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
|
2020-07-13 14:16:18 +02:00
|
|
|
|
|
|
|
$this->document->saveHTML();
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
public function updateVariable(string $element, string $variable, string $value)
|
|
|
|
{
|
|
|
|
$element = $this->document->getElementById($element);
|
|
|
|
|
|
|
|
$original = $element->nodeValue;
|
|
|
|
|
|
|
|
$element->nodeValue = '';
|
|
|
|
|
|
|
|
$replaced = strtr($original, [$variable => $value]);
|
|
|
|
|
|
|
|
$element->appendChild(
|
|
|
|
$this->document->createTextNode($replaced)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
2020-08-25 10:51:49 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
public function getEmptyElements(array &$elements, array $variables)
|
|
|
|
{
|
|
|
|
foreach ($elements as &$element) {
|
|
|
|
if (isset($element['elements'])) {
|
|
|
|
$this->getEmptyChildrens($element['elements'], $variables);
|
|
|
|
}
|
2020-10-27 23:51:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
public function getEmptyChildrens(array &$children, array $variables)
|
|
|
|
{
|
|
|
|
foreach ($children as $key => &$child) {
|
|
|
|
if (isset($child['content']) && isset($child['show_empty']) && $child['show_empty'] === false) {
|
|
|
|
$value = strtr($child['content'], $variables['values']);
|
2023-03-18 08:24:56 +01:00
|
|
|
if ($value === '' || $value === ' ' || $value === ' ') {
|
2020-11-25 15:19:52 +01:00
|
|
|
$child['is_empty'] = true;
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 23:51:39 +01:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (isset($child['elements'])) {
|
|
|
|
$this->getEmptyChildrens($child['elements'], $variables);
|
|
|
|
}
|
2020-10-27 23:51:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 14:16:18 +02:00
|
|
|
}
|