Context.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * The context object handles the current configuration file and different
  4. * useful functions associated to the current view state.
  5. */
  6. class FreshRSS_Context {
  7. public static $conf = null;
  8. public static $categories = array();
  9. public static $name = '';
  10. public static $total_unread = 0;
  11. public static $total_starred = array(
  12. 'all' => 0,
  13. 'read' => 0,
  14. 'unread' => 0,
  15. );
  16. public static $get_unread = 0;
  17. public static $current_get = array(
  18. 'all' => false,
  19. 'starred' => false,
  20. 'feed' => false,
  21. 'category' => false,
  22. );
  23. public static $next_get = 'a';
  24. public static $state = 0;
  25. public static $order = 'DESC';
  26. public static $number = 0;
  27. public static $search = '';
  28. public static $first_id = '';
  29. public static $next_id = '';
  30. public static $id_max = '';
  31. /**
  32. * Initialize the context.
  33. *
  34. * Set the correct $conf and $categories variables.
  35. */
  36. public static function init() {
  37. // Init configuration.
  38. $current_user = Minz_Session::param('currentUser');
  39. try {
  40. self::$conf = new FreshRSS_Configuration($current_user);
  41. } catch(Minz_Exception $e) {
  42. Minz_Log::error('Cannot load configuration file of user `' . $current_user . '`');
  43. die($e->getMessage());
  44. }
  45. $catDAO = new FreshRSS_CategoryDAO();
  46. self::$categories = $catDAO->listCategories();
  47. }
  48. /**
  49. * Returns if the current state includes $state parameter.
  50. */
  51. public static function isStateEnabled($state) {
  52. return self::$state & $state;
  53. }
  54. /**
  55. * Returns the current state with or without $state parameter.
  56. */
  57. public static function getRevertState($state) {
  58. if (self::$state & $state) {
  59. return self::$state & ~$state;
  60. } else {
  61. return self::$state | $state;
  62. }
  63. }
  64. /**
  65. * Return the current get as a string or an array.
  66. *
  67. * If $array is true, the first item of the returned value is 'f' or 'c' and
  68. * the second is the id.
  69. */
  70. public static function currentGet($array = false) {
  71. if (self::$current_get['all']) {
  72. return 'a';
  73. } elseif (self::$current_get['starred']) {
  74. return 's';
  75. } elseif (self::$current_get['feed']) {
  76. if ($array) {
  77. return array('f', self::$current_get['feed']);
  78. } else {
  79. return 'f_' . self::$current_get['feed'];
  80. }
  81. } elseif (self::$current_get['category']) {
  82. if ($array) {
  83. return array('c', self::$current_get['category']);
  84. } else {
  85. return 'c_' . self::$current_get['category'];
  86. }
  87. }
  88. }
  89. /**
  90. * Return true if $get parameter correspond to the $current_get attribute.
  91. */
  92. public static function isCurrentGet($get) {
  93. $type = $get[0];
  94. $id = substr($get, 2);
  95. switch($type) {
  96. case 'a':
  97. return self::$current_get['all'];
  98. case 's':
  99. return self::$current_get['starred'];
  100. case 'f':
  101. return self::$current_get['feed'] == $id;
  102. case 'c':
  103. return self::$current_get['category'] == $id;
  104. default:
  105. return false;
  106. }
  107. }
  108. /**
  109. * Set the current $get attribute.
  110. *
  111. * Valid $get parameter are:
  112. * - a
  113. * - s
  114. * - f_<feed id>
  115. * - c_<category id>
  116. *
  117. * $name and $get_unread attributes are also updated as $next_get
  118. * Raise an exception if id or $get is invalid.
  119. */
  120. public static function _get($get) {
  121. $type = $get[0];
  122. $id = substr($get, 2);
  123. $nb_unread = 0;
  124. switch($type) {
  125. case 'a':
  126. self::$current_get['all'] = true;
  127. self::$name = _t('your_rss_feeds');
  128. self::$get_unread = self::$total_unread;
  129. break;
  130. case 's':
  131. self::$current_get['starred'] = true;
  132. self::$name = _t('your_favorites');
  133. self::$get_unread = self::$total_starred['unread'];
  134. // Update state if favorite is not yet enabled.
  135. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  136. break;
  137. case 'f':
  138. // We try to find the corresponding feed.
  139. $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id);
  140. if ($feed === null) {
  141. $feedDAO = FreshRSS_Factory::createFeedDao();
  142. $feed = $feedDAO->searchById($id);
  143. if (!$feed) {
  144. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  145. }
  146. }
  147. self::$current_get['feed'] = $id;
  148. self::$current_get['category'] = $feed->category();
  149. self::$name = $feed->name();
  150. self::$get_unread = $feed->nbNotRead();
  151. break;
  152. case 'c':
  153. // We try to find the corresponding category.
  154. self::$current_get['category'] = $id;
  155. if (!isset(self::$categories[$id])) {
  156. $catDAO = new FreshRSS_CategoryDAO();
  157. $cat = $catDAO->searchById($id);
  158. if (!$cat) {
  159. throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
  160. }
  161. } else {
  162. $cat = self::$categories[$id];
  163. }
  164. self::$name = $cat->name();
  165. self::$get_unread = $cat->nbNotRead();
  166. break;
  167. default:
  168. throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
  169. }
  170. self::_nextGet();
  171. }
  172. /**
  173. * Set the value of $next_get attribute.
  174. */
  175. public static function _nextGet() {
  176. $get = self::currentGet();
  177. // By default, $next_get == $get
  178. self::$next_get = $get;
  179. if (self::$conf->onread_jump_next && strlen($get) > 2) {
  180. $another_unread_id = '';
  181. $found_current_get = false;
  182. switch ($get[0]) {
  183. case 'f':
  184. // We search the next feed with at least one unread article in
  185. // same category as the currend feed.
  186. foreach (self::$categories as $cat) {
  187. if ($cat->id() != self::$current_get['category']) {
  188. // We look into the category of the current feed!
  189. continue;
  190. }
  191. foreach ($cat->feeds() as $feed) {
  192. if ($feed->id() == self::$current_get['feed']) {
  193. // Here is our current feed! Fine, the next one will
  194. // be a potential candidate.
  195. $found_current_get = true;
  196. continue;
  197. }
  198. if ($feed->nbNotRead() > 0) {
  199. $another_unread_id = $feed->id();
  200. if ($found_current_get) {
  201. // We have found our current feed and now we
  202. // have an feed with unread articles. Leave the
  203. // loop!
  204. break;
  205. }
  206. }
  207. }
  208. break;
  209. }
  210. // If no feed have been found, next_get is the current category.
  211. self::$next_get = empty($another_unread_id) ?
  212. 'c_' . self::$current_get['category'] :
  213. 'f_' . $another_unread_id;
  214. break;
  215. case 'c':
  216. // We search the next category with at least one unread article.
  217. foreach (self::$categories as $cat) {
  218. if ($cat->id() == self::$current_get['category']) {
  219. // Here is our current category! Next one could be our
  220. // champion if it has unread articles.
  221. $found_current_get = true;
  222. continue;
  223. }
  224. if ($cat->nbNotRead() > 0) {
  225. $another_unread_id = $cat->id();
  226. if ($found_current_get) {
  227. // Unread articles and the current category has
  228. // already been found? Leave the loop!
  229. break;
  230. }
  231. }
  232. }
  233. // No unread category? The main stream will be our destination!
  234. self::$next_get = empty($another_unread_id) ?
  235. 'a' :
  236. 'c_' . $another_unread_id;
  237. break;
  238. }
  239. }
  240. }
  241. }