feedController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. class feedController extends ActionController {
  3. public function addAction () {
  4. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  5. Error::error (
  6. 403,
  7. array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
  8. );
  9. } else {
  10. if (Request::isPost ()) {
  11. $url = Request::param ('url_rss');
  12. try {
  13. $feed = new Feed ($url);
  14. $feed->load ();
  15. $feedDAO = new FeedDAO ();
  16. $values = array (
  17. 'id' => $feed->id (),
  18. 'url' => $feed->url (),
  19. 'category' => null,
  20. 'name' => $feed->name (),
  21. 'website' => $feed->website (),
  22. 'description' => $feed->description (),
  23. 'lastUpdate' => time ()
  24. );
  25. $feedDAO->addFeed ($values);
  26. $entryDAO = new EntryDAO ();
  27. $entries = $feed->entries ();
  28. foreach ($entries as $entry) {
  29. $values = array (
  30. 'id' => $entry->id (),
  31. 'guid' => $entry->guid (),
  32. 'title' => $entry->title (),
  33. 'author' => $entry->author (),
  34. 'content' => $entry->content (),
  35. 'link' => $entry->link (),
  36. 'date' => $entry->date (true),
  37. 'is_read' => $entry->isRead (),
  38. 'is_favorite' => $entry->isFavorite (),
  39. 'id_feed' => $feed->id ()
  40. );
  41. $entryDAO->addEntry ($values);
  42. }
  43. // notif
  44. $notif = array (
  45. 'type' => 'good',
  46. 'content' => 'Le flux <em>' . $feed->url () . '</em> a bien été ajouté'
  47. );
  48. Session::_param ('notification', $notif);
  49. } catch (Exception $e) {
  50. // notif
  51. $notif = array (
  52. 'type' => 'bad',
  53. 'content' => 'L\'url <em>' . $url . '</em> est invalide'
  54. );
  55. Session::_param ('notification', $notif);
  56. }
  57. Request::forward (array (), true);
  58. }
  59. }
  60. }
  61. public function actualizeAction () {
  62. $feedDAO = new FeedDAO ();
  63. $entryDAO = new EntryDAO ();
  64. $feeds = $feedDAO->listFeedsOrderUpdate ();
  65. // pour ne pas ajouter des entrées trop anciennes
  66. $nb_month_old = $this->view->conf->oldEntries ();
  67. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  68. $i = 0;
  69. foreach ($feeds as $feed) {
  70. $feed->load ();
  71. $entries = $feed->entries ();
  72. foreach ($entries as $entry) {
  73. if ($entry->date (true) >= $date_min) {
  74. $values = array (
  75. 'id' => $entry->id (),
  76. 'guid' => $entry->guid (),
  77. 'title' => $entry->title (),
  78. 'author' => $entry->author (),
  79. 'content' => $entry->content (),
  80. 'link' => $entry->link (),
  81. 'date' => $entry->date (true),
  82. 'is_read' => $entry->isRead (),
  83. 'is_favorite' => $entry->isFavorite (),
  84. 'id_feed' => $feed->id ()
  85. );
  86. $entryDAO->addEntry ($values);
  87. }
  88. }
  89. $feedDAO->updateLastUpdate ($feed->id ());
  90. $i++;
  91. if ($i >= 10) {
  92. break;
  93. }
  94. }
  95. $entryDAO->cleanOldEntries ($nb_month_old);
  96. // notif
  97. $notif = array (
  98. 'type' => 'good',
  99. 'content' => 'Les flux ont été mis à jour'
  100. );
  101. Session::_param ('notification', $notif);
  102. Request::forward (array (), true);
  103. }
  104. public function massiveImportAction () {
  105. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  106. Error::error (
  107. 403,
  108. array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
  109. );
  110. } else {
  111. $entryDAO = new EntryDAO ();
  112. $feedDAO = new FeedDAO ();
  113. $catDAO = new CategoryDAO ();
  114. $categories = Request::param ('categories', array ());
  115. $feeds = Request::param ('feeds', array ());
  116. foreach ($categories as $cat) {
  117. $values = array (
  118. 'id' => $cat->id (),
  119. 'name' => $cat->name (),
  120. 'color' => $cat->color ()
  121. );
  122. $catDAO->addCategory ($values);
  123. }
  124. $nb_month_old = $this->view->conf->oldEntries ();
  125. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  126. $i = 0;
  127. foreach ($feeds as $feed) {
  128. $feed->load ();
  129. // on ajoute les entrées que de 10 flux pour limiter un peu la charge
  130. // si on ajoute pas les entrées du flux, alors on met la date du dernier update à 0
  131. $update = 0;
  132. $i++;
  133. if ($i < 10) {
  134. $update = time ();
  135. $entries = $feed->entries ();
  136. foreach ($entries as $entry) {
  137. if ($entry->date (true) >= $date_min) {
  138. $values = array (
  139. 'id' => $entry->id (),
  140. 'guid' => $entry->guid (),
  141. 'title' => $entry->title (),
  142. 'author' => $entry->author (),
  143. 'content' => $entry->content (),
  144. 'link' => $entry->link (),
  145. 'date' => $entry->date (true),
  146. 'is_read' => $entry->isRead (),
  147. 'is_favorite' => $entry->isFavorite (),
  148. 'id_feed' => $feed->id ()
  149. );
  150. $entryDAO->addEntry ($values);
  151. }
  152. }
  153. }
  154. // Enregistrement du flux
  155. $values = array (
  156. 'id' => $feed->id (),
  157. 'url' => $feed->url (),
  158. 'category' => $feed->category (),
  159. 'name' => $feed->name (),
  160. 'website' => $feed->website (),
  161. 'description' => $feed->description (),
  162. 'lastUpdate' => $update
  163. );
  164. $feedDAO->addFeed ($values);
  165. }
  166. // notif
  167. $notif = array (
  168. 'type' => 'good',
  169. 'content' => 'Les flux ont été importés'
  170. );
  171. Session::_param ('notification', $notif);
  172. Request::forward (array ('c' => 'configure', 'a' => 'importExport'));
  173. }
  174. }
  175. public function deleteAction () {
  176. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  177. Error::error (
  178. 403,
  179. array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
  180. );
  181. } else {
  182. $id = Request::param ('id');
  183. $feedDAO = new FeedDAO ();
  184. $feedDAO->deleteFeed ($id);
  185. // notif
  186. $notif = array (
  187. 'type' => 'good',
  188. 'content' => 'Le flux a été supprimé'
  189. );
  190. Session::_param ('notification', $notif);
  191. Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  192. }
  193. }
  194. }