1<?php
2
3namespace FYKOS\dokuwiki\Extension\PluginNewsFeed\Model;
4
5use helper_plugin_sqlite;
6
7/**
8 * Class Priority
9 * @author Michal Červeňák <miso@fykos.cz>
10 */
11class ModelPriority extends AbstractModel {
12
13    public int $priorityId;
14
15    public ?string $priorityFrom;
16
17    public ?string $priorityTo;
18
19    private int $priorityValue;
20
21    public int $newsId;
22
23    public int $streamId;
24
25    public function getPriorityValue(): int {
26        if ((time() < strtotime($this->priorityFrom)) || (time() > strtotime($this->priorityTo))) {
27            return 0;
28        }
29        return $this->priorityValue;
30    }
31
32    public function delete() {
33        $res = $this->sqlite->query('DELETE FROM priority WHERE stream_id=? AND news_id =?', $this->streamId, $this->newsId);
34        return $this->sqlite->res2arr($res);
35    }
36
37    public static function createFromArray(helper_plugin_sqlite $helperPluginSqlite, array $data): self {
38        $model = new self($helperPluginSqlite);
39        $model->priorityId = $data['priority_id'];
40        $model->newsId = $data['news_id'];
41        $model->streamId = $data['stream_id'];
42        $model->priorityValue = $data['priority'] ?? 0;
43        $model->priorityFrom = $data['priority_from'];
44        $model->priorityTo = $data['priority_to'];
45
46        return $model;
47    }
48}
49