Entry.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 () {
  123. $list = $this->array;
  124. if (!is_array ($list)) {
  125. $list = array ();
  126. }
  127. return HelperEntry::daoToEntry ($list);
  128. }
  129. public function listNotReadEntries () {
  130. $list = $this->array;
  131. $list_not_read = array ();
  132. if (!is_array ($list)) {
  133. $list = array ();
  134. }
  135. foreach ($list as $key => $entry) {
  136. if (!$entry['is_read']) {
  137. $list_not_read[$key] = $entry;
  138. }
  139. }
  140. return HelperEntry::daoToEntry ($list_not_read);
  141. }
  142. public function count () {
  143. return count ($this->array);
  144. }
  145. }
  146. class HelperEntry {
  147. public static function daoToEntry ($listDAO) {
  148. $list = array ();
  149. if (!is_array ($listDAO)) {
  150. $listDAO = array ($listDAO);
  151. }
  152. foreach ($listDAO as $key => $dao) {
  153. $list[$key] = new Entry (
  154. $dao['feed'],
  155. $dao['guid'],
  156. $dao['title'],
  157. $dao['author'],
  158. $dao['content'],
  159. $dao['link'],
  160. $dao['date'],
  161. $dao['is_read'],
  162. $dao['is_favorite']
  163. );
  164. }
  165. return $list;
  166. }
  167. }