171 lines
6.1 KiB
PHP
171 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\LocalCommitments\StoreLocalCommitmentRequest;
|
|
use App\Http\Requests\Bo\LocalCommitments\UpdateLocalCommitmentRequest;
|
|
use App\Models\LocalCommitment;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class LocalCommitmentController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
use ChangesModelOrder;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(LocalCommitment::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 $localCommitmentModels */
|
|
$localCommitmentModels = LocalCommitment::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$localCommitmentModels,
|
|
$search,
|
|
null,
|
|
'title',
|
|
'text'
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
Str::ucfirst(trans('validation.attributes.title')),
|
|
trans('validation.attributes.text')
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($localCommitmentModels);
|
|
|
|
/** Custom pagination */
|
|
$localCommitmentModels = $this->paginate($localCommitmentModels);
|
|
|
|
return view('bo.pages.local_commitments.index', compact('localCommitmentModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\LocalCommitment $localCommitment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(LocalCommitment $localCommitment): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.local_commitments.create', ['localCommitmentModel' => $localCommitment]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\LocalCommitments\StoreLocalCommitmentRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreLocalCommitmentRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$localCommitment = new LocalCommitment();
|
|
$localCommitment->fill($request->validated());
|
|
|
|
if ($localCommitment->saveOrFail()) {
|
|
return redirect()->route('bo.localcommitments.edit', $localCommitment)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.local_commitment', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.local_commitment', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param App\Models\LocalCommitment $localCommitment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(LocalCommitment $localCommitment): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.local_commitments.edit', ['localCommitmentModel' => $localCommitment]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\LocalCommitments\UpdateLocalCommitmentRequest $request
|
|
* @param \App\Models\LocalCommitment $localCommitment
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(
|
|
UpdateLocalCommitmentRequest $request,
|
|
LocalCommitment $localCommitment
|
|
): \Illuminate\Http\RedirectResponse {
|
|
return DB::transaction(function () use ($request, $localCommitment) {
|
|
$localCommitment->fill($request->validated());
|
|
|
|
if ($localCommitment->saveOrFail()) {
|
|
return redirect()->route('bo.localcommitments.edit', $localCommitment)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.local_commitment', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.localcommitments.edit', $localCommitment)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.local_commitment', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\LocalCommitment $localCommitment
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(LocalCommitment $localCommitment): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($localCommitment) {
|
|
if ($localCommitment->deleteOrFail()) {
|
|
return redirect()->route('bo.localcommitments.index')
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.local_commitment', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.local_commitment', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\LocalCommitment $localCommitment
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(LocalCommitment $localCommitment): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($localCommitment->replicate());
|
|
}
|
|
}
|