Feed.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. class Feed extends Model {
  3. private $url;
  4. private $category = '';
  5. private $entries_list = array ();
  6. private $entries = null;
  7. private $name = '';
  8. private $website = '';
  9. private $description = '';
  10. public function __construct ($url) {
  11. $this->_url ($url);
  12. }
  13. public function id () {
  14. return small_hash ($this->url . Configuration::selApplication ());
  15. }
  16. public function url () {
  17. return $this->url;
  18. }
  19. public function category () {
  20. return $this->category;
  21. }
  22. public function entries ($list = true) {
  23. if ($list) {
  24. return $this->entries_list;
  25. } elseif (!is_null ($this->entries)) {
  26. return $this->entries;
  27. } else {
  28. return false;
  29. }
  30. }
  31. public function name () {
  32. return $this->name;
  33. }
  34. public function website () {
  35. return $this->website;
  36. }
  37. public function description () {
  38. return $this->description;
  39. }
  40. public function _url ($value) {
  41. if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
  42. $this->url = $value;
  43. } else {
  44. throw new Exception ();
  45. }
  46. }
  47. public function _category ($value) {
  48. $this->category = $value;
  49. }
  50. public function _entries ($value) {
  51. if (!is_array ($value)) {
  52. $value = array ($value);
  53. }
  54. $this->entries_list = $value;
  55. }
  56. public function _name ($value) {
  57. $this->name = $value;
  58. }
  59. public function _website ($value) {
  60. $this->website = $value;
  61. }
  62. public function _description ($value) {
  63. $this->description = $value;
  64. }
  65. public function load () {
  66. if (!is_null ($this->url)) {
  67. $feed = new SimplePie ();
  68. $feed->set_feed_url ($this->url);
  69. $feed->set_cache_location (CACHE_PATH);
  70. $feed->init ();
  71. $title = $feed->get_title ();
  72. $this->loadEntries ($feed);
  73. $this->_name (!is_null ($title) ? $title : $this->url);
  74. $this->_website ($feed->get_link ());
  75. $this->_description ($feed->get_description ());
  76. }
  77. }
  78. private function loadEntries ($feed) {
  79. $entries = array ();
  80. foreach ($feed->get_items () as $item) {
  81. $title = $item->get_title ();
  82. $author = $item->get_author ();
  83. $content = $item->get_content ();
  84. $link = $item->get_permalink ();
  85. $date = strtotime ($item->get_date ());
  86. $entry = new Entry (
  87. $this->id (),
  88. $item->get_id (),
  89. !is_null ($title) ? $title : '',
  90. !is_null ($author) ? $author->name : '',
  91. !is_null ($content) ? $content : '',
  92. !is_null ($link) ? $link : '',
  93. $date ? $date : time ()
  94. );
  95. $entries[$entry->id ()] = $entry;
  96. }
  97. $this->entries = $entries;
  98. }
  99. }
  100. class FeedDAO extends Model_array {
  101. public function __construct () {
  102. parent::__construct (PUBLIC_PATH . '/data/db/Feeds.array.php');
  103. }
  104. public function addFeed ($values) {
  105. $id = $values['id'];
  106. unset ($values['id']);
  107. if (!isset ($this->array[$id])) {
  108. $this->array[$id] = array ();
  109. foreach ($values as $key => $value) {
  110. $this->array[$id][$key] = $value;
  111. }
  112. $this->writeFile ($this->array);
  113. } else {
  114. return false;
  115. }
  116. }
  117. public function updateFeed ($id, $values) {
  118. foreach ($values as $key => $value) {
  119. $this->array[$id][$key] = $value;
  120. }
  121. $this->writeFile($this->array);
  122. }
  123. public function searchById ($id) {
  124. $list = HelperFeed::daoToFeed ($this->array);
  125. if (isset ($list[$id])) {
  126. return $list[$id];
  127. } else {
  128. return false;
  129. }
  130. }
  131. public function listFeeds () {
  132. $list = $this->array;
  133. if (!is_array ($list)) {
  134. $list = array ();
  135. }
  136. return HelperFeed::daoToFeed ($list);
  137. }
  138. public function listByCategory ($cat) {
  139. $list = array ();
  140. foreach ($this->array as $key => $feed) {
  141. if ($feed['category'] == $cat) {
  142. $list[$key] = $feed;
  143. }
  144. }
  145. return HelperFeed::daoToFeed ($list);
  146. }
  147. public function count () {
  148. return count ($this->array);
  149. }
  150. }
  151. class HelperFeed {
  152. public static function daoToFeed ($listDAO) {
  153. $list = array ();
  154. if (!is_array ($listDAO)) {
  155. $listDAO = array ($listDAO);
  156. }
  157. foreach ($listDAO as $key => $dao) {
  158. $list[$key] = new Feed ($dao['url']);
  159. $list[$key]->_category ($dao['category']);
  160. $list[$key]->_entries ($dao['entries']);
  161. $list[$key]->_name ($dao['name']);
  162. $list[$key]->_website ($dao['website']);
  163. $list[$key]->_description ($dao['description']);
  164. }
  165. return $list;
  166. }
  167. }