4
0

Context.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 $state = 0;
  24. public static $order = 'DESC';
  25. public static $number = 0;
  26. public static $search = '';
  27. public static $first_id = '';
  28. public static $next_id = '';
  29. public static function init() {
  30. // Init configuration.
  31. $current_user = Minz_Session::param('currentUser');
  32. try {
  33. self::$conf = new FreshRSS_Configuration($current_user);
  34. } catch(Minz_Exception $e) {
  35. Minz_Log::error('Cannot load configuration file of user `' . $current_user . '`');
  36. die($e->getMessage());
  37. }
  38. // Init i18n.
  39. Minz_Session::_param('language', self::$conf->language);
  40. Minz_Translate::init();
  41. $catDAO = new FreshRSS_CategoryDAO();
  42. $entryDAO = FreshRSS_Factory::createEntryDao();
  43. self::$categories = $catDAO->listCategories();
  44. // Update number of read / unread variables.
  45. self::$total_starred = $entryDAO->countUnreadReadFavorites();
  46. self::$total_unread = FreshRSS_CategoryDAO::CountUnreads(self::$categories, 1);
  47. }
  48. public static function isStateEnabled($state) {
  49. return self::$state & $state;
  50. }
  51. public static function getRevertState($state) {
  52. if (self::$state & $state) {
  53. return self::$state & ~$state;
  54. } else {
  55. return self::$state | $state;
  56. }
  57. }
  58. public static function _get($get) {
  59. $type = $get[0];
  60. $id = substr($get, 2);
  61. $nb_unread = 0;
  62. switch($type) {
  63. case 'a':
  64. self::$current_get['all'] = true;
  65. self::$name = _t('your_rss_feeds');
  66. self::$get_unread = self::$total_unread;
  67. break;
  68. case 's':
  69. self::$current_get['starred'] = true;
  70. self::$name = _t('your_favorites');
  71. self::$get_unread = self::$total_starred['unread'];
  72. // Update state if favorite is not yet enabled.
  73. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  74. break;
  75. case 'f':
  76. self::$current_get['feed'] = $id;
  77. $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id);
  78. if ($feed === null) {
  79. $feedDAO = FreshRSS_Factory::createFeedDao();
  80. $feed = $feedDAO->searchById($id);
  81. if (!$feed) {
  82. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  83. }
  84. }
  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. }
  106. public static function currentGet($array = false) {
  107. if (self::$current_get['all']) {
  108. return 'a';
  109. } elseif (self::$current_get['starred']) {
  110. return 's';
  111. } elseif (self::$current_get['feed']) {
  112. if ($array) {
  113. return array('f', self::$current_get['feed']);
  114. } else {
  115. return 'f_' . self::$current_get['feed'];
  116. }
  117. } elseif (self::$current_get['category']) {
  118. if ($array) {
  119. return array('c', self::$current_get['category']);
  120. } else {
  121. return 'c_' . self::$current_get['category'];
  122. }
  123. }
  124. }
  125. public static function isCurrentGet($get) {
  126. $type = $get[0];
  127. $id = substr($get, 2);
  128. switch($type) {
  129. case 'a':
  130. return self::$current_get['all'];
  131. case 's':
  132. return self::$current_get['starred'];
  133. case 'f':
  134. return self::$current_get['feed'] === $id;
  135. case 'c':
  136. return self::$current_get['category'] === $id;
  137. default:
  138. return false;
  139. }
  140. }
  141. public static function nextStep() {
  142. // TODO: fix this method.
  143. return array(
  144. 'get' => 'a',
  145. 'idMax' => (time() - 1) . '000000'
  146. );
  147. }
  148. }