2013-01-22 19:32:17 +01:00
|
|
|
<?php
|
2013-04-17 16:34:18 +02:00
|
|
|
function label_find_id($label, $owner_uid) {
|
2013-04-17 14:23:15 +02:00
|
|
|
$result = db_query(
|
2013-01-22 19:32:17 +01:00
|
|
|
"SELECT id FROM ttrss_labels2 WHERE caption = '$label'
|
|
|
|
AND owner_uid = '$owner_uid' LIMIT 1");
|
|
|
|
|
|
|
|
if (db_num_rows($result) == 1) {
|
|
|
|
return db_fetch_result($result, 0, "id");
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function get_article_labels($id, $owner_uid = false) {
|
2013-01-22 19:32:17 +01:00
|
|
|
$rv = array();
|
|
|
|
|
|
|
|
if (!$owner_uid) $owner_uid = $_SESSION["uid"];
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$result = db_query("SELECT label_cache FROM
|
2013-01-22 19:32:17 +01:00
|
|
|
ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
|
|
|
|
$owner_uid);
|
|
|
|
|
|
|
|
if (db_num_rows($result) > 0) {
|
|
|
|
$label_cache = db_fetch_result($result, 0, "label_cache");
|
|
|
|
|
|
|
|
if ($label_cache) {
|
|
|
|
$label_cache = json_decode($label_cache, true);
|
|
|
|
|
|
|
|
if ($label_cache["no-labels"] == 1)
|
|
|
|
return $rv;
|
|
|
|
else
|
|
|
|
return $label_cache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 14:23:15 +02:00
|
|
|
$result = db_query(
|
2013-01-22 19:32:17 +01:00
|
|
|
"SELECT DISTINCT label_id,caption,fg_color,bg_color
|
|
|
|
FROM ttrss_labels2, ttrss_user_labels2
|
|
|
|
WHERE id = label_id
|
|
|
|
AND article_id = '$id'
|
|
|
|
AND owner_uid = ". $owner_uid . "
|
|
|
|
ORDER BY caption");
|
|
|
|
|
|
|
|
while ($line = db_fetch_assoc($result)) {
|
2013-07-28 21:36:57 +02:00
|
|
|
$rk = array(label_to_feed_id($line["label_id"]),
|
|
|
|
$line["caption"], $line["fg_color"],
|
2013-01-22 19:32:17 +01:00
|
|
|
$line["bg_color"]);
|
|
|
|
array_push($rv, $rk);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($rv) > 0)
|
2013-04-17 16:34:18 +02:00
|
|
|
label_update_cache($owner_uid, $id, $rv);
|
2013-01-22 19:32:17 +01:00
|
|
|
else
|
2013-04-17 16:34:18 +02:00
|
|
|
label_update_cache($owner_uid, $id, array("no-labels" => 1));
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
return $rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function label_find_caption($label, $owner_uid) {
|
2013-04-17 14:23:15 +02:00
|
|
|
$result = db_query(
|
2013-01-22 19:32:17 +01:00
|
|
|
"SELECT caption FROM ttrss_labels2 WHERE id = '$label'
|
|
|
|
AND owner_uid = '$owner_uid' LIMIT 1");
|
|
|
|
|
|
|
|
if (db_num_rows($result) == 1) {
|
|
|
|
return db_fetch_result($result, 0, "caption");
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function get_all_labels($owner_uid) {
|
2013-01-22 19:32:17 +01:00
|
|
|
$rv = array();
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$result = db_query("SELECT fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = " . $owner_uid);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
while ($line = db_fetch_assoc($result)) {
|
|
|
|
array_push($rv, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rv;
|
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function label_update_cache($owner_uid, $id, $labels = false, $force = false) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if ($force)
|
2013-04-17 16:34:18 +02:00
|
|
|
label_clear_cache($id);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if (!$labels)
|
2013-04-17 16:34:18 +02:00
|
|
|
$labels = get_article_labels($id);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$labels = db_escape_string(json_encode($labels));
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("UPDATE ttrss_user_entries SET
|
2013-01-22 19:32:17 +01:00
|
|
|
label_cache = '$labels' WHERE ref_id = '$id' AND owner_uid = '$owner_uid'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function label_clear_cache($id) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("UPDATE ttrss_user_entries SET
|
2013-01-22 19:32:17 +01:00
|
|
|
label_cache = '' WHERE ref_id = '$id'");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function label_remove_article($id, $label, $owner_uid) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$label_id = label_find_id($label, $owner_uid);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if (!$label_id) return;
|
|
|
|
|
2014-02-19 12:42:52 +01:00
|
|
|
db_query(
|
2013-01-22 19:32:17 +01:00
|
|
|
"DELETE FROM ttrss_user_labels2
|
|
|
|
WHERE
|
|
|
|
label_id = '$label_id' AND
|
|
|
|
article_id = '$id'");
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
label_clear_cache($id);
|
2013-01-22 19:32:17 +01:00
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function label_add_article($id, $label, $owner_uid) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$label_id = label_find_id($label, $owner_uid);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if (!$label_id) return;
|
|
|
|
|
2013-04-17 14:23:15 +02:00
|
|
|
$result = db_query(
|
2013-01-22 19:32:17 +01:00
|
|
|
"SELECT
|
|
|
|
article_id FROM ttrss_labels2, ttrss_user_labels2
|
|
|
|
WHERE
|
|
|
|
label_id = id AND
|
|
|
|
label_id = '$label_id' AND
|
|
|
|
article_id = '$id' AND owner_uid = '$owner_uid'
|
|
|
|
LIMIT 1");
|
|
|
|
|
|
|
|
if (db_num_rows($result) == 0) {
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("INSERT INTO ttrss_user_labels2
|
2013-01-22 19:32:17 +01:00
|
|
|
(label_id, article_id) VALUES ('$label_id', '$id')");
|
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
label_clear_cache($id);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function label_remove($id, $owner_uid) {
|
2013-01-22 19:32:17 +01:00
|
|
|
if (!$owner_uid) $owner_uid = $_SESSION["uid"];
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("BEGIN");
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$result = db_query("SELECT caption FROM ttrss_labels2
|
2013-01-22 19:32:17 +01:00
|
|
|
WHERE id = '$id'");
|
|
|
|
|
|
|
|
$caption = db_fetch_result($result, 0, "caption");
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$result = db_query("DELETE FROM ttrss_labels2 WHERE id = '$id'
|
2013-01-22 19:32:17 +01:00
|
|
|
AND owner_uid = " . $owner_uid);
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
if (db_affected_rows($result) != 0 && $caption) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
/* Remove access key for the label */
|
|
|
|
|
2013-03-27 06:40:07 +01:00
|
|
|
$ext_id = LABEL_BASE_INDEX - 1 - $id;
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("DELETE FROM ttrss_access_keys WHERE
|
2013-01-22 19:32:17 +01:00
|
|
|
feed_id = '$ext_id' AND owner_uid = $owner_uid");
|
|
|
|
|
|
|
|
/* Remove cached data */
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("UPDATE ttrss_user_entries SET label_cache = ''
|
2013-01-22 19:32:17 +01:00
|
|
|
WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("COMMIT");
|
2013-01-22 19:32:17 +01:00
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
function label_create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if (!$owner_uid) $owner_uid = $_SESSION['uid'];
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("BEGIN");
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
$result = false;
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$result = db_query("SELECT id FROM ttrss_labels2
|
2013-01-22 19:32:17 +01:00
|
|
|
WHERE caption = '$caption' AND owner_uid = $owner_uid");
|
|
|
|
|
|
|
|
if (db_num_rows($result) == 0) {
|
2013-04-17 14:23:15 +02:00
|
|
|
$result = db_query(
|
2013-01-22 19:32:17 +01:00
|
|
|
"INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color)
|
|
|
|
VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')");
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
$result = db_affected_rows($result) != 0;
|
2013-01-22 19:32:17 +01:00
|
|
|
}
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
db_query("COMMIT");
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|