Entry.php 4.4 KB

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