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