]> git.wh0rd.org - tt-rss.git/blob - classes/labels.php
labels: PDO
[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 = db_escape_string(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
135 $pdo->beginTransaction();
136
137 $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2
138 WHERE id = ?");
139 $sth->execute([$id]);
140
141 $row = $sth->fetch();
142 $caption = $row['caption'];
143
144 $sth = $pdo->prepare("DELETE FROM ttrss_labels2 WHERE id = ?
145 AND owner_uid = ?");
146 $sth->execute([$id, $owner_uid]);
147
148 if ($sth->rowCount() != 0 && $caption) {
149
150 /* Remove access key for the label */
151
152 $ext_id = LABEL_BASE_INDEX - 1 - $id;
153
154 $sth = $pdo->prepare("DELETE FROM ttrss_access_keys WHERE
155 feed_id = ? AND owner_uid = ?");
156 $sth->execute([$ext_id, $owner_uid]);
157
158 /* Remove cached data */
159
160 $sth = $pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
161 WHERE label_cache LIKE ? AND owner_uid = ?");
162 $sth->execute(["%$caption%", $owner_uid]);
163
164 }
165
166 $pdo->commit();
167 }
168
169 static function create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
170
171 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
172
173 $pdo = Db::pdo();
174
175 $pdo->beginTransaction();
176
177 $sth = $pdo->prepare("SELECT id FROM ttrss_labels2
178 WHERE caption = ? AND owner_uid = ?");
179 $sth->execute([$caption, $owner_uid]);
180
181 if (!$sth->fetch()) {
182 $sth = $pdo->prepare("INSERT INTO ttrss_labels2
183 (caption,owner_uid,fg_color,bg_color) VALUES (?, ?, ?, ?)");
184
185 $sth->execute([$caption, $owner_uid, $fg_color, $bg_color]);
186
187 $result = $sth->rowCount();
188 }
189
190 $pdo->commit();
191
192 return $result;
193 }
194 }