invoiceninja/app/Models/Traits/SendsEmails.php

219 lines
6 KiB
PHP
Raw Permalink Normal View History

2017-01-30 21:40:43 +02:00
<?php
namespace App\Models\Traits;
use App\Constants\Domain;
2017-01-30 21:40:43 +02:00
use Utils;
2017-08-04 16:39:11 +03:00
use HTMLUtils;
/**
2017-01-30 21:40:43 +02:00
* Class SendsEmails.
*/
trait SendsEmails
{
2017-02-05 14:48:26 +02:00
/**
* @param $entityType
*
* @return mixed
*/
public function getDefaultEmailSubject($entityType)
{
if (strpos($entityType, 'reminder') !== false) {
$entityType = 'reminder';
}
return trans("texts.{$entityType}_subject", [
'invoice' => '$invoice',
'account' => '$account',
'quote' => '$quote',
'number' => '$number',
]);
2017-02-05 14:48:26 +02:00
}
/**
* @param $entityType
*
* @return mixed
*/
public function getEmailSubject($entityType)
{
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
$field = "email_subject_{$entityType}";
2017-03-31 18:02:56 +03:00
$value = $this->account_email_settings->$field;
2017-02-05 14:48:26 +02:00
if ($value) {
2017-08-04 16:39:11 +03:00
$value = preg_replace("/\r\n|\r|\n/", ' ', $value);
return HTMLUtils::sanitizeHTML($value);
2017-02-05 14:48:26 +02:00
}
}
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;
}
2018-04-25 23:41:03 +03:00
$template = '<div>$client,</div><br />';
2017-02-05 14:48:26 +02:00
2019-01-30 22:00:26 +11:00
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS) && $this->account_email_settings->email_design_id != EMAIL_DESIGN_PLAIN) {
2018-04-25 23:41:03 +03:00
$template .= '<div>' . trans("texts.{$entityType}_message_button", ['amount' => '$amount']) . '</div><br />' .
2018-04-25 23:46:12 +03:00
'<div style="text-align:center;">$viewButton</div><br />';
2017-02-05 14:48:26 +02:00
} else {
2018-04-25 23:41:03 +03:00
$template .= '<div>' . trans("texts.{$entityType}_message", ['amount' => '$amount']) . '</div><br />' .
'<div>$viewLink</div><br />';
2017-02-05 14:48:26 +02:00
}
if ($message) {
2017-03-19 09:15:21 +02:00
$template .= "$message<p/>";
2017-02-05 14:48:26 +02:00
}
2017-04-20 13:11:42 +03:00
return $template . '$emailSignature';
2017-02-05 14:48:26 +02:00
}
/**
* @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}";
2017-03-31 18:02:56 +03:00
$template = $this->account_email_settings->$field;
2017-02-05 14:48:26 +02:00
}
if (! $template) {
$template = $this->getDefaultEmailTemplate($entityType, $message);
}
2017-03-19 09:15:21 +02:00
$template = preg_replace("/\r\n|\r|\n/", ' ', $template);
2017-02-05 14:48:26 +02:00
// <br/> is causing page breaks with the email designs
2017-08-04 16:39:11 +03:00
$template = str_replace('/>', ' />', $template);
return HTMLUtils::sanitizeHTML($template);
2017-02-05 14:48:26 +02:00
}
/**
* @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()
{
2019-01-30 22:00:26 +11:00
if ($this->isPro() && $this->account_email_settings->email_footer) {
2017-02-05 14:48:26 +02:00
// Add line breaks if HTML isn't already being used
2019-01-30 22:00:26 +11:00
return strip_tags($this->account_email_settings->email_footer) == $this->account_email_settings->email_footer ? nl2br($this->account_email_settings->email_footer) : $this->account_email_settings->email_footer;
2017-02-05 14:48:26 +02:00
} else {
return '<p><div>' . trans('texts.email_signature') . "\n<br>\$account</div></p>";
}
}
/**
* @param $reminder
*
* @return bool
*/
2017-07-18 21:15:51 +03:00
public function getReminderDate($reminder, $filterEnabled = true)
2017-02-05 14:48:26 +02:00
{
if ($filterEnabled && ! $this->account_email_settings->{"enable_{$reminder}"}) {
2017-02-05 14:48:26 +02:00
return false;
}
$numDays = $this->account_email_settings->{"num_days_{$reminder}"};
$plusMinus = $this->account_email_settings->{"direction_{$reminder}"} == REMINDER_DIRECTION_AFTER ? '-' : '+';
2017-02-05 14:48:26 +02:00
return date('Y-m-d', strtotime("$plusMinus $numDays days"));
}
/**
* @param Invoice $invoice
*
* @return bool|string
*/
2017-07-18 21:15:51 +03:00
public function getInvoiceReminder($invoice, $filterEnabled = true)
2017-02-05 14:48:26 +02:00
{
$reminder = $invoice->isQuote() ? 'quote_reminder' : 'reminder';
2017-02-05 14:48:26 +02:00
for ($i = 1; $i <= 3; $i++) {
if ($date = $this->getReminderDate($reminder.$i, $filterEnabled)) {
if ($this->account_email_settings->{'field_'.$reminder.$i} == REMINDER_FIELD_DUE_DATE) {
if (($invoice->partial && $invoice->partial_due_date == $date)
|| $invoice->due_date == $date) {
return $reminder.$i;
}
} else {
if ($invoice->invoice_date == $date) {
return $reminder.$i;
}
2017-02-05 14:48:26 +02:00
}
}
}
return false;
}
2017-02-06 11:41:16 +02:00
public function setTemplateDefaults($type, $subject, $body)
{
2017-03-31 18:02:56 +03:00
$settings = $this->account_email_settings;
2017-02-06 11:41:16 +02:00
if ($subject) {
2017-03-31 18:02:56 +03:00
$settings->{"email_subject_" . $type} = $subject;
2017-02-06 11:41:16 +02:00
}
if ($body) {
2017-03-31 18:02:56 +03:00
$settings->{"email_template_" . $type} = $body;
2017-02-06 11:41:16 +02:00
}
2017-03-31 18:02:56 +03:00
$settings->save();
2017-02-06 11:41:16 +02:00
}
2017-02-05 14:48:26 +02:00
public function getBccEmail()
{
2017-03-31 18:02:56 +03:00
return $this->isPro() ? $this->account_email_settings->bcc_email : false;
}
2017-03-31 18:02:56 +03:00
public function getReplyToEmail()
{
2017-03-31 18:02:56 +03:00
return $this->isPro() ? $this->account_email_settings->reply_to_email : false;
}
2017-03-31 12:32:28 +03:00
2017-03-31 18:02:56 +03:00
public function getFromEmail()
2017-03-31 12:32:28 +03:00
{
2017-03-31 18:02:56 +03:00
if (! $this->isPro() || ! Utils::isNinja() || Utils::isReseller()) {
2017-03-31 12:32:28 +03:00
return false;
}
2017-03-31 18:02:56 +03:00
return Domain::getEmailFromId($this->domain_id);
2017-03-31 12:32:28 +03:00
}
public function getDailyEmailLimit()
{
$limit = MAX_EMAILS_SENT_PER_DAY;
$limit += $this->created_at->diffInMonths() * 100;
2019-01-30 22:00:26 +11:00
return min($limit, 5000);
}
}