1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Break out email settings

This commit is contained in:
Hillel Coren 2017-03-31 18:02:56 +03:00
parent 8687d55163
commit dac8c85185
13 changed files with 260 additions and 52 deletions

View File

@ -789,10 +789,18 @@ class AccountController extends BaseController
public function saveEmailSettings(SaveEmailSettings $request)
{
$account = $request->user()->account;
$account->fill($request->all());
$account->bcc_email = $request->bcc_email;
$account->pdf_email_attachment = boolval(Input::get('pdf_email_attachment'));
$account->document_email_attachment = boolval(Input::get('document_email_attachment'));
$account->enable_email_markup = boolval(Input::get('enable_email_markup'));
$account->email_design_id = Input::get('email_design_id');
$account->email_footer = trim(Input::get('email_footer'));
$account->save();
$settings = $account->account_email_settings;
$settings->bcc_email = trim(Input::get('bcc_email'));
$settings->reply_to_email = trim(Input::get('reply_to_email'));
$settings->save();
return redirect('settings/' . ACCOUNT_EMAIL_SETTINGS)
->with('message', trans('texts.updated_settings'));
}
@ -808,11 +816,11 @@ class AccountController extends BaseController
foreach ([ENTITY_INVOICE, ENTITY_QUOTE, ENTITY_PAYMENT, REMINDER1, REMINDER2, REMINDER3] as $type) {
$subjectField = "email_subject_{$type}";
$subject = Input::get($subjectField, $account->getEmailSubject($type));
$account->$subjectField = ($subject == $account->getDefaultEmailSubject($type) ? null : $subject);
$account->account_email_settings->$subjectField = ($subject == $account->getDefaultEmailSubject($type) ? null : $subject);
$bodyField = "email_template_{$type}";
$body = Input::get($bodyField, $account->getEmailTemplate($type));
$account->$bodyField = ($body == $account->getDefaultEmailTemplate($type) ? null : $body);
$account->account_email_settings->$bodyField = ($body == $account->getDefaultEmailTemplate($type) ? null : $body);
}
foreach ([REMINDER1, REMINDER2, REMINDER3] as $type) {
@ -824,6 +832,7 @@ class AccountController extends BaseController
}
$account->save();
$account->account_email_settings->save();
Session::flash('message', trans('texts.updated_settings'));
}

View File

@ -23,6 +23,7 @@ class SaveEmailSettings extends Request
{
return [
'bcc_email' => 'email',
'reply_to_email' => 'email',
];
}
}

View File

@ -173,7 +173,6 @@ class Account extends Eloquent
'payment_type_id',
'gateway_fee_enabled',
'reset_counter_date',
'reply_to_email',
];
/**
@ -278,6 +277,14 @@ class Account extends Eloquent
return $this->hasMany('App\Models\AccountGatewaySettings');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function account_email_settings()
{
return $this->hasOne('App\Models\AccountEmailSettings');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/

View File

@ -0,0 +1,32 @@
<?php
namespace App\Models;
use Eloquent;
/**
* Class Account.
*/
class AccountEmailSettings extends Eloquent
{
/**
* @var array
*/
protected $fillable = [
'bcc_email',
'reply_to_email',
'email_subject_invoice',
'email_subject_quote',
'email_subject_payment',
'email_template_invoice',
'email_template_quote',
'email_template_payment',
'email_subject_reminder1',
'email_subject_reminder2',
'email_subject_reminder3',
'email_template_reminder1',
'email_template_reminder2',
'email_template_reminder3',
];
}

View File

@ -33,7 +33,7 @@ trait SendsEmails
{
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
$field = "email_subject_{$entityType}";
$value = $this->$field;
$value = $this->account_email_settings->$field;
if ($value) {
return preg_replace("/\r\n|\r|\n/", ' ', $value);
@ -84,7 +84,7 @@ trait SendsEmails
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
$field = "email_template_{$entityType}";
$template = $this->$field;
$template = $this->account_email_settings->$field;
}
if (! $template) {
@ -158,20 +158,27 @@ trait SendsEmails
public function setTemplateDefaults($type, $subject, $body)
{
$settings = $this->account_email_settings;
if ($subject) {
$this->{"email_subject_" . $type} = $subject;
$settings->{"email_subject_" . $type} = $subject;
}
if ($body) {
$this->{"email_template_" . $type} = $body;
$settings->{"email_template_" . $type} = $body;
}
$this->save();
$settings->save();
}
public function getBccEmail()
{
return $this->isPro() ? $this->bcc_email : false;
return $this->isPro() ? $this->account_email_settings->bcc_email : false;
}
public function getReplyToEmail()
{
return $this->isPro() ? $this->account_email_settings->reply_to_email : false;
}
public function getFromEmail()
@ -182,13 +189,4 @@ trait SendsEmails
return Domain::getEmailFromId($this->domain_id);
}
public function getReplyToEmail()
{
if (! $this->isPro()) {
return false;
}
return $this->reply_to_email;
}
}

