Context.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. $catDAO = new FreshRSS_CategoryDAO();
  39. $entryDAO = FreshRSS_Factory::createEntryDao();
  40. self::$categories = $catDAO->listCategories();
  41. // Update number of read / unread variables.
  42. self::$total_starred = $entryDAO->countUnreadReadFavorites();
  43. self::$total_unread = FreshRSS_CategoryDAO::CountUnreads(self::$categories, 1);
  44. }
  45. public static function isStateEnabled($state) {
  46. return self::$state & $state;
  47. }
  48. public static function getRevertState($state) {
  49. if (self::$state & $state) {
  50. return self::$state & ~$state;
  51. } else {
  52. return self::$state | $state;
  53. }
  54. }
  55. public static function _get($get) {
  56. $type = $get[0];
  57. $id = substr($get, 2);
  58. $nb_unread = 0;
  59. switch($type) {
  60. case 'a':
  61. self::$current_get['all'] = true;
  62. self::$name = _t('your_rss_feeds');
  63. self::$get_unread = self::$total_unread;
  64. break;
  65. case 's':
  66. self::$current_get['starred'] = true;
  67. self::$name = _t('your_favorites');
  68. self::$get_unread = self::$total_starred['unread'];
  69. // Update state if favorite is not yet enabled.
  70. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  71. break;
  72. case 'f':
  73. self::$current_get['feed'] = $id;
  74. $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id);
  75. if ($feed === null) {
  76. $feedDAO = FreshRSS_Factory::createFeedDao();
  77. $feed = $feedDAO->searchById($id);
  78. if (!$feed) {
  79. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  80. }
  81. }
  82. self::$name = $feed->name();
  83. self::$get_unread = $feed->nbNotRead();
  84. break;
  85. case 'c':
  86. self::$current_get['category'] = $id;
  87. if (!isset(self::$categories[$id])) {
  88. $catDAO = new FreshRSS_CategoryDAO();
  89. $cat = $catDAO->searchById($id);
  90. if (!$cat) {
  91. throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
  92. }
  93. } else {
  94. $cat = self::$categories[$id];
  95. }
  96. self::$name = $cat->name();
  97. self::$get_unread = $cat->nbNotRead();
  98. break;
  99. default:
  100. throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
  101. }
  102. }
  103. public static function currentGet($array = false) {
  104. if (self::$current_get['all']) {
  105. return 'a';
  106. } elseif (self::$current_get['starred']) {
  107. return 's';
  108. } elseif (self::$current_get['feed']) {
  109. if ($array) {
  110. return array('f', self::$current_get['feed']);
  111. } else {
  112. return 'f_' . self::$current_get['feed'];
  113. }
  114. } elseif (self::$current_get['category']) {
  115. if ($array) {
  116. return array('c', self::$current_get['category']);
  117. } else {
  118. return 'c_' . self::$current_get['category'];
  119. }
  120. }
  121. }
  122. public static function isCurrentGet($get) {
  123. $type = $get[0];
  124. $id = substr($get, 2);
  125. switch($type) {
  126. case 'a':
  127. return self::$current_get['all'];
  128. case 's':
  129. return self::$current_get['starred'];
  130. case 'f':
  131. return self::$current_get['feed'] === $id;
  132. case 'c':
  133. return self::$current_get['category'] === $id;
  134. default:
  135. return false;
  136. }
  137. }
  138. public static function nextStep() {
  139. // TODO: fix this method.
  140. return array(
  141. 'get' => 'a',
  142. 'idMax' => (time() - 1) . '000000'
  143. );
  144. }
  145. }