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

121 lines
5.2 KiB
PHP
Raw Permalink 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\Invitation $invitation */
$invitation = $data['invitation'];
/** @var \App\Models\Account $account */
$account = ! empty($data['account']) ? $data['account'] : $invitation->account;
/** @var \App\Models\Client $client */
$client = ! empty($data['client']) ? $data['client'] : $invitation->invoice->client;
$amount = ! empty($data['amount']) ? $data['amount'] : $invitation->invoice->getRequestedAmount();
2018-02-12 10:23:16 +01:00
// check if it's a proposal
if ($invitation->proposal) {
$invoice = $invitation->proposal->invoice;
$entityType = ENTITY_PROPOSAL;
2018-02-12 10:23:16 +01:00
} else {
$invoice = $invitation->invoice;
$entityType = $invoice->getEntityType();
2018-02-12 10:23:16 +01:00
}
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(),
2018-05-14 08:19:17 +02:00
'$dueDate' => $account->formatDate($invoice->getOriginal('partial_due_date') ?: $invoice->getOriginal('due_date')),
'$invoiceDate' => $account->formatDate($invoice->getOriginal('invoice_date')),
2017-04-16 13:31:14 +02:00
'$contact' => $contact->getDisplayName(),
'$firstName' => $contact->first_name,
'$amount' => $account->formatMoney($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,
2019-01-30 11:45:46 +01:00
'$poNumber' => $invoice->po_number,
'$terms' => $invoice->terms,
'$notes' => $invoice->public_notes,
2016-05-05 16:46:22 +02:00
'$link' => $invitation->getLink(),
'$password' => $passwordHTML,
'$viewLink' => $invitation->getLink().'$password',
'$viewButton' => Form::emailViewButton($invitation->getLink(), $entityType).'$password',
2016-05-05 16:46:22 +02:00
'$paymentLink' => $invitation->getLink('payment').'$password',
'$paymentButton' => Form::emailPaymentButton($invitation->getLink('payment')).'$password',
'$approveLink' => $invitation->getLink('approve').'$password',
'$approveButton' => Form::emailPaymentButton($invitation->getLink('approve'), 'approve').'$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
}
}