feedController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. $url = array ();
  126. if ($i == 1) {
  127. $feed = reset ($feeds);
  128. $notif = array (
  129. 'type' => 'good',
  130. 'content' => '<em>' . $feed->name () . '</em> a été mis à jour'
  131. );
  132. $url['params'] = array ('get' => 'f_' . $feed->id ());
  133. } elseif ($i > 0) {
  134. $notif = array (
  135. 'type' => 'good',
  136. 'content' => $i . ' flux ont été mis à jour'
  137. );
  138. } else {
  139. $notif = array (
  140. 'type' => 'bad',
  141. 'content' => 'Aucun flux n\'a pu être mis à jour'
  142. );
  143. }
  144. if (Request::param ('ajax', 0) == 0) {
  145. Session::_param ('notification', $notif);
  146. Request::forward ($url, true);
  147. } else {
  148. $notif = array (
  149. 'type' => 'good',
  150. 'content' => 'Les flux ont été mis à jour'
  151. );
  152. Session::_param ('notification', $notif);
  153. $this->view->_useLayout (false);
  154. }
  155. }
  156. public function massiveImportAction () {
  157. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  158. Error::error (
  159. 403,
  160. array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
  161. );
  162. } else {
  163. $entryDAO = new EntryDAO ();
  164. $feedDAO = new FeedDAO ();
  165. $categories = Request::param ('categories', array ());
  166. $feeds = Request::param ('feeds', array ());
  167. $this->addCategories ($categories);
  168. $nb_month_old = $this->view->conf->oldEntries ();
  169. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  170. $error = false;
  171. $i = 0;
  172. foreach ($feeds as $feed) {
  173. try {
  174. $feed->load ();
  175. // Enregistrement du flux
  176. $values = array (
  177. 'id' => $feed->id (),
  178. 'url' => $feed->url (),
  179. 'category' => $feed->category (),
  180. 'name' => $feed->name (),
  181. 'website' => $feed->website (),
  182. 'description' => $feed->description (),
  183. 'lastUpdate' => 0
  184. );
  185. if (!$feedDAO->searchByUrl ($values['url'])) {
  186. if (!$feedDAO->addFeed ($values)) {
  187. $error = true;
  188. }
  189. }
  190. } catch (FeedException $e) {
  191. $error = true;
  192. Log::record ($e->getMessage (), Log::ERROR);
  193. }
  194. }
  195. if ($error) {
  196. $res = 'Les flux ont été importés mais des erreurs sont survenus';
  197. } else {
  198. $res = 'Les flux ont été importés';
  199. }
  200. $notif = array (
  201. 'type' => 'good',
  202. 'content' => $res
  203. );
  204. Session::_param ('notification', $notif);
  205. Request::forward (array (
  206. 'c' => 'configure',
  207. 'a' => 'importExport'
  208. ), true);
  209. }
  210. }
  211. public function deleteAction () {
  212. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  213. Error::error (
  214. 403,
  215. array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
  216. );
  217. } else {
  218. $type = Request::param ('type', 'feed');
  219. $id = Request::param ('id');
  220. $feedDAO = new FeedDAO ();
  221. if ($type == 'category') {
  222. if ($feedDAO->deleteFeedByCategory ($id)) {
  223. $notif = array (
  224. 'type' => 'good',
  225. 'content' => 'La catégorie a été vidée'
  226. );
  227. } else {
  228. $notif = array (
  229. 'type' => 'bad',
  230. 'content' => 'Un problème est survenu'
  231. );
  232. }
  233. } else {
  234. if ($feedDAO->deleteFeed ($id)) {
  235. $notif = array (
  236. 'type' => 'good',
  237. 'content' => 'Le flux a été supprimé'
  238. );
  239. } else {
  240. $notif = array (
  241. 'type' => 'bad',
  242. 'content' => 'Un problème est survenu'
  243. );
  244. }
  245. }
  246. Session::_param ('notification', $notif);
  247. if ($type == 'category') {
  248. Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
  249. } else {
  250. Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  251. }
  252. }
  253. }
  254. private function addCategories ($categories) {
  255. $catDAO = new CategoryDAO ();
  256. foreach ($categories as $cat) {
  257. if (!$catDAO->searchByName ($cat->name ())) {
  258. $values = array (
  259. 'id' => $cat->id (),
  260. 'name' => $cat->name (),
  261. 'color' => $cat->color ()
  262. );
  263. $catDAO->addCategory ($values);
  264. }
  265. }
  266. }
  267. }