Entry.php 4.3 KB

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