ldc-volena/app/Http/Controllers/Fo/NewsController.php
2025-07-21 15:27:01 +02:00

66 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Fo;
use App\Enums\Sections\PageSectionEnum;
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('displayed_date', 'DESC')
->simplePaginate(4)
->fragment('corpo');
$lifeSection = $sectionModels->where('section', PageSectionEnum::move_life)->first();
$commitmentSection = $sectionModels->where('section', PageSectionEnum::move_commitments)->first();
return view('fo.pages.news.index', \compact(
'staticPageModel',
'sectionModels',
'newsModels',
'lifeSection',
'commitmentSection'
));
}
/**
* 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.
$publishedNews = News::query()->where('published', true)->orderBy('publish_at', 'DESC')->get()->pluck('id');
// 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', [
'news' => $news,
'nextNews' => $nextNews,
'prevNews' => $prevNews,
]);
}
}