228 lines
8.1 KiB
PHP
228 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Recipes\StoreRecipeRequest;
|
|
use App\Http\Requests\Bo\Recipes\UpdateRecipeRequest;
|
|
use App\Models\ConsumptionMoment;
|
|
use App\Models\Product;
|
|
use App\Models\Recipe;
|
|
use App\Models\Video;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RecipeController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Recipe::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $recipeModels */
|
|
$recipeModels = Recipe::query()->with('ratings')->withAvg('ratings', 'rate');
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$recipeModels,
|
|
$search,
|
|
null,
|
|
'title',
|
|
'tips',
|
|
'preparation',
|
|
'advice',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.title'),
|
|
trans('validation.custom.tips'),
|
|
trans('validation.custom.preparation'),
|
|
trans('validation.custom.advice'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($recipeModels);
|
|
|
|
/** Custom pagination */
|
|
$recipeModels = $this->paginate($recipeModels);
|
|
|
|
return view('bo.pages.recipes.index', compact('recipeModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(Recipe $recipe): \Illuminate\Contracts\View\View
|
|
{
|
|
$productModels = Product::query()->where('published', true)->get();
|
|
return view('bo.pages.recipes.show', [
|
|
'recipeModel' => $recipe,
|
|
'productModels' => $productModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Recipe $recipe): \Illuminate\Contracts\View\View
|
|
{
|
|
$productModels = Product::query()->where('published', true)->orderBy('label', 'ASC')->paginate(12);
|
|
$videoModels = Video::query()->where('published', true)->orderBy('order', 'ASC')->paginate(10);
|
|
$consumptionMomentModels = ConsumptionMoment::query()->orderBy('title', 'ASC')->paginate(10);
|
|
return view('bo.pages.recipes.create', [
|
|
'recipeModel' => $recipe,
|
|
'productModels' => $productModels,
|
|
'consumptionMomentModels' => $consumptionMomentModels,
|
|
'videoModels' => $videoModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Recipes\StoreRecipeRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreRecipeRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$recipe = new Recipe();
|
|
$recipe->fill(Arr::except($request->validated(), ['products']));
|
|
|
|
if ($recipe->saveOrFail()) {
|
|
$recipe->products()->sync($request->products);
|
|
return redirect()->route('bo.recipes.edit', $recipe)
|
|
->with('success', trans('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans('models.classes.recipe'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans('models.classes.recipe'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Recipe $recipe): \Illuminate\Contracts\View\View
|
|
{
|
|
$productModels = Product::query()->where('published', true)->orderBy('label', 'ASC')->paginate(12);
|
|
$videoModels = Video::query()->where('published', true)->orderBy('title', 'ASC')->paginate(10);
|
|
$consumptionMomentModels = ConsumptionMoment::query()->orderBy('title', 'ASC')->paginate(10);
|
|
return view('bo.pages.recipes.edit', [
|
|
'recipeModel' => $recipe,
|
|
'productModels' => $productModels,
|
|
'consumptionMomentModels' => $consumptionMomentModels,
|
|
'videoModels' => $videoModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Recipes\UpdateRecipeRequest $request
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateRecipeRequest $request, Recipe $recipe): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $recipe) {
|
|
$recipe->fill(Arr::except($request->validated(), ['products']));
|
|
$recipe->products()->sync([]);
|
|
$recipe->products()->sync($request->products);
|
|
|
|
if ($recipe->saveOrFail()) {
|
|
return redirect()->route('bo.recipes.edit', $recipe)
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans('models.classes.recipe'))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.recipes.edit', $recipe)
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans('models.classes.recipe'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Recipe $recipe): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($recipe->deleteOrFail()) {
|
|
return redirect()->route('bo.recipes.index', $previousQueries)
|
|
->with('success', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans('models.classes.recipe'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans('models.classes.recipe'))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Recipe $recipe): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($recipe->replicate());
|
|
}
|
|
|
|
/**
|
|
* Change recipe highlight.
|
|
*
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function changeHighlight(Recipe $recipe)
|
|
{
|
|
/** @var \App\Models\Recipe|null $recipeOld */
|
|
$recipeOld = Recipe::query()->where('highlight', true)->first();
|
|
if ($recipeOld && $recipeOld->getkey() !== $recipe->getkey()) {
|
|
$recipeOld->highlight = false;
|
|
$recipeOld->saveQuietly();
|
|
}
|
|
$recipe->highlight = true;
|
|
$recipe->saveQuietly();
|
|
return redirect()->back()->with('success', trans('crud.messages.highlight_status_saved'));
|
|
}
|
|
}
|