feedController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 (Translate::t ('access_denied')))
  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' => Translate::t ('already_subscribed', $feed->name ())
  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' => Translate::t ('feed_added', $feed->name ())
  49. );
  50. Session::_param ('notification', $notif);
  51. $params['id'] = $feed->id ();
  52. } else {
  53. // notif
  54. $notif = array (
  55. 'type' => 'bad',
  56. 'content' => Translate::t ('feed_not_added', $feed->name ())
  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' => Translate::t ('internal_problem_feed')
  65. );
  66. Session::_param ('notification', $notif);
  67. } catch (Exception $e) {
  68. // notif
  69. $notif = array (
  70. 'type' => 'bad',
  71. 'content' => Translate::t ('invalid_url', $url)
  72. );
  73. Session::_param ('notification', $notif);
  74. }
  75. Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => $params), true);
  76. }
  77. }
  78. }
  79. public function actualizeAction () {
  80. $feedDAO = new FeedDAO ();
  81. $entryDAO = new EntryDAO ();
  82. $id = Request::param ('id');
  83. $feeds = array ();
  84. if ($id) {
  85. $feed = $feedDAO->searchById ($id);
  86. if ($feed) {
  87. $feeds = array ($feed);
  88. }
  89. } else {
  90. $feeds = $feedDAO->listFeedsOrderUpdate ();
  91. }
  92. // pour ne pas ajouter des entrées trop anciennes
  93. $nb_month_old = $this->view->conf->oldEntries ();
  94. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  95. $i = 0;
  96. foreach ($feeds as $feed) {
  97. try {
  98. $feed->load ();
  99. $entries = $feed->entries ();
  100. foreach ($entries as $entry) {
  101. if ($entry->date (true) >= $date_min) {
  102. $values = $entry->toArray ();
  103. $entryDAO->addEntry ($values);
  104. }
  105. }
  106. $feedDAO->updateLastUpdate ($feed->id ());
  107. } catch (FeedException $e) {
  108. Log::record ($e->getMessage (), Log::ERROR);
  109. }
  110. $i++;
  111. if ($i >= 10) {
  112. break;
  113. }
  114. }
  115. $entryDAO->cleanOldEntries ($nb_month_old);
  116. // notif
  117. $url = array ();
  118. if ($i == 1) {
  119. $feed = reset ($feeds);
  120. $notif = array (
  121. 'type' => 'good',
  122. 'content' => Translate::t ('feed_actualized', $feed->name ())
  123. );
  124. $url['params'] = array ('get' => 'f_' . $feed->id ());
  125. } elseif ($i > 0) {
  126. $notif = array (
  127. 'type' => 'good',
  128. 'content' => Translate::t ('n_feeds_actualized', $i)
  129. );
  130. } else {
  131. $notif = array (
  132. 'type' => 'bad',
  133. 'content' => Translate::t ('no_feed_actualized')
  134. );
  135. }
  136. if (Request::param ('ajax', 0) == 0) {
  137. Session::_param ('notification', $notif);
  138. Request::forward ($url, true);
  139. } else {
  140. $notif = array (
  141. 'type' => 'good',
  142. 'content' => Translate::t ('feeds_actualized')
  143. );
  144. Session::_param ('notification', $notif);
  145. $this->view->_useLayout (false);
  146. }
  147. }
  148. public function massiveImportAction () {
  149. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  150. Error::error (
  151. 403,
  152. array ('error' => array (Translate::t ('access_denied')))
  153. );
  154. } else {
  155. $entryDAO = new EntryDAO ();
  156. $feedDAO = new FeedDAO ();
  157. $categories = Request::param ('categories', array ());
  158. $feeds = Request::param ('feeds', array ());
  159. $this->addCategories ($categories);
  160. $nb_month_old = $this->view->conf->oldEntries ();
  161. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  162. $error = false;
  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. if (!$feedDAO->addFeed ($values)) {
  179. $error = true;
  180. }
  181. }
  182. } catch (FeedException $e) {
  183. $error = true;
  184. Log::record ($e->getMessage (), Log::ERROR);
  185. }
  186. }
  187. if ($error) {
  188. $res = Translate::t ('feeds_imported_with_errors');
  189. } else {
  190. $res = Translate::t ('feeds_imported');
  191. }
  192. $notif = array (
  193. 'type' => 'good',
  194. 'content' => $res
  195. );
  196. Session::_param ('notification', $notif);
  197. Request::forward (array (
  198. 'c' => 'configure',
  199. 'a' => 'importExport'
  200. ), true);
  201. }
  202. }
  203. public function deleteAction () {
  204. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  205. Error::error (
  206. 403,
  207. array ('error' => array (Translate::t ('access_denied')))
  208. );
  209. } else {
  210. $type = Request::param ('type', 'feed');
  211. $id = Request::param ('id');
  212. $feedDAO = new FeedDAO ();
  213. if ($type == 'category') {
  214. if ($feedDAO->deleteFeedByCategory ($id)) {
  215. $notif = array (
  216. 'type' => 'good',
  217. 'content' => Translate::t ('category_emptied')
  218. );
  219. } else {
  220. $notif = array (
  221. 'type' => 'bad',
  222. 'content' => Translate::t ('error_occured')
  223. );
  224. }
  225. } else {
  226. if ($feedDAO->deleteFeed ($id)) {
  227. $notif = array (
  228. 'type' => 'good',
  229. 'content' => Translate::t ('feed_deleted')
  230. );
  231. } else {
  232. $notif = array (
  233. 'type' => 'bad',
  234. 'content' => Translate::t ('error_occured')
  235. );
  236. }
  237. }
  238. Session::_param ('notification', $notif);
  239. if ($type == 'category') {
  240. Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
  241. } else {
  242. Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  243. }
  244. }
  245. }
  246. private function addCategories ($categories) {
  247. $catDAO = new CategoryDAO ();
  248. foreach ($categories as $cat) {
  249. if (!$catDAO->searchByName ($cat->name ())) {
  250. $values = array (
  251. 'id' => $cat->id (),
  252. 'name' => $cat->name (),
  253. 'color' => $cat->color ()
  254. );
  255. $catDAO->addCategory ($values);
  256. }
  257. }
  258. }
  259. }