get('cooking_mode_enum')) ? CookingModeEnum::make($request->get('cooking_mode_enum')) : null; /** @var \App\Enums\PreparationTimeEnum|null */ $preparation_time_enum = !empty($request->get('preparation_time_enum')) ? PreparationTimeEnum::make($request->get('preparation_time_enum')) : null; /** @var \App\Enums\DifficultyEnum|null */ $difficulty_enum = !empty($request->get('difficulty_enum')) ? DifficultyEnum::make($request->get('difficulty_enum')) : null; if ($cooking_mode_enum) { $query->where('cooking_mode', $cooking_mode_enum); } if ($preparation_time_enum) { /** @var integer */ $min_time = Arr::get($preparation_time_enum->time(), 'min', 0); /** @var integer */ $max_time = Arr::get($preparation_time_enum->time(), 'max', 9999); $query->whereBetween('preparation_time', [$min_time, $max_time]); } if ($difficulty_enum) { $query->where('difficulty', $difficulty_enum); } $publishedRecipes = $query ->where('status', true) ->orderBy('created_at') ->paginate(9); return view('fo.pages.recipes.index', compact('publishedRecipes')); } /** * Return a view with a specific model. * * @param \App\Models\Recipe $publishedRecipe * @return \Illuminate\Contracts\View\View */ public function show(Recipe $publishedRecipe) { $products = $publishedRecipe->products()->get(); $relatedRecipes = $publishedRecipe->recipes()->where('status', true)->get(); // 3 Recettes. $publishedRecipes = Recipe::query() ->where('status', true) ->where('id', '!=', $publishedRecipe->id) ->take(3) ->get(); $recipeModel = $publishedRecipe; return view( 'fo.pages.recipes.recipe', compact('recipeModel', 'products', 'relatedRecipes') ); } /** * Print recipe informations * * @param \App\Models\Recipe $publishedRecipe * @return \Symfony\Component\HttpFoundation\Response */ public function printRecipe(Recipe $publishedRecipe): \Symfony\Component\HttpFoundation\Response { $title = trans('Fiche commerciale du produit : :label - :appname', [ 'label' => $publishedRecipe->name, 'appname' => \config('app.name', 'Lionor') ]); $filename = trans('fiche-commerciale-:slug-:date', [ 'slug' => Str::slug($publishedRecipe->name), 'date' => \now()->format('Y-m-d') ]); /** * ! Réponse mano car en fait \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) * * (From LeGauloisPro) */ return \response( $this->generateRecipePdf($publishedRecipe, $title)->download($filename), 200, [ 'Content-Type' => 'application/pdf', 'Content-disposition' => "inline; filename=\"{$filename}\"", 'Cache-Control' => 'public, must-revalidate, max-age=0', 'Pragma' => 'public', // * For glory '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 PDF * * @param \App\Models\Recipe $recipe * @param string $title * @return \Mccarlosen\LaravelMpdf\LaravelMpdf */ private function generateRecipePdf(Recipe $recipe, string $title): \Mccarlosen\LaravelMpdf\LaravelMpdf { $tabRecipe = \json_decode(\json_encode(new RecipeResource($recipe))); $mpdf = app('laravel-mpdf'); /** @var \Mccarlosen\LaravelMpdf\LaravelMpdf */ $pdf = $mpdf->loadView( 'fo.documents.recipe', // * Dirty convert to object. [ 'title' => $title, 'recipe' => $tabRecipe ], [], [ 'mode' => 'utf-8', 'format' => 'A4', 'default_font_size' => '12', 'default_font' => 'sans-serif', // Recto Verso. 'margin_left' => 10, 'margin_right' => 10, 'mirrorMargins' => true, 'margin_top' => 25, 'margin_bottom' => 20, 'margin_header' => 0, 'margin_footer' => 0, 'orientation' => 'P', 'title' => $title, 'author' => \config('app.name', 'Lionor'), '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; } }