149 lines
5.3 KiB
PHP
149 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\UpdateChangeOrderRequest;
|
|
use App\Http\Requests\StoreHomepageNewscastRequest;
|
|
use App\Models\HomepageNewscast;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class HomepageNewscastsController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(): \Illuminate\Contracts\View\View
|
|
{
|
|
$homepagenewscasts = HomepageNewscast::orderBy('order', 'ASC')->get();
|
|
|
|
return view('back.homepage_newscasts.index', compact('homepagenewscasts'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param HomepageNewscast $homepagenewscast
|
|
* @return \Illuminate\Contracts\View\View
|
|
* @ignore phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
*/
|
|
public function create(HomepageNewscast $homepagenewscast): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('back.homepage_newscasts.create', compact('homepagenewscast'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\StoreHomepageNewscastRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreHomepageNewscastRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$homepagenewscast = new HomepageNewscast($request->validated());
|
|
$homepagenewscast->order = $this->getLastOrder();
|
|
|
|
if ($homepagenewscast->saveOrFail()) {
|
|
return to_route('bo.homepagenewscasts.edit', $homepagenewscast->id)
|
|
->with('success', trans(__('Creation_saved')));
|
|
}
|
|
return back()->with('error', trans(__('Creation_failed')));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param HomepageNewscast $homepagenewscast
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(HomepageNewscast $homepagenewscast): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('back.homepage_newscasts.edit', compact('homepagenewscast'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\StoreHomepageNewscastRequest $request
|
|
* @param HomepageNewscast $homepagenewscast
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
|
|
*/
|
|
public function update(StoreHomepageNewscastRequest $request, HomepageNewscast $homepagenewscast): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $homepagenewscast) {
|
|
$homepagenewscast->fill($request->validated());
|
|
|
|
if ($homepagenewscast->saveOrFail()) {
|
|
return to_route('bo.homepagenewscasts.edit', $homepagenewscast->id)
|
|
->with('success', trans(__('Changes_saved')));
|
|
}
|
|
|
|
return to_route('bo.homepagenewscasts.edit', $homepagenewscast->id)
|
|
->with('error', trans(__('Changes_failed')));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param HomepageNewscast $homepagenewscast
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(HomepageNewscast $homepagenewscast): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if ($homepagenewscast->deleteOrFail()) {
|
|
return to_route('bo.homepagenewscasts.index')->with('success', trans(__('Delete_saved')));
|
|
}
|
|
|
|
return to_route('bo.homepagenewscasts.index')->with('error', trans(__('Delete_failed')));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param UpdateChangeOrderRequest $request
|
|
* @param HomepageNewscast $homepagenewscast
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function changeOrder(UpdateChangeOrderRequest $request, HomepageNewscast $homepagenewscast)
|
|
{
|
|
if ($request->validated()['action'] === 'up') {
|
|
$tmp = HomepageNewscast::where('order', '<', $homepagenewscast->order)
|
|
->orderBy('order', 'DESC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $homepagenewscast->order;
|
|
$homepagenewscast->order = $newOrder;
|
|
} elseif ($request->validated()['action'] === 'down') {
|
|
$tmp = HomepageNewscast::where('order', '>', $homepagenewscast->order)
|
|
->orderBy('order', 'ASC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $homepagenewscast->order;
|
|
$homepagenewscast->order = $newOrder;
|
|
} else {
|
|
return to_route('bo.homepage_newscasts.index');
|
|
}
|
|
$tmp->save();
|
|
$homepagenewscast->save();
|
|
|
|
return back();
|
|
}
|
|
|
|
/**
|
|
* Get by order the last element of the list.
|
|
*
|
|
* @return integer
|
|
*/
|
|
private function getLastOrder(): int
|
|
{
|
|
$order = HomepageNewscast::select('order')->orderBy('order', 'DESC')->first();
|
|
if ($order === null) {
|
|
return 1;
|
|
}
|
|
return $order->order + 1;
|
|
}
|
|
}
|