]> git.wh0rd.org - tt-rss.git/blame - classes/counters.php
auth/base: PDO
[tt-rss.git] / classes / counters.php
CommitLineData
65af3b2c
AD
1<?php
2class Counters {
3
4 static function getAllCounters() {
5 $data = Counters::getGlobalCounters();
6
7 $data = array_merge($data, Counters::getVirtCounters());
8 $data = array_merge($data, Counters::getLabelCounters());
9 $data = array_merge($data, Counters::getFeedCounters());
10 $data = array_merge($data, Counters::getCategoryCounters());
11
12 return $data;
13 }
14
15 static function getCategoryCounters() {
16 $ret_arr = array();
17
18 /* Labels category */
19
20 $cv = array("id" => -2, "kind" => "cat",
21 "counter" => Feeds::getCategoryUnread(-2));
22
23 array_push($ret_arr, $cv);
24
25 $result = db_query("SELECT id AS cat_id, value AS unread,
26 (SELECT COUNT(id) FROM ttrss_feed_categories AS c2
27 WHERE c2.parent_cat = ttrss_feed_categories.id) AS num_children
28 FROM ttrss_feed_categories, ttrss_cat_counters_cache
29 WHERE ttrss_cat_counters_cache.feed_id = id AND
30 ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid AND
31 ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
32
33 while ($line = db_fetch_assoc($result)) {
34 $line["cat_id"] = (int) $line["cat_id"];
35
36 if ($line["num_children"] > 0) {
37 $child_counter = Feeds::getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]);
38 } else {
39 $child_counter = 0;
40 }
41
42 $cv = array("id" => $line["cat_id"], "kind" => "cat",
43 "counter" => $line["unread"] + $child_counter);
44
45 array_push($ret_arr, $cv);
46 }
47
48 /* Special case: NULL category doesn't actually exist in the DB */
49
50 $cv = array("id" => 0, "kind" => "cat",
51 "counter" => (int) CCache::find(0, $_SESSION["uid"], true));
52
53 array_push($ret_arr, $cv);
54
55 return $ret_arr;
56 }
57
58 static function getGlobalCounters($global_unread = -1) {
59 $ret_arr = array();
60
61 if ($global_unread == -1) {
62 $global_unread = Feeds::getGlobalUnread();
63 }
64
65 $cv = array("id" => "global-unread",
66 "counter" => (int) $global_unread);
67
68 array_push($ret_arr, $cv);
69
70 $result = db_query("SELECT COUNT(id) AS fn FROM
71 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
72
73 $subscribed_feeds = db_fetch_result($result, 0, "fn");
74
75 $cv = array("id" => "subscribed-feeds",
76 "counter" => (int) $subscribed_feeds);
77
78 array_push($ret_arr, $cv);
79
80 return $ret_arr;
81 }
82
83 static function getVirtCounters() {
84
85 $ret_arr = array();
86
87 for ($i = 0; $i >= -4; $i--) {
88
89 $count = getFeedUnread($i);
90
91 if ($i == 0 || $i == -1 || $i == -2)
92 $auxctr = Feeds::getFeedArticles($i, false);
93 else
94 $auxctr = 0;
95
96 $cv = array("id" => $i,
97 "counter" => (int) $count,
98 "auxcounter" => (int) $auxctr);
99
100// if (get_pref('EXTENDED_FEEDLIST'))
101// $cv["xmsg"] = getFeedArticles($i)." ".__("total");
102
103 array_push($ret_arr, $cv);
104 }
105
106 $feeds = PluginHost::getInstance()->get_feeds(-1);
107
108 if (is_array($feeds)) {
109 foreach ($feeds as $feed) {
110 $cv = array("id" => PluginHost::pfeed_to_feed_id($feed['id']),
111 "counter" => $feed['sender']->get_unread($feed['id']));
112
113 if (method_exists($feed['sender'], 'get_total'))
114 $cv["auxcounter"] = $feed['sender']->get_total($feed['id']);
115
116 array_push($ret_arr, $cv);
117 }
118 }
119
120 return $ret_arr;
121 }
122
123 static function getLabelCounters($descriptions = false) {
124
125 $ret_arr = array();
126
127 $owner_uid = $_SESSION["uid"];
128
129 $result = db_query("SELECT id,caption,SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS unread, COUNT(u1.unread) AS total
130 FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
131 (ttrss_labels2.id = label_id)
132 LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id
133 WHERE ttrss_labels2.owner_uid = $owner_uid AND u1.owner_uid = $owner_uid
134 GROUP BY ttrss_labels2.id,
135 ttrss_labels2.caption");
136
137 while ($line = db_fetch_assoc($result)) {
138
139 $id = Labels::label_to_feed_id($line["id"]);
140
141 $cv = array("id" => $id,
142 "counter" => (int) $line["unread"],
143 "auxcounter" => (int) $line["total"]);
144
145 if ($descriptions)
146 $cv["description"] = $line["caption"];
147
148 array_push($ret_arr, $cv);
149 }
150
151 return $ret_arr;
152 }
153
154 static function getFeedCounters($active_feed = false) {
155
156 $ret_arr = array();
157
158 $query = "SELECT ttrss_feeds.id,
159 ttrss_feeds.title,
160 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
161 last_error, value AS count
162 FROM ttrss_feeds, ttrss_counters_cache
163 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
164 AND ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid
165 AND ttrss_counters_cache.feed_id = id";
166
167 $result = db_query($query);
168
169 while ($line = db_fetch_assoc($result)) {
170
171 $id = $line["id"];
172 $count = $line["count"];
173 $last_error = htmlspecialchars($line["last_error"]);
174
175 $last_updated = make_local_datetime($line['last_updated'], false);
176
177 $has_img = feed_has_icon($id);
178
179 if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2)
180 $last_updated = '';
181
182 $cv = array("id" => $id,
183 "updated" => $last_updated,
184 "counter" => (int) $count,
185 "has_img" => (int) $has_img);
186
187 if ($last_error)
188 $cv["error"] = $last_error;
189
190// if (get_pref('EXTENDED_FEEDLIST'))
191// $cv["xmsg"] = getFeedArticles($id)." ".__("total");
192
193 if ($active_feed && $id == $active_feed)
194 $cv["title"] = truncate_string($line["title"], 30);
195
196 array_push($ret_arr, $cv);
197
198 }
199
200 return $ret_arr;
201 }
202
203}