Context.php 8.4 KB

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