ldc-volena/app/Http/Controllers/Fo/NewsController.php
2024-03-18 16:10:01 +01:00

53 lines
2 KiB
PHP

<?php
namespace App\Http\Controllers\Fo;
use App\Enums\StaticPageType;
use App\Http\Controllers\Controller;
use App\Models\News;
use App\Models\Section;
use App\Models\StaticPage;
class NewsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$staticPageModel = StaticPage::query()->where('type', StaticPageType::we_move)->first();
$sectionModels = Section::query()->where('page_id', $staticPageModel->id)->get();
$newsModels = News::query()->where('published', true)->orderBy('publish_at', 'DESC')->simplePaginate(4);
return view('fo.pages.news.index', \compact('staticPageModel', 'sectionModels', 'newsModels'));
}
/**
* Display a news articles
*
* @param \App\Models\News $news
* @return \Illuminate\Contracts\View\View
*/
public function show(News $news): \Illuminate\Contracts\View\View
{
// Get only id from published news.
// @phpcs:disabled
// @phpstan-ignore-next-line
$publishedNews = News::query()->where('published', true)->orderBy('publish_at', 'DESC')->get()->pluck('id');
// @phpcs:enable
// Search the position of the current news id.
$positionId = \array_search($news->id, $publishedNews->toArray());
// Check if it's the last position.
$nextPositionId = ($positionId === count($publishedNews) - 1) ? 0 : intval($positionId) + 1;
// Find the next news from the id.
$nextNews = News::query()->where('id', $publishedNews[$nextPositionId])->firstOrFail();
// Check if it's the first position.
$prevPositionId = ($positionId === 0) ? count($publishedNews) - 1 : intval($positionId) - 1;
// Find the next news from the id.
$prevNews = News::query()->where('id', $publishedNews[$prevPositionId])->firstOrFail();
return view('fo.pages.news.show', \compact('news', 'nextNews', 'prevNews'));
}
}