]> git.wh0rd.org - tt-rss.git/blob - include/labels.php
remove $link
[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($line["label_id"], $line["caption"], $line["fg_color"],
46 $line["bg_color"]);
47 array_push($rv, $rk);
48 }
49
50 if (count($rv) > 0)
51 label_update_cache( $owner_uid, $id, $rv);
52 else
53 label_update_cache( $owner_uid, $id, array("no-labels" => 1));
54
55 return $rv;
56 }
57
58
59 function label_find_caption( $label, $owner_uid) {
60 $result = db_query(
61 "SELECT caption FROM ttrss_labels2 WHERE id = '$label'
62 AND owner_uid = '$owner_uid' LIMIT 1");
63
64 if (db_num_rows($result) == 1) {
65 return db_fetch_result($result, 0, "caption");
66 } else {
67 return "";
68 }
69 }
70
71 function get_all_labels( $owner_uid) {
72 $rv = array();
73
74 $result = db_query( "SELECT fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = " . $owner_uid);
75
76 while ($line = db_fetch_assoc($result)) {
77 array_push($rv, $line);
78 }
79
80 return $rv;
81 }
82
83 function label_update_cache( $owner_uid, $id, $labels = false, $force = false) {
84
85 if ($force)
86 label_clear_cache( $id);
87
88 if (!$labels)
89 $labels = get_article_labels( $id);
90
91 $labels = db_escape_string( json_encode($labels));
92
93 db_query( "UPDATE ttrss_user_entries SET
94 label_cache = '$labels' WHERE ref_id = '$id' AND owner_uid = '$owner_uid'");
95
96 }
97
98 function label_clear_cache( $id) {
99
100 db_query( "UPDATE ttrss_user_entries SET
101 label_cache = '' WHERE ref_id = '$id'");
102
103 }
104
105 function label_remove_article( $id, $label, $owner_uid) {
106
107 $label_id = label_find_id( $label, $owner_uid);
108
109 if (!$label_id) return;
110
111 $result = db_query(
112 "DELETE FROM ttrss_user_labels2
113 WHERE
114 label_id = '$label_id' AND
115 article_id = '$id'");
116
117 label_clear_cache( $id);
118 }
119
120 function label_add_article( $id, $label, $owner_uid) {
121
122 $label_id = label_find_id( $label, $owner_uid);
123
124 if (!$label_id) return;
125
126 $result = db_query(
127 "SELECT
128 article_id FROM ttrss_labels2, ttrss_user_labels2
129 WHERE
130 label_id = id AND
131 label_id = '$label_id' AND
132 article_id = '$id' AND owner_uid = '$owner_uid'
133 LIMIT 1");
134
135 if (db_num_rows($result) == 0) {
136 db_query( "INSERT INTO ttrss_user_labels2
137 (label_id, article_id) VALUES ('$label_id', '$id')");
138 }
139
140 label_clear_cache( $id);
141
142 }
143
144 function label_remove( $id, $owner_uid) {
145 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
146
147 db_query( "BEGIN");
148
149 $result = db_query( "SELECT caption FROM ttrss_labels2
150 WHERE id = '$id'");
151
152 $caption = db_fetch_result($result, 0, "caption");
153
154 $result = db_query( "DELETE FROM ttrss_labels2 WHERE id = '$id'
155 AND owner_uid = " . $owner_uid);
156
157 if (db_affected_rows( $result) != 0 && $caption) {
158
159 /* Remove access key for the label */
160
161 $ext_id = LABEL_BASE_INDEX - 1 - $id;
162
163 db_query( "DELETE FROM ttrss_access_keys WHERE
164 feed_id = '$ext_id' AND owner_uid = $owner_uid");
165
166 /* Remove cached data */
167
168 db_query( "UPDATE ttrss_user_entries SET label_cache = ''
169 WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
170
171 }
172
173 db_query( "COMMIT");
174 }
175
176 function label_create( $caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
177
178 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
179
180 db_query( "BEGIN");
181
182 $result = false;
183
184 $result = db_query( "SELECT id FROM ttrss_labels2
185 WHERE caption = '$caption' AND owner_uid = $owner_uid");
186
187 if (db_num_rows($result) == 0) {
188 $result = db_query(
189 "INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color)
190 VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')");
191
192 $result = db_affected_rows( $result) != 0;
193 }
194
195 db_query( "COMMIT");
196
197 return $result;
198 }
199
200 ?>