From 80cffa6de51771cd80995fb1c4f1e04ee868eb45 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Tue, 21 Oct 2014 16:46:36 +0200 Subject: Views are in dedicated actions + improve Context - Seperate normal, global and rss outputs in dedicated actions (NOT WORKING YET!) - Rewrite aside_flux and nav_menu to use Context object - Improve Context object See https://github.com/marienfressinaud/FreshRSS/issues/634 --- app/Models/Context.php | 65 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Models/Context.php b/app/Models/Context.php index d984fece7..b85179652 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -6,7 +6,22 @@ */ class FreshRSS_Context { public static $conf = null; + + public static $total_unread = 0; + public static $total_starred = array( + 'all' => 0, + 'read' => 0, + 'unread' => 0, + ); + public static $state = 0; + public static $current_get = array( + 'all' => false, + 'starred' => false, + 'feed' => false, + 'category' => false, + ); + public static $order = 'DESC'; public static function init() { // Init configuration. @@ -23,10 +38,56 @@ class FreshRSS_Context { Minz_Translate::init(); // Get the current state. - self::$state = self::$conf->default_view; + // self::$state = self::$conf->default_view; } - public static function stateEnabled($state) { + public static function isStateEnabled($state) { return self::$state & $state; } + + public static function getRevertState($state) { + if (self::$state & $state) { + return self::$state & ~$state; + } else { + return self::$state | $state; + } + } + + public static function currentGet() { + if (self::$current_get['all']) { + return 'a'; + } elseif (self::$current_get['starred']) { + return 's'; + } elseif (self::$current_get['feed']) { + return 'f_' . self::$current_get['feed']; + } elseif (self::$current_get['category']) { + return 'c_' . self::$current_get['category']; + } + } + + public static function isCurrentGet($get) { + $type = $get[0]; + $id = substr($get, 2); + + switch($type) { + case 'a': + return self::$current_get['all']; + case 's': + return self::$current_get['starred']; + case 'f': + return self::$current_get['feed'] === $id; + case 'c': + return self::$current_get['category'] === $id; + default: + return false; + } + } + + public static function nextStep() { + // TODO: fix this method. + return array( + 'get' => 'a', + 'idMax' => (time() - 1) . '000000' + ); + } } -- cgit v1.2.3 From fcae4157539306e90299e7f0e90740320a2833d7 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 22 Oct 2014 13:13:24 +0200 Subject: Update title and get parameter correctly. See https://github.com/marienfressinaud/FreshRSS/issues/634 --- app/Controllers/indexController.php | 113 ++++++++++-------------------------- app/Models/Context.php | 68 ++++++++++++++++++++++ 2 files changed, 100 insertions(+), 81 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index d348ea1d0..f163da53d 100755 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -4,7 +4,6 @@ * This class handles main actions of FreshRSS. */ class FreshRSS_index_Controller extends Minz_ActionController { - private $nb_not_read_cat = 0; public function indexAction() { // TODO: update the context with information from request. @@ -18,30 +17,6 @@ class FreshRSS_index_Controller extends Minz_ActionController { return; - $this->view->nb_favorites = $entryDAO->countUnreadReadFavorites(); - $this->view->nb_not_read = FreshRSS_CategoryDAO::CountUnreads($this->view->cat_aside, 1); - $this->view->currentName = ''; - - $this->view->get_c = ''; - $this->view->get_f = ''; - - $get = Minz_Request::param('get', 'a'); - $getType = $get[0]; - $getId = substr($get, 2); - if (!$this->checkAndProcessType($getType, $getId)) { - Minz_Log::debug('Not found [' . $getType . '][' . $getId . ']'); - Minz_Error::error(404); - return; - } - - // mise à jour des titres - $this->view->rss_title = $this->view->currentName . ' | ' . Minz_View::title(); - Minz_View::prependTitle( - ($this->nb_not_read_cat > 0 ? '(' . formatNumber($this->nb_not_read_cat) . ') ' : '') . - $this->view->currentName . - ' · ' - ); - // On récupère les différents éléments de filtrage $this->view->state = Minz_Request::param('state', FreshRSS_Context::$conf->default_view); $state_param = Minz_Request::param('state', null); @@ -115,57 +90,6 @@ class FreshRSS_index_Controller extends Minz_ActionController { } } - /* - * Vérifie que la catégorie / flux sélectionné existe - * + Initialise correctement les variables de vue get_c et get_f - * + Met à jour la variable $this->nb_not_read_cat - */ - private function checkAndProcessType($getType, $getId) { - switch($getType) { - case 'a': - $this->view->currentName = _t('your_rss_feeds'); - $this->nb_not_read_cat = $this->view->nb_not_read; - $this->view->get_c = $getType; - return true; - case 's': - $this->view->currentName = _t('your_favorites'); - $this->nb_not_read_cat = $this->view->nb_favorites['unread']; - $this->view->get_c = $getType; - return true; - case 'c': - $cat = isset($this->view->cat_aside[$getId]) ? $this->view->cat_aside[$getId] : null; - if ($cat === null) { - $catDAO = new FreshRSS_CategoryDAO(); - $cat = $catDAO->searchById($getId); - } - if ($cat) { - $this->view->currentName = $cat->name(); - $this->nb_not_read_cat = $cat->nbNotRead(); - $this->view->get_c = $getId; - return true; - } else { - return false; - } - case 'f': - $feed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId); - if (empty($feed)) { - $feedDAO = FreshRSS_Factory::createFeedDao(); - $feed = $feedDAO->searchById($getId); - } - if ($feed) { - $this->view->currentName = $feed->name(); - $this->nb_not_read_cat = $feed->nbNotRead(); - $this->view->get_f = $getId; - $this->view->get_c = $feed->category(); - return true; - } else { - return false; - } - default: - return false; - } - } - /** * This action displays the normal view of FreshRSS. */ @@ -175,11 +99,19 @@ class FreshRSS_index_Controller extends Minz_ActionController { return; } - $catDAO = new FreshRSS_CategoryDAO(); - $entryDAO = FreshRSS_Factory::createEntryDao(); + try { + $this->updateContext(); + } catch (Minz_Exception $e) { + Minz_Error::error(404); + } - $this->view->categories = $catDAO->listCategories(); + $this->view->categories = FreshRSS_Context::$categories; + $title = FreshRSS_Context::$name; + if (FreshRSS_Context::$get_unread > 0) { + $title = '(' . FreshRSS_Context::$get_unread . ') · ' . $title; + } + Minz_View::prependTitle($title . ' · '); } /** @@ -193,8 +125,13 @@ class FreshRSS_index_Controller extends Minz_ActionController { Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js'))); - $catDAO = new FreshRSS_CategoryDAO(); - $this->view->categories = $catDAO->listCategories(); + try { + $this->updateContext(); + } catch (Minz_Exception $e) { + Minz_Error::error(404); + } + + $this->view->categories = FreshRSS_Context::$categories; Minz_View::prependTitle(_t('gen.title.global_view') . ' · '); } @@ -214,11 +151,25 @@ class FreshRSS_index_Controller extends Minz_ActionController { Minz_Error::error(403); } + try { + $this->updateContext(); + } catch (Minz_Exception $e) { + Minz_Error::error(404); + } + // No layout for RSS output. + $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title(); $this->view->_useLayout(false); header('Content-Type: application/rss+xml; charset=utf-8'); } + /** + * This action updates the Context object by using request parameters. + */ + private function updateContext() { + FreshRSS_Context::_get(Minz_Request::param('get', 'a')); + } + /** * This action displays the about page of FreshRSS. */ diff --git a/app/Models/Context.php b/app/Models/Context.php index b85179652..7138fd638 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -6,6 +6,9 @@ */ class FreshRSS_Context { public static $conf = null; + public static $categories = array(); + + public static $name = ''; public static $total_unread = 0; public static $total_starred = array( @@ -21,6 +24,7 @@ class FreshRSS_Context { 'feed' => false, 'category' => false, ); + public static $get_unread = 0; public static $order = 'DESC'; public static function init() { @@ -37,8 +41,16 @@ class FreshRSS_Context { Minz_Session::_param('language', self::$conf->language); Minz_Translate::init(); + $catDAO = new FreshRSS_CategoryDAO(); + $entryDAO = FreshRSS_Factory::createEntryDao(); + // Get the current state. // self::$state = self::$conf->default_view; + self::$categories = $catDAO->listCategories(); + + // Update number of read / unread variables. + self::$total_starred = $entryDAO->countUnreadReadFavorites(); + self::$total_unread = FreshRSS_CategoryDAO::CountUnreads(self::$categories, 1); } public static function isStateEnabled($state) { @@ -53,6 +65,62 @@ class FreshRSS_Context { } } + public static function _get($get) { + $type = $get[0]; + $id = substr($get, 2); + $nb_unread = 0; + + switch($type) { + case 'a': + self::$current_get['all'] = true; + self::$name = _t('your_rss_feeds'); + self::$get_unread = self::$total_unread; + break; + case 's': + self::$current_get['starred'] = true; + self::$name = _t('your_favorites'); + self::$get_unread = self::$total_starred['unread']; + break; + case 'f': + self::$current_get['feed'] = $id; + + $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id); + if ($feed === null) { + $feedDAO = FreshRSS_Factory::createFeedDao(); + $feed = $feedDAO->searchById($id); + + if (!$feed) { + // TODO: raise an exception + return false; + } + } + + self::$name = $feed->name(); + self::$get_unread = $feed->nbNotRead(); + break; + case 'c': + self::$current_get['category'] = $id; + if (!isset(self::$categories[$id])) { + $catDAO = new FreshRSS_CategoryDAO(); + $cat = $catDAO->searchById($id); + + if (!$cat) { + // TODO: raise an exception + return false; + } + } else { + $cat = self::$categories[$id]; + } + + self::$name = $cat->name(); + self::$get_unread = $cat->nbNotRead(); + break; + default: + // TODO: raise an exception! + return false; + } + } + public static function currentGet() { if (self::$current_get['all']) { return 'a'; -- cgit v1.2.3 From b446a510ebacddd1437d907e795c83b3d05a9b98 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 22 Oct 2014 13:35:30 +0200 Subject: Finish to update context object See See https://github.com/marienfressinaud/FreshRSS/issues/634 --- app/Controllers/indexController.php | 82 ++++++++++--------------------------- app/Models/Context.php | 6 +++ app/views/index/normal.phtml | 8 ++-- 3 files changed, 32 insertions(+), 64 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index f163da53d..be85aa68d 100755 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -17,70 +17,13 @@ class FreshRSS_index_Controller extends Minz_ActionController { return; - // On récupère les différents éléments de filtrage - $this->view->state = Minz_Request::param('state', FreshRSS_Context::$conf->default_view); - $state_param = Minz_Request::param('state', null); - $filter = Minz_Request::param('search', ''); - $this->view->order = $order = Minz_Request::param('order', FreshRSS_Context::$conf->sort_order); - $nb = Minz_Request::param('nb', FreshRSS_Context::$conf->posts_per_page); - $first = Minz_Request::param('next', ''); - - $ajax_request = Minz_Request::param('ajax', false); - if ($output === 'reader') { - $nb = max(1, round($nb / 2)); - } - - if ($this->view->state === FreshRSS_Entry::STATE_NOT_READ) { //Any unread article in this category at all? - switch ($getType) { - case 'a': - $hasUnread = $this->view->nb_not_read > 0; - break; - case 's': - // This is deprecated. The favorite button does not exist anymore - $hasUnread = $this->view->nb_favorites['unread'] > 0; - break; - case 'c': - $hasUnread = (!isset($this->view->cat_aside[$getId]) || - $this->view->cat_aside[$getId]->nbNotRead() > 0); - break; - case 'f': - $myFeed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId); - $hasUnread = ($myFeed === null) || ($myFeed->nbNotRead() > 0); - break; - default: - $hasUnread = true; - break; - } - if (!$hasUnread && ($state_param === null)) { - $this->view->state = FreshRSS_Entry::STATE_ALL; - } - } - - $this->view->today = @strtotime('today'); - try { $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb + 1, $first, $filter); - // Si on a récupéré aucun article "non lus" - // on essaye de récupérer tous les articles - if ($this->view->state === FreshRSS_Entry::STATE_NOT_READ && empty($entries) && ($state_param === null) && ($filter == '')) { - Minz_Log::debug('Conflicting information about nbNotRead!'); - $feedDAO = FreshRSS_Factory::createFeedDao(); - try { - $feedDAO->updateCachedValues(); - } catch (Exception $ex) { - Minz_Log::notice('Failed to automatically correct nbNotRead! ' + $ex->getMessage()); - } - $this->view->state = FreshRSS_Entry::STATE_ALL; - $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb, $first, $filter); - } - Minz_Request::_param('state', $this->view->state); - - if (count($entries) <= $nb) { - $this->view->nextId = ''; - } else { //We have more elements for pagination - $lastEntry = array_pop($entries); - $this->view->nextId = $lastEntry->id(); + if (count($entries) > $nb) { + // We have more elements for pagination + $last_entry = array_pop($entries); + FreshRSS_Context::$next_id = $last_entry->id(); } $this->view->entries = $entries; @@ -168,6 +111,23 @@ class FreshRSS_index_Controller extends Minz_ActionController { */ private function updateContext() { FreshRSS_Context::_get(Minz_Request::param('get', 'a')); + + FreshRSS_Context::$state |= Minz_Request::param( + 'state', FreshRSS_Context::$conf->default_view + ); + if (FreshRSS_Context::$state & FreshRSS_Entry::STATE_NOT_READ && + FreshRSS_Context::$get_unread <= 0) { + FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ; + } + + FreshRSS_Context::$search = Minz_Request::param('search', ''); + FreshRSS_Context::$order = Minz_Request::param( + 'order', FreshRSS_Context::$conf->sort_order + ); + FreshRSS_Context::$number = Minz_Request::param( + 'nb', FreshRSS_Context::$conf->posts_per_page + ); + FreshRSS_Context::$first_id = Minz_Request::param('next', ''); } /** diff --git a/app/Models/Context.php b/app/Models/Context.php index 7138fd638..31ac60207 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -26,6 +26,9 @@ class FreshRSS_Context { ); public static $get_unread = 0; public static $order = 'DESC'; + public static $number = 0; + public static $search = ''; + public static $first_id = 0; public static function init() { // Init configuration. @@ -80,6 +83,9 @@ class FreshRSS_Context { self::$current_get['starred'] = true; self::$name = _t('your_favorites'); self::$get_unread = self::$total_starred['unread']; + + // Update state if favorite is not yet enabled. + self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE; break; case 'f': self::$current_get['feed'] = $id; diff --git a/app/views/index/normal.phtml b/app/views/index/normal.phtml index 6333d63db..c39dba0a9 100644 --- a/app/views/index/normal.phtml +++ b/app/views/index/normal.phtml @@ -26,6 +26,8 @@ if (!empty($this->entries)) { $bottomline_link = FreshRSS_Context::$conf->bottomline_link; $content_width = FreshRSS_Context::$conf->content_width; + + $today = @strtotime('today'); ?>
entries)) {
entries as $item) { - if ($display_today && $item->isDay(FreshRSS_Days::TODAY, $this->today)) { + if ($display_today && $item->isDay(FreshRSS_Days::TODAY, $today)) { ?>
entries)) { ?>
isDay(FreshRSS_Days::YESTERDAY, $this->today)) { + if ($display_yesterday && $item->isDay(FreshRSS_Days::YESTERDAY, $today)) { ?>
entries)) { ?>
isDay(FreshRSS_Days::BEFORE_YESTERDAY, $this->today)) { + if ($display_others && $item->isDay(FreshRSS_Days::BEFORE_YESTERDAY, $today)) { ?>
currentName; ?> Date: Wed, 22 Oct 2014 13:52:20 +0200 Subject: Entries are loaded again! It's working :) See https://github.com/marienfressinaud/FreshRSS/issues/634 --- app/Controllers/indexController.php | 58 ++++++++++++++++++++++++++----------- app/Models/Context.php | 17 ++++++++--- app/layout/layout.phtml | 4 +-- app/views/index/normal.phtml | 8 ++--- 4 files changed, 60 insertions(+), 27 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index be85aa68d..d711997be 100755 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -14,23 +14,6 @@ class FreshRSS_index_Controller extends Minz_ActionController { 'c' => 'index', 'a' => $prefered_output )); - - return; - - try { - $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb + 1, $first, $filter); - - if (count($entries) > $nb) { - // We have more elements for pagination - $last_entry = array_pop($entries); - FreshRSS_Context::$next_id = $last_entry->id(); - } - - $this->view->entries = $entries; - } catch (FreshRSS_EntriesGetter_Exception $e) { - Minz_Log::notice($e->getMessage()); - Minz_Error::error(404); - } } /** @@ -48,6 +31,21 @@ class FreshRSS_index_Controller extends Minz_ActionController { Minz_Error::error(404); } + try { + $entries = $this->listByContext(); + + if (count($entries) > FreshRSS_Context::$number) { + // We have more elements for pagination + $last_entry = array_pop($entries); + FreshRSS_Context::$next_id = $last_entry->id(); + } + + $this->view->entries = $entries; + } catch (FreshRSS_EntriesGetter_Exception $e) { + Minz_Log::notice($e->getMessage()); + Minz_Error::error(404); + } + $this->view->categories = FreshRSS_Context::$categories; $title = FreshRSS_Context::$name; @@ -100,6 +98,13 @@ class FreshRSS_index_Controller extends Minz_ActionController { Minz_Error::error(404); } + try { + $this->view->entries = $this->listByContext(); + } catch (FreshRSS_EntriesGetter_Exception $e) { + Minz_Log::notice($e->getMessage()); + Minz_Error::error(404); + } + // No layout for RSS output. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title(); $this->view->_useLayout(false); @@ -130,6 +135,25 @@ class FreshRSS_index_Controller extends Minz_ActionController { FreshRSS_Context::$first_id = Minz_Request::param('next', ''); } + private function listByContext() { + $entryDAO = FreshRSS_Factory::createEntryDao(); + + $get = FreshRSS_Context::currentGet(true); + if (count($get) > 1) { + $type = $get[0]; + $id = $get[1]; + } else { + $type = $get; + $id = ''; + } + + return $entryDAO->listWhere( + $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order, + FreshRSS_Context::$number + 1, FreshRSS_Context::$first_id, + FreshRSS_Context::$search + ); + } + /** * This action displays the about page of FreshRSS. */ diff --git a/app/Models/Context.php b/app/Models/Context.php index 31ac60207..54fb1e64a 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -28,7 +28,8 @@ class FreshRSS_Context { public static $order = 'DESC'; public static $number = 0; public static $search = ''; - public static $first_id = 0; + public static $first_id = ''; + public static $next_id = ''; public static function init() { // Init configuration. @@ -127,15 +128,23 @@ class FreshRSS_Context { } } - public static function currentGet() { + public static function currentGet($array = false) { if (self::$current_get['all']) { return 'a'; } elseif (self::$current_get['starred']) { return 's'; } elseif (self::$current_get['feed']) { - return 'f_' . self::$current_get['feed']; + if ($array) { + return array('f', self::$current_get['feed']); + } else { + return 'f_' . self::$current_get['feed']; + } } elseif (self::$current_get['category']) { - return 'c_' . self::$current_get['category']; + if ($array) { + return array('c', self::$current_get['category']); + } else { + return 'c_' . self::$current_get['category']; + } } } diff --git a/app/layout/layout.phtml b/app/layout/layout.phtml index a8c70ec64..2b38df4a1 100644 --- a/app/layout/layout.phtml +++ b/app/layout/layout.phtml @@ -10,9 +10,9 @@ renderHelper('javascript_vars'); ?> //]]> nextId)) { + if (FreshRSS_Context::$next_id !== '') { $params = Minz_Request::params(); - $params['next'] = $this->nextId; + $params['next'] = FreshRSS_Context::$next_id; $params['ajax'] = 1; ?> diff --git a/app/views/index/normal.phtml b/app/views/index/normal.phtml index c39dba0a9..36adef2f2 100644 --- a/app/views/index/normal.phtml +++ b/app/views/index/normal.phtml @@ -32,14 +32,14 @@ if (!empty($this->entries)) {
- +
entries as $item) { if ($display_today && $item->isDay(FreshRSS_Days::TODAY, $today)) { ?>
currentName; ?>
entries)) { ?>
currentName; ?>
isDay(FreshRSS_Days::BEFORE_YESTERDAY, $today)) { ?>
currentName; ?>
Date: Wed, 22 Oct 2014 17:57:22 +0200 Subject: Fix a set of TODO and bugs - Context object raises correct Exception if get is invalid - RSS feed is well-indicated on the home page - State is better calculated - Add some comments See https://github.com/marienfressinaud/FreshRSS/issues/634 --- app/Controllers/indexController.php | 39 +++++++++++++++++++++++++------------ app/Exceptions/ContextException.php | 10 ++++++++++ app/Models/Context.php | 16 ++++++--------- app/layout/layout.phtml | 17 ++++++++-------- 4 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 app/Exceptions/ContextException.php (limited to 'app/Models/Context.php') diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index d711997be..f9af2d0bb 100755 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -5,10 +5,10 @@ */ class FreshRSS_index_Controller extends Minz_ActionController { + /** + * This action only redirect on the default view mode (normal or global) + */ public function indexAction() { - // TODO: update the context with information from request. - // TODO: then, in dedicated action, get corresponding entries - $prefered_output = FreshRSS_Context::$conf->view_mode; Minz_Request::forward(array( 'c' => 'index', @@ -27,12 +27,12 @@ class FreshRSS_index_Controller extends Minz_ActionController { try { $this->updateContext(); - } catch (Minz_Exception $e) { + } catch (FreshRSS_Context_Exception $e) { Minz_Error::error(404); } try { - $entries = $this->listByContext(); + $entries = $this->listEntriesByContext(); if (count($entries) > FreshRSS_Context::$number) { // We have more elements for pagination @@ -48,6 +48,7 @@ class FreshRSS_index_Controller extends Minz_ActionController { $this->view->categories = FreshRSS_Context::$categories; + $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title(); $title = FreshRSS_Context::$name; if (FreshRSS_Context::$get_unread > 0) { $title = '(' . FreshRSS_Context::$get_unread . ') · ' . $title; @@ -68,12 +69,13 @@ class FreshRSS_index_Controller extends Minz_ActionController { try { $this->updateContext(); - } catch (Minz_Exception $e) { + } catch (FreshRSS_Context_Exception $e) { Minz_Error::error(404); } $this->view->categories = FreshRSS_Context::$categories; + $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title(); Minz_View::prependTitle(_t('gen.title.global_view') . ' · '); } @@ -94,12 +96,12 @@ class FreshRSS_index_Controller extends Minz_ActionController { try { $this->updateContext(); - } catch (Minz_Exception $e) { + } catch (FreshRSS_Context_Exception $e) { Minz_Error::error(404); } try { - $this->view->entries = $this->listByContext(); + $this->view->entries = $this->listEntriesByContext(); } catch (FreshRSS_EntriesGetter_Exception $e) { Minz_Log::notice($e->getMessage()); Minz_Error::error(404); @@ -113,15 +115,25 @@ class FreshRSS_index_Controller extends Minz_ActionController { /** * This action updates the Context object by using request parameters. + * + * Parameters are: + * - state (default: conf->default_view) + * - search (default: empty string) + * - order (default: conf->sort_order) + * - nb (default: conf->posts_per_page) + * - next (default: empty string) */ private function updateContext() { FreshRSS_Context::_get(Minz_Request::param('get', 'a')); - FreshRSS_Context::$state |= Minz_Request::param( + // TODO: change default_view by default_state. + FreshRSS_Context::$state = Minz_Request::param( 'state', FreshRSS_Context::$conf->default_view ); - if (FreshRSS_Context::$state & FreshRSS_Entry::STATE_NOT_READ && - FreshRSS_Context::$get_unread <= 0) { + $state_forced_by_user = Minz_Request::param('state', false) !== false; + if (FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_NOT_READ) && + FreshRSS_Context::$get_unread <= 0 && + !$state_forced_by_user) { FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ; } @@ -135,7 +147,10 @@ class FreshRSS_index_Controller extends Minz_ActionController { FreshRSS_Context::$first_id = Minz_Request::param('next', ''); } - private function listByContext() { + /** + * This method returns a list of entries based on the Context object. + */ + private function listEntriesByContext() { $entryDAO = FreshRSS_Factory::createEntryDao(); $get = FreshRSS_Context::currentGet(true); diff --git a/app/Exceptions/ContextException.php b/app/Exceptions/ContextException.php new file mode 100644 index 000000000..357751b7c --- /dev/null +++ b/app/Exceptions/ContextException.php @@ -0,0 +1,10 @@ + 0, ); - public static $state = 0; + public static $get_unread = 0; public static $current_get = array( 'all' => false, 'starred' => false, 'feed' => false, 'category' => false, ); - public static $get_unread = 0; + + public static $state = 0; public static $order = 'DESC'; public static $number = 0; public static $search = ''; @@ -48,8 +49,6 @@ class FreshRSS_Context { $catDAO = new FreshRSS_CategoryDAO(); $entryDAO = FreshRSS_Factory::createEntryDao(); - // Get the current state. - // self::$state = self::$conf->default_view; self::$categories = $catDAO->listCategories(); // Update number of read / unread variables. @@ -97,8 +96,7 @@ class FreshRSS_Context { $feed = $feedDAO->searchById($id); if (!$feed) { - // TODO: raise an exception - return false; + throw new FreshRSS_Context_Exception('Invalid feed: ' . $id); } } @@ -112,8 +110,7 @@ class FreshRSS_Context { $cat = $catDAO->searchById($id); if (!$cat) { - // TODO: raise an exception - return false; + throw new FreshRSS_Context_Exception('Invalid category: ' . $id); } } else { $cat = self::$categories[$id]; @@ -123,8 +120,7 @@ class FreshRSS_Context { self::$get_unread = $cat->nbNotRead(); break; default: - // TODO: raise an exception! - return false; + throw new FreshRSS_Context_Exception('Invalid getter: ' . $get); } } diff --git a/app/layout/layout.phtml b/app/layout/layout.phtml index 2b38df4a1..1827d6c26 100644 --- a/app/layout/layout.phtml +++ b/app/layout/layout.phtml @@ -10,21 +10,22 @@ renderHelper('javascript_vars'); ?> //]]> - + url)) { - $rss_url = $this->url; - $rss_url['params']['output'] = 'rss'; + if (isset($this->rss_title)) { + $url_rss = $url_base; + $url_rss['a'] = 'rss'; ?> - + -- cgit v1.2.3 From eb60b82959d768b199985523e7d4e05ba4055591 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 22 Oct 2014 18:06:28 +0200 Subject: Move back i18n init in FreshRSS.php --- app/FreshRSS.php | 4 ++++ app/Models/Context.php | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/FreshRSS.php b/app/FreshRSS.php index b997433bf..6114a5d1a 100644 --- a/app/FreshRSS.php +++ b/app/FreshRSS.php @@ -26,6 +26,10 @@ class FreshRSS extends Minz_FrontController { // Load context and configuration. FreshRSS_Context::init(); + // Init i18n. + Minz_Session::_param('language', FreshRSS_Context::$conf->language); + Minz_Translate::init(); + $this->loadStylesAndScripts(); $this->loadNotifications(); $this->loadExtensions(); diff --git a/app/Models/Context.php b/app/Models/Context.php index 4580a4f52..3b3c8673d 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -42,10 +42,6 @@ class FreshRSS_Context { die($e->getMessage()); } - // Init i18n. - Minz_Session::_param('language', self::$conf->language); - Minz_Translate::init(); - $catDAO = new FreshRSS_CategoryDAO(); $entryDAO = FreshRSS_Factory::createEntryDao(); -- cgit v1.2.3 From 1fe5ed5d210334a051c48442fad29a14e8aee155 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 22 Oct 2014 19:19:15 +0200 Subject: nextGet and idMax are coming back. See https://github.com/marienfressinaud/FreshRSS/issues/634 --- app/Controllers/entryController.php | 14 ++----- app/Controllers/indexController.php | 15 +++++++- app/Models/Context.php | 73 ++++++++++++++++++++++++++++++++----- app/layout/nav_menu.phtml | 5 +-- 4 files changed, 82 insertions(+), 25 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Controllers/entryController.php b/app/Controllers/entryController.php index 449029648..d11f3a520 100755 --- a/app/Controllers/entryController.php +++ b/app/Controllers/entryController.php @@ -17,14 +17,6 @@ class FreshRSS_entry_Controller extends Minz_ActionController { ); } - // Keep parameter information (output) to do a correct redirection at - // the end. - $this->params = array(); - $output = Minz_Request::param('output', ''); - if ($output != '' && FreshRSS_Context::$conf->view_mode !== $output) { - $this->params['output'] = $output; - } - // If ajax request, we do not print layout $this->ajax = Minz_Request::param('ajax'); if ($this->ajax) { @@ -53,6 +45,7 @@ class FreshRSS_entry_Controller extends Minz_ActionController { $get = Minz_Request::param('get'); $next_get = Minz_Request::param('nextGet', $get); $id_max = Minz_Request::param('idMax', 0); + $params = array(); $entryDAO = FreshRSS_Factory::createEntryDao(); if ($id === false) { @@ -86,7 +79,7 @@ class FreshRSS_entry_Controller extends Minz_ActionController { // Redirect to the correct page (category, feed or starred) // Not "a" because it is the default value if nothing is // given. - $this->params['get'] = $next_get; + $params['get'] = $next_get; } } } else { @@ -98,7 +91,7 @@ class FreshRSS_entry_Controller extends Minz_ActionController { Minz_Request::good(_t('feeds_marked_read'), array( 'c' => 'index', 'a' => 'index', - 'params' => $this->params, + 'params' => $params, ), true); } } @@ -123,7 +116,6 @@ class FreshRSS_entry_Controller extends Minz_ActionController { Minz_Request::forward(array( 'c' => 'index', 'a' => 'index', - 'params' => $this->params, ), true); } } diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index 2dd4c3068..80675b3a6 100755 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -34,12 +34,25 @@ class FreshRSS_index_Controller extends Minz_ActionController { try { $entries = $this->listEntriesByContext(); - if (count($entries) > FreshRSS_Context::$number) { + $nb_entries = count($entries); + if ($nb_entries > FreshRSS_Context::$number) { // We have more elements for pagination $last_entry = array_pop($entries); FreshRSS_Context::$next_id = $last_entry->id(); } + $first_entry = $nb_entries > 0 ? $entries[0] : null; + FreshRSS_Context::$id_max = $first_entry === null ? + (time() - 1) . '000000' : + $first_entry->id(); + if (FreshRSS_Context::$order === 'ASC') { + // In this case we do not know but we guess id_max + $id_max = (time() - 1) . '000000'; + if (strcmp($id_max, FreshRSS_Context::$id_max) > 0) { + FreshRSS_Context::$id_max = $id_max; + } + } + $this->view->entries = $entries; } catch (FreshRSS_EntriesGetter_Exception $e) { Minz_Log::notice($e->getMessage()); diff --git a/app/Models/Context.php b/app/Models/Context.php index 3b3c8673d..3d184dcaa 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -24,6 +24,7 @@ class FreshRSS_Context { 'feed' => false, 'category' => false, ); + public static $next_get = 'a'; public static $state = 0; public static $order = 'DESC'; @@ -31,6 +32,7 @@ class FreshRSS_Context { public static $search = ''; public static $first_id = ''; public static $next_id = ''; + public static $id_max = ''; public static function init() { // Init configuration. @@ -84,8 +86,6 @@ class FreshRSS_Context { self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE; break; case 'f': - self::$current_get['feed'] = $id; - $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id); if ($feed === null) { $feedDAO = FreshRSS_Factory::createFeedDao(); @@ -96,6 +96,8 @@ class FreshRSS_Context { } } + self::$current_get['feed'] = $id; + self::$current_get['category'] = $feed->category(); self::$name = $feed->name(); self::$get_unread = $feed->nbNotRead(); break; @@ -118,6 +120,8 @@ class FreshRSS_Context { default: throw new FreshRSS_Context_Exception('Invalid getter: ' . $get); } + + self::_nextGet(); } public static function currentGet($array = false) { @@ -150,19 +154,68 @@ class FreshRSS_Context { case 's': return self::$current_get['starred']; case 'f': - return self::$current_get['feed'] === $id; + return self::$current_get['feed'] == $id; case 'c': - return self::$current_get['category'] === $id; + return self::$current_get['category'] == $id; default: return false; } } - public static function nextStep() { - // TODO: fix this method. - return array( - 'get' => 'a', - 'idMax' => (time() - 1) . '000000' - ); + public static function _nextGet() { + $get = self::currentGet(); + self::$next_get = $get; + + if (self::$conf->onread_jump_next && strlen($get) > 2) { + $another_unread_id = ''; + $found_current_get = false; + switch ($get[0]) { + case 'f': + foreach (self::$categories as $cat) { + if ($cat->id() != self::$current_get['category']) { + continue; + } + + foreach ($cat->feeds() as $feed) { + if ($feed->id() == self::$current_get['feed']) { + $found_current_get = true; + continue; + } + + if ($feed->nbNotRead() > 0) { + $another_unread_id = $feed->id(); + if ($found_current_get) { + break; + } + } + } + break; + } + + self::$next_get['get'] = empty($another_unread_id) ? + 'c_' . self::$current_get['category'] : + 'f_' . $another_unread_id; + break; + case 'c': + foreach (self::$categories as $cat) { + if ($cat->id() == self::$current_get['category']) { + $found_current_get = true; + continue; + } + + if ($cat->nbNotRead() > 0) { + $another_unread_id = $cat->id(); + if ($found_current_get) { + break; + } + } + } + + self::$next_get['get'] = empty($another_unread_id) ? + 'a' : + 'c_' . $another_unread_id; + break; + } + } } } diff --git a/app/layout/nav_menu.phtml b/app/layout/nav_menu.phtml index 440e4d0b6..2c9f8724d 100644 --- a/app/layout/nav_menu.phtml +++ b/app/layout/nav_menu.phtml @@ -61,7 +61,6 @@ 'read', 'params' => array( 'get' => $get, - 'nextGet' => $next_step['get'], - 'idMax' => $next_step['idMax'] + 'nextGet' => FreshRSS_Context::$next_get, + 'idMax' => FreshRSS_Context::$id_max, ) ); ?> -- cgit v1.2.3 From db1ad1de233ac7a0e0674c6e64a6a01f3d0e8fbe Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 22 Oct 2014 19:36:55 +0200 Subject: Fix a bug in nextGet --- app/Models/Context.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Models/Context.php b/app/Models/Context.php index 3d184dcaa..d4aeee7ac 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -192,9 +192,9 @@ class FreshRSS_Context { break; } - self::$next_get['get'] = empty($another_unread_id) ? - 'c_' . self::$current_get['category'] : - 'f_' . $another_unread_id; + self::$next_get = empty($another_unread_id) ? + 'c_' . self::$current_get['category'] : + 'f_' . $another_unread_id; break; case 'c': foreach (self::$categories as $cat) { @@ -211,9 +211,9 @@ class FreshRSS_Context { } } - self::$next_get['get'] = empty($another_unread_id) ? - 'a' : - 'c_' . $another_unread_id; + self::$next_get = empty($another_unread_id) ? + 'a' : + 'c_' . $another_unread_id; break; } } -- cgit v1.2.3 From 1efbf6fb86dfe4ff549ce1b7884db17dfbf5554f Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 22 Oct 2014 20:05:00 +0200 Subject: Add comments to Context object. See https://github.com/marienfressinaud/FreshRSS/issues/634 --- app/Controllers/indexController.php | 7 ++ app/Models/Context.php | 135 ++++++++++++++++++++++++------------ 2 files changed, 98 insertions(+), 44 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index 80675b3a6..ed5b58b45 100755 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -137,6 +137,13 @@ class FreshRSS_index_Controller extends Minz_ActionController { * - next (default: empty string) */ private function updateContext() { + // Update number of read / unread variables. + $entryDAO = FreshRSS_Factory::createEntryDao(); + FreshRSS_Context::$total_starred = $entryDAO->countUnreadReadFavorites(); + FreshRSS_Context::$total_unread = FreshRSS_CategoryDAO::CountUnreads( + FreshRSS_Context::$categories, 1 + ); + FreshRSS_Context::_get(Minz_Request::param('get', 'a')); FreshRSS_Context::$state = Minz_Request::param( diff --git a/app/Models/Context.php b/app/Models/Context.php index d4aeee7ac..36c4087eb 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -34,6 +34,11 @@ class FreshRSS_Context { public static $next_id = ''; public static $id_max = ''; + /** + * Initialize the context. + * + * Set the correct $conf and $categories variables. + */ public static function init() { // Init configuration. $current_user = Minz_Session::param('currentUser'); @@ -45,19 +50,19 @@ class FreshRSS_Context { } $catDAO = new FreshRSS_CategoryDAO(); - $entryDAO = FreshRSS_Factory::createEntryDao(); - self::$categories = $catDAO->listCategories(); - - // Update number of read / unread variables. - self::$total_starred = $entryDAO->countUnreadReadFavorites(); - self::$total_unread = FreshRSS_CategoryDAO::CountUnreads(self::$categories, 1); } + /** + * Returns if the current state includes $state parameter. + */ public static function isStateEnabled($state) { return self::$state & $state; } + /** + * Returns the current state with or without $state parameter. + */ public static function getRevertState($state) { if (self::$state & $state) { return self::$state & ~$state; @@ -66,6 +71,65 @@ class FreshRSS_Context { } } + /** + * Return the current get as a string or an array. + * + * If $array is true, the first item of the returned value is 'f' or 'c' and + * the second is the id. + */ + public static function currentGet($array = false) { + if (self::$current_get['all']) { + return 'a'; + } elseif (self::$current_get['starred']) { + return 's'; + } elseif (self::$current_get['feed']) { + if ($array) { + return array('f', self::$current_get['feed']); + } else { + return 'f_' . self::$current_get['feed']; + } + } elseif (self::$current_get['category']) { + if ($array) { + return array('c', self::$current_get['category']); + } else { + return 'c_' . self::$current_get['category']; + } + } + } + + /** + * Return true if $get parameter correspond to the $current_get attribute. + */ + public static function isCurrentGet($get) { + $type = $get[0]; + $id = substr($get, 2); + + switch($type) { + case 'a': + return self::$current_get['all']; + case 's': + return self::$current_get['starred']; + case 'f': + return self::$current_get['feed'] == $id; + case 'c': + return self::$current_get['category'] == $id; + default: + return false; + } + } + + /** + * Set the current $get attribute. + * + * Valid $get parameter are: + * - a + * - s + * - f_ + * - c_ + * + * $name and $get_unread attributes are also updated as $next_get + * Raise an exception if id or $get is invalid. + */ public static function _get($get) { $type = $get[0]; $id = substr($get, 2); @@ -86,6 +150,7 @@ class FreshRSS_Context { self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE; break; case 'f': + // We try to find the corresponding feed. $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id); if ($feed === null) { $feedDAO = FreshRSS_Factory::createFeedDao(); @@ -102,6 +167,7 @@ class FreshRSS_Context { self::$get_unread = $feed->nbNotRead(); break; case 'c': + // We try to find the corresponding category. self::$current_get['category'] = $id; if (!isset(self::$categories[$id])) { $catDAO = new FreshRSS_CategoryDAO(); @@ -124,46 +190,12 @@ class FreshRSS_Context { self::_nextGet(); } - public static function currentGet($array = false) { - if (self::$current_get['all']) { - return 'a'; - } elseif (self::$current_get['starred']) { - return 's'; - } elseif (self::$current_get['feed']) { - if ($array) { - return array('f', self::$current_get['feed']); - } else { - return 'f_' . self::$current_get['feed']; - } - } elseif (self::$current_get['category']) { - if ($array) { - return array('c', self::$current_get['category']); - } else { - return 'c_' . self::$current_get['category']; - } - } - } - - public static function isCurrentGet($get) { - $type = $get[0]; - $id = substr($get, 2); - - switch($type) { - case 'a': - return self::$current_get['all']; - case 's': - return self::$current_get['starred']; - case 'f': - return self::$current_get['feed'] == $id; - case 'c': - return self::$current_get['category'] == $id; - default: - return false; - } - } - + /** + * Set the value of $next_get attribute. + */ public static function _nextGet() { $get = self::currentGet(); + // By default, $next_get == $get self::$next_get = $get; if (self::$conf->onread_jump_next && strlen($get) > 2) { @@ -171,13 +203,18 @@ class FreshRSS_Context { $found_current_get = false; switch ($get[0]) { case 'f': + // We search the next feed with at least one unread article in + // same category as the currend feed. foreach (self::$categories as $cat) { if ($cat->id() != self::$current_get['category']) { + // We look into the category of the current feed! continue; } foreach ($cat->feeds() as $feed) { if ($feed->id() == self::$current_get['feed']) { + // Here is our current feed! Fine, the next one will + // be a potential candidate. $found_current_get = true; continue; } @@ -185,6 +222,9 @@ class FreshRSS_Context { if ($feed->nbNotRead() > 0) { $another_unread_id = $feed->id(); if ($found_current_get) { + // We have found our current feed and now we + // have an feed with unread articles. Leave the + // loop! break; } } @@ -192,13 +232,17 @@ class FreshRSS_Context { break; } + // If no feed have been found, next_get is the current category. self::$next_get = empty($another_unread_id) ? 'c_' . self::$current_get['category'] : 'f_' . $another_unread_id; break; case 'c': + // We search the next category with at least one unread article. foreach (self::$categories as $cat) { if ($cat->id() == self::$current_get['category']) { + // Here is our current category! Next one could be our + // champion if it has unread articles. $found_current_get = true; continue; } @@ -206,11 +250,14 @@ class FreshRSS_Context { if ($cat->nbNotRead() > 0) { $another_unread_id = $cat->id(); if ($found_current_get) { + // Unread articles and the current category has + // already been found? Leave the loop! break; } } } + // No unread category? The main stream will be our destination! self::$next_get = empty($another_unread_id) ? 'a' : 'c_' . $another_unread_id; -- cgit v1.2.3