2 function label_find_id($label, $owner_uid) {
4 "SELECT id FROM ttrss_labels2 WHERE caption = '$label'
5 AND owner_uid = '$owner_uid' LIMIT 1");
7 if (db_num_rows($result) == 1) {
8 return db_fetch_result($result, 0, "id");
14 function get_article_labels($id, $owner_uid = false) {
17 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
19 $result = db_query("SELECT label_cache FROM
20 ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
23 if (db_num_rows($result) > 0) {
24 $label_cache = db_fetch_result($result, 0, "label_cache");
27 $label_cache = json_decode($label_cache, true);
29 if ($label_cache["no-labels"] == 1)
37 "SELECT DISTINCT label_id,caption,fg_color,bg_color
38 FROM ttrss_labels2, ttrss_user_labels2
40 AND article_id = '$id'
41 AND owner_uid = ". $owner_uid . "
44 while ($line = db_fetch_assoc($result)) {
45 $rk = array(label_to_feed_id($line["label_id"]),
46 $line["caption"], $line["fg_color"],
52 label_update_cache($owner_uid, $id, $rv);
54 label_update_cache($owner_uid, $id, array("no-labels" => 1));
60 function label_find_caption($label, $owner_uid) {
62 "SELECT caption FROM ttrss_labels2 WHERE id = '$label'
63 AND owner_uid = '$owner_uid' LIMIT 1");
65 if (db_num_rows($result) == 1) {
66 return db_fetch_result($result, 0, "caption");
72 function get_all_labels($owner_uid) {
75 $result = db_query("SELECT fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = " . $owner_uid);
77 while ($line = db_fetch_assoc($result)) {
78 array_push($rv, $line);
84 function label_update_cache($owner_uid, $id, $labels = false, $force = false) {
87 label_clear_cache($id);
90 $labels = get_article_labels($id);
92 $labels = db_escape_string(json_encode($labels));
94 db_query("UPDATE ttrss_user_entries SET
95 label_cache = '$labels' WHERE ref_id = '$id' AND owner_uid = '$owner_uid'");
99 function label_clear_cache($id) {
101 db_query("UPDATE ttrss_user_entries SET
102 label_cache = '' WHERE ref_id = '$id'");
106 function label_remove_article($id, $label, $owner_uid) {
108 $label_id = label_find_id($label, $owner_uid);
110 if (!$label_id) return;
113 "DELETE FROM ttrss_user_labels2
115 label_id = '$label_id' AND
116 article_id = '$id'");
118 label_clear_cache($id);
121 function label_add_article($id, $label, $owner_uid) {
123 $label_id = label_find_id($label, $owner_uid);
125 if (!$label_id) return;
129 article_id FROM ttrss_labels2, ttrss_user_labels2
132 label_id = '$label_id' AND
133 article_id = '$id' AND owner_uid = '$owner_uid'
136 if (db_num_rows($result) == 0) {
137 db_query("INSERT INTO ttrss_user_labels2
138 (label_id, article_id) VALUES ('$label_id', '$id')");
141 label_clear_cache($id);
145 function label_remove($id, $owner_uid) {
146 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
150 $result = db_query("SELECT caption FROM ttrss_labels2
153 $caption = db_fetch_result($result, 0, "caption");
155 $result = db_query("DELETE FROM ttrss_labels2 WHERE id = '$id'
156 AND owner_uid = " . $owner_uid);
158 if (db_affected_rows($result) != 0 && $caption) {
160 /* Remove access key for the label */
162 $ext_id = LABEL_BASE_INDEX - 1 - $id;
164 db_query("DELETE FROM ttrss_access_keys WHERE
165 feed_id = '$ext_id' AND owner_uid = $owner_uid");
167 /* Remove cached data */
169 db_query("UPDATE ttrss_user_entries SET label_cache = ''
170 WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
177 function label_create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
179 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
185 $result = db_query("SELECT id FROM ttrss_labels2
186 WHERE caption = '$caption' AND owner_uid = $owner_uid");
188 if (db_num_rows($result) == 0) {
190 "INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color)
191 VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')");
193 $result = db_affected_rows($result) != 0;