1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 00:11:35 +02:00
This commit is contained in:
Hillel Coren 2017-02-05 14:48:26 +02:00
parent c65f09d6ab
commit 80ccd4f924
9 changed files with 254 additions and 249 deletions

View File

@ -699,7 +699,6 @@ class AccountController extends BaseController
'template' => $account->getDefaultEmailTemplate($type),
];
}
$data['emailFooter'] = $account->getEmailFooter();
$data['title'] = trans('texts.email_templates');
return View::make('accounts.templates_and_reminders', $data);

View File

@ -1317,150 +1317,6 @@ class Account extends Eloquent
return $this;
}
/**
* @param $entityType
*
* @return mixed
*/
public function getDefaultEmailSubject($entityType)
{
if (strpos($entityType, 'reminder') !== false) {
$entityType = 'reminder';
}
return trans("texts.{$entityType}_subject", ['invoice' => '$invoice', 'account' => '$account']);
}
/**
* @param $entityType
*
* @return mixed
*/
public function getEmailSubject($entityType)
{
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
$field = "email_subject_{$entityType}";
$value = $this->$field;
if ($value) {
return $value;
}
}
return $this->getDefaultEmailSubject($entityType);
}
/**
* @param $entityType
* @param bool $message
*
* @return string
*/
public function getDefaultEmailTemplate($entityType, $message = false)
{
if (strpos($entityType, 'reminder') !== false) {
$entityType = ENTITY_INVOICE;
}
$template = '<div>$client,</div><br>';
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS) && $this->email_design_id != EMAIL_DESIGN_PLAIN) {
$template .= '<div>' . trans("texts.{$entityType}_message_button", ['amount' => '$amount']) . '</div><br>' .
'<div style="text-align: center;">$viewButton</div><br>';
} else {
$template .= '<div>' . trans("texts.{$entityType}_message", ['amount' => '$amount']) . '</div><br>' .
'<div>$viewLink</div><br>';
}
if ($message) {
$template .= "$message<p/>\r\n\r\n";
}
return $template . '$footer';
}
/**
* @param $entityType
* @param bool $message
*
* @return mixed
*/
public function getEmailTemplate($entityType, $message = false)
{
$template = false;
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
$field = "email_template_{$entityType}";
$template = $this->$field;
}
if (! $template) {
$template = $this->getDefaultEmailTemplate($entityType, $message);
}
// <br/> is causing page breaks with the email designs
return str_replace('/>', ' />', $template);
}
/**
* @param string $view
*
* @return string
*/
public function getTemplateView($view = '')
{
return $this->getEmailDesignId() == EMAIL_DESIGN_PLAIN ? $view : 'design' . $this->getEmailDesignId();
}
/**
* @return mixed|string
*/
public function getEmailFooter()
{
if ($this->email_footer) {
// Add line breaks if HTML isn't already being used
return strip_tags($this->email_footer) == $this->email_footer ? nl2br($this->email_footer) : $this->email_footer;
} else {
return '<p><div>' . trans('texts.email_signature') . "\n<br>\$account</div></p>";
}
}
/**
* @param $reminder
*
* @return bool
*/
public function getReminderDate($reminder)
{
if (! $this->{"enable_reminder{$reminder}"}) {
return false;
}
$numDays = $this->{"num_days_reminder{$reminder}"};
$plusMinus = $this->{"direction_reminder{$reminder}"} == REMINDER_DIRECTION_AFTER ? '-' : '+';
return date('Y-m-d', strtotime("$plusMinus $numDays days"));
}
/**
* @param Invoice $invoice
*
* @return bool|string
*/
public function getInvoiceReminder(Invoice $invoice)
{
for ($i = 1; $i <= 3; $i++) {
if ($date = $this->getReminderDate($i)) {
$field = $this->{"field_reminder{$i}"} == REMINDER_FIELD_DUE_DATE ? 'due_date' : 'invoice_date';
if ($invoice->$field == $date) {
return "reminder{$i}";
}
}
}
return false;
}
/**
* @param null $storage_gateway
*

View File

@ -10,6 +10,151 @@ use Utils;
*/
trait SendsEmails
{
/**
* @param $entityType
*
* @return mixed
*/
public function getDefaultEmailSubject($entityType)
{
if (strpos($entityType, 'reminder') !== false) {
$entityType = 'reminder';
}
return trans("texts.{$entityType}_subject", ['invoice' => '$invoice', 'account' => '$account']);
}
/**
* @param $entityType
*
* @return mixed
*/
public function getEmailSubject($entityType)
{
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
$field = "email_subject_{$entityType}";
$value = $this->$field;
if ($value) {
return $value;
}
}
return $this->getDefaultEmailSubject($entityType);
}
/**
* @param $entityType
* @param bool $message
*
* @return string
*/
public function getDefaultEmailTemplate($entityType, $message = false)
{
if (strpos($entityType, 'reminder') !== false) {
$entityType = ENTITY_INVOICE;
}
$template = '<div>$client,</div><br>';
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS) && $this->email_design_id != EMAIL_DESIGN_PLAIN) {
$template .= '<div>' . trans("texts.{$entityType}_message_button", ['amount' => '$amount']) . '</div><br>' .
'<div style="text-align: center;">$viewButton</div><br>';
} else {
$template .= '<div>' . trans("texts.{$entityType}_message", ['amount' => '$amount']) . '</div><br>' .
'<div>$viewLink</div><br>';
}
if ($message) {
$template .= "$message<p/>\r\n\r\n";
}
return $template . '$footer';
}
/**
* @param $entityType
* @param bool $message
*
* @return mixed
*/
public function getEmailTemplate($entityType, $message = false)
{
$template = false;
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
$field = "email_template_{$entityType}";
$template = $this->$field;
}
if (! $template) {
$template = $this->getDefaultEmailTemplate($entityType, $message);
}
// <br/> is causing page breaks with the email designs
return str_replace('/>', ' />', $template);
}
/**
* @param string $view
*
* @return string
*/
public function getTemplateView($view = '')
{
return $this->getEmailDesignId() == EMAIL_DESIGN_PLAIN ? $view : 'design' . $this->getEmailDesignId();
}
/**
* @return mixed|string
*/
public function getEmailFooter()
{
if ($this->email_footer) {
// Add line breaks if HTML isn't already being used
return strip_tags($this->email_footer) == $this->email_footer ? nl2br($this->email_footer) : $this->email_footer;
} else {
return '<p><div>' . trans('texts.email_signature') . "\n<br>\$account</div></p>";
}
}
/**
* @param $reminder
*
* @return bool
*/
public function getReminderDate($reminder)
{
if (! $this->{"enable_reminder{$reminder}"}) {
return false;
}
$numDays = $this->{"num_days_reminder{$reminder}"};
$plusMinus = $this->{"direction_reminder{$reminder}"} == REMINDER_DIRECTION_AFTER ? '-' : '+';
return date('Y-m-d', strtotime("$plusMinus $numDays days"));
}
/**
* @param Invoice $invoice
*
* @return bool|string
*/
public function getInvoiceReminder(Invoice $invoice)
{
for ($i = 1; $i <= 3; $i++) {
if ($date = $this->getReminderDate($i)) {
$field = $this->{"field_reminder{$i}"} == REMINDER_FIELD_DUE_DATE ? 'due_date' : 'invoice_date';
if ($invoice->$field == $date) {
return "reminder{$i}";
}
}
}
return false;
}
public function getBccEmail()
{
return $this->isPro() ? $this->bcc_email : false;

View File

@ -14,31 +14,6 @@ use Utils;
class ContactMailer extends Mailer
{
/**
* @var array
*/
public static $variableFields = [
'footer',
'account',
'dueDate',
'invoiceDate',
'client',
'amount',
'contact',
'firstName',
'invoice',
'quote',
'password',
'documents',
'viewLink',
'viewButton',
'paymentLink',
'paymentButton',
'autoBill',
'portalLink',
'portalButton',
];
/**
* @var TemplateService
*/

View File

@ -33,6 +33,7 @@
) !!}
</div>
</div>
<br/>
@endif
<div class="row">
<div class="col-md-6">

