feedController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. );
  24. $feedDAO->addFeed ($values);
  25. $entryDAO = new EntryDAO ();
  26. $entries = $feed->entries ();
  27. foreach ($entries as $entry) {
  28. $values = array (
  29. 'id' => $entry->id (),
  30. 'guid' => $entry->guid (),
  31. 'title' => $entry->title (),
  32. 'author' => $entry->author (),
  33. 'content' => $entry->content (),
  34. 'link' => $entry->link (),
  35. 'date' => $entry->date (true),
  36. 'is_read' => $entry->isRead (),
  37. 'is_favorite' => $entry->isFavorite (),
  38. 'id_feed' => $feed->id ()
  39. );
  40. $entryDAO->addEntry ($values);
  41. }
  42. // notif
  43. $notif = array (
  44. 'type' => 'good',
  45. 'content' => 'Le flux <em>' . $feed->url () . '</em> a bien été ajouté'
  46. );
  47. Session::_param ('notification', $notif);
  48. } catch (Exception $e) {
  49. // notif
  50. $notif = array (
  51. 'type' => 'bad',
  52. 'content' => 'L\'url <em>' . $url . '</em> est invalide'
  53. );
  54. Session::_param ('notification', $notif);
  55. }
  56. Request::forward (array (), true);
  57. }
  58. }
  59. }
  60. public function actualizeAction () {
  61. $feedDAO = new FeedDAO ();
  62. $entryDAO = new EntryDAO ();
  63. $feeds = $feedDAO->listFeeds ();
  64. foreach ($feeds as $feed) {
  65. $feed->load ();
  66. $entries = $feed->entries ();
  67. foreach ($entries as $entry) {
  68. $values = array (
  69. 'id' => $entry->id (),
  70. 'guid' => $entry->guid (),
  71. 'title' => $entry->title (),
  72. 'author' => $entry->author (),
  73. 'content' => $entry->content (),
  74. 'link' => $entry->link (),
  75. 'date' => $entry->date (true),
  76. 'is_read' => $entry->isRead (),
  77. 'is_favorite' => $entry->isFavorite (),
  78. 'id_feed' => $feed->id ()
  79. );
  80. $entryDAO->addEntry ($values);
  81. }
  82. }
  83. $entryDAO->cleanOldEntries ($this->view->conf->oldEntries ());
  84. // notif
  85. $notif = array (
  86. 'type' => 'good',
  87. 'content' => 'Les flux ont été mis à jour'
  88. );
  89. Session::_param ('notification', $notif);
  90. Request::forward (array (), true);
  91. }
  92. public function massiveImportAction () {
  93. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  94. Error::error (
  95. 403,
  96. array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
  97. );
  98. } else {
  99. $entryDAO = new EntryDAO ();
  100. $feedDAO = new FeedDAO ();
  101. $catDAO = new CategoryDAO ();
  102. $categories = Request::param ('categories', array ());
  103. $feeds = Request::param ('feeds', array ());
  104. foreach ($categories as $cat) {
  105. $values = array (
  106. 'id' => $cat->id (),
  107. 'name' => $cat->name (),
  108. 'color' => $cat->color ()
  109. );
  110. $catDAO->addCategory ($values);
  111. }
  112. foreach ($feeds as $feed) {
  113. $feed->load ();
  114. $entries = $feed->entries ();
  115. // Chargement du flux
  116. foreach ($entries as $entry) {
  117. $values = array (
  118. 'id' => $entry->id (),
  119. 'guid' => $entry->guid (),
  120. 'title' => $entry->title (),
  121. 'author' => $entry->author (),
  122. 'content' => $entry->content (),
  123. 'link' => $entry->link (),
  124. 'date' => $entry->date (true),
  125. 'is_read' => $entry->isRead (),
  126. 'is_favorite' => $entry->isFavorite (),
  127. 'id_feed' => $feed->id ()
  128. );
  129. $entryDAO->addEntry ($values);
  130. }
  131. // Enregistrement du flux
  132. $values = array (
  133. 'id' => $feed->id (),
  134. 'url' => $feed->url (),
  135. 'category' => $feed->category (),
  136. 'name' => $feed->name (),
  137. 'website' => $feed->website (),
  138. 'description' => $feed->description (),
  139. );
  140. $feedDAO->addFeed ($values);
  141. }
  142. // notif
  143. $notif = array (
  144. 'type' => 'good',
  145. 'content' => 'Les flux ont été importés'
  146. );
  147. Session::_param ('notification', $notif);
  148. Request::forward (array ('c' => 'configure', 'a' => 'importExport'));
  149. }
  150. }
  151. public function deleteAction () {
  152. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  153. Error::error (
  154. 403,
  155. array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
  156. );
  157. } else {
  158. $id = Request::param ('id');
  159. $feedDAO = new FeedDAO ();
  160. $feedDAO->deleteFeed ($id);
  161. // notif
  162. $notif = array (
  163. 'type' => 'good',
  164. 'content' => 'Le flux a été supprimé'
  165. );
  166. Session::_param ('notification', $notif);
  167. Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  168. }
  169. }
  170. }