]> git.wh0rd.org - tt-rss.git/blob - classes/labels.php
c46f70c946da6087605123040c7733114541e6d6
[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 $result = db_query(
14 "SELECT id FROM ttrss_labels2 WHERE caption = '$label'
15 AND owner_uid = '$owner_uid' LIMIT 1");
16
17 if (db_num_rows($result) == 1) {
18 return db_fetch_result($result, 0, "id");
19 } else {
20 return 0;
21 }
22 }
23
24 static function find_caption($label, $owner_uid) {
25 $result = db_query(
26 "SELECT caption FROM ttrss_labels2 WHERE id = '$label'
27 AND owner_uid = '$owner_uid' LIMIT 1");
28
29 if (db_num_rows($result) == 1) {
30 return db_fetch_result($result, 0, "caption");
31 } else {
32 return "";
33 }
34 }
35
36 static function get_all_labels($owner_uid) {
37 $rv = array();
38
39 $result = db_query("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = '$owner_uid' ORDER BY caption");
40
41 while ($line = db_fetch_assoc($result)) {
42 array_push($rv, $line);
43 }
44
45 return $rv;
46 }
47
48 static function update_cache($owner_uid, $id, $labels = false, $force = false) {
49
50 if ($force)
51 Labels::clear_cache($id);
52
53 if (!$labels)
54 $labels = Article::get_article_labels($id);
55
56 $labels = db_escape_string(json_encode($labels));
57
58 db_query("UPDATE ttrss_user_entries SET
59 label_cache = '$labels' WHERE ref_id = '$id' AND owner_uid = '$owner_uid'");
60
61 }
62
63 static function clear_cache($id) {
64
65 db_query("UPDATE ttrss_user_entries SET
66 label_cache = '' WHERE ref_id = '$id'");
67
68 }
69
70 static function remove_article($id, $label, $owner_uid) {
71
72 $label_id = Labels::find_id($label, $owner_uid);
73
74 if (!$label_id) return;
75
76 db_query(
77 "DELETE FROM ttrss_user_labels2
78 WHERE
79 label_id = '$label_id' AND
80 article_id = '$id'");
81
82 Labels::clear_cache($id);
83 }
84
85 static function add_article($id, $label, $owner_uid) {
86
87 $label_id = Labels::find_id($label, $owner_uid);
88
89 if (!$label_id) return;
90
91 $result = db_query(
92 "SELECT
93 article_id FROM ttrss_labels2, ttrss_user_labels2
94 WHERE
95 label_id = id AND
96 label_id = '$label_id' AND
97 article_id = '$id' AND owner_uid = '$owner_uid'
98 LIMIT 1");
99
100 if (db_num_rows($result) == 0) {
101 db_query("INSERT INTO ttrss_user_labels2
102 (label_id, article_id) VALUES ('$label_id', '$id')");
103 }
104
105 Labels::clear_cache($id);
106
107 }
108
109 static function remove($id, $owner_uid) {
110 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
111
112 db_query("BEGIN");
113
114 $result = db_query("SELECT caption FROM ttrss_labels2
115 WHERE id = '$id'");
116
117 $caption = db_fetch_result($result, 0, "caption");
118
119 $result = db_query("DELETE FROM ttrss_labels2 WHERE id = '$id'
120 AND owner_uid = " . $owner_uid);
121
122 if (db_affected_rows($result) != 0 && $caption) {
123
124 /* Remove access key for the label */
125
126 $ext_id = LABEL_BASE_INDEX - 1 - $id;
127
128 db_query("DELETE FROM ttrss_access_keys WHERE
129 feed_id = '$ext_id' AND owner_uid = $owner_uid");
130
131 /* Remove cached data */
132
133 db_query("UPDATE ttrss_user_entries SET label_cache = ''
134 WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
135
136 }
137
138 db_query("COMMIT");
139 }
140
141 static function create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
142
143 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
144
145 db_query("BEGIN");
146
147 $result = db_query("SELECT id FROM ttrss_labels2
148 WHERE caption = '$caption' AND owner_uid = $owner_uid");
149
150 if (db_num_rows($result) == 0) {
151 $result = db_query(
152 "INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color)
153 VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')");
154
155 $result = db_affected_rows($result) != 0;
156 }
157
158 db_query("COMMIT");
159
160 return $result;
161 }
162 }