Context.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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::$description = self::$system_conf->meta_description;
  133. self::$get_unread = self::$total_unread;
  134. break;
  135. case 's':
  136. self::$current_get['starred'] = true;
  137. self::$name = _t('index.feed.title_fav');
  138. self::$description = self::$system_conf->meta_description;
  139. self::$get_unread = self::$total_starred['unread'];
  140. // Update state if favorite is not yet enabled.
  141. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  142. break;
  143. case 'f':
  144. // We try to find the corresponding feed. When allowing robots, always retrieve the full feed including description
  145. $feed = FreshRSS_Context::$system_conf->allow_robots ? null : FreshRSS_CategoryDAO::findFeed(self::$categories, $id);
  146. if ($feed === null) {
  147. $feedDAO = FreshRSS_Factory::createFeedDao();
  148. $feed = $feedDAO->searchById($id);
  149. if (!$feed) {
  150. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  151. }
  152. }
  153. self::$current_get['feed'] = $id;
  154. self::$current_get['category'] = $feed->category();
  155. self::$name = $feed->name();
  156. self::$description = $feed->description();
  157. self::$get_unread = $feed->nbNotRead();
  158. break;
  159. case 'c':
  160. // We try to find the corresponding category.
  161. self::$current_get['category'] = $id;
  162. if (!isset(self::$categories[$id])) {
  163. $catDAO = new FreshRSS_CategoryDAO();
  164. $cat = $catDAO->searchById($id);
  165. if (!$cat) {
  166. throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
  167. }
  168. } else {
  169. $cat = self::$categories[$id];
  170. }
  171. self::$name = $cat->name();
  172. self::$get_unread = $cat->nbNotRead();
  173. break;
  174. default:
  175. throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
  176. }
  177. self::_nextGet();
  178. }
  179. /**
  180. * Set the value of $next_get attribute.
  181. */
  182. public static function _nextGet() {
  183. $get = self::currentGet();
  184. // By default, $next_get == $get
  185. self::$next_get = $get;
  186. if (self::$user_conf->onread_jump_next && strlen($get) > 2) {
  187. $another_unread_id = '';
  188. $found_current_get = false;
  189. switch ($get[0]) {
  190. case 'f':
  191. // We search the next feed with at least one unread article in
  192. // same category as the currend feed.
  193. foreach (self::$categories as $cat) {
  194. if ($cat->id() != self::$current_get['category']) {
  195. // We look into the category of the current feed!
  196. continue;
  197. }
  198. foreach ($cat->feeds() as $feed) {
  199. if ($feed->id() == self::$current_get['feed']) {
  200. // Here is our current feed! Fine, the next one will
  201. // be a potential candidate.
  202. $found_current_get = true;
  203. continue;
  204. }
  205. if ($feed->nbNotRead() > 0) {
  206. $another_unread_id = $feed->id();
  207. if ($found_current_get) {
  208. // We have found our current feed and now we
  209. // have an feed with unread articles. Leave the
  210. // loop!
  211. break;
  212. }
  213. }
  214. }
  215. break;
  216. }
  217. // If no feed have been found, next_get is the current category.
  218. self::$next_get = empty($another_unread_id) ?
  219. 'c_' . self::$current_get['category'] :
  220. 'f_' . $another_unread_id;
  221. break;
  222. case 'c':
  223. // We search the next category with at least one unread article.
  224. foreach (self::$categories as $cat) {
  225. if ($cat->id() == self::$current_get['category']) {
  226. // Here is our current category! Next one could be our
  227. // champion if it has unread articles.
  228. $found_current_get = true;
  229. continue;
  230. }
  231. if ($cat->nbNotRead() > 0) {
  232. $another_unread_id = $cat->id();
  233. if ($found_current_get) {
  234. // Unread articles and the current category has
  235. // already been found? Leave the loop!
  236. break;
  237. }
  238. }
  239. }
  240. // No unread category? The main stream will be our destination!
  241. self::$next_get = empty($another_unread_id) ?
  242. 'a' :
  243. 'c_' . $another_unread_id;
  244. break;
  245. }
  246. }
  247. }
  248. /**
  249. * Determine if the auto remove is available in the current context.
  250. * This feature is available if:
  251. * - it is activated in the configuration
  252. * - the "read" state is not enable
  253. * - the "unread" state is enable
  254. *
  255. * @return boolean
  256. */
  257. public static function isAutoRemoveAvailable() {
  258. if (!self::$user_conf->auto_remove_article) {
  259. return false;
  260. }
  261. if (self::isStateEnabled(FreshRSS_Entry::STATE_READ)) {
  262. return false;
  263. }
  264. if (!self::isStateEnabled(FreshRSS_Entry::STATE_NOT_READ)) {
  265. return false;
  266. }
  267. return true;
  268. }
  269. /**
  270. * Determine if the "sticky post" option is enabled. It can be enable
  271. * by the user when it is selected in the configuration page or by the
  272. * application when the context allows to auto-remove articles when they
  273. * are read.
  274. *
  275. * @return boolean
  276. */
  277. public static function isStickyPostEnabled() {
  278. if (self::$user_conf->sticky_post) {
  279. return true;
  280. }
  281. if (self::isAutoRemoveAvailable()) {
  282. return true;
  283. }
  284. return false;
  285. }
  286. }