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

92 lines
3.8 KiB
PHP
Raw Normal View History

2016-05-05 16:46:22 +02:00
<?php namespace App\Services;
use Form;
use HTML;
use Utils;
use App\Models\Gateway;
class TemplateService
{
/**
* @param $template
* @param array $data
* @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;
$passwordHTML = isset($data['password'])?'<p>'.trans('texts.password').': '.$data['password'].'<p>':false;
$documentsHTML = '';
if ($account->hasFeature(FEATURE_DOCUMENTS) && $invoice->hasDocuments()) {
$documentsHTML .= trans('texts.email_documents_header').'<ul>';
foreach($invoice->documents as $document){
$documentsHTML .= '<li><a href="'.HTML::entities($document->getClientUrl($invitation)).'">'.HTML::entities($document->name).'</a></li>';
}
foreach($invoice->expenses as $expense){
foreach($expense->documents as $document){
$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' => $account->custom_client_label1,
'$customClient2' => $account->custom_client_label2,
'$customInvoice1' => $account->custom_invoice_text_label1,
'$customInvoice2' => $account->custom_invoice_text_label2,
'$documents' => $documentsHTML,
'$autoBill' => empty($data['autobill'])?'':$data['autobill'],
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) {
$camelType = Utils::toCamelCase($type);
2016-05-05 16:46:22 +02:00
$variables["\${$camelType}Link"] = $invitation->getLink('payment') . "/{$type}";
$variables["\${$camelType}Button"] = Form::emailPaymentButton($invitation->getLink('payment') . "/{$type}");
}
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);
if (!$includesPasswordPlaceholder && $passwordHTML) {
$pos = strrpos($str, '$password');
if ($pos !== false)
{
$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
}
}