Context.php 8.1 KB

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