mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 10:54:34 -04:00
Clean up
This commit is contained in:
parent
ec04f3fd1e
commit
a93ff3e1e5
@ -64,26 +64,39 @@ class PreviewController extends BaseController
|
|||||||
{
|
{
|
||||||
Cache::pull("preview_".auth()->user()->id);
|
Cache::pull("preview_".auth()->user()->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function live(PreviewInvoiceRequest $request)
|
/**
|
||||||
|
* Refactor - 2023-10-19
|
||||||
|
*
|
||||||
|
* New method does not require Transactions.
|
||||||
|
*
|
||||||
|
* @param PreviewInvoiceRequest $request
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function live(PreviewInvoiceRequest $request): mixed
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if (Ninja::isHosted() && !in_array($request->getHost(), ['preview.invoicing.co','staging.invoicing.co'])) {
|
||||||
|
return response()->json(['message' => 'This server cannot handle this request.'], 400);
|
||||||
|
}
|
||||||
|
|
||||||
$start = microtime(true);
|
$start = microtime(true);
|
||||||
|
|
||||||
nlog("1 ".$start);
|
/** Build models */
|
||||||
$invitation = $request->resolveInvitation();
|
$invitation = $request->resolveInvitation();
|
||||||
$client = $request->getClient();
|
$client = $request->getClient();
|
||||||
$settings = $client->getMergedSettings();
|
$settings = $client->getMergedSettings();
|
||||||
|
|
||||||
|
/** Set translations */
|
||||||
App::forgetInstance('translator');
|
App::forgetInstance('translator');
|
||||||
$t = app('translator');
|
$t = app('translator');
|
||||||
App::setLocale($invitation->contact->preferredLocale());
|
App::setLocale($invitation->contact->preferredLocale());
|
||||||
$t->replace(Ninja::transformTranslations($settings));
|
$t->replace(Ninja::transformTranslations($settings));
|
||||||
|
|
||||||
nlog("2 ".microtime(true));
|
|
||||||
$entity_prop = str_replace("recurring_", "", $request->entity);
|
$entity_prop = str_replace("recurring_", "", $request->entity);
|
||||||
$entity_obj = $invitation->{$request->entity};
|
$entity_obj = $invitation->{$request->entity};
|
||||||
|
|
||||||
|
/** Update necessary objecty props */
|
||||||
if(!$entity_obj->id) {
|
if(!$entity_obj->id) {
|
||||||
$entity_obj->design_id = intval($this->decodePrimaryKey($settings->{$entity_prop."_design_id"}));
|
$entity_obj->design_id = intval($this->decodePrimaryKey($settings->{$entity_prop."_design_id"}));
|
||||||
$entity_obj->footer = $settings->{$entity_prop."_footer"};
|
$entity_obj->footer = $settings->{$entity_prop."_footer"};
|
||||||
@ -92,10 +105,10 @@ nlog("2 ".microtime(true));
|
|||||||
$invitation->{$request->entity} = $entity_obj;
|
$invitation->{$request->entity} = $entity_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Generate variables */
|
||||||
$html = new HtmlEngine($invitation);
|
$html = new HtmlEngine($invitation);
|
||||||
$html->settings = $settings;
|
$html->settings = $settings;
|
||||||
$variables = $html->generateLabelsAndValues();
|
$variables = $html->generateLabelsAndValues();
|
||||||
nlog("3 ".microtime(true));
|
|
||||||
|
|
||||||
$design = \App\Models\Design::withTrashed()->find($entity_obj->design_id ?? 2);
|
$design = \App\Models\Design::withTrashed()->find($entity_obj->design_id ?? 2);
|
||||||
|
|
||||||
@ -135,15 +148,15 @@ nlog("3 ".microtime(true));
|
|||||||
->design($template)
|
->design($template)
|
||||||
->build();
|
->build();
|
||||||
|
|
||||||
nlog("4 ".microtime(true));
|
/** Generate HTML */
|
||||||
|
$html = $maker->getCompiledHTML(true);
|
||||||
|
|
||||||
if (request()->query('html') == 'true') {
|
if (request()->query('html') == 'true')
|
||||||
return $maker->getCompiledHTML();
|
return $html;
|
||||||
}
|
|
||||||
|
|
||||||
//if phantom js...... inject here..
|
//if phantom js...... inject here..
|
||||||
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
||||||
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
return (new Phantom)->convertHtmlToPdf($html);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var \App\Models\User $user */
|
/** @var \App\Models\User $user */
|
||||||
@ -152,34 +165,69 @@ nlog("4 ".microtime(true));
|
|||||||
|
|
||||||
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
||||||
|
|
||||||
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
$pdf = (new NinjaPdf())->build($html);
|
||||||
|
|
||||||
$numbered_pdf = $this->pageNumbering($pdf, $company);
|
$numbered_pdf = $this->pageNumbering($pdf, $company);
|
||||||
|
|
||||||
if ($numbered_pdf) {
|
if ($numbered_pdf)
|
||||||
$pdf = $numbered_pdf;
|
$pdf = $numbered_pdf;
|
||||||
}
|
|
||||||
|
|
||||||
return $pdf;
|
return $pdf;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pdf = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
|
$pdf = (new PreviewPdf($html, $company))->handle();
|
||||||
|
|
||||||
nlog("5 ".microtime(true));
|
if (Ninja::isHosted()) {
|
||||||
nlog("total = ".microtime(true)-$start);
|
LightLogs::create(new LivePreview())
|
||||||
|
->increment()
|
||||||
|
->batch();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return PDF */
|
||||||
return response()->streamDownload(function () use ($pdf) {
|
return response()->streamDownload(function () use ($pdf) {
|
||||||
echo $pdf;
|
echo $pdf;
|
||||||
}, 'preview.pdf', ['Content-Disposition' => 'inline', 'Content-Type' => 'application/pdf','Cache-Control:' => 'no-cache', 'Server-Timing' => microtime(true)-$start]);
|
}, 'preview.pdf', [
|
||||||
|
'Content-Disposition' => 'inline',
|
||||||
|
'Content-Type' => 'application/pdf',
|
||||||
|
'Cache-Control:' => 'no-cache',
|
||||||
|
'Server-Timing' => microtime(true)-$start
|
||||||
|
]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the mocked PDF for the invoice design preview.
|
||||||
|
*
|
||||||
|
* Only used in Settings > Invoice Design as a general overview
|
||||||
|
*
|
||||||
|
* @param DesignPreviewRequest $request
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function design(DesignPreviewRequest $request): mixed
|
||||||
|
{
|
||||||
|
$start = microtime(true);
|
||||||
|
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
/** @var \App\Models\Company $company */
|
||||||
|
$company = $user->company();
|
||||||
|
|
||||||
|
$pdf = (new PdfMock($request->all(), $company))->build()->getPdf();
|
||||||
|
|
||||||
|
$response = Response::make($pdf, 200);
|
||||||
|
$response->header('Content-Type', 'application/pdf');
|
||||||
|
$response->header('Server-Timing', microtime(true)-$start);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a template filled with entity variables.
|
* Returns a template filled with entity variables.
|
||||||
*
|
*
|
||||||
|
* Used in the Custom Designer to preview design changes
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public function show()
|
public function show()
|
||||||
{
|
{
|
||||||
if (request()->has('entity') &&
|
if (request()->has('entity') &&
|
||||||
@ -187,6 +235,7 @@ nlog("total = ".microtime(true)-$start);
|
|||||||
! empty(request()->input('entity')) &&
|
! empty(request()->input('entity')) &&
|
||||||
! empty(request()->input('entity_id')) &&
|
! empty(request()->input('entity_id')) &&
|
||||||
request()->has('body')) {
|
request()->has('body')) {
|
||||||
|
|
||||||
$design_object = json_decode(json_encode(request()->input('design')));
|
$design_object = json_decode(json_encode(request()->input('design')));
|
||||||
|
|
||||||
if (! is_object($design_object)) {
|
if (! is_object($design_object)) {
|
||||||
@ -243,56 +292,50 @@ nlog("total = ".microtime(true)-$start);
|
|||||||
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @var \App\Models\User $user */
|
/** @var \App\Models\User $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
$company = $user->company();
|
$company = $user->company();
|
||||||
|
|
||||||
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
||||||
|
|
||||||
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
||||||
|
|
||||||
$numbered_pdf = $this->pageNumbering($pdf, $company);
|
$numbered_pdf = $this->pageNumbering($pdf, $company);
|
||||||
|
if ($numbered_pdf)
|
||||||
if ($numbered_pdf) {
|
|
||||||
$pdf = $numbered_pdf;
|
$pdf = $numbered_pdf;
|
||||||
}
|
|
||||||
|
|
||||||
return $pdf;
|
return $pdf;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
|
$pdf = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
|
||||||
|
|
||||||
|
return response()->streamDownload(function () use ($pdf) {
|
||||||
|
echo $pdf;
|
||||||
|
}, 'preview.pdf', [
|
||||||
|
'Content-Disposition' => 'inline',
|
||||||
|
'Content-Type' => 'application/pdf',
|
||||||
|
'Cache-Control:' => 'no-cache',
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->blankEntity();
|
return $this->blankEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function design(DesignPreviewRequest $request)
|
|
||||||
{
|
|
||||||
/** @var \App\Models\User $user */
|
/**
|
||||||
$user = auth()->user();
|
* @deprecated due to usage of transactions
|
||||||
|
*
|
||||||
/** @var \App\Models\Company $company */
|
* @param mixed $request
|
||||||
$company = $user->company();
|
* @return void
|
||||||
|
*/
|
||||||
$pdf = (new PdfMock($request->all(), $company))->build()->getPdf();
|
|
||||||
|
|
||||||
$response = Response::make($pdf, 200);
|
|
||||||
$response->header('Content-Type', 'application/pdf');
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function livex(PreviewInvoiceRequest $request)
|
public function livex(PreviewInvoiceRequest $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
// if(Cache::has("preview_".auth()->user()->id))
|
if(Cache::has("preview_".auth()->user()->id))
|
||||||
// return response()->json(['message' => 'Please wait a few seconds before trying again, this many requests are not good.'], 400);
|
return response()->json(['message' => 'Please wait a few seconds before trying again, this many requests are not good.'], 400);
|
||||||
|
|
||||||
nlog("should not see a lot of these");
|
|
||||||
|
|
||||||
if (Ninja::isHosted() && !in_array($request->getHost(), ['preview.invoicing.co','staging.invoicing.co'])) {
|
if (Ninja::isHosted() && !in_array($request->getHost(), ['preview.invoicing.co','staging.invoicing.co'])) {
|
||||||
return response()->json(['message' => 'This server cannot handle this request.'], 400);
|
return response()->json(['message' => 'This server cannot handle this request.'], 400);
|
||||||
@ -463,7 +506,6 @@ nlog("total = ".microtime(true)-$start);
|
|||||||
$response->header('Content-Type', 'application/pdf');
|
$response->header('Content-Type', 'application/pdf');
|
||||||
$response->header('Server-Timing', microtime(true)-$start);
|
$response->header('Server-Timing', microtime(true)-$start);
|
||||||
|
|
||||||
nlog("returning a response");
|
|
||||||
$this->purgeCache();
|
$this->purgeCache();
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
@ -652,7 +694,6 @@ nlog("total = ".microtime(true)-$start);
|
|||||||
|
|
||||||
$response = Response::make($file_path, 200);
|
$response = Response::make($file_path, 200);
|
||||||
$response->header('Content-Type', 'application/pdf');
|
$response->header('Content-Type', 'application/pdf');
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,11 +32,14 @@ class DesignPreviewRequest extends Request
|
|||||||
*/
|
*/
|
||||||
public function authorize() : bool
|
public function authorize() : bool
|
||||||
{
|
{
|
||||||
return auth()->user()->can('create', Invoice::class) ||
|
/** @var \App\Models\User $user */
|
||||||
auth()->user()->can('create', Quote::class) ||
|
$user = auth()->user();
|
||||||
auth()->user()->can('create', RecurringInvoice::class) ||
|
|
||||||
auth()->user()->can('create', Credit::class) ||
|
return $user->can('create', Invoice::class) ||
|
||||||
auth()->user()->can('create', PurchaseOrder::class);
|
$user->can('create', Quote::class) ||
|
||||||
|
$user->can('create', RecurringInvoice::class) ||
|
||||||
|
$user->can('create', Credit::class) ||
|
||||||
|
$user->can('create', PurchaseOrder::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rules()
|
public function rules()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user