1<?php 2 3namespace FYKOS\dokuwiki\Extension\PluginNewsFeed\Model; 4 5use FYKOS\dokuwiki\Extension\PluginNewsFeed\ORM\ServiceNews; 6use FYKOS\dokuwiki\Extension\PluginNewsFeed\ORM\ServiceStream; 7use helper_plugin_sqlite; 8 9/** 10 * Class Stream 11 * @author Michal Červeňák <miso@fykos.cz> 12 */ 13class ModelStream extends AbstractModel { 14 15 public int $streamId; 16 17 public string $name; 18 19 /** 20 * @return ModelNews[] 21 */ 22 public function getNews(): array { 23 $res = $this->sqlite->query('SELECT * FROM priority o JOIN news n ON o.news_id=n.news_id WHERE stream_id=? ', 24 $this->streamId); 25 $ars = $this->sqlite->res2arr($res); 26 $news = []; 27 $service = new ServiceNews($this->sqlite); 28 foreach ($ars as $ar) { 29 $priority = ModelPriority::createFromArray($this->sqlite, $ar); 30 31 $feed = $service->getById($ar['news_id']); 32 $feed->setPriority($priority); 33 $news[] = $feed; 34 } 35 return $this->sortNews($news); 36 } 37 38 private function sortNews(array $news): array { 39 usort($news, 40 function (ModelNews $a, ModelNews $b) { 41 if ($a->getPriority()->getPriorityValue() > $b->getPriority()->getPriorityValue()) { 42 return -1; 43 } elseif ($a->getPriority()->getPriorityValue() < $b->getPriority()->getPriorityValue()) { 44 return 1; 45 } else { 46 return strcmp($b->newsDate, $a->newsDate); 47 } 48 }); 49 return $news; 50 } 51 52 /** 53 * @return ModelStream[] 54 */ 55 public function getAllParentDependence(): array { 56 $streams = []; 57 $res = $this->sqlite->query('SELECT * FROM dependence WHERE parent=?', $this->streamId); 58 $service = new ServiceStream($this->sqlite); 59 foreach ($this->sqlite->res2arr($res) as $row) { 60 $streams[] = $service->getById($row['child']); 61 } 62 return $streams; 63 } 64 65 /** 66 * @return ModelStream[] 67 */ 68 public function getAllChildDependence(): array { 69 $streams = []; 70 $res = $this->sqlite->query('SELECT * FROM dependence WHERE child=?', $this->streamId); 71 $service = new ServiceStream($this->sqlite); 72 foreach ($this->sqlite->res2arr($res) as $row) { 73 $streams[] = $service->getById($row['parent']); 74 } 75 return $streams; 76 } 77 78 public static function createFromArray(helper_plugin_sqlite $helperPluginSqlite, array $data): self { 79 $model = new self($helperPluginSqlite); 80 $model->name = $data['name']; 81 $model->streamId = $data['stream_id']; 82 return $model; 83 } 84} 85