Context.php 8.2 KB

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