1
|
|
|
<?php |
2
|
|
|
class Labels |
3
|
|
|
{ |
4
|
|
|
public static function label_to_feed_id($label) { |
5
|
|
|
return LABEL_BASE_INDEX - 1 - abs($label); |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
public static function feed_to_label_id($feed) { |
9
|
|
|
return LABEL_BASE_INDEX - 1 + abs($feed); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public 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
|
|
|
public 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
|
|
|
public 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
|
|
|
public 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
|
|
|
|
63
|
|
|
if (!$labels) { |
64
|
|
|
$labels = Article::get_article_labels($id); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$labels = json_encode($labels); |
68
|
|
|
|
69
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET |
70
|
|
|
label_cache = ? WHERE ref_id = ? AND owner_uid = ?"); |
71
|
|
|
$sth->execute([$labels, $id, $owner_uid]); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function clear_cache($id) { |
76
|
|
|
|
77
|
|
|
$pdo = Db::pdo(); |
78
|
|
|
|
79
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET |
80
|
|
|
label_cache = '' WHERE ref_id = ?"); |
81
|
|
|
$sth->execute([$id]); |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function remove_article($id, $label, $owner_uid) { |
86
|
|
|
|
87
|
|
|
$label_id = Labels::find_id($label, $owner_uid); |
88
|
|
|
|
89
|
|
|
if (!$label_id) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$pdo = Db::pdo(); |
94
|
|
|
|
95
|
|
|
$sth = $pdo->prepare("DELETE FROM ttrss_user_labels2 |
96
|
|
|
WHERE |
97
|
|
|
label_id = ? AND |
98
|
|
|
article_id = ?"); |
99
|
|
|
|
100
|
|
|
$sth->execute([$label_id, $id]); |
101
|
|
|
|
102
|
|
|
Labels::clear_cache($id); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function add_article($id, $label, $owner_uid) { |
106
|
|
|
|
107
|
|
|
$label_id = Labels::find_id($label, $owner_uid); |
108
|
|
|
|
109
|
|
|
if (!$label_id) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$pdo = Db::pdo(); |
114
|
|
|
|
115
|
|
|
$sth = $pdo->prepare("SELECT |
116
|
|
|
article_id FROM ttrss_labels2, ttrss_user_labels2 |
117
|
|
|
WHERE |
118
|
|
|
label_id = id AND |
119
|
|
|
label_id = ? AND |
120
|
|
|
article_id = ? AND owner_uid = ? |
121
|
|
|
LIMIT 1"); |
122
|
|
|
|
123
|
|
|
$sth->execute([$label_id, $id, $owner_uid]); |
124
|
|
|
|
125
|
|
|
if (!$sth->fetch()) { |
126
|
|
|
$sth = $pdo->prepare("INSERT INTO ttrss_user_labels2 |
127
|
|
|
(label_id, article_id) VALUES (?, ?)"); |
128
|
|
|
|
129
|
|
|
$sth->execute([$label_id, $id]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
Labels::clear_cache($id); |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public static function remove($id, $owner_uid) { |
137
|
|
|
if (!$owner_uid) { |
138
|
|
|
$owner_uid = $_SESSION["uid"]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$pdo = Db::pdo(); |
142
|
|
|
$tr_in_progress = false; |
143
|
|
|
|
144
|
|
|
try { |
145
|
|
|
$pdo->beginTransaction(); |
146
|
|
|
} catch (Exception $e) { |
147
|
|
|
$tr_in_progress = true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 |
151
|
|
|
WHERE id = ?"); |
152
|
|
|
$sth->execute([$id]); |
153
|
|
|
|
154
|
|
|
$row = $sth->fetch(); |
155
|
|
|
$caption = $row['caption']; |
156
|
|
|
|
157
|
|
|
$sth = $pdo->prepare("DELETE FROM ttrss_labels2 WHERE id = ? |
158
|
|
|
AND owner_uid = ?"); |
159
|
|
|
$sth->execute([$id, $owner_uid]); |
160
|
|
|
|
161
|
|
|
if ($sth->rowCount() != 0 && $caption) { |
162
|
|
|
|
163
|
|
|
/* Remove access key for the label */ |
164
|
|
|
|
165
|
|
|
$ext_id = LABEL_BASE_INDEX - 1 - $id; |
166
|
|
|
|
167
|
|
|
$sth = $pdo->prepare("DELETE FROM ttrss_access_keys WHERE |
168
|
|
|
feed_id = ? AND owner_uid = ?"); |
169
|
|
|
$sth->execute([$ext_id, $owner_uid]); |
170
|
|
|
|
171
|
|
|
/* Remove cached data */ |
172
|
|
|
|
173
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET label_cache = '' |
174
|
|
|
WHERE owner_uid = ?"); |
175
|
|
|
$sth->execute([$owner_uid]); |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (!$tr_in_progress) { |
180
|
|
|
$pdo->commit(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public static function create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) { |
185
|
|
|
|
186
|
|
|
if (!$owner_uid) { |
187
|
|
|
$owner_uid = $_SESSION['uid']; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$pdo = Db::pdo(); |
191
|
|
|
|
192
|
|
|
$tr_in_progress = false; |
193
|
|
|
|
194
|
|
|
try { |
195
|
|
|
$pdo->beginTransaction(); |
196
|
|
|
} catch (Exception $e) { |
197
|
|
|
$tr_in_progress = true; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$sth = $pdo->prepare("SELECT id FROM ttrss_labels2 |
201
|
|
|
WHERE caption = ? AND owner_uid = ?"); |
202
|
|
|
$sth->execute([$caption, $owner_uid]); |
203
|
|
|
|
204
|
|
|
if (!$sth->fetch()) { |
205
|
|
|
$sth = $pdo->prepare("INSERT INTO ttrss_labels2 |
206
|
|
|
(caption,owner_uid,fg_color,bg_color) VALUES (?, ?, ?, ?)"); |
207
|
|
|
|
208
|
|
|
$sth->execute([$caption, $owner_uid, $fg_color, $bg_color]); |
209
|
|
|
|
210
|
|
|
$result = $sth->rowCount(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (!$tr_in_progress) { |
214
|
|
|
$pdo->commit(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $result; |
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|