2016-05-05 16:46:22 +02:00
|
|
|
<?php namespace App\Services;
|
|
|
|
|
|
|
|
use Form;
|
|
|
|
use HTML;
|
|
|
|
use Utils;
|
|
|
|
use App\Models\Gateway;
|
|
|
|
|
|
|
|
class TemplateService
|
|
|
|
{
|
|
|
|
public function processVariables($template, $data)
|
|
|
|
{
|
|
|
|
$account = $data['account'];
|
|
|
|
$client = $data['client'];
|
|
|
|
$invitation = $data['invitation'];
|
|
|
|
$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,
|
2016-05-25 05:14:19 +02:00
|
|
|
'$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
|
|
|
}
|
|
|
|
}
|