View File

@ -135,9 +135,6 @@
<div class="modal-body">
<p>{{ trans('texts.template_help_1') }}</p>
<ul>
@foreach (\App\Ninja\Mailers\ContactMailer::$variableFields as $field)
<li>${{ $field }}</li>
@endforeach
@if ($account->custom_client_label1)
<li>$customClient1</li>
@endif
@ -200,7 +197,7 @@
var idName = '#email_' + stringType + '_' + entityType;
var value = $(idName).val();
var previewName = '#' + entityType + '_' + stringType + '_preview';
$(previewName).html(processVariables(value));
$(previewName).html(renderEmailTemplate(value));
}
}
}
@ -258,73 +255,6 @@
}
}
function processVariables(str) {
if (!str) {
return '';
}
var keys = {!! json_encode(\App\Ninja\Mailers\ContactMailer::$variableFields) !!};
var passwordHtml = "{!! $account->isPro() && $account->enable_portal_password && $account->send_portal_password?'<p>'.trans('texts.password').': 6h2NWNdw6<p>':'' !!}";
@if ($account->isPro())
var documentsHtml = "{!! trans('texts.email_documents_header').'<ul><li><a>'.trans('texts.email_documents_example_1').'</a></li><li><a>'.trans('texts.email_documents_example_2').'</a></li></ul>' !!}";
@else
var documentsHtml = "";
@endif
var vals = [
{!! json_encode($emailFooter) !!},
"{{ $account->getDisplayName() }}",
"{{ $account->formatDate($account->getDateTime()) }}",
"{{ $account->formatDate($account->getDateTime()) }}",
"Client Name",
formatMoneyAccount(100, account),
"Contact Name",
"First Name",
"0001",
"0001",
passwordHtml,
documentsHtml,
"{{ URL::to('/view/...') }}$password",
'{!! Form::flatButton('view_invoice', '#0b4d78') !!}$password',
"{{ URL::to('/payment/...') }}$password",
'{!! Form::flatButton('pay_now', '#36c157') !!}$password',
'{{ trans('texts.auto_bill_notification_placeholder') }}',
"{{ URL::to('/client/portal/...') }}",
'{!! Form::flatButton('view_portal', '#36c157') !!}',
];
// Add blanks for custom values
keys.push('customClient1', 'customClient2', 'customInvoice1', 'customInvoice2');
vals.push('custom value', 'custom value', 'custom value', 'custom value');
// Add any available payment method links
@foreach (\App\Models\Gateway::$gatewayTypes as $type)
@if ($type != GATEWAY_TYPE_TOKEN)
{!! "keys.push('" . Utils::toCamelCase(\App\Models\GatewayType::getAliasFromId($type)).'Link' . "');" !!}
{!! "vals.push('" . URL::to('/payment/...') . "');" !!}
{!! "keys.push('" . Utils::toCamelCase(\App\Models\GatewayType::getAliasFromId($type)).'Button' . "');" !!}
{!! "vals.push('" . Form::flatButton('pay_now', '#36c157') . "');" !!}
@endif
@endforeach
var includesPasswordPlaceholder = str.indexOf('$password') != -1;
for (var i=0; i<keys.length; i++) {
var regExp = new RegExp('\\$'+keys[i], 'g');
str = str.replace(regExp, vals[i]);
}
if(!includesPasswordPlaceholder){
var lastSpot = str.lastIndexOf('$password')
str = str.slice(0, lastSpot) + str.slice(lastSpot).replace('$password', passwordHtml);
}
str = str.replace(/\$password/g,'');
return str;
}
function resetText(section, field) {
sweetConfirm(function() {
var fieldName = 'email_' + section + '_' + field;
@ -388,4 +318,6 @@
</script>
@include('partials.email_templates')
@stop

View File

@ -799,6 +799,7 @@
</div>
</div>
@include('partials.email_templates')
@include('invoices.email')
{!! Former::close() !!}
@ -1286,8 +1287,7 @@
return;
}
$('#recipients').html(getSendToEmails());
$('#emailModal').modal('show');
showEmailModal();
}
function onConfirmEmailClick() {
@ -1307,11 +1307,9 @@
}
}
/*
$(function() {
onEmailClick();
//onEmailClick();
})
*/
function onSaveDraftClick() {
model.invoice().is_public(false);

View File

@ -14,8 +14,9 @@
->value('') !!}
{!! Former::select('template')
->onchange('refreshPreview()')
->options([
'initial' => trans('texts.initial_email'),
$invoice->getEntityType() => trans('texts.initial_email'),
'reminder1' => trans('texts.first_reminder'),
'reminder2' => trans('texts.second_reminder'),
'reminder3' => trans('texts.third_reminder'),
@ -36,8 +37,10 @@
</ul>
</div>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="preview">
<div role="tabpanel" class="tab-pane well active" id="preview">
<br/>
<div id="emailSubject"></div>
<br/>
<div id="emailBody"></div>
</div>
<div role="tabpanel" class="tab-pane" id="customize">
@ -75,7 +78,6 @@
@endif
</div>
</div>
</div>
</div>
@ -90,4 +92,34 @@
<script type="text/javascript">
var emailSubjects = [];
emailSubjects['{{ $invoice->getEntityType() }}'] = "{{ $account->getEmailSubject($invoice->getEntityType()) }}";
emailSubjects['reminder1'] = "{{ $account->getEmailSubject('reminder1') }}";
emailSubjects['reminder2'] = "{{ $account->getEmailSubject('reminder2') }}";
emailSubjects['reminder3'] = "{{ $account->getEmailSubject('reminder3') }}";
var emailTemplates = [];
emailTemplates['{{ $invoice->getEntityType() }}'] = "{{ $account->getEmailTemplate($invoice->getEntityType()) }}";
emailTemplates['reminder1'] = "{{ $account->getEmailTemplate('reminder1') }}";
emailTemplates['reminder2'] = "{{ $account->getEmailTemplate('reminder2') }}";
emailTemplates['reminder3'] = "{{ $account->getEmailTemplate('reminder3') }}";
function showEmailModal() {
refreshPreview();
$('#recipients').html(getSendToEmails());
$('#emailModal').modal('show');
}
function refreshPreview() {
var invoice = createInvoiceModel();
var template = dencodeEntities(emailSubjects[$('#template').val()]);
$('#emailSubject').html('<b>' + renderEmailTemplate(template, invoice) + '</b>');
var template = dencodeEntities(emailTemplates[$('#template').val()]);
$('#emailBody').html(renderEmailTemplate(template, invoice));
}
function dencodeEntities(s){
return $("<div/>").html(s).text();
}
</script>

View File

@ -0,0 +1,67 @@
<script type="text/javascript">
function renderEmailTemplate(str, invoice) {
if (!str) {
return '';
}
var passwordHtml = "{!! $account->isPro() && $account->enable_portal_password && $account->send_portal_password?'<p>'.trans('texts.password').': XXXXXXXXX<p>':'' !!}";
@if ($account->isPro())
var documentsHtml = "{!! trans('texts.email_documents_header').'<ul><li><a>'.trans('texts.email_documents_example_1').'</a></li><li><a>'.trans('texts.email_documents_example_2').'</a></li></ul>' !!}";
@else
var documentsHtml = "";
@endif
var keys = {
'footer': {!! json_encode($account->getEmailFooter()) !!},
'account': "{{ $account->getDisplayName() }}",
'dueDate': "{{ $account->formatDate($account->getDateTime()) }}",
'invoiceDate': "{{ $account->formatDate($account->getDateTime()) }}",
'client': invoice ? getClientDisplayName(invoice.client) : 'Client Name',
'amount': invoice ? formatMoneyInvoice(invoice.amount, invoice) : formatMoneyAccount(100, account),
'contact': invoice ? getContactDisplayName(invoice.client.contacts[0]) : 'Contact Name',
'firstName': invoice ? invoice.client.contacts[0].first_name : 'First Name',
'invoice': invoice ? invoice.invoice_number : '0001',
'quote': invoice ? invoice.invoice_number : '0001',
'password': passwordHtml,
'documents': documentsHtml,
'viewLink': "{{ URL::to('/view/...') }}$password",
'viewButton': '{!! Form::flatButton('view_invoice', '#0b4d78') !!}$password',
'paymentLink': "{{ URL::to('/payment/...') }}$password",
'paymentButton': '{!! Form::flatButton('pay_now', '#36c157') !!}$password',
'autoBill': '{{ trans('texts.auto_bill_notification_placeholder') }}',
'portalLink': "{{ URL::to('/client/portal/...') }}",
'portalButton': '{!! Form::flatButton('view_portal', '#36c157') !!}',
'customClient1': invoice ? invoice.client.custom_value1 : 'custom value',
'customClient2': invoice ? invoice.client.custom_value2 : 'custom value',
'customInvoice1': invoice ? invoice.custom_value1 : 'custom value',
'customInvoice2': invoice ? invoice.custom_value2 : 'custom value',
};
// Add any available payment method links
@foreach (\App\Models\Gateway::$gatewayTypes as $type)
@if ($type != GATEWAY_TYPE_TOKEN)
{!! "keys['" . Utils::toCamelCase(\App\Models\GatewayType::getAliasFromId($type)) . "Link'] = '" . URL::to('/payment/...') . "';" !!}
{!! "keys['" . Utils::toCamelCase(\App\Models\GatewayType::getAliasFromId($type)) . "Button'] = '" . Form::flatButton('pay_now', '#36c157') . "';" !!}
@endif
@endforeach
var includesPasswordPlaceholder = str.indexOf('$password') != -1;
for (var key in keys) {
var val = keys[key];
var regExp = new RegExp('\\$'+key, 'g');
str = str.replace(regExp, val);
}
if (!includesPasswordPlaceholder){
var lastSpot = str.lastIndexOf('$password')
str = str.slice(0, lastSpot) + str.slice(lastSpot).replace('$password', passwordHtml);
}
str = str.replace(/\$password/g,'');
return str;
}
</script>