188 lines
7 KiB
PHP
188 lines
7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\HistoricalMilestones\StoreHistoricalMilestoneRequest;
|
|
use App\Http\Requests\Bo\HistoricalMilestones\UpdateHistoricalMilestoneRequest;
|
|
use App\Models\HistoricalMilestone;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class HistoricalMilestoneController extends Controller
|
|
{
|
|
use ChangesModelOrder;
|
|
use UpdateModelPublished;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(HistoricalMilestone::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 $historicalMilestoneModels */
|
|
$historicalMilestoneModels = HistoricalMilestone::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$historicalMilestoneModels,
|
|
$search,
|
|
null,
|
|
'year',
|
|
'title',
|
|
'description',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.year'),
|
|
trans('validation.attributes.title'),
|
|
trans('validation.attributes.description'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($historicalMilestoneModels);
|
|
|
|
/** Custom pagination */
|
|
$historicalMilestoneModels = $this->paginate($historicalMilestoneModels);
|
|
|
|
return view('bo.pages.historical_milestones.index', compact(
|
|
'historicalMilestoneModels',
|
|
'search',
|
|
'searchFields'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*
|
|
* @param \App\Models\HistoricalMilestone $historicalMilestone
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(HistoricalMilestone $historicalMilestone): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.historical_milestones.show', ['historicalMilestoneModel' => $historicalMilestone]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\HistoricalMilestone $historicalMilestone
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(HistoricalMilestone $historicalMilestone): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.historical_milestones.create', ['historicalMilestoneModel' => $historicalMilestone]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\HistoricalMilestones\StoreHistoricalMilestoneRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreHistoricalMilestoneRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$historicalMilestone = new HistoricalMilestone();
|
|
$historicalMilestone->fill($request->validated());
|
|
|
|
if ($historicalMilestone->saveOrFail()) {
|
|
return redirect()->route('bo.historical_milestones.edit', $historicalMilestone)
|
|
->with('success', trans('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.historical_milestone', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.historical_milestone', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\HistoricalMilestone $historicalMilestone
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(HistoricalMilestone $historicalMilestone): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.historical_milestones.edit', ['historicalMilestoneModel' => $historicalMilestone]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\HistoricalMilestones\UpdateHistoricalMilestoneRequest $request
|
|
* @param \App\Models\HistoricalMilestone $historicalMilestone
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(
|
|
UpdateHistoricalMilestoneRequest $request,
|
|
HistoricalMilestone $historicalMilestone
|
|
): \Illuminate\Http\RedirectResponse {
|
|
return DB::transaction(function () use ($request, $historicalMilestone) {
|
|
$historicalMilestone->fill($request->validated());
|
|
|
|
if ($historicalMilestone->saveOrFail()) {
|
|
return redirect()->route('bo.historical_milestones.edit', $historicalMilestone)
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.historical_milestone', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.historical_milestones.edit', $historicalMilestone)
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.historical_milestone', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\HistoricalMilestone $historicalMilestone
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(HistoricalMilestone $historicalMilestone): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($historicalMilestone->deleteOrFail()) {
|
|
return redirect()->route('bo.historical_milestones.index', $previousQueries)
|
|
->with('success', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.historical_milestone', 1))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.historical_milestone', 1))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\HistoricalMilestone $historicalMilestone
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(HistoricalMilestone $historicalMilestone): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($historicalMilestone->replicate());
|
|
}
|
|
}
|