153 lines
4.8 KiB
PHP
Executable File
153 lines
4.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Model;
|
|
|
|
require_once "NewsImage.php";
|
|
require_once "NewsSource.php";
|
|
require_once "MetaData.php";
|
|
|
|
class NewsItem extends Model {
|
|
public $id;
|
|
public $title;
|
|
public $author;
|
|
public $content;
|
|
public $published;
|
|
public $edited;
|
|
public $keywords;
|
|
|
|
public $source;
|
|
public $category;
|
|
public $theme;
|
|
public $region;
|
|
public $tags;
|
|
|
|
public $podcast;
|
|
public $images;
|
|
public $video;
|
|
public $type;
|
|
public $url;
|
|
public $metadata;
|
|
|
|
public function __construct($data, $images = null, $podcast = null) {
|
|
// Convert arrays to objects for consistent handling
|
|
if(is_array($data)) {
|
|
$data = (object) $data;
|
|
}
|
|
|
|
parent::__construct($data);
|
|
|
|
// Ensure id is a string, not an array
|
|
if(is_array($this->id)) {
|
|
$this->id = $this->id[0] ?? '';
|
|
}
|
|
|
|
// Ensure title is a string, not an array
|
|
if(is_array($this->title)) {
|
|
$this->title = $this->title[0] ?? '';
|
|
}
|
|
|
|
parent::ConvertToDateTime($this->published);
|
|
parent::ConvertToDateTime($this->edited);
|
|
|
|
if($this->edited && ($this->edited->getTimestamp() - $this->published->getTimestamp() < 1800 /* == 30 minutes */)) {
|
|
// If last edit was within grace period, consider it unedited (note: currently RES always saves edited == published on creation)
|
|
$this->edited = null;
|
|
}
|
|
|
|
$this->source = null;
|
|
if(isset($data->source))
|
|
{
|
|
if(is_object($data->source))
|
|
{
|
|
$this->source = new \Model\NewsSource($data->source->title ?? null, $data->source->url ?? null, $data->source->show ?? false);
|
|
} else if($data->source) {
|
|
$this->source = new \Model\NewsSource($data->source, $data->source_url ?? null, $data->showsource ?? false);
|
|
}
|
|
}
|
|
|
|
if($podcast) {
|
|
$this->podcast = new \Model\Podcast($podcast);
|
|
} else if(isset($data->podcast) && $data->podcast) {
|
|
$this->podcast = new \Model\Podcast($data->podcast);
|
|
}
|
|
|
|
$this->keywords = null;
|
|
if(isset($data->keywords)) {
|
|
if(is_array($data->keywords) || is_object($data->keywords)) {
|
|
$this->keywords = $data->keywords;
|
|
} else if(trim((string) $data->keywords)) {
|
|
$this->keywords = explode(' ', $data->keywords);
|
|
}
|
|
}
|
|
|
|
if(isset($data->region)) {
|
|
if(is_object($data->region)) {
|
|
$this->region = new \Model\NewsRegion($data->region->title ?? null, $data->region->slug ?? null);
|
|
} else {
|
|
$this->region = new \Model\NewsRegion($data->region, $data->region_slug ?? null);
|
|
}
|
|
}
|
|
|
|
if(isset($data->theme)) {
|
|
if(is_object($data->theme)) {
|
|
$this->theme = new \Model\NewsRegion($data->theme->title ?? null, $data->theme->slug ?? null);
|
|
} else {
|
|
$this->theme = new \Model\NewsRegion($data->theme, $data->theme_slug ?? null);
|
|
}
|
|
}
|
|
|
|
if(isset($data->tags)) {
|
|
$this->tags = [];
|
|
foreach($data->tags as $tag) {
|
|
if(is_object($tag)) {
|
|
$this->tags[] = new \Model\NewsRegion($tag->titel ?? null, $tag->slug ?? null);
|
|
} elseif(is_array($tag)) {
|
|
$this->tags[] = new \Model\NewsRegion($tag['titel'] ?? null, $tag['slug'] ?? null);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ensure content is iterable for views - convert string to empty array
|
|
if(isset($this->content) && is_string($this->content)) {
|
|
$this->content = [];
|
|
} elseif(!isset($this->content)) {
|
|
$this->content = [];
|
|
}
|
|
|
|
$images = ($images != null) ? $images
|
|
: (isset($data->images) ? $data->images : null);
|
|
if($images) {
|
|
$this->images = [];
|
|
foreach($images as $image) {
|
|
$this->images[] = new NewsImage($image, '/img/news/');
|
|
}
|
|
}
|
|
|
|
$this->url = "/nieuws/{$this->id}/" . parent::url_slug($this->title ?? '');
|
|
$this->metadata = (new MetaData())
|
|
->set('title', $this->title)
|
|
->set('description', strip_tags($this->excerpt()))
|
|
->set('image', isset($this->images) && count($this->images) ? $this->images[0]->url : null )
|
|
->set('video', isset($this->video) ? $this->video : null)
|
|
->set('audio', isset($this->podcast) ? $this->podcast->url : null)
|
|
->set('article:published_time', $this->published->format('r'))
|
|
->set('article:modified_time', $this->edited ? $this->edited->format('r') : null)
|
|
;
|
|
}
|
|
|
|
public function excerpt() {
|
|
// Handle case where content might be an array of blocks instead of a string
|
|
if(is_array($this->content) || is_object($this->content)) {
|
|
return '<p class="news-excerpt"></p>';
|
|
}
|
|
|
|
$hasImages = isset($this->images) && count($this->images) > 0;
|
|
$maxLength = $hasImages ? 200 : 500;
|
|
$text = (string) $this->content;
|
|
return '<p class="news-excerpt ' . ($hasImages ? 'short' : 'long') . '">' .
|
|
substr($text, 0, $maxLength) .
|
|
(strlen($text) > $maxLength ? '...' : '') .
|
|
'</p>';
|
|
}
|
|
}
|