Context.php 4.1 KB

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