feedController.php 5.6 KB

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