1<?php 2 3namespace FYKOS\dokuwiki\Extension\PluginNewsFeed\Model; 4 5use helper_plugin_sqlite; 6 7class ModelNews extends AbstractModel { 8 9 const SIMPLE_RENDER_PATTERN = '{{news-feed>id="@id@" even="@even@" editable="@editable@" stream="@stream@" page_id="@page_id@"}}'; 10 11 public int $newsId; 12 13 public string $title; 14 15 public string $authorName; 16 17 public string $authorEmail; 18 19 public string $text; 20 21 public string $newsDate; 22 23 public ?string $image; 24 25 public string $category; 26 27 public ?string $linkHref; 28 29 public ?string $linkTitle; 30 31 private ModelPriority $priority; 32 33 public function getPriority(): ModelPriority { 34 return $this->priority; 35 } 36 37 public function renderText(string $mode = 'xhtml'): ?string { 38 return p_render($mode, p_get_instructions($this->text), $info); 39 } 40 41 public function getLocalDate(callable $getLang): string { 42 $date = date('j\. F Y', strtotime($this->newsDate)); 43 $enMonth = [ 44 'January', 45 'February', 46 'March', 47 'April', 48 'May', 49 'June', 50 'July', 51 'August', 52 'September', 53 'October', 54 'November', 55 'December', 56 ]; 57 $langMonth = [ 58 $getLang('jan'), 59 $getLang('feb'), 60 $getLang('mar'), 61 $getLang('apr'), 62 $getLang('may'), 63 $getLang('jun'), 64 $getLang('jul'), 65 $getLang('aug'), 66 $getLang('sep'), 67 $getLang('oct'), 68 $getLang('now'), 69 $getLang('dec'), 70 ]; 71 return (string)str_replace($enMonth, $langMonth, $date); 72 } 73 74 public function hasImage(): bool { 75 return (bool)$this->image; 76 } 77 78 public function hasLink(): bool { 79 return (bool)$this->linkHref; 80 } 81 82 public function getToken($pageId = ''): string { 83 return (string)wl($pageId, null, true) . '?news-id=' . $this->newsId; 84 } 85 86 public function getCacheFile(): string { 87 return static::getCacheFileById($this->newsId); 88 } 89 90 public static function getCacheFileById(int $id): string { 91 return 'news-feed_news_' . $id; 92 } 93 94 public function render(string $even, string $stream, string $pageId = '', bool $editable = true): ?string { 95 $renderPattern = str_replace(['@id@', '@even@', '@editable@', '@stream@', '@page_id@'], 96 [ 97 $this->newsId, 98 $even, 99 $editable ? 'true' : 'false', 100 $stream, 101 $pageId, 102 ], 103 self::SIMPLE_RENDER_PATTERN); 104 $info = []; 105 return p_render('xhtml', p_get_instructions($renderPattern), $info); 106 } 107 108 public function setPriority(ModelPriority $priority): void { 109 $this->priority = $priority; 110 } 111 112 public static function createFromArray(helper_plugin_sqlite $helperPluginSqlite, array $data): self { 113 $model = new self($helperPluginSqlite); 114 $model->newsId = $data['news_id']; 115 $model->title = $data['title']; 116 $model->authorName = $data['author_name']; 117 $model->authorEmail = $data['author_email']; 118 $model->text = $data['text']; 119 $model->newsDate = $data['news_date']; 120 $model->image = $data['image']; 121 $model->category = $data['category']; 122 $model->linkHref = $data['link_href']; 123 $model->linkTitle = $data['link_title']; 124 return $model; 125 } 126 127 public function loadDefault(): self { 128 global $INFO; 129 $this->authorName = $INFO['userinfo']['name']; 130 $this->newsDate = date('Y-m-d\TH:i:s'); 131 $this->authorEmail = $INFO['userinfo']['mail']; 132 return $this; 133 } 134} 135