1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 00:53:10 +01:00
invoiceninja/app/Utils/TemplateEngine.php

321 lines
10 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils;
use App\DataMapper\EmailTemplateDefaults;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
2021-04-13 16:43:35 +02:00
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\MakesTemplateData;
2020-10-28 11:10:49 +01:00
use DB;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang;
use League\CommonMark\CommonMarkConverter;
2021-04-13 16:43:35 +02:00
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
class TemplateEngine
{
use MakesHash;
use MakesTemplateData;
use MakesInvoiceHtml;
public $body;
public $subject;
public $entity;
public $entity_id;
public $template;
private $entity_obj;
private $settings_entity;
private $settings;
2021-01-19 01:46:00 +01:00
private $raw_body;
private $raw_subject;
2021-04-22 12:29:00 +02:00
/**
* @var array
*/
private $labels_and_values;
2021-01-19 01:46:00 +01:00
public function __construct($body, $subject, $entity, $entity_id, $template)
{
$this->body = $body;
$this->subject = $subject;
$this->entity = $entity;
$this->entity_id = $entity_id;
$this->template = $template;
$this->entity_obj = null;
$this->settings_entity = null;
}
public function build()
{
return $this->setEntity()
->setSettingsObject()
->setTemplates()
->replaceValues()
->renderTemplate();
}
private function setEntity()
{
if (strlen($this->entity) > 1 && strlen($this->entity_id) > 1) {
$class = 'App\Models\\'.ucfirst($this->entity);
$this->entity_obj = $class::withTrashed()->where('id', $this->decodePrimaryKey($this->entity_id))->company()->first();
2020-11-25 15:19:52 +01:00
} else {
$this->mockEntity();
2020-11-25 15:19:52 +01:00
}
return $this;
}
private function setSettingsObject()
{
if ($this->entity_obj) {
$this->settings_entity = $this->entity_obj->client;
$this->settings = $this->settings_entity->getMergedSettings();
} else {
$this->settings_entity = auth()->user()->company();
$this->settings = $this->settings_entity->settings;
}
App::forgetInstance('translator');
Lang::replace(Ninja::transformTranslations($this->settings));
return $this;
}
/* If the body / subject are not populated we need to get the defaults */
private function setTemplates()
{
if (strlen($this->subject) == 0 && strlen($this->template) > 1) {
$subject_template = str_replace('template', 'subject', $this->template);
2020-09-17 00:22:23 +02:00
2020-11-25 15:19:52 +01:00
if (strlen($this->settings_entity->getSetting($subject_template)) > 1) {
2020-09-17 00:22:23 +02:00
$this->subject = $this->settings_entity->getSetting($subject_template);
2020-11-25 15:19:52 +01:00
} else {
2020-09-17 00:22:23 +02:00
$this->subject = EmailTemplateDefaults::getDefaultTemplate($subject_template, $this->settings_entity->locale());
2020-11-25 15:19:52 +01:00
}
}
if (strlen($this->body) == 0 && strlen($this->template) > 1) {
2020-11-25 15:19:52 +01:00
if (strlen($this->settings_entity->getSetting($this->template)) > 1) {
2020-09-17 00:22:23 +02:00
$this->body = $this->settings_entity->getSetting($this->template);
2020-11-25 15:19:52 +01:00
} else {
2020-09-17 00:22:23 +02:00
$this->body = EmailTemplateDefaults::getDefaultTemplate($this->template, $this->settings_entity->locale());
2020-11-25 15:19:52 +01:00
}
}
2021-04-20 13:31:21 +02:00
return $this;
}
private function replaceValues()
{
2021-01-19 01:46:00 +01:00
$this->raw_body = $this->body;
$this->raw_subject = $this->subject;
if ($this->entity_obj) {
$this->entityValues($this->entity_obj->client->primary_contact()->first());
} else {
$this->fakerValues();
}
return $this;
}
private function fakerValues()
{
$labels = $this->makeFakerLabels();
$values = $this->makeFakerValues();
$this->body = strtr($this->body, $labels);
$this->body = strtr($this->body, $values);
$this->subject = strtr($this->subject, $labels);
$this->subject = strtr($this->subject, $values);
$converter = new CommonMarkConverter([
'allow_unsafe_links' => false,
]);
$this->body = $converter->convertToHtml($this->body);
}
private function entityValues($contact)
{
2021-04-22 12:29:00 +02:00
$this->labels_and_values = (new HtmlEngine($this->entity_obj->invitations->first()))->generateLabelsAndValues();
2020-10-28 06:50:06 +01:00
2021-04-22 12:29:00 +02:00
$this->body = strtr($this->body, $this->labels_and_values['labels']);
$this->body = strtr($this->body, $this->labels_and_values['values']);
2021-04-13 16:43:35 +02:00
// $this->body = str_replace("\n", "<br>", $this->body);
2021-01-19 01:46:00 +01:00
2021-04-22 12:29:00 +02:00
$this->subject = strtr($this->subject, $this->labels_and_values['labels']);
$this->subject = strtr($this->subject, $this->labels_and_values['values']);
$email_style = $this->settings_entity->getSetting('email_style');
if ($email_style !== 'custom') {
$this->body = DesignHelpers::parseMarkdownToHtml($this->body);
}
}
private function renderTemplate()
{
/* wrapper */
$email_style = $this->settings_entity->getSetting('email_style');
$data['title'] = '';
$data['body'] = '$body';
$data['footer'] = '';
2020-10-28 11:10:49 +01:00
$data = array_merge($data, Helpers::sharedEmailVariables($this->entity_obj->client));
if ($email_style == 'custom') {
$wrapper = $this->settings_entity->getSetting('email_style_custom');
2021-04-22 12:29:00 +02:00
// In order to parse variables such as $signature in the body,
// we need to replace strings with the values from HTMLEngine.
$wrapper = strtr($wrapper, $this->labels_and_values['values']);
/*If no custom design exists, send back a blank!*/
if (strlen($wrapper) > 1) {
$wrapper = $this->renderView($wrapper, $data);
} else {
$wrapper = '';
}
} else {
$wrapper = view($this->getTemplatePath($email_style), $data)->render();
$injection = '';
$wrapper = str_replace('<head>', $injection, $wrapper);
}
$data = [
'subject' => $this->subject,
2021-04-22 13:03:30 +02:00
'body' => $email_style == 'custom' ? $this->body : self::wrapElementsIntoTables(strtr('<div id="content-wrapper"></div>', ['$body' => '']), $this->body, $this->entity_obj->client->getMergedSettings()),
'wrapper' => $wrapper,
2021-01-19 01:46:00 +01:00
'raw_body' => $this->raw_body,
'raw_subject' => $this->raw_subject
];
$this->tearDown();
return $data;
}
private function mockEntity()
{
2020-10-28 11:10:49 +01:00
DB::beginTransaction();
$client = Client::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
]);
$contact = ClientContact::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'client_id' => $client->id,
'is_primary' => 1,
'send_email' => true,
]);
$this->entity_obj = Invoice::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'client_id' => $client->id,
]);
$invitation = InvoiceInvitation::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'invoice_id' => $this->entity_obj->id,
'client_contact_id' => $contact->id,
]);
$this->entity_obj->setRelation('invitations', $invitation);
$this->entity_obj->setRelation('client', $client);
$this->entity_obj->setRelation('company', auth()->user()->company());
$this->entity_obj->load('client');
$client->setRelation('company', auth()->user()->company());
$client->load('company');
}
private function tearDown()
{
2020-10-28 11:10:49 +01:00
DB::rollBack();
}
2021-04-13 16:43:35 +02:00
2021-04-19 11:41:56 +02:00
public static function wrapElementsIntoTables(string $wrapper, string $body, $settings): ?string
2021-04-14 16:09:37 +02:00
{
2021-04-15 15:56:20 +02:00
$documents['wrapper'] = new \DOMDocument();
2021-04-22 13:03:30 +02:00
@$documents['wrapper']->loadHTML($wrapper);
2021-04-14 16:09:37 +02:00
$documents['master'] = new \DOMDocument();
$documents['master']->loadHTML(
2021-04-19 11:41:56 +02:00
view('email.template.master', ['header' => '', 'slot' => '', 'settings' => $settings])->render()
);
$styles = $documents['master']->getElementsByTagName('style')->item(0)->nodeValue;
2021-04-15 15:56:20 +02:00
$documents['wrapper']->saveHTML();
$documents['body'] = new \DOMDocument();
2021-04-20 13:31:21 +02:00
$documents['body']->loadHTML(empty($body) ? '<div></div>' : mb_convert_encoding((new CssToInlineStyles())->convert($body, $styles), 'HTML-ENTITIES', 'UTF-8'));
2021-04-15 15:56:20 +02:00
$table_html ='
<table style="font-family:arial,helvetica,sans-serif;" role="presentation" cellpadding="0" cellspacing="0" width="100%" border="0">
<tbody>
<tr>
<td style="overflow-wrap:break-word;word-break:break-word;padding:5px;font-family:arial,helvetica,sans-serif;" align="left">
2021-04-18 17:35:33 +02:00
<div style="color: #000000; line-height: 140%; text-align: left; word-wrap: break-word;" id="table-content" class="content-contrast-color"></div>
2021-04-15 15:56:20 +02:00
</td>
</tr>
</tbody>
</table>';
foreach ($documents['body']->getElementsByTagName('body')->item(0)->childNodes as $element) {
$table = new \DOMDocument();
$table->loadHTML($table_html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$element = $table->importNode($element, true);
$table->getElementById('table-content')->appendChild($element);
$node = $documents['wrapper']->importNode($table->documentElement, true);
$documents['wrapper']->getElementById('content-wrapper')->appendChild($node);
}
2021-04-13 16:43:35 +02:00
2021-04-15 15:56:20 +02:00
return $documents['wrapper']->getElementById('content-wrapper')->ownerDocument->saveHTML($documents['wrapper']->getElementById('content-wrapper'));
2021-04-13 16:43:35 +02:00
}
}