- Canonical URL includes ?pagina=N, so later pages aren't treated as
duplicates of page 1; 'load more' button links to the next page so
search engines can reach older articles (JavaScript still loads in place)
- Description per page ('Het laatste nieuws over Blaricum van GOOI&meer...')
and '(pagina N)' in the title from page 2
- Real tag name in title/heading ('Gooise Meren' instead of 'Gooise-meren')
- CollectionPage + ItemList (articles, numbered across pages) and
BreadcrumbList structured data
- Open Graph tags (type website, title, description, url, image)
- Search results: noindex, follow and no structured data
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
267 lines
9.2 KiB
PHP
267 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use \Model\NewsItem;
|
|
|
|
class NewsController extends Controller
|
|
{
|
|
private static function TimestampToDateTime($timestamp)
|
|
{
|
|
$result = new \DateTime;
|
|
$result->setTimestamp($timestamp);
|
|
return $result;
|
|
}
|
|
|
|
public function show(Request $request, $id)
|
|
{
|
|
parent::registerView($request, 'nieuws', $id);
|
|
$preview = "";
|
|
if (request()->get('preview', null) != null) {
|
|
$preview = "?preview=" . request()->get('preview');
|
|
}
|
|
$apiResult = $this->API('nieuws/bericht/' . $id . $preview);
|
|
$newsItem = new \Model\NewsItem($apiResult->news);
|
|
|
|
switch ($apiResult->version) {
|
|
case 1:
|
|
if (!$newsItem->content)
|
|
return redirect('//nhnieuws.nl/gooi');
|
|
|
|
case 2:
|
|
if (isset($apiResult->source->article)) {
|
|
$source = $apiResult->source->article;
|
|
$newsItem->published = self::TimestampToDateTime($source->created);
|
|
$newsItem->edited = self::TimestampToDateTime($source->updated);
|
|
$newsItem->author = $source->author;
|
|
$newsItem->images = null; // Images will be embedded
|
|
$newsItem->video = null; // Videos will be embedded
|
|
$newsItem->content = $source->blocks;
|
|
} elseif (isset($apiResult->source->blocks)) {
|
|
$newsItem->content = $apiResult->source->blocks;
|
|
}
|
|
// Dates, author and content may have changed above
|
|
$newsItem->buildMetadata();
|
|
return view('newsitem', array_merge($this->getSidebareData(), ['type' => $apiResult->type, 'news' => $newsItem, 'metadata' => $newsItem->metadata, 'searchURL' => 'nieuws/zoeken']));
|
|
}
|
|
}
|
|
|
|
public function overview(Request $request)
|
|
{
|
|
return $this->listNews($request, 'overzicht', null, 'items-more-news', 10);
|
|
}
|
|
|
|
public function more(Request $request)
|
|
{
|
|
$page = (int) $request->get('pagina', 1);
|
|
$id = $request->get('id', '');
|
|
$apiResult = $this->API('nieuws/overzicht?pagina=' . (int) max(1, $page) . '&aantal=5');
|
|
$news = [];
|
|
foreach ($apiResult->news as $newsItem) {
|
|
$news[] = new \Model\NewsItem($newsItem);
|
|
}
|
|
|
|
return view('partial/newslist_small', ['id' => $id, 'news' => $news]);
|
|
}
|
|
|
|
public function populair(Request $request)
|
|
{
|
|
$page = (int) $request->get('pagina', 1);
|
|
$id = $request->get('id', '');
|
|
$populair = [];
|
|
$apiResult = $this->API('nieuws/populair?pagina=' . (int) max(1, $page) . '&aantal=5');
|
|
foreach ($apiResult->news as $_newsItem) {
|
|
$populair[] = new \Model\NewsItem($_newsItem);
|
|
}
|
|
|
|
return view('partial/newslist_small', ['id' => $id, 'news' => $populair]);
|
|
}
|
|
|
|
public function taglist(Request $request, $tag)
|
|
{
|
|
return $this->listNews($request, 'tag/' . $tag, ucfirst($tag), 'items', null, $tag);
|
|
}
|
|
|
|
public function regionlist(Request $request, $region)
|
|
{
|
|
return $this->listNews($request, 'tag/' . $region, ucfirst($region), 'items', null, $region);
|
|
}
|
|
|
|
public function themelist(Request $request, $theme)
|
|
{
|
|
return $this->listNews($request, 'tag/' . $theme, ucfirst($theme), 'items', null, $theme);
|
|
}
|
|
|
|
public function search(Request $request, $query)
|
|
{
|
|
return $this->listNews($request, 'zoeken/' . $query, 'Zoekresultaat')->with('query', urldecode($query));
|
|
}
|
|
|
|
|
|
public function activeblog()
|
|
{
|
|
$apiResult = $this->API('blog/overzicht');
|
|
if (count($apiResult)) {
|
|
$blog = new \Model\Blog($apiResult[0]);
|
|
if ($blog->is_active) {
|
|
return redirect($blog->url);
|
|
}
|
|
}
|
|
|
|
return abort(404);
|
|
}
|
|
|
|
public function liveblog(Request $request, $id)
|
|
{
|
|
|
|
parent::registerView($request, 'blog', $id);
|
|
|
|
$blog = $this->API('nieuws/liveblog/' . (int) $id);
|
|
foreach ($blog->artikelen as &$item) {
|
|
$item = new \Model\NewsItem($item);
|
|
}
|
|
|
|
if (request()->ajax()) {
|
|
return $blog;
|
|
}
|
|
|
|
if (count($blog->artikelen)) {
|
|
return view('blog', ['blog' => $blog]);
|
|
}
|
|
|
|
return abort();
|
|
}
|
|
|
|
/**
|
|
* @param string|null $slug tag slug of a tag/region/theme page; used to show
|
|
* the tag's real name ('Gooise Meren' instead of 'Gooise-meren')
|
|
*/
|
|
private function listNews(Request $request, $url, $title = null, $id = 'items', $total = null, $slug = null)
|
|
{
|
|
if ($request->ajax()) {
|
|
$total = 5;
|
|
}
|
|
$page = (int) $request->get('pagina', 1);
|
|
if ($url == 'overzicht' && $request->get('dateStart', null) && $request->get('dateEnd', null)) {
|
|
$url = 'datum/' . $request->get('dateStart', null) . '/' . $request->get('dateEnd', null);
|
|
}
|
|
$apiResult = $this->API('nieuws/' . $url . '?pagina=' . (int) max(1, $page) . ($total ? '&aantal=' . $total : ''));
|
|
$news = [];
|
|
foreach ($apiResult->news ?? [] as $newsItem) {
|
|
$news[] = new \Model\NewsItem($newsItem);
|
|
}
|
|
if ($slug) {
|
|
$title = self::tagTitle($news, $slug) ?: $title;
|
|
}
|
|
|
|
$populair = [];
|
|
if ($title == null) {
|
|
$total = 5;
|
|
}
|
|
$apiResult = $this->API('nieuws/populair?pagina=' . (int) max(
|
|
1,
|
|
$page
|
|
) . ($total ? '&aantal=' . $total : ''));
|
|
foreach ($apiResult->news as $newsItem) {
|
|
$populair[] = new \Model\NewsItem($newsItem);
|
|
}
|
|
|
|
$podcast = null;
|
|
$podcasts = [];
|
|
if ($title == null) {
|
|
$apiResult = $this->API('podcast/overzicht?aantal=3');
|
|
$podcast = new \Model\Podcast($apiResult->podcasts[0]);
|
|
foreach ($apiResult->podcasts as $_podcast) {
|
|
$podcasts[] = new \Model\Podcast($_podcast);
|
|
}
|
|
}
|
|
|
|
$newsItems = [];
|
|
$apiResult = $this->API('nieuws/overzicht?aantal=5');
|
|
foreach ($apiResult->news as $_newsItem) {
|
|
$newsItems[] = new \Model\NewsItem($_newsItem);
|
|
}
|
|
|
|
$data = ['populair' => $populair, 'newsItems' => $newsItems, 'podcasts' => $podcasts, 'podcast' => $podcast, 'id' => $id, 'title' => $title, 'news' => $news, 'searchURL' => 'nieuws/zoeken'];
|
|
if (!$request->ajax() && $title !== null) {
|
|
$data += $this->listingSeo($url, $title, $news, max(1, $page));
|
|
}
|
|
|
|
return view($request->ajax() ? ($title == null ? 'partial/home_newslist_small' : 'partial/newslist_small') : ($title == null ? 'news' : 'newslist'), $data);
|
|
}
|
|
|
|
/** The tag's name as used in the articles, e.g. 'Gooise Meren' for slug 'gooise-meren'. */
|
|
private static function tagTitle(array $news, $slug)
|
|
{
|
|
foreach ($news as $item) {
|
|
foreach ((is_array($item->tags) ? $item->tags : []) as $tag) {
|
|
if (($tag->slug ?? null) === $slug && !empty($tag->titel)) {
|
|
return $tag->titel;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* SEO data for news overview pages (tag/region/theme pages and search results):
|
|
* canonical URL including the page number, description, Open Graph tags and
|
|
* whether to add structured data. Search results are kept out of the index.
|
|
*/
|
|
private function listingSeo($url, $title, array $news, $page)
|
|
{
|
|
$isSearch = strpos($url, 'zoeken/') === 0;
|
|
$canonical = url()->current() . ($page > 1 ? '?pagina=' . $page : '');
|
|
$description = 'Het laatste nieuws over ' . $title . ' van GOOI&meer, de streekomroep voor Gooi & Vechtstreek.'
|
|
. ($page > 1 ? ' Pagina ' . $page . '.' : '');
|
|
|
|
$image = null;
|
|
foreach ($news as $item) {
|
|
if ($images = $item->imageList()) {
|
|
$image = $images[0]['url'];
|
|
break;
|
|
}
|
|
}
|
|
|
|
$metadata = (new \Model\MetaData())
|
|
->set('type', 'website')
|
|
->set('title', $title . ' - Nieuws' . ($page > 1 ? ' (pagina ' . $page . ')' : ''))
|
|
->set('description', $isSearch ? null : $description)
|
|
->set('image', $image);
|
|
|
|
return [
|
|
'canonical' => $canonical,
|
|
'pageNumber' => $page,
|
|
'metadata' => $metadata,
|
|
'noindex' => $isSearch,
|
|
'listingJsonld' => !$isSearch,
|
|
];
|
|
}
|
|
|
|
public function popular()
|
|
{
|
|
$apiResult = $this->API('nieuws/populair');
|
|
$news = [];
|
|
foreach ($apiResult->news as $newsItem) {
|
|
$news[] = new \Model\NewsItem($newsItem);
|
|
}
|
|
|
|
return view('popularnews', ['news' => $news]);
|
|
}
|
|
|
|
public function regionieuws()
|
|
{
|
|
$data = $this->API('nieuws/regionieuws.json');
|
|
return view('listen', [
|
|
'source' => $this->API_URL . 'nieuws/regionieuws.mp3',
|
|
'title' => 'Regionieuws',
|
|
'content' => 'het laatste nieuws uit de regio',
|
|
'isStream' => false,
|
|
'canDownload' => true,
|
|
'lengte' => $data->length * 0.25,
|
|
'waveform' => $data
|
|
]);
|
|
}
|
|
}
|