aboutsummaryrefslogtreecommitdiff
path: root/app/Models
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-10-24 15:17:07 +0200
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-10-24 15:17:07 +0200
commit3e2d34c8671acae33568fdeb1398afad8296d9bf (patch)
tree6a8fa1430115d12e78855362e093dae9667008f8 /app/Models
parent5d7d01be296a23058026e0dc610b2e2077b3262c (diff)
parent83d95ca4b894a84de467a97c7f413c1d04c43631 (diff)
Merge branch '634-refactor-with-context' into dev
Diffstat (limited to 'app/Models')
-rw-r--r--app/Models/Configuration.php21
-rw-r--r--app/Models/Context.php250
-rw-r--r--app/Models/Entry.php3
-rw-r--r--app/Models/EntryDAO.php2
4 files changed, 257 insertions, 19 deletions
diff --git a/app/Models/Configuration.php b/app/Models/Configuration.php
index 2f208e509..53f136513 100644
--- a/app/Models/Configuration.php
+++ b/app/Models/Configuration.php
@@ -14,7 +14,8 @@ class FreshRSS_Configuration {
'apiPasswordHash' => '', //CRYPT_BLOWFISH
'posts_per_page' => 20,
'view_mode' => 'normal',
- 'default_view' => FreshRSS_Entry::STATE_NOT_READ,
+ 'default_view' => 'adaptive',
+ 'default_state' => FreshRSS_Entry::STATE_NOT_READ,
'auto_load_more' => true,
'display_posts' => false,
'display_categories' => false,
@@ -153,18 +154,22 @@ class FreshRSS_Configuration {
}
public function _default_view($value) {
switch ($value) {
- case FreshRSS_Entry::STATE_ALL:
- // left blank on purpose
- case FreshRSS_Entry::STATE_NOT_READ:
- // left blank on purpose
- case FreshRSS_Entry::STATE_STRICT + FreshRSS_Entry::STATE_NOT_READ:
+ case 'all':
$this->data['default_view'] = $value;
+ $this->data['default_state'] = (FreshRSS_Entry::STATE_READ +
+ FreshRSS_Entry::STATE_NOT_READ);
break;
+ case 'adaptive':
+ case 'unread':
default:
- $this->data['default_view'] = FreshRSS_Entry::STATE_ALL;
- break;
+ $this->data['default_view'] = $value;
+ $this->data['default_state'] = FreshRSS_Entry::STATE_NOT_READ;
}
}
+ public function _default_state($value) {
+ $this->data['default_state'] = (int)$value;
+ }
+
public function _display_posts($value) {
$this->data['display_posts'] = ((bool)$value) && $value !== 'no';
}
diff --git a/app/Models/Context.php b/app/Models/Context.php
index d984fece7..36c4087eb 100644
--- a/app/Models/Context.php
+++ b/app/Models/Context.php
@@ -6,8 +6,39 @@
*/
class FreshRSS_Context {
public static $conf = null;
+ public static $categories = array();
+
+ public static $name = '';
+
+ public static $total_unread = 0;
+ public static $total_starred = array(
+ 'all' => 0,
+ 'read' => 0,
+ 'unread' => 0,
+ );
+
+ public static $get_unread = 0;
+ public static $current_get = array(
+ 'all' => false,
+ 'starred' => false,
+ 'feed' => false,
+ 'category' => false,
+ );
+ public static $next_get = 'a';
+
public static $state = 0;
+ public static $order = 'DESC';
+ public static $number = 0;
+ public static $search = '';
+ public static $first_id = '';
+ 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');
@@ -18,15 +49,220 @@ class FreshRSS_Context {
die($e->getMessage());
}
- // Init i18n.
- Minz_Session::_param('language', self::$conf->language);
- Minz_Translate::init();
-
- // Get the current state.
- self::$state = self::$conf->default_view;
+ $catDAO = new FreshRSS_CategoryDAO();
+ self::$categories = $catDAO->listCategories();
}
- public static function stateEnabled($state) {
+ /**
+ * 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;
+ } else {
+ return self::$state | $state;
+ }
+ }
+
+ /**
+ * 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_<feed id>
+ * - c_<category id>
+ *
+ * $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);
+ $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'];
+
+ // Update state if favorite is not yet enabled.
+ 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();
+ $feed = $feedDAO->searchById($id);
+
+ if (!$feed) {
+ throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
+ }
+ }
+
+ self::$current_get['feed'] = $id;
+ self::$current_get['category'] = $feed->category();
+ self::$name = $feed->name();
+ 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();
+ $cat = $catDAO->searchById($id);
+
+ if (!$cat) {
+ throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
+ }
+ } else {
+ $cat = self::$categories[$id];
+ }
+
+ self::$name = $cat->name();
+ self::$get_unread = $cat->nbNotRead();
+ break;
+ default:
+ throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
+ }
+
+ self::_nextGet();
+ }
+
+ /**
+ * 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) {
+ $another_unread_id = '';
+ $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;
+ }
+
+ 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;
+ }
+ }
+ }
+ 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;
+ }
+
+ 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;
+ break;
+ }
+ }
+ }
}
diff --git a/app/Models/Entry.php b/app/Models/Entry.php
index ee94d1110..346c98a92 100644
--- a/app/Models/Entry.php
+++ b/app/Models/Entry.php
@@ -1,12 +1,11 @@
<?php
class FreshRSS_Entry extends Minz_Model {
- const STATE_ALL = 0;
const STATE_READ = 1;
const STATE_NOT_READ = 2;
+ const STATE_ALL = 3;
const STATE_FAVORITE = 4;
const STATE_NOT_FAVORITE = 8;
- const STATE_STRICT = 16;
private $id = 0;
private $guid;
diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php
index 5a136499a..5d2909c62 100644
--- a/app/Models/EntryDAO.php
+++ b/app/Models/EntryDAO.php
@@ -333,8 +333,6 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {
if ($state & FreshRSS_Entry::STATE_NOT_READ) {
if (!($state & FreshRSS_Entry::STATE_READ)) {
$where .= 'AND e1.is_read=0 ';
- } elseif ($state & FreshRSS_Entry::STATE_STRICT) {
- $where .= 'AND e1.is_read=0 ';
}
}
elseif ($state & FreshRSS_Entry::STATE_READ) {