2017-05-05 08:54:31 +00:00
|
|
|
<?php
|
|
|
|
class Counters {
|
|
|
|
|
|
|
|
static function getAllCounters() {
|
|
|
|
$data = Counters::getGlobalCounters();
|
|
|
|
|
|
|
|
$data = array_merge($data, Counters::getVirtCounters());
|
|
|
|
$data = array_merge($data, Counters::getLabelCounters());
|
|
|
|
$data = array_merge($data, Counters::getFeedCounters());
|
|
|
|
$data = array_merge($data, Counters::getCategoryCounters());
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getCategoryCounters() {
|
2020-01-24 11:25:31 +00:00
|
|
|
$ret = [];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
/* Labels category */
|
|
|
|
|
|
|
|
$cv = array("id" => -2, "kind" => "cat",
|
|
|
|
"counter" => Feeds::getCategoryUnread(-2));
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
$pdo = DB::pdo();
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$sth = $pdo->prepare("SELECT fc.id,
|
|
|
|
SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
|
|
|
|
SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked,
|
|
|
|
(SELECT COUNT(id) FROM ttrss_feed_categories fcc
|
|
|
|
WHERE fcc.parent_cat = fc.id) AS num_children
|
|
|
|
FROM ttrss_feed_categories fc, ttrss_feeds f, ttrss_user_entries ue
|
|
|
|
WHERE f.cat_id = fc.id AND
|
|
|
|
ue.feed_id = f.id AND
|
|
|
|
ue.owner_uid = :uid
|
|
|
|
GROUP BY fc.id
|
|
|
|
UNION
|
|
|
|
SELECT 0,
|
|
|
|
SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
|
|
|
|
SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked,
|
|
|
|
0
|
|
|
|
FROM ttrss_feeds f, ttrss_user_entries ue
|
|
|
|
WHERE f.cat_id IS NULL AND
|
|
|
|
ue.feed_id = f.id AND
|
|
|
|
ue.owner_uid = :uid");
|
|
|
|
|
|
|
|
$sth->execute(["uid" => $_SESSION['uid']]);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
while ($line = $sth->fetch()) {
|
2017-05-05 08:54:31 +00:00
|
|
|
if ($line["num_children"] > 0) {
|
|
|
|
$child_counter = Feeds::getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]);
|
|
|
|
} else {
|
|
|
|
$child_counter = 0;
|
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => (int)$line["id"],
|
|
|
|
"kind" => "cat",
|
|
|
|
"markedcounter" => (int) $line["count_marked"],
|
|
|
|
"counter" => (int) $line["count"] + $child_counter
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static function getFeedCounters($active_feed = false) {
|
|
|
|
|
|
|
|
$ret = [];
|
|
|
|
|
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT f.id,
|
|
|
|
f.title,
|
|
|
|
".SUBSTRING_FOR_DATE."(f.last_updated,1,19) AS last_updated,
|
|
|
|
f.last_error,
|
|
|
|
SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count,
|
|
|
|
SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked
|
|
|
|
FROM ttrss_feeds f, ttrss_user_entries ue
|
|
|
|
WHERE f.id = ue.feed_id AND ue.owner_uid = :uid
|
|
|
|
GROUP BY f.id");
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:27:24 +00:00
|
|
|
$sth->execute(["uid" => $_SESSION['uid']]);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
while ($line = $sth->fetch()) {
|
|
|
|
|
|
|
|
$id = $line["id"];
|
|
|
|
$last_error = htmlspecialchars($line["last_error"]);
|
|
|
|
$last_updated = make_local_datetime($line['last_updated'], false);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
if (Feeds::feedHasIcon($id)) {
|
|
|
|
$has_img = filemtime(Feeds::getIconFile($id));
|
|
|
|
} else {
|
|
|
|
$has_img = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2)
|
|
|
|
$last_updated = '';
|
|
|
|
|
|
|
|
$cv = [
|
|
|
|
"id" => $id,
|
|
|
|
"updated" => $last_updated,
|
|
|
|
"counter" => (int) $line["count"],
|
|
|
|
"markedcounter" => (int) $line["count_marked"],
|
|
|
|
"has_img" => (int) $has_img
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($last_error)
|
|
|
|
$cv["error"] = $last_error;
|
|
|
|
|
|
|
|
if ($active_feed && $id == $active_feed)
|
|
|
|
$cv["title"] = truncate_string($line["title"], 30);
|
|
|
|
|
|
|
|
array_push($ret, $cv);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static function getGlobalCounters($global_unread = -1) {
|
2020-01-24 11:25:31 +00:00
|
|
|
$ret = [];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
if ($global_unread == -1) {
|
|
|
|
$global_unread = Feeds::getGlobalUnread();
|
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => "global-unread",
|
|
|
|
"counter" => (int) $global_unread
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT COUNT(id) AS fn FROM
|
|
|
|
ttrss_feeds WHERE owner_uid = ?");
|
|
|
|
$sth->execute([$_SESSION['uid']]);
|
|
|
|
$row = $sth->fetch();
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
$subscribed_feeds = $row["fn"];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => "subscribed-feeds",
|
|
|
|
"counter" => (int) $subscribed_feeds
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
return $ret;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static function getVirtCounters() {
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$ret = [];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
for ($i = 0; $i >= -4; $i--) {
|
|
|
|
|
|
|
|
$count = getFeedUnread($i);
|
|
|
|
|
|
|
|
if ($i == 0 || $i == -1 || $i == -2)
|
|
|
|
$auxctr = Feeds::getFeedArticles($i, false);
|
|
|
|
else
|
|
|
|
$auxctr = 0;
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => $i,
|
2017-05-05 08:54:31 +00:00
|
|
|
"counter" => (int) $count,
|
2020-01-24 11:25:31 +00:00
|
|
|
"auxcounter" => (int) $auxctr
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$feeds = PluginHost::getInstance()->get_feeds(-1);
|
|
|
|
|
|
|
|
if (is_array($feeds)) {
|
|
|
|
foreach ($feeds as $feed) {
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => PluginHost::pfeed_to_feed_id($feed['id']),
|
|
|
|
"counter" => $feed['sender']->get_unread($feed['id'])
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
if (method_exists($feed['sender'], 'get_total'))
|
|
|
|
$cv["auxcounter"] = $feed['sender']->get_total($feed['id']);
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
return $ret;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static function getLabelCounters($descriptions = false) {
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$ret = [];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
$pdo = Db::pdo();
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$sth = $pdo->prepare("SELECT id,
|
|
|
|
caption,
|
|
|
|
SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS unread,
|
|
|
|
COUNT(u1.unread) AS total
|
2017-05-05 08:54:31 +00:00
|
|
|
FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
|
|
|
|
(ttrss_labels2.id = label_id)
|
2020-01-24 11:25:31 +00:00
|
|
|
LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id
|
|
|
|
WHERE ttrss_labels2.owner_uid = :uid AND u1.owner_uid = :uid
|
|
|
|
GROUP BY ttrss_labels2.id, ttrss_labels2.caption");
|
2017-12-01 14:47:29 +00:00
|
|
|
$sth->execute([":uid" => $_SESSION['uid']]);
|
2017-05-05 08:54:31 +00:00
|
|
|
|
2017-12-01 14:47:29 +00:00
|
|
|
while ($line = $sth->fetch()) {
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
$id = Labels::label_to_feed_id($line["id"]);
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
$cv = [
|
|
|
|
"id" => $id,
|
2017-05-05 08:54:31 +00:00
|
|
|
"counter" => (int) $line["unread"],
|
2020-01-24 11:25:31 +00:00
|
|
|
"auxcounter" => (int) $line["total"]
|
|
|
|
];
|
2017-05-05 08:54:31 +00:00
|
|
|
|
|
|
|
if ($descriptions)
|
|
|
|
$cv["description"] = $line["caption"];
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
array_push($ret, $cv);
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 11:25:31 +00:00
|
|
|
return $ret;
|
2017-05-05 08:54:31 +00:00
|
|
|
}
|
2020-01-24 11:25:31 +00:00
|
|
|
}
|