moved logo method to view composer

This commit is contained in:
denisdulici 2018-04-26 16:04:27 +03:00
parent 88ba1968c1
commit c283796fe1
4 changed files with 65 additions and 94 deletions

View File

@ -511,9 +511,7 @@ class Bills extends Controller
{
$bill = $this->prepareBill($bill);
$logo = $this->getLogo($bill);
return view($bill->template_path, compact('bill', 'logo'));
return view($bill->template_path, compact('bill'));
}
/**
@ -527,9 +525,7 @@ class Bills extends Controller
{
$bill = $this->prepareBill($bill);
$logo = $this->getLogo($bill);
$html = view($bill->template_path, compact('bill', 'logo'))->render();
$html = view($bill->template_path, compact('bill'))->render();
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
@ -736,39 +732,4 @@ class Bills extends Controller
'sort_order' => $sort_order,
]);
}
protected function getLogo($bill)
{
$logo = '';
$media_id = setting('general.company_logo');
if (isset($bill->vendor->logo) && !empty($bill->vendor->logo->id)) {
$media_id = $bill->vendor->logo->id;
}
$media = Media::find($media_id);
if (!empty($media)) {
$path = Storage::path($media->getDiskPath());
if (!is_file($path)) {
return $logo;
}
} else {
$path = asset('public/img/company.png');
}
$image = Image::make($path)->encode()->getEncoded();
if (empty($image)) {
return $logo;
}
$extension = File::extension($path);
$logo = 'data:image/' . $extension . ';base64,' . base64_encode($image);
return $logo;
}
}

View File

@ -107,17 +107,9 @@ class Invoices extends Controller
$categories = Category::enabled()->type('income')->pluck('name', 'id');
$recurrings = [
'0' => trans('general.no'),
'1' => trans('recurring.weekly'),
'2' => trans('recurring.monthly'),
'3' => trans('recurring.yearly'),
'4' => trans('recurring.custom'),
];
$number = $this->getNextInvoiceNumber();
return view('incomes.invoices.create', compact('customers', 'currencies', 'items', 'taxes', 'categories', 'recurrings', 'number'));
return view('incomes.invoices.create', compact('customers', 'currencies', 'items', 'taxes', 'categories', 'number'));
}
/**
@ -554,9 +546,7 @@ class Invoices extends Controller
$invoice = $this->prepareInvoice($invoice);
$logo = $this->getLogo();
$html = view($invoice->template_path, compact('invoice', 'logo'))->render();
$html = view($invoice->template_path, compact('invoice'))->render();
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
@ -610,9 +600,7 @@ class Invoices extends Controller
{
$invoice = $this->prepareInvoice($invoice);
$logo = $this->getLogo();
return view($invoice->template_path, compact('invoice', 'logo'));
return view($invoice->template_path, compact('invoice'));
}
/**
@ -626,9 +614,7 @@ class Invoices extends Controller
{
$invoice = $this->prepareInvoice($invoice);
$logo = $this->getLogo();
$html = view($invoice->template_path, compact('invoice', 'logo'))->render();
$html = view($invoice->template_path, compact('invoice'))->render();
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
@ -877,39 +863,4 @@ class Invoices extends Controller
'sort_order' => $sort_order,
]);
}
protected function getLogo()
{
$logo = '';
$media_id = setting('general.company_logo');
if (setting('general.invoice_logo')) {
$media_id = setting('general.invoice_logo');
}
$media = Media::find($media_id);
if (!empty($media)) {
$path = Storage::path($media->getDiskPath());
if (!is_file($path)) {
return $logo;
}
} else {
$path = asset('public/img/company.png');
}
$image = Image::make($path)->encode()->getEncoded();
if (empty($image)) {
return $logo;
}
$extension = File::extension($path);
$logo = 'data:image/' . $extension . ';base64,' . base64_encode($image);
return $logo;
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace App\Http\ViewComposers;
use App\Models\Common\Media;
use Illuminate\View\View;
use File;
use Image;
use Storage;
class Logo
{
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$logo = '';
$media_id = setting('general.company_logo');
if (setting('general.invoice_logo')) {
$media_id = setting('general.invoice_logo');
}
$media = Media::find($media_id);
if (!empty($media)) {
$path = Storage::path($media->getDiskPath());
if (!is_file($path)) {
return $logo;
}
} else {
$path = asset('public/img/company.png');
}
$image = Image::make($path)->encode()->getEncoded();
if (empty($image)) {
return $logo;
}
$extension = File::extension($path);
$logo = 'data:image/' . $extension . ';base64,' . base64_encode($image);
$view->with(['logo' => $logo]);
}
}

View File

@ -38,6 +38,11 @@ class ViewComposerServiceProvider extends ServiceProvider
View::composer(
'modules.*', 'App\Http\ViewComposers\Modules'
);
// Add logo
View::composer(
['incomes.invoices.invoice', 'expenses.bills.bill'], 'App\Http\ViewComposers\Logo'
);
}
/**