Entry.php 4.5 KB

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