summaryrefslogtreecommitdiff
path: root/classes/counters.php
blob: 4518fa9d2d671730159fbe995b3160d7861aa3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?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 private function getCategoryChildrenCounters($cat_id, $owner_uid) {
		$pdo = Db::pdo();

		$sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories WHERE parent_cat = ?
				AND owner_uid = ?");
		$sth->execute([$cat_id, $owner_uid]);

		$unread = 0;
		$marked = 0;

		while ($line = $sth->fetch()) {
			list ($tmp_unread, $tmp_marked) = Counters::getCategoryChildrenCounters($line["id"], $owner_uid);

			$unread += $tmp_unread + Feeds::getCategoryUnread($line["id"], $owner_uid);
			$marked += $tmp_marked + Feeds::getCategoryMarked($line["id"], $owner_uid);
		}

		return [$unread, $marked];
	}

	static function getCategoryCounters() {
		$ret = [];

		/* Labels category */

		$cv = array("id" => -2, "kind" => "cat",
			"counter" => Feeds::getCategoryUnread(-2));

		array_push($ret, $cv);

		$pdo = Db::pdo();

		$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
    			LEFT JOIN ttrss_feeds f ON (f.cat_id = fc.id)
    			LEFT JOIN ttrss_user_entries ue ON (ue.feed_id = f.id)
			WHERE fc.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']]);

		while ($line = $sth->fetch()) {
			if ($line["num_children"] > 0) {
				list ($child_counter, $child_marked_counter) = Counters::getCategoryChildrenCounters($line["id"], $_SESSION["uid"]);
			} else {
				$child_counter = 0;
				$child_marked_counter = 0;
			}

			$cv = [
				"id" => (int)$line["id"],
				"kind" => "cat",
				"markedcounter" => (int) $line["count_marked"] + $child_marked_counter,
				"counter" => (int) $line["count"] + $child_counter
			];

			array_push($ret, $cv);
		}

		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");

		$sth->execute(["uid" => $_SESSION['uid']]);

		while ($line = $sth->fetch()) {

			$id = $line["id"];
			$last_error = htmlspecialchars($line["last_error"]);
			$last_updated = make_local_datetime($line['last_updated'], false);

			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;
	}

	static function getGlobalCounters($global_unread = -1) {
		$ret = [];

		if ($global_unread == -1) {
			$global_unread = Feeds::getGlobalUnread();
		}

		$cv = [
			"id" => "global-unread",
			"counter" => (int) $global_unread
		];

		array_push($ret, $cv);

		$pdo = Db::pdo();

		$sth = $pdo->prepare("SELECT COUNT(id) AS fn FROM
			ttrss_feeds WHERE owner_uid = ?");
		$sth->execute([$_SESSION['uid']]);
		$row = $sth->fetch();

		$subscribed_feeds = $row["fn"];

		$cv = [
			"id" => "subscribed-feeds",
			"counter" => (int) $subscribed_feeds
		];

		array_push($ret, $cv);

		return $ret;
	}

	static function getVirtCounters() {

		$ret = [];

		for ($i = 0; $i >= -4; $i--) {

			$count = getFeedUnread($i);

			if ($i == 0 || $i == -1 || $i == -2)
				$auxctr = Feeds::getFeedArticles($i, false);
			else
				$auxctr = 0;

			$cv = [
				"id" => $i,
				"counter" => (int) $count,
				"auxcounter" => (int) $auxctr
			];

			if ($i == -1)
				$cv["markedcounter"] = $auxctr;

			array_push($ret, $cv);
		}

		$feeds = PluginHost::getInstance()->get_feeds(-1);

		if (is_array($feeds)) {
			foreach ($feeds as $feed) {
				$cv = [
					"id" => PluginHost::pfeed_to_feed_id($feed['id']),
					"counter" => $feed['sender']->get_unread($feed['id'])
				];

				if (method_exists($feed['sender'], 'get_total'))
					$cv["auxcounter"] = $feed['sender']->get_total($feed['id']);

				array_push($ret, $cv);
			}
		}

		return $ret;
	}

	static function getLabelCounters($descriptions = false) {

		$ret = [];

		$pdo = Db::pdo();

		$sth = $pdo->prepare("SELECT id,
       			caption,
       			SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS count_unread,
       			SUM(CASE WHEN u1.marked = true THEN 1 ELSE 0 END) AS count_marked,
       			COUNT(u1.unread) AS total
			FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
				(ttrss_labels2.id = label_id)
					LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id AND u1.owner_uid = :uid
						WHERE ttrss_labels2.owner_uid = :uid
							GROUP BY ttrss_labels2.id, ttrss_labels2.caption");
		$sth->execute([":uid" => $_SESSION['uid']]);

		while ($line = $sth->fetch()) {

			$id = Labels::label_to_feed_id($line["id"]);

			$cv = [
				"id" => $id,
				"counter" => (int) $line["count_unread"],
				"auxcounter" => (int) $line["total"],
				"markedcounter" => (int) $line["count_marked"]
			];

			if ($descriptions)
				$cv["description"] = $line["caption"];

			array_push($ret, $cv);
		}

		return $ret;
	}
}