2020-03-26 04:23:57 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-26 04:23:57 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-26 04:23:57 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils;
|
|
|
|
|
|
|
|
use App\DataMapper\EmailTemplateDefaults;
|
2020-10-01 23:29:11 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
2020-03-26 04:23:57 +01:00
|
|
|
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;
|
2020-03-26 04:23:57 +01:00
|
|
|
use League\CommonMark\CommonMarkConverter;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-09-18 14:27:20 +02:00
|
|
|
private $settings;
|
|
|
|
|
2020-03-26 09:25:44 +01:00
|
|
|
public function __construct($body, $subject, $entity, $entity_id, $template)
|
2020-03-26 04:23:57 +01:00
|
|
|
{
|
|
|
|
$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()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (strlen($this->entity) > 1 && strlen($this->entity_id) > 1) {
|
2020-03-26 04:23:57 +01:00
|
|
|
$class = 'App\Models\\'.ucfirst($this->entity);
|
|
|
|
$this->entity_obj = $class::whereId($this->decodePrimaryKey($this->entity_id))->company()->first();
|
2020-11-25 15:19:52 +01:00
|
|
|
} else {
|
2020-10-01 23:29:11 +02:00
|
|
|
$this->mockEntity();
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-03-26 04:23:57 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setSettingsObject()
|
|
|
|
{
|
|
|
|
if ($this->entity_obj) {
|
|
|
|
$this->settings_entity = $this->entity_obj->client;
|
2020-09-18 14:27:20 +02:00
|
|
|
$this->settings = $this->settings_entity->getMergedSettings();
|
2020-03-26 04:23:57 +01:00
|
|
|
} else {
|
|
|
|
$this->settings_entity = auth()->user()->company();
|
2020-09-18 14:27:20 +02:00
|
|
|
$this->settings = $this->settings_entity->settings;
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the body / subject are not populated we need to get the defaults */
|
|
|
|
private function setTemplates()
|
|
|
|
{
|
2020-03-26 09:25:44 +01:00
|
|
|
if (strlen($this->subject) == 0 && strlen($this->template) > 1) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$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
|
|
|
}
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 09:25:44 +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
|
|
|
}
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function replaceValues()
|
|
|
|
{
|
2020-03-26 09:25:44 +01:00
|
|
|
if ($this->entity_obj) {
|
2020-04-16 10:41:25 +02:00
|
|
|
$this->entityValues($this->entity_obj->client->primary_contact()->first());
|
2020-03-26 09:25:44 +01:00
|
|
|
} else {
|
2020-03-26 04:23:57 +01:00
|
|
|
$this->fakerValues();
|
2020-03-26 09:25:44 +01:00
|
|
|
}
|
2020-03-26 04:23:57 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function fakerValues()
|
|
|
|
{
|
|
|
|
$labels = $this->makeFakerLabels();
|
|
|
|
$values = $this->makeFakerValues();
|
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$this->body = strtr($this->body, $labels);
|
|
|
|
$this->body = strtr($this->body, $values);
|
|
|
|
|
|
|
|
$this->subject = strtr($this->subject, $labels);
|
|
|
|
$this->subject = strtr($this->subject, $values);
|
2020-03-26 04:23:57 +01:00
|
|
|
|
|
|
|
$converter = new CommonMarkConverter([
|
|
|
|
'allow_unsafe_links' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->body = $converter->convertToHtml($this->body);
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
private function entityValues($contact)
|
2020-03-26 04:23:57 +01:00
|
|
|
{
|
2020-10-28 06:50:06 +01:00
|
|
|
//$data = $this->entity_obj->buildLabelsAndValues($contact);
|
|
|
|
|
|
|
|
$data = (new HtmlEngine($this->entity_obj->invitations->first()))->generateLabelsAndValues();
|
2020-09-17 00:22:23 +02:00
|
|
|
// $arrKeysLength = array_map('strlen', array_keys($data));
|
|
|
|
// array_multisort($arrKeysLength, SORT_DESC, $data);
|
2020-03-26 04:23:57 +01:00
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
$this->body = strtr($this->body, $data['labels']);
|
|
|
|
$this->body = strtr($this->body, $data['values']);
|
2020-09-17 00:22:23 +02:00
|
|
|
$this->body = str_replace("\n", "<br>", $this->body);
|
2020-04-16 10:41:25 +02:00
|
|
|
|
|
|
|
$this->subject = strtr($this->subject, $data['labels']);
|
|
|
|
$this->subject = strtr($this->subject, $data['values']);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-26 04:23:57 +01:00
|
|
|
$converter = new CommonMarkConverter([
|
|
|
|
'allow_unsafe_links' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->body = $converter->convertToHtml($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
|
|
|
|
2020-09-28 12:36:51 +02:00
|
|
|
$data = array_merge($data, Helpers::sharedEmailVariables($this->entity_obj->client));
|
2020-03-26 04:23:57 +01:00
|
|
|
|
|
|
|
if ($email_style == 'custom') {
|
|
|
|
$wrapper = $this->settings_entity->getSetting('email_style_custom');
|
2020-03-28 12:34:04 +01:00
|
|
|
|
|
|
|
/*If no custom design exists, send back a blank!*/
|
2020-04-04 12:32:42 +02:00
|
|
|
if (strlen($wrapper) > 1) {
|
2020-03-28 12:34:04 +01:00
|
|
|
$wrapper = $this->renderView($wrapper, $data);
|
2020-04-04 12:32:42 +02:00
|
|
|
} else {
|
2020-03-28 12:34:04 +01:00
|
|
|
$wrapper = '';
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-03-26 04:23:57 +01:00
|
|
|
} else {
|
|
|
|
$wrapper = view($this->getTemplatePath($email_style), $data)->render();
|
2020-08-02 11:59:32 +02:00
|
|
|
$injection = '<head><link rel="stylesheet" type="text/css" property="stylesheet" href="'.config('ninja.app_url').'/css/tailwind-1.2.0.css">';
|
2020-09-06 11:38:10 +02:00
|
|
|
$wrapper = str_replace('<head>', $injection, $wrapper);
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'subject' => $this->subject,
|
|
|
|
'body' => $this->body,
|
|
|
|
'wrapper' => $wrapper,
|
|
|
|
];
|
|
|
|
|
2020-10-01 23:29:11 +02:00
|
|
|
|
|
|
|
$this->tearDown();
|
|
|
|
|
2020-03-26 04:23:57 +01:00
|
|
|
return $data;
|
|
|
|
}
|
2020-10-01 23:29:11 +02:00
|
|
|
|
|
|
|
private function mockEntity()
|
|
|
|
{
|
2020-10-28 11:10:49 +01:00
|
|
|
DB::beginTransaction();
|
2020-10-01 23:29:11 +02:00
|
|
|
|
|
|
|
$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();
|
2020-10-01 23:29:11 +02:00
|
|
|
}
|
2020-03-26 09:25:44 +01:00
|
|
|
}
|