Context.php 8.1 KB

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