217 lines
9.2 KiB
PHP
217 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Enums\Settings\SettingsType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\Fo\Recipes\RecipeCommericalSheetResource;
|
|
use App\Models\GeneralSetting;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use App\Models\Recipe;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RecipeController extends Controller
|
|
{
|
|
/**
|
|
* Get recipes by a specific range into a json response.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(): \Illuminate\Contracts\View\View
|
|
{
|
|
$recipeModels = Recipe::query()->where('published', true)->get();
|
|
return view('fo.pages.recipes.index', ['recipeModel' => $recipeModels]);
|
|
}
|
|
|
|
/**
|
|
* Get recipes by a specific range into a json response.
|
|
*
|
|
* @param string $slug
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(string $slug): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \App\Models\Recipe|null $recipeModel Current recipe model */
|
|
$recipeModel = Recipe::query()->where('slug', $slug)->with('products.submeats')->first();
|
|
if (is_null($recipeModel)) {
|
|
abort(404);
|
|
}
|
|
|
|
$generalSettingModel = GeneralSetting::query()->where('type', SettingsType::recipe)->first();
|
|
|
|
return view('fo.pages.recipes.show', [
|
|
'recipeModel' => $recipeModel,
|
|
'recipeModels' => $this->getRecipesFiltered($recipeModel),
|
|
'generalSettingModel' => $generalSettingModel,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get recipes by a specific range into a json response.
|
|
*
|
|
* @param \App\Models\Recipe $recipeModel
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function getRecipesFiltered(Recipe $recipeModel): \Illuminate\Support\Collection
|
|
{
|
|
/** @var int $nbrMaxRecipes Maximum number of recipes required */
|
|
$nbrMaxRecipes = 9;
|
|
|
|
/** @var array<integer> $recipeProducts Id of recipe products relation */
|
|
$recipeProducts = $recipeModel->products->pluck('id')->toArray();
|
|
/** @var \Illuminate\Support\Collection<\App\Models\Recipe> $recipeWithProducts Recipe query builder for products relation */
|
|
$recipeWithProducts = Recipe::query()
|
|
->with(['products', 'products.submeats', 'consumptionMoment'])
|
|
->whereNot('id', $recipeModel->id)
|
|
->where('published', true)
|
|
->whereHas('products', function (Builder $q) use ($recipeProducts) {
|
|
$q->where('id', $recipeProducts[0])
|
|
->when(isset($recipeProducts[1]), function (Builder $q) use ($recipeProducts) {
|
|
$q->orWhere('id', $recipeProducts[1]);
|
|
});
|
|
})
|
|
->get();
|
|
|
|
/** @var \Illuminate\Support\Collection<\App\Models\Recipe> $recipeModels All collections of recipes merged */
|
|
$recipeModels = collect();
|
|
if (count($recipeWithProducts) < $nbrMaxRecipes) {
|
|
/** @var array<integer> $recipeProductsSubmeats */
|
|
$recipeProductsSubmeats = [];
|
|
$recipeModel->products->map(function ($product) use (&$recipeProductsSubmeats) {
|
|
$recipeProductsSubmeats = array_merge(
|
|
$recipeProductsSubmeats,
|
|
$product->submeats->pluck('id')->toArray()
|
|
);
|
|
});
|
|
/** @var \Illuminate\Support\Collection<\App\Models\Recipe> $recipeWithMeats Recipe query builder for products relation */
|
|
$recipeWithMeats = Recipe::query()
|
|
->with(['products', 'products.submeats', 'consumptionMoment'])
|
|
->whereNot('id', $recipeModel->id)
|
|
->where('published', true)
|
|
->whereHas('products', function (Builder $q) use ($recipeProducts, $recipeProductsSubmeats) {
|
|
$q->where('id', $recipeProducts[0])
|
|
->whereHas('submeats', function (Builder $q) use ($recipeProductsSubmeats) {
|
|
$q->where('published', true)->whereIn('id', $recipeProductsSubmeats);
|
|
})
|
|
->when(
|
|
isset($recipeProducts[1]),
|
|
function (Builder $q) use ($recipeProducts, $recipeProductsSubmeats) {
|
|
$q->orWhere('id', $recipeProducts[1])
|
|
->whereHas('submeats', function (Builder $q) use ($recipeProductsSubmeats) {
|
|
$q->where('published', true)->whereIn('id', $recipeProductsSubmeats);
|
|
});
|
|
}
|
|
);
|
|
})
|
|
->get();
|
|
$recipeModels = $recipeWithProducts->merge($recipeWithMeats);
|
|
|
|
if (count($recipeModels) < $nbrMaxRecipes) {
|
|
/** @var \Illuminate\Support\Collection<\App\Models\Recipe> $recipeWithConsumptionMoment Recipe query builder for consumption moment relation */
|
|
$recipeWithConsumptionMoment = Recipe::query()
|
|
->with(['products', 'products.submeats', 'consumptionMoment'])
|
|
->whereNot('id', $recipeModel->id)
|
|
->where('published', true)
|
|
->where('consumption_moment_id', $recipeModel->consumption_moment_id)
|
|
->get();
|
|
$recipeModels = $recipeModels->merge($recipeWithConsumptionMoment);
|
|
}
|
|
} //end if
|
|
|
|
return $recipeModels->slice(0, $nbrMaxRecipes);
|
|
}
|
|
|
|
/**
|
|
* Print product commercial sheet
|
|
*
|
|
* @param string $slug
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function printCommericalSheet(string $slug): \Illuminate\Http\Response
|
|
{
|
|
$recipeModel = Recipe::query()->where('slug', $slug)->first();
|
|
$title = trans('Fiche commerciale de la recette : :title - :appname', [
|
|
'label' => $recipeModel->title,
|
|
'appname' => \config('app.name', 'Le Gaulois')
|
|
]);
|
|
$filename = trans('fiche-commerciale-:slug-:date', [
|
|
'slug' => Str::slug($recipeModel->title),
|
|
'date' => \now()->format('Y-m-d')
|
|
]);
|
|
|
|
/**
|
|
* ! Réponse mano car enfait \Mccarlosen\LaravelMpdf\LaravelMpdf
|
|
* ! ne retourne pas vraiemnt une http response, mais provoque une 500
|
|
* ! apres avoir header et echo dans le corps de la réponse php (Nasty trick)
|
|
*/
|
|
return \response(
|
|
$this->generateRecipeCommercialPdf($recipeModel, $title)->output(),
|
|
200,
|
|
[
|
|
'Content-Type' => 'application/pdf',
|
|
'Content-disposition' => "inline; filename=\"{$filename}\"",
|
|
'Cache-Control' => 'public, must-revalidate, max-age=0',
|
|
'Pragma' => 'public',
|
|
'X-Generator' => 'mPDF',
|
|
'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT',
|
|
'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT',
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Generate recipe commercial sheet PDF
|
|
*
|
|
* @param \App\Models\Recipe $recipeModel
|
|
* @param string $title
|
|
* @return \Mccarlosen\LaravelMpdf\LaravelMpdf
|
|
*/
|
|
private function generateRecipeCommercialPdf(
|
|
Recipe $recipeModel,
|
|
string $title
|
|
): \Mccarlosen\LaravelMpdf\LaravelMpdf {
|
|
/** @var \Mccarlosen\LaravelMpdf\LaravelMpdfWrapper */
|
|
$mpdf = app('laravel-mpdf');
|
|
/** @var \Mccarlosen\LaravelMpdf\LaravelMpdf */
|
|
$pdf = $mpdf->loadView(
|
|
'fo.documents.recipe-commercial-sheet',
|
|
// * Dirty convert to object.
|
|
[
|
|
'title' => $title,
|
|
'recipeModel' => \json_decode(\json_encode(new RecipeCommericalSheetResource($recipeModel)))
|
|
],
|
|
[],
|
|
[
|
|
'mode' => 'utf-8',
|
|
'format' => 'A4',
|
|
'default_font_size' => '12',
|
|
'default_font' => 'sans-serif',
|
|
'margin_left' => 0,
|
|
'margin_right' => 0,
|
|
'mirrorMargins' => true,
|
|
'margin_top' => 0,
|
|
'margin_bottom' => 0,
|
|
'margin_header' => 0,
|
|
'margin_footer' => 0,
|
|
'orientation' => 'P',
|
|
'title' => $title,
|
|
'author' => \config('app.name', 'Le Gaulois'),
|
|
'watermark' => '',
|
|
'show_watermark' => false,
|
|
'watermark_font' => 'sans-serif',
|
|
'display_mode' => 'fullpage',
|
|
'watermark_text_alpha' => 0.1,
|
|
'custom_font_dir' => '',
|
|
'custom_font_data' => [],
|
|
'auto_language_detection' => false,
|
|
'temp_dir' => rtrim(
|
|
ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir(),
|
|
DIRECTORY_SEPARATOR
|
|
),
|
|
'pdfa' => false,
|
|
'pdfaauto' => false,
|
|
]
|
|
);
|
|
return $pdf;
|
|
}
|
|
}
|