From 80ccd4f924ff9bdfd06ada7b052603c8f540ec94 Mon Sep 17 00:00:00 2001
From: Hillel Coren
Date: Sun, 5 Feb 2017 14:48:26 +0200
Subject: [PATCH] Working on #1234
---
app/Http/Controllers/AccountController.php | 1 -
app/Models/Account.php | 144 -----------------
app/Models/Traits/SendsEmails.php | 145 ++++++++++++++++++
app/Ninja/Mailers/ContactMailer.php | 25 ---
resources/views/accounts/template.blade.php | 1 +
.../templates_and_reminders.blade.php | 74 +--------
resources/views/invoices/edit.blade.php | 8 +-
resources/views/invoices/email.blade.php | 38 ++++-
.../views/partials/email_templates.blade.php | 67 ++++++++
9 files changed, 254 insertions(+), 249 deletions(-)
create mode 100644 resources/views/partials/email_templates.blade.php
diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php
index ca9ec52fae76..0ab8a72cb48f 100644
--- a/app/Http/Controllers/AccountController.php
+++ b/app/Http/Controllers/AccountController.php
@@ -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);
diff --git a/app/Models/Account.php b/app/Models/Account.php
index 9e9523a7af36..b6c229958ce6 100644
--- a/app/Models/Account.php
+++ b/app/Models/Account.php
@@ -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 = '$client,
';
-
- if ($this->hasFeature(FEATURE_CUSTOM_EMAILS) && $this->email_design_id != EMAIL_DESIGN_PLAIN) {
- $template .= '' . trans("texts.{$entityType}_message_button", ['amount' => '$amount']) . '
' .
- '$viewButton
';
- } else {
- $template .= '' . trans("texts.{$entityType}_message", ['amount' => '$amount']) . '
' .
- '$viewLink
';
- }
-
- if ($message) {
- $template .= "$message\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);
- }
-
- //
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 '' . trans('texts.email_signature') . "\n
\$account
";
- }
- }
-
- /**
- * @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
*
diff --git a/app/Models/Traits/SendsEmails.php b/app/Models/Traits/SendsEmails.php
index 883f97b5ffa5..4b0d9bbe464b 100644
--- a/app/Models/Traits/SendsEmails.php
+++ b/app/Models/Traits/SendsEmails.php
@@ -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 = '$client,
';
+
+ if ($this->hasFeature(FEATURE_CUSTOM_EMAILS) && $this->email_design_id != EMAIL_DESIGN_PLAIN) {
+ $template .= '' . trans("texts.{$entityType}_message_button", ['amount' => '$amount']) . '
' .
+ '$viewButton
';
+ } else {
+ $template .= '' . trans("texts.{$entityType}_message", ['amount' => '$amount']) . '
' .
+ '$viewLink
';
+ }
+
+ if ($message) {
+ $template .= "$message\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);
+ }
+
+ //
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 '' . trans('texts.email_signature') . "\n
\$account
";
+ }
+ }
+
+ /**
+ * @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;
diff --git a/app/Ninja/Mailers/ContactMailer.php b/app/Ninja/Mailers/ContactMailer.php
index 46d5d3955eec..963bc82aa1cd 100644
--- a/app/Ninja/Mailers/ContactMailer.php
+++ b/app/Ninja/Mailers/ContactMailer.php
@@ -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
*/
diff --git a/resources/views/accounts/template.blade.php b/resources/views/accounts/template.blade.php
index 2788d32c75c6..b3831f5b97b1 100644
--- a/resources/views/accounts/template.blade.php
+++ b/resources/views/accounts/template.blade.php
@@ -33,6 +33,7 @@
) !!}
+
@endif
diff --git a/resources/views/accounts/templates_and_reminders.blade.php b/resources/views/accounts/templates_and_reminders.blade.php
index cb0b0534d20e..3d8e1fe6032e 100644
--- a/resources/views/accounts/templates_and_reminders.blade.php
+++ b/resources/views/accounts/templates_and_reminders.blade.php
@@ -135,9 +135,6 @@
{{ trans('texts.template_help_1') }}
- @foreach (\App\Ninja\Mailers\ContactMailer::$variableFields as $field)
- - ${{ $field }}
- @endforeach
@if ($account->custom_client_label1)
- $customClient1
@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?''.trans('texts.password').': 6h2NWNdw6
':'' !!}";
-
- @if ($account->isPro())
- var documentsHtml = "{!! trans('texts.email_documents_header').'
' !!}";
- @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
+ @include('partials.email_templates')
+
@stop
diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php
index 12846ce22539..d7944a25ff49 100644
--- a/resources/views/invoices/edit.blade.php
+++ b/resources/views/invoices/edit.blade.php
@@ -799,6 +799,7 @@
+ @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);
diff --git a/resources/views/invoices/email.blade.php b/resources/views/invoices/email.blade.php
index 6b3f01ebb416..3a0ca637f383 100644
--- a/resources/views/invoices/email.blade.php
+++ b/resources/views/invoices/email.blade.php
@@ -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 @@
-
+
@@ -75,7 +78,6 @@
@endif
-
@@ -90,4 +92,34 @@
diff --git a/resources/views/partials/email_templates.blade.php b/resources/views/partials/email_templates.blade.php
new file mode 100644
index 000000000000..6731094b6417
--- /dev/null
+++ b/resources/views/partials/email_templates.blade.php
@@ -0,0 +1,67 @@
+