Entry.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. public function __construct ($guid = '', $title = '', $author = '', $content = '',
  12. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false) {
  13. $this->_guid ($guid);
  14. $this->_title ($title);
  15. $this->_author ($author);
  16. $this->_content ($content);
  17. $this->_link ($link);
  18. $this->_date ($pubdate);
  19. $this->_isRead ($is_read);
  20. $this->_isFavorite ($is_favorite);
  21. }
  22. public function id () {
  23. return small_hash ($this->guid . Configuration::selApplication ());
  24. }
  25. public function guid () {
  26. return $this->guid;
  27. }
  28. public function title () {
  29. return $this->title;
  30. }
  31. public function author () {
  32. return $this->author;
  33. }
  34. public function content () {
  35. return $this->content;
  36. }
  37. public function link () {
  38. return $this->link;
  39. }
  40. public function date ($raw = false) {
  41. if ($raw) {
  42. return $this->date;
  43. } else {
  44. return timestamptodate ($this->date);
  45. }
  46. }
  47. public function isRead () {
  48. return $this->is_read;
  49. }
  50. public function isFavorite () {
  51. return $this->is_favorite;
  52. }
  53. public function _guid ($value) {
  54. $this->guid = $value;
  55. }
  56. public function _title ($value) {
  57. $this->title = $value;
  58. }
  59. public function _author ($value) {
  60. $this->author = $value;
  61. }
  62. public function _content ($value) {
  63. $this->content = $value;
  64. }
  65. public function _link ($value) {
  66. $this->link = $value;
  67. }
  68. public function _date ($value) {
  69. $this->date = $value;
  70. }
  71. public function _isRead ($value) {
  72. $this->is_read = $value;
  73. }
  74. public function _isFavorite ($value) {
  75. $this->is_favorite = $value;
  76. }
  77. }
  78. class EntryDAO extends Model_array {
  79. public function __construct () {
  80. parent::__construct (PUBLIC_PATH . '/data/db/Entries.array.php');
  81. }
  82. public function addEntry ($values) {
  83. $id = $values['id'];
  84. unset ($values['id']);
  85. if (!isset ($this->array[$id])) {
  86. $this->array[$id] = array ();
  87. foreach ($values as $key => $value) {
  88. $this->array[$id][$key] = $value;
  89. }
  90. $this->writeFile ($this->array);
  91. } else {
  92. return false;
  93. }
  94. }
  95. public function updateEntry ($id, $values) {
  96. foreach ($values as $key => $value) {
  97. $this->array[$id][$key] = $value;
  98. }
  99. $this->writeFile($this->array);
  100. }
  101. public function searchById ($id) {
  102. $list = HelperEntry::daoToEntry ($this->array);
  103. if (isset ($list[$id])) {
  104. return $list[$id];
  105. } else {
  106. return false;
  107. }
  108. }
  109. public function listEntries () {
  110. $list = $this->array;
  111. if (!is_array ($list)) {
  112. $list = array ();
  113. }
  114. return HelperEntry::daoToEntry ($list);
  115. }
  116. public function listNotReadEntries () {
  117. $list = $this->array;
  118. $list_not_read = array ();
  119. if (!is_array ($list)) {
  120. $list = array ();
  121. }
  122. foreach ($list as $key => $entry) {
  123. if (!$entry['is_read']) {
  124. $list_not_read[$key] = $entry;
  125. }
  126. }
  127. return HelperEntry::daoToEntry ($list_not_read);
  128. }
  129. public function count () {
  130. return count ($this->array);
  131. }
  132. }
  133. class HelperEntry {
  134. public static function daoToEntry ($listDAO) {
  135. $list = array ();
  136. if (!is_array ($listDAO)) {
  137. $listDAO = array ($listDAO);
  138. }
  139. foreach ($listDAO as $key => $dao) {
  140. $list[$key] = new Entry (
  141. $dao['guid'],
  142. $dao['title'],
  143. $dao['author'],
  144. $dao['content'],
  145. $dao['link'],
  146. $dao['date'],
  147. $dao['is_read'],
  148. $dao['is_favorite']
  149. );
  150. }
  151. return $list;
  152. }
  153. }