View File

@ -3,6 +3,7 @@
namespace App\Ninja\Repositories;
use App\Models\Account;
use App\Models\AccountEmailSettings;
use App\Models\AccountGateway;
use App\Models\AccountToken;
use App\Models\Client;
@ -82,6 +83,9 @@ class AccountRepository
$account->users()->save($user);
$emailSettings = new AccountEmailSettings();
$account->account_email_settings()->save($emailSettings);
return $account;
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Ninja\Transformers;
use App\Models\AccountEmailSettings;
/**
* Class AccountTransformer.
*/
class AccountEmailSettingsTransformer extends EntityTransformer
{
/**
* @var array
*/
protected $defaultIncludes = [];
/**
* @var array
*/
protected $availableIncludes = [];
/**
* @param Account $settings
*
* @throws \Laracasts\Presenter\Exceptions\PresenterException
*
* @return array
*/
public function transform(AccountEmailSettings $settings)
{
return [
'reply_to_email' => $settings->reply_to_email,
'bcc_email' => $settings->bcc_email,
'email_subject_invoice' => $settings->email_subject_invoice,
'email_subject_quote' => $settings->email_subject_quote,
'email_subject_payment' => $settings->email_subject_payment,
'email_template_invoice' => $settings->email_template_invoice,
'email_template_quote' => $settings->email_template_quote,
'email_template_payment' => $settings->email_template_payment,
'email_subject_reminder1' => $settings->email_subject_reminder1,
'email_subject_reminder2' => $settings->email_subject_reminder2,
'email_subject_reminder3' => $settings->email_subject_reminder3,
'email_template_reminder1' => $settings->email_template_reminder1,
'email_template_reminder2' => $settings->email_template_reminder2,
'email_template_reminder3' => $settings->email_template_reminder3,
];
}
}

View File

