Files
api/common/classes/NewsItem.php

79 lines
2.1 KiB
PHP

<?php
namespace Model;
require "NewsImage.php";
require "NewsSource.php";
class NewsItem extends Model {
public $id;
public $title;
public $content;
public $published;
public $edited;
public $keywords;
public $source;
public $category;
public $theme;
public $region;
public $podcast;
public $images;
public $video;
public $url;
public function __construct($data, $images = null) {
parent::__construct($data);
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($data->source)
{
$data->source = is_object($data->source)
? new \Model\NewsSource($data->source->title, $data->source->url, $data->source->url)
: new \Model\NewsSource($data->source, $data->source_url, $data->showsource);
}
$this->keywords = null;
if(is_array($data->keywords)) {
$this->keywords = $data->keywords;
} else if(trim($data->keywords)) {
$this->keywords = explode(' ', $data->keywords);
}
$this->podcast = $data->podcast ? $data->podcast : null;
$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);
}
public function detailsUrl() {
return "/{$this->id}/" . urlencode($this->title);
}
public function excerpt() {
$hasImages = count($this->images) > 0;
$maxLength = $hasImages ? 200 : 500;
return '<p class="news-excerpt ' . ($hasImages ? 'short' : 'long') . '">' .
substr($this->content, 0, $maxLength) .
(strlen($this->content) > $maxLength ? '...' : '') .
'</p>';
}
}