1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 12:12:48 +01:00
invoiceninja/app/Services/TemplateService.php

106 lines
4.4 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2016-05-05 16:46:22 +02:00
2017-01-30 20:40:43 +01:00
namespace App\Services;
use App\Models\Gateway;
use App\Models\GatewayType;
2016-05-05 16:46:22 +02:00
use Form;
use HTML;
use Utils;
class TemplateService
{
/**
* @param $template
* @param array $data
2017-01-30 20:40:43 +01:00
*
* @return mixed|string
*/
public function processVariables($template, array $data)
2016-05-05 16:46:22 +02:00
{
/** @var \App\Models\Account $account */
2016-05-05 16:46:22 +02:00
$account = $data['account'];
/** @var \App\Models\Client $client */
2016-05-05 16:46:22 +02:00
$client = $data['client'];
/** @var \App\Models\Invitation $invitation */
2016-05-05 16:46:22 +02:00
$invitation = $data['invitation'];
2016-05-05 16:46:22 +02:00
$invoice = $invitation->invoice;
2017-04-16 13:31:14 +02:00
$contact = $invitation->contact;
2017-01-30 20:40:43 +01:00
$passwordHTML = isset($data['password']) ? '<p>'.trans('texts.password').': '.$data['password'].'<p>' : false;
2016-05-05 16:46:22 +02:00
$documentsHTML = '';
if ($account->hasFeature(FEATURE_DOCUMENTS) && $invoice->hasDocuments()) {
$documentsHTML .= trans('texts.email_documents_header').'<ul>';
2017-07-21 13:01:48 +02:00
foreach ($invoice->allDocuments() as $document) {
2016-05-05 16:46:22 +02:00
$documentsHTML .= '<li><a href="'.HTML::entities($document->getClientUrl($invitation)).'">'.HTML::entities($document->name).'</a></li>';
}
$documentsHTML .= '</ul>';
}
2016-06-20 16:14:43 +02:00
2016-05-05 16:46:22 +02:00
$variables = [
'$footer' => $account->getEmailFooter(),
2017-04-20 12:11:42 +02:00
'$emailSignature' => $account->getEmailFooter(),
2016-05-05 16:46:22 +02:00
'$client' => $client->getDisplayName(),
'$idNumber' => $client->id_number,
'$vatNumber' => $client->vat_number,
2016-05-05 16:46:22 +02:00
'$account' => $account->getDisplayName(),
'$dueDate' => $account->formatDate($invoice->partial_due_date ?: $invoice->due_date),
2016-05-05 16:46:22 +02:00
'$invoiceDate' => $account->formatDate($invoice->invoice_date),
2017-04-16 13:31:14 +02:00
'$contact' => $contact->getDisplayName(),
'$firstName' => $contact->first_name,
2016-05-05 16:46:22 +02:00
'$amount' => $account->formatMoney($data['amount'], $client),
2017-05-30 15:02:15 +02:00
'$total' => $invoice->present()->amount,
'$balance' => $invoice->present()->balance,
2016-05-05 16:46:22 +02:00
'$invoice' => $invoice->invoice_number,
'$quote' => $invoice->invoice_number,
'$number' => $invoice->invoice_number,
'$partial' => $invoice->present()->partial,
2016-05-05 16:46:22 +02:00
'$link' => $invitation->getLink(),
'$password' => $passwordHTML,
'$viewLink' => $invitation->getLink().'$password',
'$viewButton' => Form::emailViewButton($invitation->getLink(), $invoice->getEntityType()).'$password',
'$paymentLink' => $invitation->getLink('payment').'$password',
'$paymentButton' => Form::emailPaymentButton($invitation->getLink('payment')).'$password',
'$customClient1' => $client->custom_value1,
'$customClient2' => $client->custom_value2,
2017-04-16 13:31:14 +02:00
'$customContact1' => $contact->custom_value1,
'$customContact2' => $contact->custom_value2,
'$customInvoice1' => $invoice->custom_text_value1,
'$customInvoice2' => $invoice->custom_text_value2,
2016-05-05 16:46:22 +02:00
'$documents' => $documentsHTML,
2017-01-30 20:40:43 +01:00
'$autoBill' => empty($data['autobill']) ? '' : $data['autobill'],
2016-07-04 20:47:05 +02:00
'$portalLink' => $invitation->contact->link,
'$portalButton' => Form::emailViewButton($invitation->contact->link, 'portal'),
2016-05-05 16:46:22 +02:00
];
// Add variables for available payment types
2016-06-20 16:14:43 +02:00
foreach (Gateway::$gatewayTypes as $type) {
2016-11-29 19:21:17 +01:00
if ($type == GATEWAY_TYPE_TOKEN) {
continue;
}
$camelType = Utils::toCamelCase(GatewayType::getAliasFromId($type));
2017-01-08 12:51:53 +01:00
$snakeCase = Utils::toSnakeCase(GatewayType::getAliasFromId($type));
$variables["\${$camelType}Link"] = $invitation->getLink('payment') . "/{$snakeCase}";
$variables["\${$camelType}Button"] = Form::emailPaymentButton($invitation->getLink('payment') . "/{$snakeCase}");
2016-05-05 16:46:22 +02:00
}
2016-06-20 16:14:43 +02:00
2016-05-05 16:46:22 +02:00
$includesPasswordPlaceholder = strpos($template, '$password') !== false;
2016-06-20 16:14:43 +02:00
2016-05-05 16:46:22 +02:00
$str = str_replace(array_keys($variables), array_values($variables), $template);
2017-01-30 20:40:43 +01:00
if (! $includesPasswordPlaceholder && $passwordHTML) {
2016-05-05 16:46:22 +02:00
$pos = strrpos($str, '$password');
2017-01-30 17:05:31 +01:00
if ($pos !== false) {
2016-05-05 16:46:22 +02:00
$str = substr_replace($str, $passwordHTML, $pos, 9/* length of "$password" */);
}
2016-06-20 16:14:43 +02:00
}
2016-05-05 16:46:22 +02:00
$str = str_replace('$password', '', $str);
$str = autolink($str, 100);
2016-06-20 16:14:43 +02:00
2016-05-05 16:46:22 +02:00
return $str;
2016-06-20 16:14:43 +02:00
}
}