counters.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. class Counters {
  3. static function getAllCounters() {
  4. $data = Counters::getGlobalCounters();
  5. $data = array_merge($data, Counters::getVirtCounters());
  6. $data = array_merge($data, Counters::getLabelCounters());
  7. $data = array_merge($data, Counters::getFeedCounters());
  8. $data = array_merge($data, Counters::getCategoryCounters());
  9. return $data;
  10. }
  11. static function getCategoryCounters() {
  12. $ret_arr = array();
  13. /* Labels category */
  14. $cv = array("id" => -2, "kind" => "cat",
  15. "counter" => Feeds::getCategoryUnread(-2));
  16. array_push($ret_arr, $cv);
  17. $result = db_query("SELECT id AS cat_id, value AS unread,
  18. (SELECT COUNT(id) FROM ttrss_feed_categories AS c2
  19. WHERE c2.parent_cat = ttrss_feed_categories.id) AS num_children
  20. FROM ttrss_feed_categories, ttrss_cat_counters_cache
  21. WHERE ttrss_cat_counters_cache.feed_id = id AND
  22. ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid AND
  23. ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
  24. while ($line = db_fetch_assoc($result)) {
  25. $line["cat_id"] = (int) $line["cat_id"];
  26. if ($line["num_children"] > 0) {
  27. $child_counter = Feeds::getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]);
  28. } else {
  29. $child_counter = 0;
  30. }
  31. $cv = array("id" => $line["cat_id"], "kind" => "cat",
  32. "counter" => $line["unread"] + $child_counter);
  33. array_push($ret_arr, $cv);
  34. }
  35. /* Special case: NULL category doesn't actually exist in the DB */
  36. $cv = array("id" => 0, "kind" => "cat",
  37. "counter" => (int) CCache::find(0, $_SESSION["uid"], true));
  38. array_push($ret_arr, $cv);
  39. return $ret_arr;
  40. }
  41. static function getGlobalCounters($global_unread = -1) {
  42. $ret_arr = array();
  43. if ($global_unread == -1) {
  44. $global_unread = Feeds::getGlobalUnread();
  45. }
  46. $cv = array("id" => "global-unread",
  47. "counter" => (int) $global_unread);
  48. array_push($ret_arr, $cv);
  49. $result = db_query("SELECT COUNT(id) AS fn FROM
  50. ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
  51. $subscribed_feeds = db_fetch_result($result, 0, "fn");
  52. $cv = array("id" => "subscribed-feeds",
  53. "counter" => (int) $subscribed_feeds);
  54. array_push($ret_arr, $cv);
  55. return $ret_arr;
  56. }
  57. static function getVirtCounters() {
  58. $ret_arr = array();
  59. for ($i = 0; $i >= -4; $i--) {
  60. $count = getFeedUnread($i);
  61. if ($i == 0 || $i == -1 || $i == -2)
  62. $auxctr = Feeds::getFeedArticles($i, false);
  63. else
  64. $auxctr = 0;
  65. $cv = array("id" => $i,
  66. "counter" => (int) $count,
  67. "auxcounter" => (int) $auxctr);
  68. // if (get_pref('EXTENDED_FEEDLIST'))
  69. // $cv["xmsg"] = getFeedArticles($i)." ".__("total");
  70. array_push($ret_arr, $cv);
  71. }
  72. $feeds = PluginHost::getInstance()->get_feeds(-1);
  73. if (is_array($feeds)) {
  74. foreach ($feeds as $feed) {
  75. $cv = array("id" => PluginHost::pfeed_to_feed_id($feed['id']),
  76. "counter" => $feed['sender']->get_unread($feed['id']));
  77. if (method_exists($feed['sender'], 'get_total'))
  78. $cv["auxcounter"] = $feed['sender']->get_total($feed['id']);
  79. array_push($ret_arr, $cv);
  80. }
  81. }
  82. return $ret_arr;
  83. }
  84. static function getLabelCounters($descriptions = false) {
  85. $ret_arr = array();
  86. $owner_uid = $_SESSION["uid"];
  87. $result = db_query("SELECT id,caption,SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS unread, COUNT(u1.unread) AS total
  88. FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
  89. (ttrss_labels2.id = label_id)
  90. LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id
  91. WHERE ttrss_labels2.owner_uid = $owner_uid AND u1.owner_uid = $owner_uid
  92. GROUP BY ttrss_labels2.id,
  93. ttrss_labels2.caption");
  94. while ($line = db_fetch_assoc($result)) {
  95. $id = Labels::label_to_feed_id($line["id"]);
  96. $cv = array("id" => $id,
  97. "counter" => (int) $line["unread"],
  98. "auxcounter" => (int) $line["total"]);
  99. if ($descriptions)
  100. $cv["description"] = $line["caption"];
  101. array_push($ret_arr, $cv);
  102. }
  103. return $ret_arr;
  104. }
  105. static function getFeedCounters($active_feed = false) {
  106. $ret_arr = array();
  107. $query = "SELECT ttrss_feeds.id,
  108. ttrss_feeds.title,
  109. ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
  110. last_error, value AS count
  111. FROM ttrss_feeds, ttrss_counters_cache
  112. WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
  113. AND ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid
  114. AND ttrss_counters_cache.feed_id = id";
  115. $result = db_query($query);
  116. while ($line = db_fetch_assoc($result)) {
  117. $id = $line["id"];
  118. $count = $line["count"];
  119. $last_error = htmlspecialchars($line["last_error"]);
  120. $last_updated = make_local_datetime($line['last_updated'], false);
  121. $has_img = feed_has_icon($id);
  122. if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2)
  123. $last_updated = '';
  124. $cv = array("id" => $id,
  125. "updated" => $last_updated,
  126. "counter" => (int) $count,
  127. "has_img" => (int) $has_img);
  128. if ($last_error)
  129. $cv["error"] = $last_error;
  130. // if (get_pref('EXTENDED_FEEDLIST'))
  131. // $cv["xmsg"] = getFeedArticles($id)." ".__("total");
  132. if ($active_feed && $id == $active_feed)
  133. $cv["title"] = truncate_string($line["title"], 30);
  134. array_push($ret_arr, $cv);
  135. }
  136. return $ret_arr;
  137. }
  138. }