]> git.wh0rd.org Git - tt-rss.git/blob - include/labels.php
another implementation of pull 386 (show next when finished reading) (2)
[tt-rss.git] / include / labels.php
1 <?php
2         function label_find_id($label, $owner_uid) {
3                 $result = db_query(
4                         "SELECT id FROM ttrss_labels2 WHERE caption = '$label'
5                                 AND owner_uid = '$owner_uid' LIMIT 1");
6
7                 if (db_num_rows($result) == 1) {
8                         return db_fetch_result($result, 0, "id");
9                 } else {
10                         return 0;
11                 }
12         }
13
14         function get_article_labels($id, $owner_uid = false) {
15                 $rv = array();
16
17                 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
18
19                 $result = db_query("SELECT label_cache FROM
20                         ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
21                         $owner_uid);
22
23                 if (db_num_rows($result) > 0) {
24                         $label_cache = db_fetch_result($result, 0, "label_cache");
25
26                         if ($label_cache) {
27                                 $label_cache = json_decode($label_cache, true);
28
29                                 if ($label_cache["no-labels"] == 1)
30                                         return $rv;
31                                 else
32                                         return $label_cache;
33                         }
34                 }
35
36                 $result = db_query(
37                         "SELECT DISTINCT label_id,caption,fg_color,bg_color
38                                 FROM ttrss_labels2, ttrss_user_labels2
39                         WHERE id = label_id
40                                 AND article_id = '$id'
41                                 AND owner_uid = ". $owner_uid . "
42                         ORDER BY caption");
43
44                 while ($line = db_fetch_assoc($result)) {
45                         $rk = array(label_to_feed_id($line["label_id"]),
46                                 $line["caption"], $line["fg_color"],
47                                 $line["bg_color"]);
48                         array_push($rv, $rk);
49                 }
50
51                 if (count($rv) > 0)
52                         label_update_cache($owner_uid, $id, $rv);
53                 else
54                         label_update_cache($owner_uid, $id, array("no-labels" => 1));
55
56                 return $rv;
57         }
58
59
60         function label_find_caption($label, $owner_uid) {
61                 $result = db_query(
62                         "SELECT caption FROM ttrss_labels2 WHERE id = '$label'
63                                 AND owner_uid = '$owner_uid' LIMIT 1");
64
65                 if (db_num_rows($result) == 1) {
66                         return db_fetch_result($result, 0, "caption");
67                 } else {
68                         return "";
69                 }
70         }
71
72         function get_all_labels($owner_uid) {
73                 $rv = array();
74
75                 $result = db_query("SELECT fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = " . $owner_uid);
76
77                 while ($line = db_fetch_assoc($result)) {
78                         array_push($rv, $line);
79                 }
80
81                 return $rv;
82         }
83
84         function label_update_cache($owner_uid, $id, $labels = false, $force = false) {
85
86                 if ($force)
87                         label_clear_cache($id);
88
89                 if (!$labels)
90                         $labels = get_article_labels($id);
91
92                 $labels = db_escape_string(json_encode($labels));
93
94                 db_query("UPDATE ttrss_user_entries SET
95                         label_cache = '$labels' WHERE ref_id = '$id' AND  owner_uid = '$owner_uid'");
96
97         }
98
99         function label_clear_cache($id) {
100
101                 db_query("UPDATE ttrss_user_entries SET
102                         label_cache = '' WHERE ref_id = '$id'");
103
104         }
105
106         function label_remove_article($id, $label, $owner_uid) {
107
108                 $label_id = label_find_id($label, $owner_uid);
109
110                 if (!$label_id) return;
111
112                 db_query(
113                         "DELETE FROM ttrss_user_labels2
114                         WHERE
115                                 label_id = '$label_id' AND
116                                 article_id = '$id'");
117
118                 label_clear_cache($id);
119         }
120
121         function label_add_article($id, $label, $owner_uid) {
122
123                 $label_id = label_find_id($label, $owner_uid);
124
125                 if (!$label_id) return;
126
127                 $result = db_query(
128                         "SELECT
129                                 article_id FROM ttrss_labels2, ttrss_user_labels2
130                         WHERE
131                                 label_id = id AND
132                                 label_id = '$label_id' AND
133                                 article_id = '$id' AND owner_uid = '$owner_uid'
134                         LIMIT 1");
135
136                 if (db_num_rows($result) == 0) {
137                         db_query("INSERT INTO ttrss_user_labels2
138                                 (label_id, article_id) VALUES ('$label_id', '$id')");
139                 }
140
141                 label_clear_cache($id);
142
143         }
144
145         function label_remove($id, $owner_uid) {
146                 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
147
148                 db_query("BEGIN");
149
150                 $result = db_query("SELECT caption FROM ttrss_labels2
151                         WHERE id = '$id'");
152
153                 $caption = db_fetch_result($result, 0, "caption");
154
155                 $result = db_query("DELETE FROM ttrss_labels2 WHERE id = '$id'
156                         AND owner_uid = " . $owner_uid);
157
158                 if (db_affected_rows($result) != 0 && $caption) {
159
160                         /* Remove access key for the label */
161
162                         $ext_id = LABEL_BASE_INDEX - 1 - $id;
163
164                         db_query("DELETE FROM ttrss_access_keys WHERE
165                                 feed_id = '$ext_id' AND owner_uid = $owner_uid");
166
167                         /* Remove cached data */
168
169                         db_query("UPDATE ttrss_user_entries SET label_cache = ''
170                                 WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
171
172                 }
173
174                 db_query("COMMIT");
175         }
176
177         function label_create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
178
179                 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
180
181                 db_query("BEGIN");
182
183                 $result = false;
184
185                 $result = db_query("SELECT id FROM ttrss_labels2
186                         WHERE caption = '$caption' AND owner_uid = $owner_uid");
187
188                 if (db_num_rows($result) == 0) {
189                         $result = db_query(
190                                 "INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color)
191                                         VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')");
192
193                         $result = db_affected_rows($result) != 0;
194                 }
195
196                 db_query("COMMIT");
197
198                 return $result;
199         }
200
201 ?>