Save workspace changes
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\News;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Services\News\NewsService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\View\View;
|
||||
use cPad\Plugins\News\Models\NewsArticle;
|
||||
use cPad\Plugins\News\Models\NewsCategory;
|
||||
use cPad\Plugins\News\Models\NewsTag;
|
||||
use cPad\Plugins\News\Models\NewsView;
|
||||
|
||||
class NewsController extends Controller
|
||||
{
|
||||
public function __construct(private readonly NewsService $news)
|
||||
{
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Homepage — /news
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
|
||||
$featured = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->editorialOrder()
|
||||
->first();
|
||||
|
||||
$highlightQuery = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->editorialOrder();
|
||||
|
||||
if ($featured) {
|
||||
$highlightQuery->where('id', '!=', $featured->id);
|
||||
}
|
||||
|
||||
$highlights = $highlightQuery->limit(3)->get();
|
||||
$excludedIds = collect([$featured?->id])->merge($highlights->pluck('id'))->filter()->all();
|
||||
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->when($excludedIds !== [], fn ($query) => $query->whereNotIn('id', $excludedIds))
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.index', [
|
||||
'featured' => $featured,
|
||||
'highlights' => $highlights,
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Category page — /news/category/{slug}
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public function category(Request $request, string $slug): View
|
||||
{
|
||||
$category = NewsCategory::where('slug', $slug)->where('is_active', true)->firstOrFail();
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->byCategory($category->id)
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.category', [
|
||||
'category' => $category,
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Tag page — /news/tag/{slug}
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public function tag(Request $request, string $slug): View
|
||||
{
|
||||
$tag = NewsTag::where('slug', $slug)->firstOrFail();
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->whereHas('tags', fn ($q) => $q->where('news_tags.slug', $slug))
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.tag', [
|
||||
'tag' => $tag,
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
public function archive(Request $request, int $year, int $month): View
|
||||
{
|
||||
abort_unless($month >= 1 && $month <= 12, 404);
|
||||
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->whereYear('published_at', $year)
|
||||
->whereMonth('published_at', $month)
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.archive', [
|
||||
'archiveDate' => now()->setDate($year, $month, 1),
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
public function author(Request $request, string $username): View
|
||||
{
|
||||
$author = User::query()->with('profile')->where('username', $username)->firstOrFail();
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->where('author_id', $author->id)
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.author', [
|
||||
'author' => $author,
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Article page — /news/{slug}
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public function show(Request $request, string $slug): View
|
||||
{
|
||||
$article = NewsArticle::with('author.profile', 'category', 'tags', 'relatedEntities')
|
||||
->published()
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
|
||||
// Track view (once per session / IP)
|
||||
$this->trackView($request, $article);
|
||||
|
||||
// Related articles (same category, excluding current)
|
||||
$related = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->when($article->category_id, fn ($q) => $q->where('category_id', $article->category_id))
|
||||
->where('id', '!=', $article->id)
|
||||
->editorialOrder()
|
||||
->limit(config('news.related_limit', 4))
|
||||
->get();
|
||||
|
||||
return view('news.show', [
|
||||
'article' => $article,
|
||||
'related' => $related,
|
||||
'relatedEntities' => $this->news->resolveRelatedEntities($article, $request->user()),
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Helpers
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
private function trackView(Request $request, NewsArticle $article): void
|
||||
{
|
||||
$ip = $request->ip();
|
||||
$userId = Auth::id();
|
||||
$session = 'news_view_' . $article->id;
|
||||
|
||||
if ($request->session()->has($session)) {
|
||||
return;
|
||||
}
|
||||
|
||||
NewsView::create([
|
||||
'article_id' => $article->id,
|
||||
'user_id' => $userId,
|
||||
'ip' => $ip,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$article->incrementViews();
|
||||
|
||||
$request->session()->put($session, true);
|
||||
}
|
||||
|
||||
private function sidebarData(): array
|
||||
{
|
||||
return $this->news->sidebarData();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\News;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Response;
|
||||
use cPad\Plugins\News\Models\NewsArticle;
|
||||
|
||||
class NewsRssController extends Controller
|
||||
{
|
||||
/**
|
||||
* Generate RSS 2.0 feed for published news articles.
|
||||
* Endpoint: GET /rss/news
|
||||
*/
|
||||
public function feed(): Response
|
||||
{
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->orderByDesc('published_at')
|
||||
->limit(config('news.rss_limit', 25))
|
||||
->get();
|
||||
|
||||
$xml = $this->buildRss($articles);
|
||||
|
||||
return response($xml, 200, [
|
||||
'Content-Type' => 'application/rss+xml; charset=UTF-8',
|
||||
]);
|
||||
}
|
||||
|
||||
private function buildRss($articles): string
|
||||
{
|
||||
$siteUrl = config('app.url');
|
||||
$title = e(config('news.rss_title', 'News'));
|
||||
$description = e(config('news.rss_description', 'Latest news.'));
|
||||
$now = now()->toRfc2822String();
|
||||
|
||||
$items = '';
|
||||
foreach ($articles as $article) {
|
||||
$link = e(url('/news/' . $article->slug));
|
||||
$pubDate = $article->published_at?->toRfc2822String() ?? $now;
|
||||
$articleTitle = e($article->title);
|
||||
$excerpt = e(strip_tags((string) ($article->excerpt ?? '')));
|
||||
$category = e((string) ($article->category?->name ?? ''));
|
||||
$author = e((string) ($article->author?->name ?? ''));
|
||||
|
||||
$items .= <<<ITEM
|
||||
<item>
|
||||
<title><![CDATA[{$articleTitle}]]></title>
|
||||
<link>{$link}</link>
|
||||
<guid isPermaLink="true">{$link}</guid>
|
||||
<description><![CDATA[{$excerpt}]]></description>
|
||||
<pubDate>{$pubDate}</pubDate>
|
||||
<author>{$author}</author>
|
||||
<category>{$category}</category>
|
||||
</item>
|
||||
|
||||
ITEM;
|
||||
}
|
||||
|
||||
return <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<channel>
|
||||
<title>{$title}</title>
|
||||
<link>{$siteUrl}/news</link>
|
||||
<description>{$description}</description>
|
||||
<language>en-us</language>
|
||||
<lastBuildDate>{$now}</lastBuildDate>
|
||||
<atom:link href="{$siteUrl}/rss/news" rel="self" type="application/rss+xml"/>
|
||||
{$items} </channel>
|
||||
</rss>
|
||||
XML;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user