@ -18,6 +18,7 @@ class AccountTransformer extends EntityTransformer
'tax_rates',
'expense_categories',
'projects',
'account_email_settings',
];
/**
@ -29,6 +30,18 @@ class AccountTransformer extends EntityTransformer
'payments',
];
/**
* @param Account $account
*
* @return \League\Fractal\Resource\Collection
*/
public function includeAccountEmailSettings(Account $account)
{
$transformer = new AccountEmailSettingsTransformer($account, $this->serializer);
return $this->includeItem($account->account_email_settings, $transformer, 'account_email_settings');
}
/**
* @param Account $account
*
@ -184,9 +197,6 @@ class AccountTransformer extends EntityTransformer
'quote_number_prefix' => $account->quote_number_prefix,
'quote_number_counter' => $account->quote_number_counter,
'share_counter' => (bool) $account->share_counter,
'email_template_invoice' => $account->email_template_invoice,
'email_template_quote' => $account->email_template_quote,
'email_template_payment' => $account->email_template_payment,
'token_billing_type_id' => (int) $account->token_billing_type_id,
'invoice_footer' => $account->invoice_footer,
'pdf_email_attachment' => (bool) $account->pdf_email_attachment,
@ -195,15 +205,6 @@ class AccountTransformer extends EntityTransformer
'custom_design' => $account->custom_design,
'show_item_taxes' => (bool) $account->show_item_taxes,
'military_time' => (bool) $account->military_time,
'email_subject_invoice' => $account->email_subject_invoice,
'email_subject_quote' => $account->email_subject_quote,
'email_subject_payment' => $account->email_subject_payment,
'email_subject_reminder1' => $account->email_subject_reminder1,
'email_subject_reminder2' => $account->email_subject_reminder2,
'email_subject_reminder3' => $account->email_subject_reminder3,
'email_template_reminder1' => $account->email_template_reminder1,
'email_template_reminder2' => $account->email_template_reminder2,
'email_template_reminder3' => $account->email_template_reminder3,
'enable_reminder1' => $account->enable_reminder1,
'enable_reminder2' => $account->enable_reminder2,
'enable_reminder3' => $account->enable_reminder3,
@ -265,7 +266,6 @@ class AccountTransformer extends EntityTransformer
'payment_type_id' => (int) $account->payment_type_id,
'gateway_fee_enabled' => (bool) $account->gateway_fee_enabled,
'reset_counter_date' => $account->reset_counter_date,
'reply_to_email' => $account->reply_to_email,
];
}
}

View File

@ -170,6 +170,10 @@ class ImportService
$account = Auth::user()->account;
$account->fill($settings);
$account->save();
$emailSettings = $account->account_email_settings;
$emailSettings->fill($settings['account_email_settings']);
$emailSettings->save();
}
if ($includeData) {

View File

@ -24,7 +24,6 @@ class AddGatewayFeeLocation extends Migration
}
$table->boolean('gateway_fee_enabled')->default(0);
$table->date('reset_counter_date')->nullable();
$table->string('reply_to_email')->nullable();
});
Schema::table('clients', function ($table) {
@ -41,6 +40,78 @@ class AddGatewayFeeLocation extends Migration
left join invoices on invoices.id = invoice_items.invoice_id
set invoice_item_type_id = 2
where invoices.has_tasks = 1');
Schema::create('account_email_settings', function ($table) {
$table->increments('id');
$table->unsignedInteger('account_id')->index();
$table->timestamps();
$table->string('reply_to_email')->nullable();
$table->string('bcc_email')->nullable();
$table->string('email_subject_invoice');
$table->string('email_subject_quote');
$table->string('email_subject_payment');
$table->text('email_template_invoice');
$table->text('email_template_quote');
$table->text('email_template_payment');
$table->string('email_subject_reminder1');
$table->string('email_subject_reminder2');
$table->string('email_subject_reminder3');
$table->text('email_template_reminder1');
$table->text('email_template_reminder2');
$table->text('email_template_reminder3');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
});
DB::statement('insert into account_email_settings (account_id,
bcc_email,
email_subject_invoice,
email_subject_quote,
email_subject_payment,
email_template_invoice,
email_template_quote,
email_template_payment,
email_subject_reminder1,
email_subject_reminder2,
email_subject_reminder3,
email_template_reminder1,
email_template_reminder2,
email_template_reminder3
)
select id,
bcc_email,
email_subject_invoice,
email_subject_quote,
email_subject_payment,
email_template_invoice,
email_template_quote,
email_template_payment,
email_subject_reminder1,
email_subject_reminder2,
email_subject_reminder3,
email_template_reminder1,
email_template_reminder2,
email_template_reminder3
from accounts;');
Schema::table('accounts', function ($table) {
$table->dropColumn('email_subject_invoice');
$table->dropColumn('email_subject_quote');
$table->dropColumn('email_subject_payment');
$table->dropColumn('email_template_invoice');
$table->dropColumn('email_template_quote');
$table->dropColumn('email_template_payment');
$table->dropColumn('email_subject_reminder1');
$table->dropColumn('email_subject_reminder2');
$table->dropColumn('email_subject_reminder3');
$table->dropColumn('email_template_reminder1');
$table->dropColumn('email_template_reminder2');
$table->dropColumn('email_template_reminder3');
});
}
/**
@ -53,7 +124,6 @@ class AddGatewayFeeLocation extends Migration
Schema::table('accounts', function ($table) {
$table->dropColumn('gateway_fee_enabled');
$table->dropColumn('reset_counter_date');
$table->dropColumn('reply_to_email');
});
Schema::table('clients', function ($table) {

View File

@ -1,6 +1,7 @@
<?php
use App\Models\Account;
use App\Models\AccountEmailSettings;
use App\Models\Affiliate;
use App\Models\Client;
use App\Models\Company;
@ -44,6 +45,10 @@ class UserTableSeeder extends Seeder
'pdf_email_attachment' => true,
]);
$emailSettings = AccountEmailSettings::create([
'account_id' => $account->id
]);
$user = User::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,

File diff suppressed because one or more lines are too long

View File

@ -26,6 +26,8 @@
{{ Former::populateField('pdf_email_attachment', intval($account->pdf_email_attachment)) }}
{{ Former::populateField('document_email_attachment', intval($account->document_email_attachment)) }}
{{ Former::populateField('enable_email_markup', intval($account->enable_email_markup)) }}
{{ Former::populateField('bcc_email', $account->account_email_settings->bcc_email) }}
{{ Former::populateField('reply_to_email', $account->account_email_settings->reply_to_email) }}
<div class="panel panel-default">
<div class="panel-heading">