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