feedController.php 6.3 KB

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