+45
-11
@@ -29,7 +29,23 @@ class NewsItem extends Model {
|
||||
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);
|
||||
|
||||
@@ -43,9 +59,9 @@ class NewsItem extends Model {
|
||||
{
|
||||
if(is_object($data->source))
|
||||
{
|
||||
$this->source = new \Model\NewsSource($data->source->title, $data->source->url, $data->source->show);
|
||||
$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, $data->showsource);
|
||||
$this->source = new \Model\NewsSource($data->source, $data->source_url ?? null, $data->showsource ?? false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,33 +75,45 @@ class NewsItem extends Model {
|
||||
if(isset($data->keywords)) {
|
||||
if(is_array($data->keywords) || is_object($data->keywords)) {
|
||||
$this->keywords = $data->keywords;
|
||||
} else if(trim($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, $data->region->slug);
|
||||
$this->region = new \Model\NewsRegion($data->region->title ?? null, $data->region->slug ?? null);
|
||||
} else {
|
||||
$this->region = new \Model\NewsRegion($data->region, $data->region_slug);
|
||||
$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, $data->theme->slug);
|
||||
$this->theme = new \Model\NewsRegion($data->theme->title ?? null, $data->theme->slug ?? null);
|
||||
} else {
|
||||
$this->theme = new \Model\NewsRegion($data->theme, $data->theme_slug);
|
||||
$this->theme = new \Model\NewsRegion($data->theme, $data->theme_slug ?? null);
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data->tags)) {
|
||||
$this->tags = [];
|
||||
foreach($data->tags as $tag) {
|
||||
$this->theme = new \Model\NewsRegion($tag->titel, $tag->slug);
|
||||
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) {
|
||||
@@ -95,7 +123,7 @@ class NewsItem extends Model {
|
||||
}
|
||||
}
|
||||
|
||||
$this->url = "/nieuws/{$this->id}/" . parent::url_slug($this->title);
|
||||
$this->url = "/nieuws/{$this->id}/" . parent::url_slug($this->title ?? '');
|
||||
$this->metadata = (new MetaData())
|
||||
->set('title', $this->title)
|
||||
->set('description', strip_tags($this->excerpt()))
|
||||
@@ -108,11 +136,17 @@ class NewsItem extends Model {
|
||||
}
|
||||
|
||||
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($this->content, 0, $maxLength) .
|
||||
(strlen($this->content) > $maxLength ? '...' : '') .
|
||||
substr($text, 0, $maxLength) .
|
||||
(strlen($text) > $maxLength ? '...' : '') .
|
||||
'</p>';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user