counters.php 5.3 KB

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