From 24c9121b1b7accf931d716a9fae8a72321b186c5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 25 Mar 2020 13:50:08 +1100 Subject: [PATCH] Template Previews (#3527) * Fixes for add / archiving / deleting users * Working on templates * Fixes for templating * Fixes for templating * Add referral code to account creation * Fixes for development environment --- app/DataMapper/EmailTemplateDefaults.php | 50 +++++++++++++++ app/Http/Controllers/TemplateController.php | 61 ++++++++++++++++++- app/Jobs/Account/CreateAccount.php | 4 ++ app/Listeners/User/DeletedUserActivity.php | 7 ++- app/Models/Company.php | 16 +++++ app/Repositories/ActivityRepository.php | 7 ++- app/Transformers/AccountTransformer.php | 22 +++++++ app/Utils/Ninja.php | 3 + app/Utils/Traits/MakesInvoiceHtml.php | 14 +++++ resources/views/email/template/dark.blade.php | 34 ++++++++--- .../views/email/template/light.blade.php | 34 ++++++++--- 11 files changed, 229 insertions(+), 23 deletions(-) diff --git a/app/DataMapper/EmailTemplateDefaults.php b/app/DataMapper/EmailTemplateDefaults.php index 2ad747375d..a5dc34aa8b 100644 --- a/app/DataMapper/EmailTemplateDefaults.php +++ b/app/DataMapper/EmailTemplateDefaults.php @@ -11,10 +11,60 @@ namespace App\DataMapper; +use Illuminate\Support\Facades\App; use League\CommonMark\CommonMarkConverter; class EmailTemplateDefaults { + + public static function getDefaultTemplate($template, $locale) + { + App::setLocale($locale); + + switch ($template) { + case 'email_template_invoice': + return self::emailInvoiceTemplate(); + break; + case 'email_template_quote': + return self::emailQuoteTemplate(); + break; + case 'email_template_payment': + return self::emailPaymentTemplate(); + break; + case 'email_template_payment_partial': + return self::emailPaymentTemplate(); + break; + case 'email_template_statement': + return self::emailStatementTemplate(); + break; + case 'email_template_reminder1': + return self::emailReminder1Template(); + break; + case 'email_template_reminder2': + return self::emailReminder2Template(); + break; + case 'email_template_reminder3': + return self::emailReminder3Template(); + break; + case 'email_template_reminder_endless': + return self::emailReminderEndlessTemplate(); + break; + case 'email_template_custom1': + return self::emailInvoiceTemplate(); + break; + case 'email_template_custom2': + return self::emailInvoiceTemplate(); + break; + case 'email_template_custom3': + return self::emailInvoiceTemplate(); + break; + + default: + return self::emailInvoiceTemplate(); + break; + } + } + public static function emailInvoiceSubject() { return ctrans('texts.invoice_subject', ['number'=>'$number', 'account'=>'$company.name']); diff --git a/app/Http/Controllers/TemplateController.php b/app/Http/Controllers/TemplateController.php index 3ede516ff2..c4af2ad14a 100644 --- a/app/Http/Controllers/TemplateController.php +++ b/app/Http/Controllers/TemplateController.php @@ -11,7 +11,9 @@ namespace App\Http\Controllers; +use App\DataMapper\EmailTemplateDefaults; use App\Utils\Traits\MakesHash; +use App\Utils\Traits\MakesInvoiceHtml; use App\Utils\Traits\MakesTemplateData; use League\CommonMark\CommonMarkConverter; @@ -19,6 +21,7 @@ class TemplateController extends BaseController { use MakesHash; use MakesTemplateData; + use MakesInvoiceHtml; public function __construct() { @@ -103,13 +106,38 @@ class TemplateController extends BaseController */ public function show() { + $entity_obj = null; + if (request()->has('entity') && request()->has('entity_id')) { $class = 'App\Models\\'.ucfirst(request()->input('entity')); $entity_obj = $class::whereId($this->decodePrimaryKey(request()->input('entity_id')))->company()->first(); } + if($entity_obj){ + $settings_entity = $entity_obj->client; + } + else{ + $settings_entity = auth()->user()->company(); + } + + $subject = request()->input('subject') ?: ''; $body = request()->input('body') ?: ''; + $template = request()->input('template') ?: ''; + $include_wrapper = request()->input('include_wrapper') ?: false; + + if(strlen($template) >1) { + + $custom_template = $settings_entity->getSetting($template); + + if(strlen($custom_template) > 1){ + $body = $custom_template; + } + else { + $body = EmailTemplateDefaults::getDefaultTemplate($template, $settings_entity->locale()); + } + + } $labels = $this->makeFakerLabels(); $values = $this->makeFakerValues(); @@ -118,13 +146,40 @@ class TemplateController extends BaseController $body = str_replace(array_keys($values), array_values($values), $body); $converter = new CommonMarkConverter([ - //'html_input' => 'strip', 'allow_unsafe_links' => false, ]); + $body = $converter->convertToHtml($body); + + if($include_wrapper){ + + $email_style = $settings_entity->getSetting('email_style'); + + $email_style = 'dark'; + + $data['title'] = ''; + $data['body'] = $body; + $data['footer'] = ''; + + if($email_style == 'custom') { + + $wrapper = $settings_entity->getSetting('email_style_custom'); + $this->renderView($wrapper, $data); + } + else { + + $wrapper = $this->getTemplate(); + $body = view($this->getTemplatePath($email_style), $data)->render(); + + } + + + + } + $data = [ - 'subject' => request()->input('subject'), - 'body' => $converter->convertToHtml($body), + 'subject' => $subject, + 'body' => $body, ]; return response()->json($data, 200); diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index ce534adae6..3a022c17ed 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -15,6 +15,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Notification; +use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\Response; class CreateAccount @@ -33,6 +34,9 @@ class CreateAccount return response()->json(array('message' => Ninja::parse()), 401); } $sp794f3f = Account::create($this->request); + $sp794f3f->referral_code = Str::random(32); + $sp794f3f->save(); + $sp035a66 = CreateCompany::dispatchNow($this->request, $sp794f3f); $sp035a66->load('account'); $sp794f3f->default_company_id = $sp035a66->id; diff --git a/app/Listeners/User/DeletedUserActivity.php b/app/Listeners/User/DeletedUserActivity.php index 0d5b3e4bac..298b705f52 100644 --- a/app/Listeners/User/DeletedUserActivity.php +++ b/app/Listeners/User/DeletedUserActivity.php @@ -13,11 +13,16 @@ namespace App\Listeners\User; use App\Models\Activity; use App\Repositories\ActivityRepository; +use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; -class DeletedUserActivity +class DeletedUserActivity implements ShouldQueue { + use Dispatchable, InteractsWithSockets, SerializesModels; + protected $activityRepo; /** * Create the event listener. diff --git a/app/Models/Company.php b/app/Models/Company.php index 8838d9a95b..4580d0850f 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -292,6 +292,22 @@ class Company extends BaseModel return $this->settings->company_logo ?: null; } + public function locale() + { + return $this->getLocale(); + } + + public function getSetting($setting) + { + + if (property_exists($this->settings, $setting) != false) { + return $this->settings->{$setting}; + } + + return null; + + } + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/app/Repositories/ActivityRepository.php b/app/Repositories/ActivityRepository.php index fa1a43ae00..88e7b24890 100644 --- a/app/Repositories/ActivityRepository.php +++ b/app/Repositories/ActivityRepository.php @@ -15,6 +15,7 @@ use App\Models\Activity; use App\Models\Backup; use App\Models\Client; use App\Models\Invoice; +use App\Models\User; use App\Utils\Traits\MakesInvoiceHtml; use Illuminate\Support\Facades\Log; @@ -64,7 +65,11 @@ class ActivityRepository extends BaseRepository if (get_class($entity) == Client::class) { $entity->load('company'); - } else { + } + else if(get_class($entity) == User::class) { + + } + else { $entity->load('company', 'client'); } diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index a9597cb98f..bbce6931fb 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -60,6 +60,28 @@ class AccountTransformer extends EntityTransformer 'id' => (string)$this->encodePrimaryKey($account->id), 'default_url' => config('ninja.app_url'), 'plan' => $account->getPlan(), + 'plan_term' => (string) $account->plan_terms, + 'plan_started' => (string) $account->plan_started, + 'plan_paid' => (string) $account->plan_paid, + 'plan_expires' => (string) $account->plan_expires, + 'user_agent' => (string) $account->user_agent, + 'payment_id' => (string) $account->payment_id, + 'trial_started' => (string) $account->trial_started, + 'trial_plan' => (string) $account->trial_plan, + 'pending_plan' => (string) $account->pending_plan, + 'pending_term' => (string) $account->pending_term, + 'plan_price' => (float) $account->plan_price, + 'pending_plan_price' => (string) $account->pending_plan_price, + 'num_users' => (int) $account->num_users, + 'pending_num_users' => (int) $account->pending_num_users, + 'utm_source' => (string) $account->utm_source, + 'utm_medium' => (string) $account->utm_medium, + 'utm_content' => (string) $account->utm_content, + 'utm_term' => (string) $account->utm_term, + 'discount' => (float) $account->discount, + 'discount_expires' => (string) $account->discount_expires, + 'bluevine_status' => (string) $account->bluevine_status, + 'referral_code' => (string) $account->referral_code, 'latest_version' => (string)$account->latest_version, 'current_version' => (string)config('ninja.app_version'), 'updated_at' => (int)$account->updated_at, diff --git a/app/Utils/Ninja.php b/app/Utils/Ninja.php index 37996af83b..6da562ad3a 100644 --- a/app/Utils/Ninja.php +++ b/app/Utils/Ninja.php @@ -57,6 +57,9 @@ class Ninja public static function boot() { + if(self::isNinjaDev()) + return true; + $data = [ 'license' => config('ninja.license'), ]; diff --git a/app/Utils/Traits/MakesInvoiceHtml.php b/app/Utils/Traits/MakesInvoiceHtml.php index 843c886d7e..4dfb0536b6 100644 --- a/app/Utils/Traits/MakesInvoiceHtml.php +++ b/app/Utils/Traits/MakesInvoiceHtml.php @@ -14,6 +14,7 @@ namespace App\Utils\Traits; use App\Designs\Designer; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Blade; +use Illuminate\Support\Facades\File; use Symfony\Component\Debug\Exception\FatalThrowableError; /** @@ -102,4 +103,17 @@ trait MakesInvoiceHtml return ob_get_clean(); } + + /* + * Returns the base template we will be using. + */ + public function getTemplate(string $template = 'plain') + { + return File::get(resource_path('views/email/template/' . $template . '.blade.php')); + } + + public function getTemplatePath(string $template = 'plain') + { + return 'email.template.' . $template; + } } diff --git a/resources/views/email/template/dark.blade.php b/resources/views/email/template/dark.blade.php index 706ea19174..d536ded96b 100644 --- a/resources/views/email/template/dark.blade.php +++ b/resources/views/email/template/dark.blade.php @@ -1,10 +1,26 @@ -@extends('email.template.master') -@section('title') -{{ $title }} -@endsection -@section('content') +@component('email.template.master', ['design' => 'dark']) + +@slot('header') + @component('email.components.header', ['p' => '', 'logo' => 'https://www.invoiceninja.com/wp-content/uploads/2019/01/InvoiceNinja-Logo-Round-300x300.png']) + + @if(isset($title)) + {{$title}} + @endif + + @endcomponent + {!! $body !!} -@endsection -@section('footer') -{!! $footer !!} -@endsection \ No newline at end of file + +@slot('footer') + + @if(isset($footer)) + {!! $footer !!} + @endif + + @component('email.components.footer', ['url' => 'https://invoiceninja.com', 'url_text' => '© InvoiceNinja']) + For any info, please visit InvoiceNinja. + @endcomponent +@endslot + + +@endcomponent diff --git a/resources/views/email/template/light.blade.php b/resources/views/email/template/light.blade.php index 706ea19174..13622d0784 100644 --- a/resources/views/email/template/light.blade.php +++ b/resources/views/email/template/light.blade.php @@ -1,10 +1,26 @@ -@extends('email.template.master') -@section('title') -{{ $title }} -@endsection -@section('content') +@component('email.template.master', ['design' => 'light']) + +@slot('header') + @component('email.components.header', ['p' => '', 'logo' => 'https://www.invoiceninja.com/wp-content/uploads/2019/01/InvoiceNinja-Logo-Round-300x300.png']) + + @if(isset($title)) + {{$title}} + @endif + + @endcomponent + {!! $body !!} -@endsection -@section('footer') -{!! $footer !!} -@endsection \ No newline at end of file + +@slot('footer') + + @if(isset($footer)) + {!! $footer !!} + @endif + + @component('email.components.footer', ['url' => 'https://invoiceninja.com', 'url_text' => '© InvoiceNinja']) + For any info, please visit InvoiceNinja. + @endcomponent +@endslot + + +@endcomponent