Feed.php 5.2 KB

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