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, $data->source->url, $data->source->show); } else if($data->source) { $this->source = new \Model\NewsSource($data->source, $data->source_url, $data->showsource); } } 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($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); } else { $this->region = new \Model\NewsRegion($data->region, $data->region_slug); } } if(isset($data->theme)) { if(is_object($data->theme)) { $this->theme = new \Model\NewsRegion($data->theme->title, $data->theme->slug); } else { $this->theme = new \Model\NewsRegion($data->theme, $data->theme_slug); } } if(isset($data->tags)) { foreach($data->tags as $tag) { $this->theme = new \Model\NewsRegion($tag->titel, $tag->slug); } } $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->buildMetadata(); } /** * (Re)build the Open Graph / article metadata. Call again after changing * the content, dates or author (e.g. for API v2 articles). */ public function buildMetadata() { $images = $this->imageList(); $tags = $this->tagTitles(); $this->metadata = (new MetaData()) ->set('title', $this->plainTitle()) ->set('description', $this->summary(200)) ->set('image', $images ? $images[0]['url'] : null) ->set('image:alt', $images ? $images[0]['title'] : null) ->set('video', isset($this->video) ? $this->video : null) ->set('audio', isset($this->podcast) ? $this->podcast->url : null) ->set('article:published_time', $this->published ? $this->published->format(DATE_ATOM) : null) ->set('article:modified_time', $this->edited ? $this->edited->format(DATE_ATOM) : null) ->set('article:section', $tags ? $tags[0] : null) ->set('article:tag', $tags) ; return $this->metadata; } /** The title without HTML tags or entities. */ public function plainTitle() { return self::normalizeText((string)$this->title); } /** Titles of the article's tags (mostly places, e.g. 'Hilversum'). */ public function tagTitles() { $titles = []; foreach((is_array($this->tags) ? $this->tags : []) as $tag) { if(isset($tag->titel) && $tag->titel) { $titles[] = $tag->titel; } } return $titles; } /** Plain text of the article; works for HTML content and API v2 content blocks. */ public function plainText() { if(is_string($this->content)) { return self::normalizeText($this->content); } $parts = []; foreach((is_array($this->content) ? $this->content : []) as $block) { if(in_array($block->type ?? '', ['intro', 'paragraph', 'text', 'header'])) { $parts[] = $block->data->text ?? $block->text ?? ''; } } return self::normalizeText(implode(' ', $parts)); } /** Short plain-text summary (for meta descriptions), cut at a word boundary. */ public function summary($maxLength = 160) { $text = $this->plainText(); if(mb_strlen($text) <= $maxLength) { return $text; } $cut = mb_substr($text, 0, $maxLength); $space = mb_strrpos($cut, ' '); return rtrim(mb_substr($cut, 0, $space ?: $maxLength), " ,.;:-") . '…'; } /** * The article's images as ['url' => ..., 'title' => ..., 'own' => bool]. * Own images ('own' => true) have a URL relative to the image base * (IMAGE_BASE_URL); embedded (v2) images get an absolute URL. */ public function imageList() { $list = []; foreach((is_array($this->images) ? $this->images : []) as $image) { $list[] = ['url' => $image->url, 'title' => $image->title ?? null, 'own' => true]; } foreach((is_array($this->content) ? $this->content : []) as $block) { $type = $block->type ?? ''; if($type == 'headerRichA' && isset($block->data->image)) { $image = $block->data->image; $url = isset($image->crops) ? ($image->crops->{'16:9'}->{'1600'} ?? null) : ($image->crop ?? null); $list[] = ['url' => $url, 'title' => $image->title ?? null]; } elseif(in_array($type, ['headerRichA', 'image']) && isset($block->image->url)) { $list[] = ['url' => $block->image->url, 'title' => $block->image->title ?? null]; } elseif($type == 'image' && isset($block->data->url)) { $list[] = ['url' => '/' . ltrim($block->data->url, '/'), 'title' => $block->data->title ?? null]; } } $list = array_values(array_filter($list, function($image) { return $image['url']; })); foreach($list as &$image) { if(!isset($image['own'])) { $image['own'] = false; if(!preg_match('#^(https?:)?//#i', $image['url'])) { $image['url'] = url($image['url']); } } } return $list; } private static function normalizeText($html) { $html = preg_replace('#|

|#i', ' ', $html); $text = html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, 'UTF-8'); return trim(preg_replace('/\s+/u', ' ', $text)); } public function excerpt() { $hasImages = isset($this->images) && count($this->images) > 0; $maxLength = $hasImages ? 200 : 500; return '

' . substr($this->content, 0, $maxLength) . (strlen($this->content) > $maxLength ? '...' : '') . '

'; } }