1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/freshrss_ynh.git synced 2024-09-03 18:36:33 +02:00
freshrss_ynh/sources/app/Models/Entry.php

192 lines
4.4 KiB
PHP
Raw Normal View History

2014-07-23 15:52:50 +02:00
<?php
class FreshRSS_Entry extends Minz_Model {
const STATE_READ = 1;
const STATE_NOT_READ = 2;
2015-02-08 18:55:48 +01:00
const STATE_ALL = 3;
2014-07-23 15:52:50 +02:00
const STATE_FAVORITE = 4;
const STATE_NOT_FAVORITE = 8;
private $id = 0;
private $guid;
private $title;
private $author;
private $content;
private $link;
private $date;
private $is_read;
private $is_favorite;
private $feed;
private $tags;
2015-02-08 18:55:48 +01:00
public function __construct($feed = '', $guid = '', $title = '', $author = '', $content = '',
$link = '', $pubdate = 0, $is_read = false, $is_favorite = false, $tags = '') {
$this->_guid($guid);
$this->_title($title);
$this->_author($author);
$this->_content($content);
$this->_link($link);
$this->_date($pubdate);
$this->_isRead($is_read);
$this->_isFavorite($is_favorite);
$this->_feed($feed);
$this->_tags(preg_split('/[\s#]/', $tags));
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
public function id() {
2014-07-23 15:52:50 +02:00
return $this->id;
}
2015-02-08 18:55:48 +01:00
public function guid() {
2014-07-23 15:52:50 +02:00
return $this->guid;
}
2015-02-08 18:55:48 +01:00
public function title() {
2014-07-23 15:52:50 +02:00
return $this->title;
}
2015-02-08 18:55:48 +01:00
public function author() {
2014-07-23 15:52:50 +02:00
return $this->author === null ? '' : $this->author;
}
2015-02-08 18:55:48 +01:00
public function content() {
2014-07-23 15:52:50 +02:00
return $this->content;
}
2015-02-08 18:55:48 +01:00
public function link() {
2014-07-23 15:52:50 +02:00
return $this->link;
}
2015-02-08 18:55:48 +01:00
public function date($raw = false) {
2014-07-23 15:52:50 +02:00
if ($raw) {
return $this->date;
} else {
2015-02-08 18:55:48 +01:00
return timestamptodate($this->date);
2014-07-23 15:52:50 +02:00
}
}
2015-02-08 18:55:48 +01:00
public function dateAdded($raw = false) {
2014-07-23 15:52:50 +02:00
$date = intval(substr($this->id, 0, -6));
if ($raw) {
return $date;
} else {
2015-02-08 18:55:48 +01:00
return timestamptodate($date);
2014-07-23 15:52:50 +02:00
}
}
2015-02-08 18:55:48 +01:00
public function isRead() {
2014-07-23 15:52:50 +02:00
return $this->is_read;
}
2015-02-08 18:55:48 +01:00
public function isFavorite() {
2014-07-23 15:52:50 +02:00
return $this->is_favorite;
}
2015-02-08 18:55:48 +01:00
public function feed($object = false) {
2014-07-23 15:52:50 +02:00
if ($object) {
$feedDAO = FreshRSS_Factory::createFeedDao();
2015-02-08 18:55:48 +01:00
return $feedDAO->searchById($this->feed);
2014-07-23 15:52:50 +02:00
} else {
return $this->feed;
}
}
2015-02-08 18:55:48 +01:00
public function tags($inString = false) {
2014-07-23 15:52:50 +02:00
if ($inString) {
2015-02-08 18:55:48 +01:00
return empty($this->tags) ? '' : '#' . implode(' #', $this->tags);
2014-07-23 15:52:50 +02:00
} else {
return $this->tags;
}
}
2015-02-08 18:55:48 +01:00
public function _id($value) {
2014-07-23 15:52:50 +02:00
$this->id = $value;
}
2015-02-08 18:55:48 +01:00
public function _guid($value) {
2014-07-23 15:52:50 +02:00
$this->guid = $value;
}
2015-02-08 18:55:48 +01:00
public function _title($value) {
2014-07-23 15:52:50 +02:00
$this->title = $value;
}
2015-02-08 18:55:48 +01:00
public function _author($value) {
2014-07-23 15:52:50 +02:00
$this->author = $value;
}
2015-02-08 18:55:48 +01:00
public function _content($value) {
2014-07-23 15:52:50 +02:00
$this->content = $value;
}
2015-02-08 18:55:48 +01:00
public function _link($value) {
2014-07-23 15:52:50 +02:00
$this->link = $value;
}
2015-02-08 18:55:48 +01:00
public function _date($value) {
2014-07-23 15:52:50 +02:00
$value = intval($value);
$this->date = $value > 1 ? $value : time();
}
2015-02-08 18:55:48 +01:00
public function _isRead($value) {
2014-07-23 15:52:50 +02:00
$this->is_read = $value;
}
2015-02-08 18:55:48 +01:00
public function _isFavorite($value) {
2014-07-23 15:52:50 +02:00
$this->is_favorite = $value;
}
2015-02-08 18:55:48 +01:00
public function _feed($value) {
2014-07-23 15:52:50 +02:00
$this->feed = $value;
}
2015-02-08 18:55:48 +01:00
public function _tags($value) {
if (!is_array($value)) {
$value = array($value);
2014-07-23 15:52:50 +02:00
}
foreach ($value as $key => $t) {
if (!$t) {
2015-02-08 18:55:48 +01:00
unset($value[$key]);
2014-07-23 15:52:50 +02:00
}
}
$this->tags = $value;
}
2015-02-08 18:55:48 +01:00
public function isDay($day, $today) {
2014-07-23 15:52:50 +02:00
$date = $this->dateAdded(true);
switch ($day) {
2015-02-08 18:55:48 +01:00
case FreshRSS_Days::TODAY:
$tomorrow = $today + 86400;
return $date >= $today && $date < $tomorrow;
case FreshRSS_Days::YESTERDAY:
$yesterday = $today - 86400;
return $date >= $yesterday && $date < $today;
case FreshRSS_Days::BEFORE_YESTERDAY:
$yesterday = $today - 86400;
return $date < $yesterday;
default:
return false;
2014-07-23 15:52:50 +02:00
}
}
public function loadCompleteContent($pathEntries) {
// Gestion du contenu
// On cherche à récupérer les articles en entier... même si le flux ne le propose pas
if ($pathEntries) {
$entryDAO = FreshRSS_Factory::createEntryDao();
$entry = $entryDAO->searchByGuid($this->feed, $this->guid);
2015-02-08 18:55:48 +01:00
if ($entry) {
2014-07-23 15:52:50 +02:00
// l'article existe déjà en BDD, en se contente de recharger ce contenu
$this->content = $entry->content();
} else {
try {
// l'article n'est pas en BDD, on va le chercher sur le site
$this->content = get_content_by_parsing(
htmlspecialchars_decode($this->link(), ENT_QUOTES), $pathEntries
);
} catch (Exception $e) {
2015-02-08 18:55:48 +01:00
// rien à faire, on garde l'ancien contenu(requête a échoué)
2014-07-23 15:52:50 +02:00
}
}
}
}
2015-02-08 18:55:48 +01:00
public function toArray() {
return array(
'id' => $this->id(),
'guid' => $this->guid(),
'title' => $this->title(),
'author' => $this->author(),
'content' => $this->content(),
'link' => $this->link(),
'date' => $this->date(true),
'is_read' => $this->isRead(),
'is_favorite' => $this->isFavorite(),
'id_feed' => $this->feed(),
'tags' => $this->tags(true),
2014-07-23 15:52:50 +02:00
);
}
}