Context.php 9.1 KB

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