| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- class feedController extends ActionController {
- public function firstAction () {
- $catDAO = new CategoryDAO ();
- $catDAO->checkDefault ();
- }
- public function addAction () {
- if (login_is_conf ($this->view->conf) && !is_logged ()) {
- Error::error (
- 403,
- array ('error' => array (Translate::t ('access_denied')))
- );
- } else {
- if (Request::isPost ()) {
- $url = Request::param ('url_rss');
- $cat = Request::param ('category');
- $params = array ();
- try {
- $feed = new Feed ($url);
- $feed->_category ($cat);
- $feed->load ();
- $feedDAO = new FeedDAO ();
- $values = array (
- 'id' => $feed->id (),
- 'url' => $feed->url (),
- 'category' => $feed->category (),
- 'name' => $feed->name (),
- 'website' => $feed->website (),
- 'description' => $feed->description (),
- 'lastUpdate' => time ()
- );
- if ($feedDAO->searchByUrl ($values['url'])) {
- $notif = array (
- 'type' => 'bad',
- 'content' => Translate::t ('already_subscribed', $feed->name ())
- );
- Session::_param ('notification', $notif);
- } elseif ($feedDAO->addFeed ($values)) {
- $entryDAO = new EntryDAO ();
- $entries = $feed->entries ();
- foreach ($entries as $entry) {
- $values = $entry->toArray ();
- $entryDAO->addEntry ($values);
- }
- // notif
- $notif = array (
- 'type' => 'good',
- 'content' => Translate::t ('feed_added', $feed->name ())
- );
- Session::_param ('notification', $notif);
- $params['id'] = $feed->id ();
- } else {
- // notif
- $notif = array (
- 'type' => 'bad',
- 'content' => Translate::t ('feed_not_added', $feed->name ())
- );
- Session::_param ('notification', $notif);
- }
- } catch (FeedException $e) {
- Log::record ($e->getMessage (), Log::ERROR);
- $notif = array (
- 'type' => 'bad',
- 'content' => Translate::t ('internal_problem_feed')
- );
- Session::_param ('notification', $notif);
- } catch (Exception $e) {
- // notif
- $notif = array (
- 'type' => 'bad',
- 'content' => Translate::t ('invalid_url', $url)
- );
- Session::_param ('notification', $notif);
- }
- Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => $params), true);
- }
- }
- }
- public function actualizeAction () {
- $feedDAO = new FeedDAO ();
- $entryDAO = new EntryDAO ();
- $id = Request::param ('id');
- $feeds = array ();
- if ($id) {
- $feed = $feedDAO->searchById ($id);
- if ($feed) {
- $feeds = array ($feed);
- }
- } else {
- $feeds = $feedDAO->listFeedsOrderUpdate ();
- }
- // pour ne pas ajouter des entrées trop anciennes
- $nb_month_old = $this->view->conf->oldEntries ();
- $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
- $i = 0;
- foreach ($feeds as $feed) {
- try {
- $feed->load ();
- $entries = $feed->entries ();
- foreach ($entries as $entry) {
- if ($entry->date (true) >= $date_min) {
- $values = $entry->toArray ();
- $entryDAO->addEntry ($values);
- }
- }
- $feedDAO->updateLastUpdate ($feed->id ());
- } catch (FeedException $e) {
- Log::record ($e->getMessage (), Log::ERROR);
- }
- $i++;
- if ($i >= 10) {
- break;
- }
- }
- $entryDAO->cleanOldEntries ($nb_month_old);
- // notif
- $url = array ();
- if ($i == 1) {
- $feed = reset ($feeds);
- $notif = array (
- 'type' => 'good',
- 'content' => Translate::t ('feed_actualized', $feed->name ())
- );
- $url['params'] = array ('get' => 'f_' . $feed->id ());
- } elseif ($i > 0) {
- $notif = array (
- 'type' => 'good',
- 'content' => Translate::t ('n_feeds_actualized', $i)
- );
- } else {
- $notif = array (
- 'type' => 'bad',
- 'content' => Translate::t ('no_feed_actualized')
- );
- }
- if (Request::param ('ajax', 0) == 0) {
- Session::_param ('notification', $notif);
- Request::forward ($url, true);
- } else {
- $notif = array (
- 'type' => 'good',
- 'content' => Translate::t ('feeds_actualized')
- );
- Session::_param ('notification', $notif);
- $this->view->_useLayout (false);
- }
- }
- public function massiveImportAction () {
- if (login_is_conf ($this->view->conf) && !is_logged ()) {
- Error::error (
- 403,
- array ('error' => array (Translate::t ('access_denied')))
- );
- } else {
- $entryDAO = new EntryDAO ();
- $feedDAO = new FeedDAO ();
- $categories = Request::param ('categories', array ());
- $feeds = Request::param ('feeds', array ());
- $this->addCategories ($categories);
- $nb_month_old = $this->view->conf->oldEntries ();
- $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
- $error = false;
- $i = 0;
- foreach ($feeds as $feed) {
- try {
- $feed->load ();
- // Enregistrement du flux
- $values = array (
- 'id' => $feed->id (),
- 'url' => $feed->url (),
- 'category' => $feed->category (),
- 'name' => $feed->name (),
- 'website' => $feed->website (),
- 'description' => $feed->description (),
- 'lastUpdate' => 0
- );
- if (!$feedDAO->searchByUrl ($values['url'])) {
- if (!$feedDAO->addFeed ($values)) {
- $error = true;
- }
- }
- } catch (FeedException $e) {
- $error = true;
- Log::record ($e->getMessage (), Log::ERROR);
- }
- }
- if ($error) {
- $res = Translate::t ('feeds_imported_with_errors');
- } else {
- $res = Translate::t ('feeds_imported');
- }
- $notif = array (
- 'type' => 'good',
- 'content' => $res
- );
- Session::_param ('notification', $notif);
- Request::forward (array (
- 'c' => 'configure',
- 'a' => 'importExport'
- ), true);
- }
- }
- public function deleteAction () {
- if (login_is_conf ($this->view->conf) && !is_logged ()) {
- Error::error (
- 403,
- array ('error' => array (Translate::t ('access_denied')))
- );
- } else {
- $type = Request::param ('type', 'feed');
- $id = Request::param ('id');
- $feedDAO = new FeedDAO ();
- if ($type == 'category') {
- if ($feedDAO->deleteFeedByCategory ($id)) {
- $notif = array (
- 'type' => 'good',
- 'content' => Translate::t ('category_emptied')
- );
- } else {
- $notif = array (
- 'type' => 'bad',
- 'content' => Translate::t ('error_occured')
- );
- }
- } else {
- if ($feedDAO->deleteFeed ($id)) {
- $notif = array (
- 'type' => 'good',
- 'content' => Translate::t ('feed_deleted')
- );
- } else {
- $notif = array (
- 'type' => 'bad',
- 'content' => Translate::t ('error_occured')
- );
- }
- }
- Session::_param ('notification', $notif);
- if ($type == 'category') {
- Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
- } else {
- Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
- }
- }
- }
- private function addCategories ($categories) {
- $catDAO = new CategoryDAO ();
- foreach ($categories as $cat) {
- if (!$catDAO->searchByName ($cat->name ())) {
- $values = array (
- 'id' => $cat->id (),
- 'name' => $cat->name (),
- 'color' => $cat->color ()
- );
- $catDAO->addCategory ($values);
- }
- }
- }
- }
|