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 @@
+ @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 @@ +