Feed.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. class Feed extends Model {
  3. private $url;
  4. private $categories;
  5. private $entries_list;
  6. public function __construct ($url = null) {
  7. $this->_url ($url);
  8. $this->_categories (array ());
  9. $this->_entries (array ());
  10. }
  11. public function id () {
  12. return small_hash ($this->url . Configuration::selApplication ());
  13. }
  14. public function url () {
  15. return $this->url;
  16. }
  17. public function categories () {
  18. return $this->categories;
  19. }
  20. public function entries () {
  21. return $this->entries_list;
  22. }
  23. public function _url ($value) {
  24. if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
  25. $this->url = $value;
  26. } else {
  27. throw new Exception ();
  28. }
  29. }
  30. public function _categories ($value) {
  31. if (!is_array ($value)) {
  32. $value = array ($value);
  33. }
  34. $this->categories = $value;
  35. }
  36. public function _entries ($value) {
  37. if (!is_array ($value)) {
  38. $value = array ($value);
  39. }
  40. $this->entries_list = $value;
  41. }
  42. public function loadEntries () {
  43. if (!is_null ($this->url)) {
  44. $feed = new SimplePie ();
  45. $feed->set_feed_url ($this->url);
  46. $feed->set_cache_location (CACHE_PATH);
  47. $feed->init ();
  48. $entries = array ();
  49. if ($feed->data) {
  50. foreach ($feed->get_items () as $item) {
  51. $title = $item->get_title ();
  52. $author = $item->get_author ();
  53. $content = $item->get_content ();
  54. $link = $item->get_permalink ();
  55. $date = strtotime ($item->get_date ());
  56. $entry = new Entry (
  57. $item->get_id (),
  58. !is_null ($title) ? $title : '',
  59. !is_null ($author) ? $author->name : '',
  60. !is_null ($content) ? $content : '',
  61. !is_null ($link) ? $link : '',
  62. $date ? $date : time ()
  63. );
  64. $entries[$entry->id ()] = $entry;
  65. }
  66. return $entries;
  67. } else {
  68. return false;
  69. }
  70. } else {
  71. return false;
  72. }
  73. }
  74. }
  75. class FeedDAO extends Model_array {
  76. public function __construct () {
  77. parent::__construct (PUBLIC_PATH . '/data/db/Feeds.array.php');
  78. }
  79. public function addFeed ($values) {
  80. $id = $values['id'];
  81. unset ($values['id']);
  82. if (!isset ($this->array[$id])) {
  83. $this->array[$id] = array ();
  84. foreach ($values as $key => $value) {
  85. $this->array[$id][$key] = $value;
  86. }
  87. $this->writeFile ($this->array);
  88. } else {
  89. return false;
  90. }
  91. }
  92. public function updateFeed ($id, $values) {
  93. foreach ($values as $key => $value) {
  94. $this->array[$id][$key] = $value;
  95. }
  96. $this->writeFile($this->array);
  97. }
  98. public function listFeeds () {
  99. $list = $this->array;
  100. if (!is_array ($list)) {
  101. $list = array ();
  102. }
  103. return HelperFeed::daoToFeed ($list);
  104. }
  105. public function count () {
  106. return count ($this->array);
  107. }
  108. }
  109. class HelperFeed {
  110. public static function daoToFeed ($listDAO) {
  111. $list = array ();
  112. if (!is_array ($listDAO)) {
  113. $listDAO = array ($listDAO);
  114. }
  115. foreach ($listDAO as $key => $dao) {
  116. $list[$key] = new Feed ($dao['url']);
  117. $list[$key]->_categories ($dao['categories']);
  118. $list[$key]->_entries ($dao['entries']);
  119. }
  120. return $list;
  121. }
  122. }