Entry.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. class Entry extends Model {
  3. private $guid;
  4. private $title;
  5. private $author;
  6. private $content;
  7. private $link;
  8. private $date;
  9. private $is_read;
  10. private $is_favorite;
  11. private $feed;
  12. public function __construct ($feed = '', $guid = '', $title = '', $author = '', $content = '',
  13. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false) {
  14. $this->_guid ($guid);
  15. $this->_title ($title);
  16. $this->_author ($author);
  17. $this->_content ($content);
  18. $this->_link ($link);
  19. $this->_date ($pubdate);
  20. $this->_isRead ($is_read);
  21. $this->_isFavorite ($is_favorite);
  22. $this->_feed ($feed);
  23. }
  24. public function id () {
  25. return small_hash ($this->guid . Configuration::selApplication ());
  26. }
  27. public function guid () {
  28. return $this->guid;
  29. }
  30. public function title () {
  31. return $this->title;
  32. }
  33. public function author () {
  34. return $this->author;
  35. }
  36. public function content () {
  37. return $this->content;
  38. }
  39. public function link () {
  40. return $this->link;
  41. }
  42. public function date ($raw = false) {
  43. if ($raw) {
  44. return $this->date;
  45. } else {
  46. return timestamptodate ($this->date);
  47. }
  48. }
  49. public function isRead () {
  50. return $this->is_read;
  51. }
  52. public function isFavorite () {
  53. return $this->is_favorite;
  54. }
  55. public function feed ($object = false) {
  56. if ($object) {
  57. $feedDAO = new FeedDAO ();
  58. return $feedDAO->searchById ($this->feed);
  59. } else {
  60. return $this->feed;
  61. }
  62. }
  63. public function _guid ($value) {
  64. $this->guid = $value;
  65. }
  66. public function _title ($value) {
  67. $this->title = $value;
  68. }
  69. public function _author ($value) {
  70. $this->author = $value;
  71. }
  72. public function _content ($value) {
  73. $this->content = $value;
  74. }
  75. public function _link ($value) {
  76. $this->link = $value;
  77. }
  78. public function _date ($value) {
  79. $this->date = $value;
  80. }
  81. public function _isRead ($value) {
  82. $this->is_read = $value;
  83. }
  84. public function _isFavorite ($value) {
  85. $this->is_favorite = $value;
  86. }
  87. public function _feed ($value) {
  88. $this->feed = $value;
  89. }
  90. }
  91. class EntryDAO extends Model_array {
  92. public function __construct () {
  93. parent::__construct (PUBLIC_PATH . '/data/db/Entries.array.php');
  94. }
  95. public function addEntry ($values) {
  96. $id = $values['id'];
  97. unset ($values['id']);
  98. if (!isset ($this->array[$id])) {
  99. $this->array[$id] = array ();
  100. foreach ($values as $key => $value) {
  101. $this->array[$id][$key] = $value;
  102. }
  103. $this->writeFile ($this->array);
  104. } else {
  105. return false;
  106. }
  107. }
  108. public function updateEntry ($id, $values) {
  109. foreach ($values as $key => $value) {
  110. $this->array[$id][$key] = $value;
  111. }
  112. $this->writeFile($this->array);
  113. }
  114. public function searchById ($id) {
  115. $list = HelperEntry::daoToEntry ($this->array);
  116. if (isset ($list[$id])) {
  117. return $list[$id];
  118. } else {
  119. return false;
  120. }
  121. }
  122. public function listEntries ($mode) {
  123. $list = $this->array;
  124. if (!is_array ($list)) {
  125. $list = array ();
  126. }
  127. return HelperEntry::daoToEntry ($list, $mode);
  128. }
  129. public function listFavorites ($mode) {
  130. $list = $this->array;
  131. if (!is_array ($list)) {
  132. $list = array ();
  133. }
  134. return HelperEntry::daoToEntry ($list, $mode, true);
  135. }
  136. public function listByCategory ($cat, $mode) {
  137. $feedDAO = new FeedDAO ();
  138. $feeds = $feedDAO->listByCategory ($cat);
  139. $list = array ();
  140. foreach ($feeds as $feed) {
  141. foreach ($feed->entries () as $id) {
  142. if (isset ($this->array[$id])) {
  143. $list[$id] = $this->array[$id];
  144. }
  145. }
  146. }
  147. return HelperEntry::daoToEntry ($list, $mode);
  148. }
  149. public function listNotReadEntries () {
  150. }
  151. public function count () {
  152. return count ($this->array);
  153. }
  154. }
  155. class HelperEntry {
  156. public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
  157. $list = array ();
  158. if (!is_array ($listDAO)) {
  159. $listDAO = array ($listDAO);
  160. }
  161. foreach ($listDAO as $key => $dao) {
  162. if (($mode != 'not_read' || !$dao['is_read'])
  163. && ($favorite == false || $dao['is_favorite'])) {
  164. $list[$key] = new Entry (
  165. $dao['feed'],
  166. $dao['guid'],
  167. $dao['title'],
  168. $dao['author'],
  169. $dao['content'],
  170. $dao['link'],
  171. $dao['date'],
  172. $dao['is_read'],
  173. $dao['is_favorite']
  174. );
  175. }
  176. }
  177. return $list;
  178. }
  179. }