1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Services/TemplateService.php

101 lines
4.1 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-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-01-30 17:05:31 +01:00
foreach ($invoice->documents 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>';
}
2017-01-30 17:05:31 +01:00
foreach ($invoice->expenses as $expense) {
foreach ($expense->documents 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(),
'$client' => $client->getDisplayName(),
'$account' => $account->getDisplayName(),
'$dueDate' => $account->formatDate($invoice->due_date),
'$invoiceDate' => $account->formatDate($invoice->invoice_date),
'$contact' => $invitation->contact->getDisplayName(),
'$firstName' => $invitation->contact->first_name,
'$amount' => $account->formatMoney($data['amount'], $client),
'$invoice' => $invoice->invoice_number,
'$quote' => $invoice->invoice_number,
'$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,
'$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
}
}