From 0658b77e714c1fa8afccd207d74b556efc51520d Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 12 Feb 2018 14:30:27 +0200 Subject: [PATCH] Proposals --- .../ProposalTemplateController.php | 43 ++++++++++++--- database/seeds/DatabaseSeeder.php | 1 + database/seeds/ProposalTemplatesSeeder.php | 40 +++++--------- database/seeds/UpdateSeeder.php | 1 + resources/lang/en/texts.php | 1 + .../views/proposals/templates/edit.blade.php | 53 +++++++++++++------ 6 files changed, 88 insertions(+), 51 deletions(-) diff --git a/app/Http/Controllers/ProposalTemplateController.php b/app/Http/Controllers/ProposalTemplateController.php index 2b7569eee1d3..40c05e0c9fda 100644 --- a/app/Http/Controllers/ProposalTemplateController.php +++ b/app/Http/Controllers/ProposalTemplateController.php @@ -51,18 +51,47 @@ class ProposalTemplateController extends BaseController public function create(ProposalTemplateRequest $request) { - $data = [ - 'account' => auth()->user()->account, + $data = array_merge($this->getViewmodel(), [ 'template' => null, 'method' => 'POST', 'url' => 'proposals/templates', 'title' => trans('texts.new_proposal_template'), - 'templates' => ProposalTemplate::scope()->orderBy('name')->get(), - ]; + ]); return View::make('proposals/templates/edit', $data); } + private function getViewmodel() + { + $customTemplates = ProposalTemplate::scope()->orderBy('name')->get(); + $defaultTemplates = ProposalTemplate::whereNull('account_id')->orderBy('public_id')->get(); + + $customLabel = trans('texts.custom'); + $defaultLabel = trans('texts.default'); + + foreach ($customTemplates as $template) { + if (! isset($options[$customLabel])) { + $options[$customLabel] = []; + } + $options[trans('texts.custom')][$template->public_id] = $template->name; + } + foreach ($defaultTemplates as $template) { + if (! isset($options[$defaultLabel])) { + $options[$defaultLabel] = []; + } + $options[trans('texts.default')][$template->public_id] = $template->name; + } + + $data = [ + 'account' => auth()->user()->account, + 'customTemplates' => $customTemplates, + 'defaultTemplates' => $defaultTemplates, + 'templateOptions' => $options, + ]; + + return $data; + } + public function show($publicId) { Session::reflash(); @@ -86,14 +115,12 @@ class ProposalTemplateController extends BaseController $url = 'proposals/templates/' . $template->public_id; } - $data = [ - 'account' => auth()->user()->account, + $data = array_merge($this->getViewmodel(), [ 'template' => $template, 'method' => $method, 'url' => $url, 'title' => trans('texts.edit_proposal_template'), - 'templates' => ProposalTemplate::scope()->orderBy('name')->get(), - ]; + ]); return View::make('proposals/templates/edit', $data); } diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 9ed4573353c0..2849d63436b1 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -27,6 +27,7 @@ class DatabaseSeeder extends Seeder $this->call('GatewayTypesSeeder'); $this->call('BanksSeeder'); $this->call('InvoiceStatusSeeder'); + $this->call('ProposalTemplatesSeeder'); $this->call('PaymentStatusSeeder'); $this->call('CurrenciesSeeder'); $this->call('DateFormatsSeeder'); diff --git a/database/seeds/ProposalTemplatesSeeder.php b/database/seeds/ProposalTemplatesSeeder.php index 0afbeb8111eb..cbf71112e9e2 100644 --- a/database/seeds/ProposalTemplatesSeeder.php +++ b/database/seeds/ProposalTemplatesSeeder.php @@ -23,34 +23,22 @@ class ProposalTemplatesSeeder extends Seeder for ($i = 0; $i < count($designs); $i++) { $design = $designs[$i]; - $fileName = storage_path() . '/templates/' . strtolower($design) . '.js'; - if (file_exists($fileName)) { - $pdfmake = file_get_contents($fileName); - if ($pdfmake) { - $record = InvoiceDesign::whereName($design)->first(); - if (! $record) { - $record = new InvoiceDesign(); - $record->id = $i + 1; - $record->name = $design; - } - $record->pdfmake = json_encode(json_decode($pdfmake)); // remove the white space - $record->save(); + $baseFileName = storage_path() . '/templates/' . strtolower($design); + $htmlFileName = $baseFileName . '.html'; + $cssFileName = $baseFileName . '.css'; + if (file_exists($htmlFileName) && file_exists($cssFileName)) { + $template = ProposalTemplate::whereName($design)->whereAccountId(0)->first(); + + if (! $template) { + $template = new ProposalTemplate(); + $template->public_id = $i + 1; + $template->name = $design; } + + $template->html = file_get_contents($htmlFileName); + $template->css = file_get_contents($cssFileName); + $template->save(); } } - - for ($i = 1; $i <= 3; $i++) { - $name = 'Custom' . $i; - $id = $i + 10; - - if (InvoiceDesign::whereName($name)->orWhere('id', '=', $id)->first()) { - continue; - } - - InvoiceDesign::create([ - 'id' => $id, - 'name' => $name, - ]); - } } } diff --git a/database/seeds/UpdateSeeder.php b/database/seeds/UpdateSeeder.php index 77d617603a33..6021f7f37a2b 100644 --- a/database/seeds/UpdateSeeder.php +++ b/database/seeds/UpdateSeeder.php @@ -21,6 +21,7 @@ class UpdateSeeder extends Seeder $this->call('CurrenciesSeeder'); $this->call('DateFormatsSeeder'); $this->call('InvoiceDesignsSeeder'); + $this->call('ProposalTemplatesSeeder'); $this->call('PaymentTermsSeeder'); $this->call('PaymentTypesSeeder'); $this->call('LanguageSeeder'); diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 03bdad392ce3..dc89429d56f7 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2733,6 +2733,7 @@ $LANG = array( 'proposal_subject' => 'New proposal :number from :account', 'proposal_message' => 'To view your proposal for :amount, click the link below.', 'emailed_proposal' => 'Successfully emailed proposal', + 'load_template' => 'Load Template', ); diff --git a/resources/views/proposals/templates/edit.blade.php b/resources/views/proposals/templates/edit.blade.php index 8412c79dec5d..0cc185ec89dd 100644 --- a/resources/views/proposals/templates/edit.blade.php +++ b/resources/views/proposals/templates/edit.blade.php @@ -34,12 +34,6 @@
{!! Former::text('name') !!} - -
{!! Former::textarea('private_notes') !!} @@ -51,6 +45,15 @@
+ + {!! Former::select() + ->style('display:inline;width:170px;background-color:white !important') + ->placeholder(trans('texts.load_template')) + ->onchange('onTemplateSelectChange()') + ->addClass('template-select') + ->options($templateOptions) + ->raw() !!} + @include('proposals.grapesjs_help') {!! Button::normal(trans('texts.cancel')) @@ -74,8 +77,11 @@