From ba8419356da854f6e08fe63eff369495b2448170 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 28 Jul 2015 10:00:00 +0300 Subject: [PATCH] Moved invoice design files --- .gitignore | 9 +---- app/Http/Controllers/AccountController.php | 2 +- app/Http/Controllers/InvoiceController.php | 4 +-- app/Http/Controllers/QuoteController.php | 2 +- app/Http/Middleware/StartupCheck.php | 1 + app/Http/routes.php | 3 ++ app/Models/InvoiceDesign.php | 24 ++++++++----- app/Ninja/Mailers/ContactMailer.php | 2 ++ app/Ninja/Mailers/Mailer.php | 6 +++- .../2015_07_27_183830_add_pdfmake_support.php | 34 +++++++++++++++++++ database/seeds/PaymentLibrariesSeeder.php | 31 +++++++++++++++++ public/js/built.js | 2 +- public/js/pdf.pdfmake.js | 2 +- .../views/accounts/customize_design.blade.php | 6 +++- {public/js => storage}/templates/bold.js | 0 {public/js => storage}/templates/clean.js | 0 {public/js => storage}/templates/modern.js | 0 {public/js => storage}/templates/plain.js | 0 18 files changed, 103 insertions(+), 25 deletions(-) create mode 100644 database/migrations/2015_07_27_183830_add_pdfmake_support.php rename {public/js => storage}/templates/bold.js (100%) rename {public/js => storage}/templates/clean.js (100%) rename {public/js => storage}/templates/modern.js (100%) rename {public/js => storage}/templates/plain.js (100%) diff --git a/.gitignore b/.gitignore index 6a6541a79431..3089745bd2b1 100644 --- a/.gitignore +++ b/.gitignore @@ -28,11 +28,4 @@ /.phpstorm.meta.php /_ide_helper.php /.idea -/.project - -/public/js/templates/business.js -/public/js/templates/creative.js -/public/js/templates/elegant.js -/public/js/templates/hipster.js -/public/js/templates/photo.js -/public/js/templates/playful.js \ No newline at end of file +/.project \ No newline at end of file diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index b77278433fdd..204e270c7e3a 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -233,7 +233,7 @@ class AccountController extends BaseController $data['invoice'] = $invoice; $data['invoiceLabels'] = json_decode($account->invoice_labels) ?: []; $data['title'] = trans('texts.invoice_design'); - $data['invoiceDesigns'] = InvoiceDesign::availableDesigns($subSection == ACCOUNT_CUSTOMIZE_DESIGN); + $data['invoiceDesigns'] = InvoiceDesign::getDesigns($subSection == ACCOUNT_CUSTOMIZE_DESIGN); $design = false; foreach ($data['invoiceDesigns'] as $item) { diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index e477944ad11b..1939ce9c65da 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -405,7 +405,7 @@ class InvoiceController extends BaseController 'sizes' => Cache::get('sizes'), 'paymentTerms' => Cache::get('paymentTerms'), 'industries' => Cache::get('industries'), - 'invoiceDesigns' => InvoiceDesign::availableDesigns(), + 'invoiceDesigns' => InvoiceDesign::getDesigns(), 'frequencies' => array( 1 => 'Weekly', 2 => 'Two weeks', @@ -646,7 +646,7 @@ class InvoiceController extends BaseController 'invoice' => $invoice, 'versionsJson' => json_encode($versionsJson), 'versionsSelect' => $versionsSelect, - 'invoiceDesigns' => InvoiceDesign::availableDesigns(), + 'invoiceDesigns' => InvoiceDesign::getDesigns(), ]; return View::make('invoices.history', $data); diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index ebfc8a6629c0..b8dfd095e44b 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -156,7 +156,7 @@ class QuoteController extends BaseController 'sizes' => Cache::get('sizes'), 'paymentTerms' => Cache::get('paymentTerms'), 'industries' => Cache::get('industries'), - 'invoiceDesigns' => InvoiceDesign::availableDesigns(), + 'invoiceDesigns' => InvoiceDesign::getDesigns(), 'invoiceLabels' => Auth::user()->account->getInvoiceLabels() ]; } diff --git a/app/Http/Middleware/StartupCheck.php b/app/Http/Middleware/StartupCheck.php index 8c5299311661..a6c2741977c8 100644 --- a/app/Http/Middleware/StartupCheck.php +++ b/app/Http/Middleware/StartupCheck.php @@ -49,6 +49,7 @@ class StartupCheck 'paymentTerms' => 'App\Models\PaymentTerm', 'paymentTypes' => 'App\Models\PaymentType', 'countries' => 'App\Models\Country', + 'invoiceDesigns' => 'App\Models\InvoiceDesign', ]; foreach ($cachedTables as $name => $class) { if (Input::has('clear_cache')) { diff --git a/app/Http/routes.php b/app/Http/routes.php index 7725b1043154..156e66ea6573 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -439,6 +439,7 @@ function otrans($text) } } +/* // Log all SQL queries to laravel.log Event::listen('illuminate.query', function($query, $bindings, $time, $name) { @@ -463,6 +464,8 @@ Event::listen('illuminate.query', function($query, $bindings, $time, $name) Log::info($query, $data); }); +*/ + /* if (Auth::check() && Auth::user()->id === 1) diff --git a/app/Models/InvoiceDesign.php b/app/Models/InvoiceDesign.php index 6a970ffb5942..d7682c24f742 100644 --- a/app/Models/InvoiceDesign.php +++ b/app/Models/InvoiceDesign.php @@ -2,32 +2,38 @@ use Eloquent; use Auth; +use Cache; use App\Models\InvoiceDesign; class InvoiceDesign extends Eloquent { public $timestamps = false; - public function scopeAvailableDesigns($query, $utf8 = false) + public static function getDesigns($forceUtf8 = false) { $account = Auth::user()->account; - $designs = $query->where('id', '<=', Auth::user()->maxInvoiceDesignId())->orderBy('id')->get(); + $designs = Cache::get('invoiceDesigns'); + $utf8 = $forceUtf8 || $account->utf8_invoices; foreach ($designs as $design) { - $fileName = public_path(strtolower("js/templates/{$design->name}.js")); - if (($utf8 || Auth::user()->account->utf8_invoices) && file_exists($fileName)) { - $design->javascript = file_get_contents($fileName); + if ($design->id > Auth::user()->maxInvoiceDesignId()) { + $designs->pull($design->id); } - + + if ($utf8) { + $design->javascript = $design->pdfmake; + } + $design->pdfmake = null; + if ($design->id == CUSTOM_DESIGN) { - if ($account->utf8_invoices && $account->custom_design) { + if ($utf8 && $account->custom_design) { $design->javascript = $account->custom_design; } else { $designs->pop(); } } } - + return $designs; } -} +} \ No newline at end of file diff --git a/app/Ninja/Mailers/ContactMailer.php b/app/Ninja/Mailers/ContactMailer.php index e7327f58959f..f68633e83414 100644 --- a/app/Ninja/Mailers/ContactMailer.php +++ b/app/Ninja/Mailers/ContactMailer.php @@ -73,6 +73,8 @@ class ContactMailer extends Mailer } Event::fire(new InvoiceSent($invoice)); + + return $response; } public function sendPaymentConfirmation(Payment $payment) diff --git a/app/Ninja/Mailers/Mailer.php b/app/Ninja/Mailers/Mailer.php index ffd0abdadee8..e727b2a9cef4 100644 --- a/app/Ninja/Mailers/Mailer.php +++ b/app/Ninja/Mailers/Mailer.php @@ -30,7 +30,11 @@ class Mailer } } - $message->to($toEmail)->from($fromEmail, $fromName)->replyTo($replyEmail, $fromName)->subject($subject); + $message->to($toEmail) + ->from($fromEmail, $fromName) + ->replyTo($replyEmail, $fromName) + ->subject($subject); + }); return true; diff --git a/database/migrations/2015_07_27_183830_add_pdfmake_support.php b/database/migrations/2015_07_27_183830_add_pdfmake_support.php new file mode 100644 index 000000000000..19ca03a25b92 --- /dev/null +++ b/database/migrations/2015_07_27_183830_add_pdfmake_support.php @@ -0,0 +1,34 @@ +text('pdfmake')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('invoice_designs', function($table) + { + $table->dropColumn('pdfmake'); + }); + } + +} diff --git a/database/seeds/PaymentLibrariesSeeder.php b/database/seeds/PaymentLibrariesSeeder.php index f6b0f80526ff..8f36a42fa2d6 100644 --- a/database/seeds/PaymentLibrariesSeeder.php +++ b/database/seeds/PaymentLibrariesSeeder.php @@ -5,6 +5,7 @@ use App\Models\PaymentTerm; use App\Models\Currency; use App\Models\DateFormat; use App\Models\DatetimeFormat; +use App\Models\InvoiceDesign; class PaymentLibrariesSeeder extends Seeder { @@ -16,6 +17,7 @@ class PaymentLibrariesSeeder extends Seeder $this->createPaymentTerms(); $this->createDateFormats(); $this->createDatetimeFormats(); + $this->createInvoiceDesigns(); } private function createGateways() { @@ -131,4 +133,33 @@ class PaymentLibrariesSeeder extends Seeder } } + private function createInvoiceDesigns() { + $designs = [ + 'Clean', + 'Bold', + 'Modern', + 'Plain', + 'Business', + 'Creative', + 'Elegant', + 'Hipster', + 'Playful', + 'Photo', + ]; + + foreach ($designs as $design) { + $fileName = storage_path() . '/templates/' . strtolower($design) . '.js'; + $pdfmake = file_get_contents($fileName); + if ($pdfmake) { + $record = InvoiceDesign::whereName($design)->first(); + if (!$record) { + $record = new InvoiceDesign; + $record->name = $design; + } + $record->pdfmake = $pdfmake; + $record->save(); + } + } + } + } diff --git a/public/js/built.js b/public/js/built.js index 8e1227e06416..f671d6057599 100644 --- a/public/js/built.js +++ b/public/js/built.js @@ -33294,7 +33294,7 @@ function GetPdfMake(invoice, javascript, callback) { //console.log(javascript); var dd = JSON.parse(javascript, jsonCallBack); - if (!invoice.is_pro && dd.footer.hasOwnProperty('columns')) { + if (!invoice.is_pro && dd.hasOwnProperty('footer') && dd.footer.hasOwnProperty('columns')) { dd.footer.columns.push({image: logoImages.imageLogo1, alignment: 'right', width: 130}) } diff --git a/public/js/pdf.pdfmake.js b/public/js/pdf.pdfmake.js index 0f7b1639f832..23b3aef51fa7 100644 --- a/public/js/pdf.pdfmake.js +++ b/public/js/pdf.pdfmake.js @@ -52,7 +52,7 @@ function GetPdfMake(invoice, javascript, callback) { //console.log(javascript); var dd = JSON.parse(javascript, jsonCallBack); - if (!invoice.is_pro && dd.footer.hasOwnProperty('columns')) { + if (!invoice.is_pro && dd.hasOwnProperty('footer') && dd.footer.hasOwnProperty('columns')) { dd.footer.columns.push({image: logoImages.imageLogo1, alignment: 'right', width: 130}) } diff --git a/resources/views/accounts/customize_design.blade.php b/resources/views/accounts/customize_design.blade.php index d5e93afe6848..b14268a5ac9e 100644 --- a/resources/views/accounts/customize_design.blade.php +++ b/resources/views/accounts/customize_design.blade.php @@ -68,7 +68,11 @@ { editorSection = section; editor.set(customDesign[section]); - editor.expandAll(); + + // the function throws an error if the editor is in code view + try { + editor.expandAll(); + } catch(err) {} } function saveEditor(data) diff --git a/public/js/templates/bold.js b/storage/templates/bold.js similarity index 100% rename from public/js/templates/bold.js rename to storage/templates/bold.js diff --git a/public/js/templates/clean.js b/storage/templates/clean.js similarity index 100% rename from public/js/templates/clean.js rename to storage/templates/clean.js diff --git a/public/js/templates/modern.js b/storage/templates/modern.js similarity index 100% rename from public/js/templates/modern.js rename to storage/templates/modern.js diff --git a/public/js/templates/plain.js b/storage/templates/plain.js similarity index 100% rename from public/js/templates/plain.js rename to storage/templates/plain.js