Context.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. public static function init() {
  32. // Init configuration.
  33. $current_user = Minz_Session::param('currentUser');
  34. try {
  35. self::$conf = new FreshRSS_Configuration($current_user);
  36. } catch(Minz_Exception $e) {
  37. Minz_Log::error('Cannot load configuration file of user `' . $current_user . '`');
  38. die($e->getMessage());
  39. }
  40. $catDAO = new FreshRSS_CategoryDAO();
  41. $entryDAO = FreshRSS_Factory::createEntryDao();
  42. self::$categories = $catDAO->listCategories();
  43. // Update number of read / unread variables.
  44. self::$total_starred = $entryDAO->countUnreadReadFavorites();
  45. self::$total_unread = FreshRSS_CategoryDAO::CountUnreads(self::$categories, 1);
  46. }
  47. public static function isStateEnabled($state) {
  48. return self::$state & $state;
  49. }
  50. public static function getRevertState($state) {
  51. if (self::$state & $state) {
  52. return self::$state & ~$state;
  53. } else {
  54. return self::$state | $state;
  55. }
  56. }
  57. public static function _get($get) {
  58. $type = $get[0];
  59. $id = substr($get, 2);
  60. $nb_unread = 0;
  61. switch($type) {
  62. case 'a':
  63. self::$current_get['all'] = true;
  64. self::$name = _t('your_rss_feeds');
  65. self::$get_unread = self::$total_unread;
  66. break;
  67. case 's':
  68. self::$current_get['starred'] = true;
  69. self::$name = _t('your_favorites');
  70. self::$get_unread = self::$total_starred['unread'];
  71. // Update state if favorite is not yet enabled.
  72. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  73. break;
  74. case 'f':
  75. $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id);
  76. if ($feed === null) {
  77. $feedDAO = FreshRSS_Factory::createFeedDao();
  78. $feed = $feedDAO->searchById($id);
  79. if (!$feed) {
  80. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  81. }
  82. }
  83. self::$current_get['feed'] = $id;
  84. self::$current_get['category'] = $feed->category();
  85. self::$name = $feed->name();
  86. self::$get_unread = $feed->nbNotRead();
  87. break;
  88. case 'c':
  89. self::$current_get['category'] = $id;
  90. if (!isset(self::$categories[$id])) {
  91. $catDAO = new FreshRSS_CategoryDAO();
  92. $cat = $catDAO->searchById($id);
  93. if (!$cat) {
  94. throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
  95. }
  96. } else {
  97. $cat = self::$categories[$id];
  98. }
  99. self::$name = $cat->name();
  100. self::$get_unread = $cat->nbNotRead();
  101. break;
  102. default:
  103. throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
  104. }
  105. self::_nextGet();
  106. }
  107. public static function currentGet($array = false) {
  108. if (self::$current_get['all']) {
  109. return 'a';
  110. } elseif (self::$current_get['starred']) {
  111. return 's';
  112. } elseif (self::$current_get['feed']) {
  113. if ($array) {
  114. return array('f', self::$current_get['feed']);
  115. } else {
  116. return 'f_' . self::$current_get['feed'];
  117. }
  118. } elseif (self::$current_get['category']) {
  119. if ($array) {
  120. return array('c', self::$current_get['category']);
  121. } else {
  122. return 'c_' . self::$current_get['category'];
  123. }
  124. }
  125. }
  126. public static function isCurrentGet($get) {
  127. $type = $get[0];
  128. $id = substr($get, 2);
  129. switch($type) {
  130. case 'a':
  131. return self::$current_get['all'];
  132. case 's':
  133. return self::$current_get['starred'];
  134. case 'f':
  135. return self::$current_get['feed'] == $id;
  136. case 'c':
  137. return self::$current_get['category'] == $id;
  138. default:
  139. return false;
  140. }
  141. }
  142. public static function _nextGet() {
  143. $get = self::currentGet();
  144. self::$next_get = $get;
  145. if (self::$conf->onread_jump_next && strlen($get) > 2) {
  146. $another_unread_id = '';
  147. $found_current_get = false;
  148. switch ($get[0]) {
  149. case 'f':
  150. foreach (self::$categories as $cat) {
  151. if ($cat->id() != self::$current_get['category']) {
  152. continue;
  153. }
  154. foreach ($cat->feeds() as $feed) {
  155. if ($feed->id() == self::$current_get['feed']) {
  156. $found_current_get = true;
  157. continue;
  158. }
  159. if ($feed->nbNotRead() > 0) {
  160. $another_unread_id = $feed->id();
  161. if ($found_current_get) {
  162. break;
  163. }
  164. }
  165. }
  166. break;
  167. }
  168. self::$next_get['get'] = empty($another_unread_id) ?
  169. 'c_' . self::$current_get['category'] :
  170. 'f_' . $another_unread_id;
  171. break;
  172. case 'c':
  173. foreach (self::$categories as $cat) {
  174. if ($cat->id() == self::$current_get['category']) {
  175. $found_current_get = true;
  176. continue;
  177. }
  178. if ($cat->nbNotRead() > 0) {
  179. $another_unread_id = $cat->id();
  180. if ($found_current_get) {
  181. break;
  182. }
  183. }
  184. }
  185. self::$next_get['get'] = empty($another_unread_id) ?
  186. 'a' :
  187. 'c_' . $another_unread_id;
  188. break;
  189. }
  190. }
  191. }
  192. }