Feed.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 nbEntries () {
  38. $feedDAO = new FeedDAO ();
  39. return $feedDAO->countEntries ($this->id ());
  40. }
  41. public function _url ($value) {
  42. if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
  43. $this->url = $value;
  44. } else {
  45. throw new Exception ();
  46. }
  47. }
  48. public function _category ($value) {
  49. $this->category = $value;
  50. }
  51. public function _name ($value) {
  52. $this->name = $value;
  53. }
  54. public function _website ($value) {
  55. $this->website = $value;
  56. }
  57. public function _description ($value) {
  58. $this->description = $value;
  59. }
  60. public function load () {
  61. if (!is_null ($this->url)) {
  62. $feed = new SimplePie ();
  63. $feed->set_feed_url ($this->url);
  64. $feed->set_cache_location (CACHE_PATH);
  65. $feed->init ();
  66. $title = $feed->get_title ();
  67. $this->loadEntries ($feed);
  68. $this->_name (!is_null ($title) ? $title : $this->url);
  69. $this->_website ($feed->get_link ());
  70. $this->_description ($feed->get_description ());
  71. }
  72. }
  73. private function loadEntries ($feed) {
  74. $entries = array ();
  75. foreach ($feed->get_items () as $item) {
  76. $title = $item->get_title ();
  77. $author = $item->get_author ();
  78. $content = $item->get_content ();
  79. $link = $item->get_permalink ();
  80. $date = strtotime ($item->get_date ());
  81. $entry = new Entry (
  82. $this->id (),
  83. $item->get_id (),
  84. !is_null ($title) ? $title : '',
  85. !is_null ($author) ? $author->name : '',
  86. !is_null ($content) ? $content : '',
  87. !is_null ($link) ? $link : '',
  88. $date ? $date : time ()
  89. );
  90. $entries[$entry->id ()] = $entry;
  91. }
  92. $this->entries = $entries;
  93. }
  94. }
  95. class FeedDAO extends Model_pdo {
  96. public function addFeed ($valuesTmp) {
  97. $sql = 'INSERT INTO feed (id, url, category, name, website, description) VALUES(?, ?, ?, ?, ?, ?)';
  98. $stm = $this->bd->prepare ($sql);
  99. $values = array (
  100. $valuesTmp['id'],
  101. $valuesTmp['url'],
  102. $valuesTmp['category'],
  103. $valuesTmp['name'],
  104. $valuesTmp['website'],
  105. $valuesTmp['description'],
  106. );
  107. if ($stm && $stm->execute ($values)) {
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. public function updateFeed ($id, $valuesTmp) {
  114. $set = '';
  115. foreach ($valuesTmp as $key => $v) {
  116. $set .= $key . '=?, ';
  117. }
  118. $set = substr ($set, 0, -2);
  119. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  120. $stm = $this->bd->prepare ($sql);
  121. foreach ($valuesTmp as $v) {
  122. $values[] = $v;
  123. }
  124. $values[] = $id;
  125. if ($stm && $stm->execute ($values)) {
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. public function deleteFeed ($id) {
  132. $sql = 'DELETE FROM feed WHERE id=?';
  133. $stm = $this->bd->prepare ($sql);
  134. $values = array ($id);
  135. if ($stm && $stm->execute ($values)) {
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. }
  141. public function searchById ($id) {
  142. $sql = 'SELECT * FROM feed WHERE id=?';
  143. $stm = $this->bd->prepare ($sql);
  144. $values = array ($id);
  145. $stm->execute ($values);
  146. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  147. $feed = HelperFeed::daoToFeed ($res);
  148. if (isset ($feed[0])) {
  149. return $feed[0];
  150. } else {
  151. return false;
  152. }
  153. }
  154. public function listFeeds () {
  155. $sql = 'SELECT * FROM feed ORDER BY name';
  156. $stm = $this->bd->prepare ($sql);
  157. $stm->execute ();
  158. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  159. }
  160. public function listByCategory ($cat) {
  161. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  162. $stm = $this->bd->prepare ($sql);
  163. $values = array ($cat);
  164. $stm->execute ($values);
  165. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  166. }
  167. public function count () {
  168. $sql = 'SELECT COUNT(*) AS count FROM feed';
  169. $stm = $this->bd->prepare ($sql);
  170. $stm->execute ();
  171. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  172. return $res[0]['count'];
  173. }
  174. public function countEntries ($id) {
  175. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  176. $stm = $this->bd->prepare ($sql);
  177. $values = array ($id);
  178. $stm->execute ($values);
  179. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  180. return $res[0]['count'];
  181. }
  182. }
  183. class HelperFeed {
  184. public static function daoToFeed ($listDAO) {
  185. $list = array ();
  186. if (!is_array ($listDAO)) {
  187. $listDAO = array ($listDAO);
  188. }
  189. foreach ($listDAO as $key => $dao) {
  190. $list[$key] = new Feed ($dao['url']);
  191. $list[$key]->_category ($dao['category']);
  192. $list[$key]->_name ($dao['name']);
  193. $list[$key]->_website ($dao['website']);
  194. $list[$key]->_description ($dao['description']);
  195. }
  196. return $list;
  197. }
  198. }