220 lines
7.7 KiB
PHP
220 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\Approaches\ApproachTypeEnum;
|
|
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;
|
|
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.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $approachModels */
|
|
$approachModels = Approach::query()->where('type', ApproachTypeEnum::make($request->approach_type));
|
|
|
|
/** @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.
|
|
*
|
|
* @param \App\Models\Approach $approach
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(Approach $approach): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.approaches.show', ['approachModel' => $approach]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Approach $approach
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Approach $approach): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.approaches.create', ['approachModel' => $approach]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Approaches\StoreApproachRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreApproachRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
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(
|
|
trans(ApproachTypeEnum::demarche->equals($approach->type)
|
|
? 'validation.custom.demarche'
|
|
: 'validation.custom.label')
|
|
)
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(
|
|
trans(ApproachTypeEnum::demarche->equals($approach->type)
|
|
? 'validation.custom.demarche'
|
|
: 'validation.custom.label')
|
|
)
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Approach $approach
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Approach $approach): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.approaches.edit', ['approachModel' => $approach]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Approaches\UpdateApproachRequest $request
|
|
* @param \App\Models\Approach $approach
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateApproachRequest $request, Approach $approach): \Illuminate\Http\RedirectResponse
|
|
{
|
|
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(
|
|
trans(ApproachTypeEnum::demarche->equals($approach->type)
|
|
? 'validation.custom.demarche'
|
|
: 'validation.custom.label')
|
|
)
|
|
]));
|
|
}
|
|
return redirect()->route('bo.approaches.edit', $approach)
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(
|
|
trans(ApproachTypeEnum::demarche->equals($approach->type)
|
|
? 'validation.custom.demarche'
|
|
: 'validation.custom.label')
|
|
)
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Approach $approach
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Approach $approach): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @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(
|
|
trans(ApproachTypeEnum::demarche->equals($approach->type)
|
|
? 'validation.custom.demarche'
|
|
: 'validation.custom.label')
|
|
)
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(
|
|
trans(ApproachTypeEnum::demarche->equals($approach->type)
|
|
? 'validation.custom.demarche'
|
|
: 'validation.custom.label')
|
|
)
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Approach $approach
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Approach $approach): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($approach->replicate());
|
|
}
|
|
|
|
/**
|
|
* Get a paginate resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function jsonPaginateDemarches(Request $request): \Illuminate\Http\JsonResponse
|
|
{
|
|
/** @var \App\Models\Approach $approachModels */
|
|
$approachModels = $request->targetModel::query()
|
|
->where('type', ApproachTypeEnum::demarche)
|
|
->where('name', 'like', "%{$request->input('search')}%")
|
|
->orderBy('name', 'ASC')
|
|
->paginate($request->input('paginate') ?? 5);
|
|
return \response()->json($approachModels);
|
|
}
|
|
}
|