ldc-legaulois/app/Http/Controllers/Bo/ApproachController.php

200 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2024-04-12 12:15:18 +02:00
<?php
namespace App\Http\Controllers\Bo;
use App\Enums\Approaches\ApproachTypeEnum;
2024-04-12 12:15:18 +02:00
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Approaches\StoreApproachRequest;
use App\Http\Requests\Bo\Approaches\UpdateApproachRequest;
use App\Models\Approach;
use App\Traits\Controllers\UpdateModelPublished;
2026-09-25 16:25:31 +02:00
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
2024-04-12 12:15:18 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class ApproachController extends Controller
{
use UpdateModelPublished;
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(Approach::class);
}
/**
* Display a listing of the resource.
*/
2026-09-25 16:25:31 +02:00
public function index(Request $request): View
2024-04-12 12:15:18 +02:00
{
/** @var \Illuminate\Database\Eloquent\Builder $approachModels */
2024-04-25 10:51:40 +02:00
$approachModels = Approach::query()->where('type', ApproachTypeEnum::make($request->approach_type));
2024-04-12 12:15:18 +02:00
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$approachModels,
$search,
null,
'name',
);
}
$searchFields = \implode(', ', [
trans('validation.attributes.name'),
]);
/** Sort columns with a query */
$this->sortQuery($approachModels);
/** Custom pagination */
$approachModels = $this->paginate($approachModels);
return view('bo.pages.approaches.index', compact('approachModels', 'search', 'searchFields'));
}
/**
* Show the specified resource.
*/
2026-09-25 16:25:31 +02:00
public function show(Approach $approach): View
2024-04-12 12:15:18 +02:00
{
return view('bo.pages.approaches.show', ['approachModel' => $approach]);
}
/**
* Show the form for creating a new resource.
*/
2026-09-25 16:25:31 +02:00
public function create(Approach $approach): View
2024-04-12 12:15:18 +02:00
{
return view('bo.pages.approaches.create', ['approachModel' => $approach]);
}
/**
* Store a newly created resource in storage.
*/
2026-09-25 16:25:31 +02:00
public function store(StoreApproachRequest $request): RedirectResponse
2024-04-12 12:15:18 +02:00
{
return DB::transaction(function () use ($request) {
$approach = new Approach();
$approach->fill($request->validated());
if ($approach->saveOrFail()) {
return redirect()->route('bo.approaches.edit', $approach)
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(
2024-06-06 11:12:12 +02:00
trans(ApproachTypeEnum::demarche->equals($approach->type)
? 'validation.custom.demarche'
: 'validation.custom.label')
2026-09-25 16:25:31 +02:00
),
2024-04-12 12:15:18 +02:00
]));
}
2026-09-25 16:25:31 +02:00
2024-04-12 12:15:18 +02:00
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_created', [
'model' => Str::ucfirst(
2024-06-06 11:12:12 +02:00
trans(ApproachTypeEnum::demarche->equals($approach->type)
? 'validation.custom.demarche'
: 'validation.custom.label')
2026-09-25 16:25:31 +02:00
),
2024-04-12 12:15:18 +02:00
]));
});
}
/**
* Show the form for editing the specified resource.
*/
2026-09-25 16:25:31 +02:00
public function edit(Approach $approach): View
2024-04-12 12:15:18 +02:00
{
return view('bo.pages.approaches.edit', ['approachModel' => $approach]);
}
/**
* Update the specified resource in storage.
*/
2026-09-25 16:25:31 +02:00
public function update(UpdateApproachRequest $request, Approach $approach): RedirectResponse
2024-04-12 12:15:18 +02:00
{
return DB::transaction(function () use ($request, $approach) {
$approach->fill($request->validated());
if ($approach->saveOrFail()) {
return redirect()->route('bo.approaches.edit', $approach)
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(
2024-06-06 11:12:12 +02:00
trans(ApproachTypeEnum::demarche->equals($approach->type)
? 'validation.custom.demarche'
: 'validation.custom.label')
2026-09-25 16:25:31 +02:00
),
2024-04-12 12:15:18 +02:00
]));
}
2026-09-25 16:25:31 +02:00
2024-04-12 12:15:18 +02:00
return redirect()->route('bo.approaches.edit', $approach)
->with('error', trans('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(
2024-06-06 11:12:12 +02:00
trans(ApproachTypeEnum::demarche->equals($approach->type)
? 'validation.custom.demarche'
: 'validation.custom.label')
2026-09-25 16:25:31 +02:00
),
2024-04-12 12:15:18 +02:00
]));
});
}
/**
* Remove the specified resource from storage.
*/
2026-09-25 16:25:31 +02:00
public function destroy(Approach $approach): RedirectResponse
2024-04-12 12:15:18 +02:00
{
/** @var array<string,string> */
$previousQueries = [];
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
if ($approach->deleteOrFail()) {
return redirect()->route('bo.approaches.index', array_merge(
['approach_type' => $approach->type],
$previousQueries
))->with('success', trans('crud.messages.has_been_deleted', [
'model' => Str::ucfirst(
2024-06-06 11:12:12 +02:00
trans(ApproachTypeEnum::demarche->equals($approach->type)
? 'validation.custom.demarche'
: 'validation.custom.label')
2026-09-25 16:25:31 +02:00
),
2024-04-12 12:15:18 +02:00
]));
}
2026-09-25 16:25:31 +02:00
2024-04-12 12:15:18 +02:00
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst(
2024-06-06 11:12:12 +02:00
trans(ApproachTypeEnum::demarche->equals($approach->type)
? 'validation.custom.demarche'
: 'validation.custom.label')
2026-09-25 16:25:31 +02:00
),
2024-04-12 12:15:18 +02:00
]));
}
/**
* Duplicate the specified resource.
*/
2026-09-25 16:25:31 +02:00
public function duplicate(Approach $approach): View
2024-04-12 12:15:18 +02:00
{
return $this->create($approach->replicate());
}
/**
* Get a paginate resource.
*/
2026-09-25 16:25:31 +02:00
public function jsonPaginateDemarches(Request $request): JsonResponse
{
2024-04-19 10:08:36 +02:00
/** @var \App\Models\Approach $approachModels */
$approachModels = $request->targetModel::query()
->where('type', ApproachTypeEnum::demarche)
->where('name', 'like', "%{$request->input('search')}%")
->orderBy('name', 'ASC')
2024-06-10 17:13:34 +02:00
->paginate($request->input('paginate') ?? 10);
2026-09-25 16:25:31 +02:00
2024-04-19 10:08:36 +02:00
return \response()->json($approachModels);
}
2024-04-12 12:15:18 +02:00
}