]>
Commit | Line | Data |
---|---|---|
87d7e850 | 1 | <?php |
7c9b5a3f AD |
2 | class Labels |
3 | { | |
4 | static function label_to_feed_id($label) { | |
0086a897 AD |
5 | return LABEL_BASE_INDEX - 1 - abs($label); |
6 | } | |
7 | ||
7c9b5a3f | 8 | static function feed_to_label_id($feed) { |
0086a897 AD |
9 | return LABEL_BASE_INDEX - 1 + abs($feed); |
10 | } | |
11 | ||
7c9b5a3f | 12 | static function find_id($label, $owner_uid) { |
c1c08aed | 13 | $pdo = Db::pdo(); |
87d7e850 | 14 | |
c1c08aed AD |
15 | $sth = $pdo->prepare("SELECT id FROM ttrss_labels2 WHERE caption = ? |
16 | AND owner_uid = ? LIMIT 1"); | |
17 | $sth->execute([$label, $owner_uid]); | |
18 | ||
19 | if ($row = $sth->fetch()) { | |
20 | return $row['id']; | |
87d7e850 AD |
21 | } else { |
22 | return 0; | |
23 | } | |
24 | } | |
25 | ||
7c9b5a3f | 26 | static function find_caption($label, $owner_uid) { |
c1c08aed AD |
27 | $pdo = Db::pdo(); |
28 | ||
29 | $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 WHERE id = ? | |
30 | AND owner_uid = ? LIMIT 1"); | |
31 | $sth->execute([$label, $owner_uid]); | |
87d7e850 | 32 | |
c1c08aed AD |
33 | if ($row = $sth->fetch()) { |
34 | return $row['caption']; | |
87d7e850 AD |
35 | } else { |
36 | return ""; | |
37 | } | |
38 | } | |
39 | ||
7c9b5a3f | 40 | static function get_all_labels($owner_uid) { |
87d7e850 AD |
41 | $rv = array(); |
42 | ||
c1c08aed AD |
43 | $pdo = Db::pdo(); |
44 | ||
3a029230 | 45 | $sth = $pdo->prepare("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2 |
c1c08aed AD |
46 | WHERE owner_uid = ? ORDER BY caption"); |
47 | $sth->execute([$owner_uid]); | |
87d7e850 | 48 | |
c1c08aed | 49 | while ($line = $sth->fetch()) { |
87d7e850 AD |
50 | array_push($rv, $line); |
51 | } | |
52 | ||
53 | return $rv; | |
54 | } | |
55 | ||
7c9b5a3f | 56 | static function update_cache($owner_uid, $id, $labels = false, $force = false) { |
c1c08aed | 57 | $pdo = Db::pdo(); |
87d7e850 AD |
58 | |
59 | if ($force) | |
7c9b5a3f | 60 | Labels::clear_cache($id); |
87d7e850 AD |
61 | |
62 | if (!$labels) | |
7c9b5a3f | 63 | $labels = Article::get_article_labels($id); |
87d7e850 | 64 | |
e4befe6b | 65 | $labels = json_encode($labels); |
87d7e850 | 66 | |
c1c08aed AD |
67 | $sth = $pdo->prepare("UPDATE ttrss_user_entries SET |
68 | label_cache = ? WHERE ref_id = ? AND owner_uid = ?"); | |
69 | $sth->execute([$labels, $id, $owner_uid]); | |
87d7e850 AD |
70 | |
71 | } | |
72 | ||
7c9b5a3f | 73 | static function clear_cache($id) { |
87d7e850 | 74 | |
c1c08aed AD |
75 | $pdo = Db::pdo(); |
76 | ||
77 | $sth = $pdo->prepare("UPDATE ttrss_user_entries SET | |
78 | label_cache = '' WHERE ref_id = ?"); | |
79 | $sth->execute([$id]); | |
87d7e850 AD |
80 | |
81 | } | |
82 | ||
7c9b5a3f | 83 | static function remove_article($id, $label, $owner_uid) { |
87d7e850 | 84 | |
7c9b5a3f | 85 | $label_id = Labels::find_id($label, $owner_uid); |
87d7e850 AD |
86 | |
87 | if (!$label_id) return; | |
88 | ||
c1c08aed AD |
89 | $pdo = Db::pdo(); |
90 | ||
91 | $sth = $pdo->prepare("DELETE FROM ttrss_user_labels2 | |
87d7e850 | 92 | WHERE |
c1c08aed AD |
93 | label_id = ? AND |
94 | article_id = ?"); | |
95 | ||
96 | $sth->execute([$label_id, $id]); | |
87d7e850 | 97 | |
7c9b5a3f | 98 | Labels::clear_cache($id); |
87d7e850 AD |
99 | } |
100 | ||
7c9b5a3f | 101 | static function add_article($id, $label, $owner_uid) { |
87d7e850 | 102 | |
7c9b5a3f | 103 | $label_id = Labels::find_id($label, $owner_uid); |
87d7e850 AD |
104 | |
105 | if (!$label_id) return; | |
106 | ||
c1c08aed AD |
107 | $pdo = Db::pdo(); |
108 | ||
109 | $sth = $pdo->prepare("SELECT | |
87d7e850 AD |
110 | article_id FROM ttrss_labels2, ttrss_user_labels2 |
111 | WHERE | |
112 | label_id = id AND | |
c1c08aed AD |
113 | label_id = ? AND |
114 | article_id = ? AND owner_uid = ? | |
87d7e850 AD |
115 | LIMIT 1"); |
116 | ||
c1c08aed AD |
117 | $sth->execute([$label_id, $id, $owner_uid]); |
118 | ||
119 | if (!$sth->fetch()) { | |
120 | $sth = $pdo->prepare("INSERT INTO ttrss_user_labels2 | |
121 | (label_id, article_id) VALUES (?, ?)"); | |
122 | ||
123 | $sth->execute([$label_id, $id]); | |
87d7e850 AD |
124 | } |
125 | ||
7c9b5a3f | 126 | Labels::clear_cache($id); |
87d7e850 AD |
127 | |
128 | } | |
129 | ||
7c9b5a3f | 130 | static function remove($id, $owner_uid) { |
87d7e850 AD |
131 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; |
132 | ||
c1c08aed | 133 | $pdo = Db::pdo(); |
c949a928 | 134 | $tr_in_progress = false; |
87d7e850 | 135 | |
c949a928 AD |
136 | try { |
137 | $pdo->beginTransaction(); | |
138 | } catch (Exception $e) { | |
139 | $tr_in_progress = true; | |
140 | } | |
87d7e850 | 141 | |
c1c08aed AD |
142 | $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 |
143 | WHERE id = ?"); | |
144 | $sth->execute([$id]); | |
87d7e850 | 145 | |
c1c08aed AD |
146 | $row = $sth->fetch(); |
147 | $caption = $row['caption']; | |
87d7e850 | 148 | |
c1c08aed AD |
149 | $sth = $pdo->prepare("DELETE FROM ttrss_labels2 WHERE id = ? |
150 | AND owner_uid = ?"); | |
151 | $sth->execute([$id, $owner_uid]); | |
152 | ||
153 | if ($sth->rowCount() != 0 && $caption) { | |
87d7e850 AD |
154 | |
155 | /* Remove access key for the label */ | |
156 | ||
f822a8e5 | 157 | $ext_id = LABEL_BASE_INDEX - 1 - $id; |
87d7e850 | 158 | |
c1c08aed AD |
159 | $sth = $pdo->prepare("DELETE FROM ttrss_access_keys WHERE |
160 | feed_id = ? AND owner_uid = ?"); | |
161 | $sth->execute([$ext_id, $owner_uid]); | |
87d7e850 | 162 | |
87d7e850 AD |
163 | /* Remove cached data */ |
164 | ||
c1c08aed AD |
165 | $sth = $pdo->prepare("UPDATE ttrss_user_entries SET label_cache = '' |
166 | WHERE label_cache LIKE ? AND owner_uid = ?"); | |
167 | $sth->execute(["%$caption%", $owner_uid]); | |
87d7e850 AD |
168 | |
169 | } | |
170 | ||
c949a928 | 171 | if (!$tr_in_progress) $pdo->commit(); |
87d7e850 AD |
172 | } |
173 | ||
7c9b5a3f | 174 | static function create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) { |
87d7e850 AD |
175 | |
176 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
177 | ||
c1c08aed AD |
178 | $pdo = Db::pdo(); |
179 | ||
c949a928 AD |
180 | $tr_in_progress = false; |
181 | ||
182 | try { | |
183 | $pdo->beginTransaction(); | |
184 | } catch (Exception $e) { | |
185 | $tr_in_progress = true; | |
186 | } | |
c1c08aed AD |
187 | |
188 | $sth = $pdo->prepare("SELECT id FROM ttrss_labels2 | |
189 | WHERE caption = ? AND owner_uid = ?"); | |
190 | $sth->execute([$caption, $owner_uid]); | |
87d7e850 | 191 | |
c1c08aed | 192 | if (!$sth->fetch()) { |
3a029230 | 193 | $sth = $pdo->prepare("INSERT INTO ttrss_labels2 |
c1c08aed | 194 | (caption,owner_uid,fg_color,bg_color) VALUES (?, ?, ?, ?)"); |
87d7e850 | 195 | |
c1c08aed | 196 | $sth->execute([$caption, $owner_uid, $fg_color, $bg_color]); |
87d7e850 | 197 | |
c1c08aed | 198 | $result = $sth->rowCount(); |
87d7e850 AD |
199 | } |
200 | ||
c949a928 | 201 | if (!$tr_in_progress) $pdo->commit(); |
87d7e850 AD |
202 | |
203 | return $result; | |
204 | } | |
7c9b5a3f | 205 | } |