Entry.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. class FreshRSS_Entry extends Minz_Model {
  3. const STATE_ALL = 0;
  4. const STATE_READ = 1;
  5. const STATE_NOT_READ = 2;
  6. const STATE_FAVORITE = 4;
  7. const STATE_NOT_FAVORITE = 8;
  8. const STATE_STRICT = 16;
  9. private $id = 0;
  10. private $guid;
  11. private $title;
  12. private $author;
  13. private $content;
  14. private $link;
  15. private $date;
  16. private $is_read;
  17. private $is_favorite;
  18. private $feed;
  19. private $tags;
  20. public function __construct($feed = '', $guid = '', $title = '', $author = '', $content = '',
  21. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false, $tags = '') {
  22. $this->_guid($guid);
  23. $this->_title($title);
  24. $this->_author($author);
  25. $this->_content($content);
  26. $this->_link($link);
  27. $this->_date($pubdate);
  28. $this->_isRead($is_read);
  29. $this->_isFavorite($is_favorite);
  30. $this->_feed($feed);
  31. $this->_tags(preg_split('/[\s#]/', $tags));
  32. }
  33. public function id() {
  34. return $this->id;
  35. }
  36. public function guid() {
  37. return $this->guid;
  38. }
  39. public function title() {
  40. return $this->title;
  41. }
  42. public function author() {
  43. return $this->author === null ? '' : $this->author;
  44. }
  45. public function content() {
  46. return $this->content;
  47. }
  48. public function link() {
  49. return $this->link;
  50. }
  51. public function date($raw = false) {
  52. if ($raw) {
  53. return $this->date;
  54. } else {
  55. return timestamptodate($this->date);
  56. }
  57. }
  58. public function dateAdded($raw = false) {
  59. $date = intval(substr($this->id, 0, -6));
  60. if ($raw) {
  61. return $date;
  62. } else {
  63. return timestamptodate($date);
  64. }
  65. }
  66. public function isRead() {
  67. return $this->is_read;
  68. }
  69. public function isFavorite() {
  70. return $this->is_favorite;
  71. }
  72. public function feed($object = false) {
  73. if ($object) {
  74. $feedDAO = FreshRSS_Factory::createFeedDao();
  75. return $feedDAO->searchById($this->feed);
  76. } else {
  77. return $this->feed;
  78. }
  79. }
  80. public function tags($inString = false) {
  81. if ($inString) {
  82. return empty($this->tags) ? '' : '#' . implode(' #', $this->tags);
  83. } else {
  84. return $this->tags;
  85. }
  86. }
  87. public function _id($value) {
  88. $this->id = $value;
  89. }
  90. public function _guid($value) {
  91. $this->guid = $value;
  92. }
  93. public function _title($value) {
  94. $this->title = $value;
  95. }
  96. public function _author($value) {
  97. $this->author = $value;
  98. }
  99. public function _content($value) {
  100. $this->content = $value;
  101. }
  102. public function _link($value) {
  103. $this->link = $value;
  104. }
  105. public function _date($value) {
  106. $value = intval($value);
  107. $this->date = $value > 1 ? $value : time();
  108. }
  109. public function _isRead($value) {
  110. $this->is_read = $value;
  111. }
  112. public function _isFavorite($value) {
  113. $this->is_favorite = $value;
  114. }
  115. public function _feed($value) {
  116. $this->feed = $value;
  117. }
  118. public function _tags($value) {
  119. if (!is_array($value)) {
  120. $value = array($value);
  121. }
  122. foreach ($value as $key => $t) {
  123. if (!$t) {
  124. unset($value[$key]);
  125. }
  126. }
  127. $this->tags = $value;
  128. }
  129. public function isDay($day, $today) {
  130. $date = $this->dateAdded(true);
  131. switch ($day) {
  132. case FreshRSS_Days::TODAY:
  133. $tomorrow = $today + 86400;
  134. return $date >= $today && $date < $tomorrow;
  135. case FreshRSS_Days::YESTERDAY:
  136. $yesterday = $today - 86400;
  137. return $date >= $yesterday && $date < $today;
  138. case FreshRSS_Days::BEFORE_YESTERDAY:
  139. $yesterday = $today - 86400;
  140. return $date < $yesterday;
  141. default:
  142. return false;
  143. }
  144. }
  145. public function loadCompleteContent($pathEntries) {
  146. // Gestion du contenu
  147. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  148. if ($pathEntries) {
  149. $entryDAO = FreshRSS_Factory::createEntryDao();
  150. $entry = $entryDAO->searchByGuid($this->feed, $this->guid);
  151. if ($entry) {
  152. // l'article existe déjà en BDD, en se contente de recharger ce contenu
  153. $this->content = $entry->content();
  154. } else {
  155. try {
  156. // l'article n'est pas en BDD, on va le chercher sur le site
  157. $this->content = get_content_by_parsing(
  158. htmlspecialchars_decode($this->link(), ENT_QUOTES), $pathEntries
  159. );
  160. } catch (Exception $e) {
  161. // rien à faire, on garde l'ancien contenu(requête a échoué)
  162. }
  163. }
  164. }
  165. }
  166. public function toArray() {
  167. return array(
  168. 'id' => $this->id(),
  169. 'guid' => $this->guid(),
  170. 'title' => $this->title(),
  171. 'author' => $this->author(),
  172. 'content' => $this->content(),
  173. 'link' => $this->link(),
  174. 'date' => $this->date(true),
  175. 'is_read' => $this->isRead(),
  176. 'is_favorite' => $this->isFavorite(),
  177. 'id_feed' => $this->feed(),
  178. 'tags' => $this->tags(true),
  179. );
  180. }
  181. }