2020-07-09 16:05:17 +02:00
|
|
|
<?php
|
|
|
|
|
2020-07-22 14:30:55 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2020-07-09 16:05:17 +02:00
|
|
|
|
|
|
|
use DOMDocument;
|
|
|
|
use DOMXPath;
|
|
|
|
|
|
|
|
trait PdfMakerUtilities
|
|
|
|
{
|
|
|
|
private function initializeDomDocument()
|
|
|
|
{
|
|
|
|
$document = new DOMDocument();
|
|
|
|
|
|
|
|
$document->validateOnParse = true;
|
|
|
|
@$document->loadHTML($this->design->html());
|
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) {
|
|
|
|
$node = $this->document->getElementById($element['id']);
|
|
|
|
|
|
|
|
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'])) {
|
2020-07-14 14:35:27 +02:00
|
|
|
$sorted = $this->processChildrenOrder($element['elements']);
|
|
|
|
|
|
|
|
$this->createElementContent($node, $sorted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processChildrenOrder(array $children)
|
|
|
|
{
|
|
|
|
$processed = [];
|
|
|
|
|
2020-08-10 16:43:25 +02:00
|
|
|
foreach ($children as $child) {
|
2020-07-14 14:35:27 +02:00
|
|
|
if (!isset($child['order'])) {
|
|
|
|
$child['order'] = 0;
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
2020-07-14 14:35:27 +02:00
|
|
|
|
|
|
|
$processed[] = $child;
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
2020-07-14 15:05:29 +02:00
|
|
|
|
2020-07-14 14:35:27 +02:00
|
|
|
usort($processed, function ($a, $b) {
|
|
|
|
return $a['order'] <=> $b['order'];
|
|
|
|
});
|
2020-07-14 15:05:29 +02:00
|
|
|
|
2020-07-14 14:35:27 +02:00
|
|
|
return $processed;
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 13:50:00 +02: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.
|
|
|
|
// hidden="true" or hidden="false" will both hide the element,
|
|
|
|
// that's why we have to create an exception here for this rule.
|
|
|
|
|
|
|
|
if ($attribute == 'hidden' && ($value == false || $value == "false")) {
|
|
|
|
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-07-14 14:35:27 +02:00
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
$_child = $this->document->createElement($child['element'], $child['content']);
|
|
|
|
$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'])) {
|
2020-07-14 15:05:29 +02:00
|
|
|
$sorted = $this->processChildrenOrder($child['elements']);
|
|
|
|
|
|
|
|
$this->createElementContent($_child, $sorted);
|
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-08-11 17:53:11 +02:00
|
|
|
@$this->document->loadHTML($html);
|
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-07-13 14:16:18 +02:00
|
|
|
}
|