]>
git.wh0rd.org - tt-rss.git/blob - classes/labels.php
4 static function label_to_feed_id($label) {
5 return LABEL_BASE_INDEX
- 1 - abs($label);
8 static function feed_to_label_id($feed) {
9 return LABEL_BASE_INDEX
- 1 +
abs($feed);
12 static function find_id($label, $owner_uid) {
15 $sth = $pdo->prepare("SELECT id FROM ttrss_labels2 WHERE caption = ?
16 AND owner_uid = ? LIMIT 1");
17 $sth->execute([$label, $owner_uid]);
19 if ($row = $sth->fetch()) {
26 static function find_caption($label, $owner_uid) {
29 $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 WHERE id = ?
30 AND owner_uid = ? LIMIT 1");
31 $sth->execute([$label, $owner_uid]);
33 if ($row = $sth->fetch()) {
34 return $row['caption'];
40 static function get_all_labels($owner_uid) {
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]);
49 while ($line = $sth->fetch()) {
50 array_push($rv, $line);
56 static function update_cache($owner_uid, $id, $labels = false, $force = false) {
60 Labels
::clear_cache($id);
63 $labels = Article
::get_article_labels($id);
65 $labels = json_encode($labels);
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]);
73 static function clear_cache($id) {
77 $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
78 label_cache = '' WHERE ref_id = ?");
83 static function remove_article($id, $label, $owner_uid) {
85 $label_id = Labels
::find_id($label, $owner_uid);
87 if (!$label_id) return;
91 $sth = $pdo->prepare("DELETE FROM ttrss_user_labels2
96 $sth->execute([$label_id, $id]);
98 Labels
::clear_cache($id);
101 static function add_article($id, $label, $owner_uid) {
103 $label_id = Labels
::find_id($label, $owner_uid);
105 if (!$label_id) return;
109 $sth = $pdo->prepare("SELECT
110 article_id FROM ttrss_labels2, ttrss_user_labels2
114 article_id = ? AND owner_uid = ?
117 $sth->execute([$label_id, $id, $owner_uid]);
119 if (!$sth->fetch()) {
120 $sth = $pdo->prepare("INSERT INTO ttrss_user_labels2
121 (label_id, article_id) VALUES (?, ?)");
123 $sth->execute([$label_id, $id]);
126 Labels
::clear_cache($id);
130 static function remove($id, $owner_uid) {
131 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
134 $tr_in_progress = false;
137 $pdo->beginTransaction();
138 } catch (Exception
$e) {
139 $tr_in_progress = true;
142 $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2
144 $sth->execute([$id]);
146 $row = $sth->fetch();
147 $caption = $row['caption'];
149 $sth = $pdo->prepare("DELETE FROM ttrss_labels2 WHERE id = ?
151 $sth->execute([$id, $owner_uid]);
153 if ($sth->rowCount() != 0 && $caption) {
155 /* Remove access key for the label */
157 $ext_id = LABEL_BASE_INDEX
- 1 - $id;
159 $sth = $pdo->prepare("DELETE FROM ttrss_access_keys WHERE
160 feed_id = ? AND owner_uid = ?");
161 $sth->execute([$ext_id, $owner_uid]);
163 /* Remove cached data */
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]);
171 if (!$tr_in_progress) $pdo->commit();
174 static function create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
176 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
180 $tr_in_progress = false;
183 $pdo->beginTransaction();
184 } catch (Exception
$e) {
185 $tr_in_progress = true;
188 $sth = $pdo->prepare("SELECT id FROM ttrss_labels2
189 WHERE caption = ? AND owner_uid = ?");
190 $sth->execute([$caption, $owner_uid]);
192 if (!$sth->fetch()) {
193 $sth = $pdo->prepare("INSERT INTO ttrss_labels2
194 (caption,owner_uid,fg_color,bg_color) VALUES (?, ?, ?, ?)");
196 $sth->execute([$caption, $owner_uid, $fg_color, $bg_color]);
198 $result = $sth->rowCount();
201 if (!$tr_in_progress) $pdo->commit();