Feed.php 4.5 KB

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