Context.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 $user_conf = null;
  8. public static $system_conf = null;
  9. public static $categories = array();
  10. public static $name = '';
  11. public static $total_unread = 0;
  12. public static $total_starred = array(
  13. 'all' => 0,
  14. 'read' => 0,
  15. 'unread' => 0,
  16. );
  17. public static $get_unread = 0;
  18. public static $current_get = array(
  19. 'all' => false,
  20. 'starred' => false,
  21. 'feed' => false,
  22. 'category' => false,
  23. );
  24. public static $next_get = 'a';
  25. public static $state = 0;
  26. public static $order = 'DESC';
  27. public static $number = 0;
  28. public static $search;
  29. public static $first_id = '';
  30. public static $next_id = '';
  31. public static $id_max = '';
  32. /**
  33. * Initialize the context.
  34. *
  35. * Set the correct configurations and $categories variables.
  36. */
  37. public static function init() {
  38. // Init configuration.
  39. self::$system_conf = Minz_Configuration::get('system');
  40. self::$user_conf = Minz_Configuration::get('user');
  41. $catDAO = new FreshRSS_CategoryDAO();
  42. self::$categories = $catDAO->listCategories();
  43. }
  44. /**
  45. * Returns if the current state includes $state parameter.
  46. */
  47. public static function isStateEnabled($state) {
  48. return self::$state & $state;
  49. }
  50. /**
  51. * Returns the current state with or without $state parameter.
  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. /**
  61. * Return the current get as a string or an array.
  62. *
  63. * If $array is true, the first item of the returned value is 'f' or 'c' and
  64. * the second is the id.
  65. */
  66. public static function currentGet($array = false) {
  67. if (self::$current_get['all']) {
  68. return 'a';
  69. } elseif (self::$current_get['starred']) {
  70. return 's';
  71. } elseif (self::$current_get['feed']) {
  72. if ($array) {
  73. return array('f', self::$current_get['feed']);
  74. } else {
  75. return 'f_' . self::$current_get['feed'];
  76. }
  77. } elseif (self::$current_get['category']) {
  78. if ($array) {
  79. return array('c', self::$current_get['category']);
  80. } else {
  81. return 'c_' . self::$current_get['category'];
  82. }
  83. }
  84. }
  85. /**
  86. * Return true if $get parameter correspond to the $current_get attribute.
  87. */
  88. public static function isCurrentGet($get) {
  89. $type = $get[0];
  90. $id = substr($get, 2);
  91. switch($type) {
  92. case 'a':
  93. return self::$current_get['all'];
  94. case 's':
  95. return self::$current_get['starred'];
  96. case 'f':
  97. return self::$current_get['feed'] == $id;
  98. case 'c':
  99. return self::$current_get['category'] == $id;
  100. default:
  101. return false;
  102. }
  103. }
  104. /**
  105. * Set the current $get attribute.
  106. *
  107. * Valid $get parameter are:
  108. * - a
  109. * - s
  110. * - f_<feed id>
  111. * - c_<category id>
  112. *
  113. * $name and $get_unread attributes are also updated as $next_get
  114. * Raise an exception if id or $get is invalid.
  115. */
  116. public static function _get($get) {
  117. $type = $get[0];
  118. $id = substr($get, 2);
  119. $nb_unread = 0;
  120. switch($type) {
  121. case 'a':
  122. self::$current_get['all'] = true;
  123. self::$name = _t('index.feed.title');
  124. self::$get_unread = self::$total_unread;
  125. break;
  126. case 's':
  127. self::$current_get['starred'] = true;
  128. self::$name = _t('index.feed.title_fav');
  129. self::$get_unread = self::$total_starred['unread'];
  130. // Update state if favorite is not yet enabled.
  131. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  132. break;
  133. case 'f':
  134. // We try to find the corresponding feed.
  135. $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id);
  136. if ($feed === null) {
  137. $feedDAO = FreshRSS_Factory::createFeedDao();
  138. $feed = $feedDAO->searchById($id);
  139. if (!$feed) {
  140. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  141. }
  142. }
  143. self::$current_get['feed'] = $id;
  144. self::$current_get['category'] = $feed->category();
  145. self::$name = $feed->name();
  146. self::$get_unread = $feed->nbNotRead();
  147. break;
  148. case 'c':
  149. // We try to find the corresponding category.
  150. self::$current_get['category'] = $id;
  151. if (!isset(self::$categories[$id])) {
  152. $catDAO = new FreshRSS_CategoryDAO();
  153. $cat = $catDAO->searchById($id);
  154. if (!$cat) {
  155. throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
  156. }
  157. } else {
  158. $cat = self::$categories[$id];
  159. }
  160. self::$name = $cat->name();
  161. self::$get_unread = $cat->nbNotRead();
  162. break;
  163. default:
  164. throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
  165. }
  166. self::_nextGet();
  167. }
  168. /**
  169. * Set the value of $next_get attribute.
  170. */
  171. public static function _nextGet() {
  172. $get = self::currentGet();
  173. // By default, $next_get == $get
  174. self::$next_get = $get;
  175. if (self::$user_conf->onread_jump_next && strlen($get) > 2) {
  176. $another_unread_id = '';
  177. $found_current_get = false;
  178. switch ($get[0]) {
  179. case 'f':
  180. // We search the next feed with at least one unread article in
  181. // same category as the currend feed.
  182. foreach (self::$categories as $cat) {
  183. if ($cat->id() != self::$current_get['category']) {
  184. // We look into the category of the current feed!
  185. continue;
  186. }
  187. foreach ($cat->feeds() as $feed) {
  188. if ($feed->id() == self::$current_get['feed']) {
  189. // Here is our current feed! Fine, the next one will
  190. // be a potential candidate.
  191. $found_current_get = true;
  192. continue;
  193. }
  194. if ($feed->nbNotRead() > 0) {
  195. $another_unread_id = $feed->id();
  196. if ($found_current_get) {
  197. // We have found our current feed and now we
  198. // have an feed with unread articles. Leave the
  199. // loop!
  200. break;
  201. }
  202. }
  203. }
  204. break;
  205. }
  206. // If no feed have been found, next_get is the current category.
  207. self::$next_get = empty($another_unread_id) ?
  208. 'c_' . self::$current_get['category'] :
  209. 'f_' . $another_unread_id;
  210. break;
  211. case 'c':
  212. // We search the next category with at least one unread article.
  213. foreach (self::$categories as $cat) {
  214. if ($cat->id() == self::$current_get['category']) {
  215. // Here is our current category! Next one could be our
  216. // champion if it has unread articles.
  217. $found_current_get = true;
  218. continue;
  219. }
  220. if ($cat->nbNotRead() > 0) {
  221. $another_unread_id = $cat->id();
  222. if ($found_current_get) {
  223. // Unread articles and the current category has
  224. // already been found? Leave the loop!
  225. break;
  226. }
  227. }
  228. }
  229. // No unread category? The main stream will be our destination!
  230. self::$next_get = empty($another_unread_id) ?
  231. 'a' :
  232. 'c_' . $another_unread_id;
  233. break;
  234. }
  235. }
  236. }
  237. /**
  238. * Determine if the auto remove is available in the current context.
  239. * This feature is available if:
  240. * - it is activated in the configuration
  241. * - the "read" state is not enable
  242. * - the "unread" state is enable
  243. *
  244. * @return boolean
  245. */
  246. public static function isAutoRemoveAvailable() {
  247. if (!self::$user_conf->auto_remove_article) {
  248. return false;
  249. }
  250. if (self::isStateEnabled(FreshRSS_Entry::STATE_READ)) {
  251. return false;
  252. }
  253. if (!self::isStateEnabled(FreshRSS_Entry::STATE_NOT_READ)) {
  254. return false;
  255. }
  256. return true;
  257. }
  258. /**
  259. * Determine if the "sticky post" option is enabled. It can be enable
  260. * by the user when it is selected in the configuration page or by the
  261. * application when the context allows to auto-remove articles when they
  262. * are read.
  263. *
  264. * @return boolean
  265. */
  266. public static function isStickyPostEnabled() {
  267. if (self::$user_conf->sticky_post) {
  268. return true;
  269. }
  270. if (self::isAutoRemoveAvailable()) {
  271. return true;
  272. }
  273. return false;
  274. }
  275. }