2024-08-30 16:09:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
|
|
|
|
|
|
use App\Enums\StaticPageType;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\News;
|
|
|
|
|
use App\Models\NewsCategory;
|
|
|
|
|
use App\Models\StaticPage;
|
2026-09-25 16:25:31 +02:00
|
|
|
use Illuminate\Contracts\View\View;
|
2025-06-10 14:55:41 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2026-09-25 16:25:31 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2025-06-10 14:55:41 +02:00
|
|
|
use Illuminate\Http\Request;
|
2024-08-30 16:09:01 +02:00
|
|
|
|
|
|
|
|
class NewsController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function index(): View
|
2024-08-30 16:09:01 +02:00
|
|
|
{
|
2024-09-26 12:18:18 +02:00
|
|
|
$staticPageModel = StaticPage::getCachedStaticPage(StaticPageType::news);
|
|
|
|
|
$newsCategories = NewsCategory::getPublishedOrdered();
|
2025-06-10 14:55:41 +02:00
|
|
|
$newsViews = News::getNewsViewsByNewsCategory();
|
2024-08-30 16:09:01 +02:00
|
|
|
|
|
|
|
|
return view('fo.pages.news.index', [
|
2025-06-10 14:55:41 +02:00
|
|
|
'newsViews' => $newsViews,
|
2024-08-30 16:09:01 +02:00
|
|
|
'newsCategories' => $newsCategories,
|
2026-09-25 16:25:31 +02:00
|
|
|
'staticPageModel' => $staticPageModel,
|
2024-08-30 16:09:01 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the view of a specific news.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function show(string $slug): View
|
2024-08-30 16:09:01 +02:00
|
|
|
{
|
|
|
|
|
/** @var \App\Models\News|null $newsModel Current news model */
|
2025-06-10 14:55:41 +02:00
|
|
|
$newsModel = News::query()
|
|
|
|
|
->where('slug', $slug)
|
|
|
|
|
->where('published', true)
|
|
|
|
|
->whereHas('newsCategory', function (Builder $q) {
|
|
|
|
|
$q->where('published', true);
|
|
|
|
|
})
|
|
|
|
|
->with('newsCategory')
|
|
|
|
|
->firstOrFail();
|
2024-08-30 16:09:01 +02:00
|
|
|
$asideNewsModels = News::query()
|
|
|
|
|
->orderByRaw('news_category_id = ? DESC', [$newsModel->news_category_id])
|
|
|
|
|
->where([
|
|
|
|
|
['published', '=', true],
|
2024-10-21 16:57:20 +02:00
|
|
|
['id', '!=', $newsModel->getKey()],
|
2024-09-26 12:18:18 +02:00
|
|
|
])
|
2024-08-30 16:09:01 +02:00
|
|
|
->inRandomOrder()
|
|
|
|
|
->take(3)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
return view('fo.pages.news.show', [
|
|
|
|
|
'newsModel' => $newsModel,
|
2026-09-25 16:25:31 +02:00
|
|
|
'asideNewsModels' => $asideNewsModels,
|
2024-09-26 12:18:18 +02:00
|
|
|
]);
|
2024-08-30 16:09:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get filtered news.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function jsonGetNewsViewsByCategory(Request $request): JsonResponse
|
2024-08-30 16:09:01 +02:00
|
|
|
{
|
|
|
|
|
return response()->json(News::getNewsViewsByNewsCategory($request->LEGENDSLUG));
|
|
|
|
|
}
|
|
|
|
|
}
|