2013-01-22 19:32:17 +01:00
|
|
|
<?php
|
2017-05-04 14:57:40 +02:00
|
|
|
class Labels
|
|
|
|
{
|
|
|
|
static function label_to_feed_id($label) {
|
2017-05-04 14:36:36 +02:00
|
|
|
return LABEL_BASE_INDEX - 1 - abs($label);
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function feed_to_label_id($feed) {
|
2017-05-04 14:36:36 +02:00
|
|
|
return LABEL_BASE_INDEX - 1 + abs($feed);
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function find_id($label, $owner_uid) {
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$sth = $pdo->prepare("SELECT id FROM ttrss_labels2 WHERE caption = ?
|
|
|
|
AND owner_uid = ? LIMIT 1");
|
|
|
|
$sth->execute([$label, $owner_uid]);
|
|
|
|
|
|
|
|
if ($row = $sth->fetch()) {
|
|
|
|
return $row['id'];
|
2013-01-22 19:32:17 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function find_caption($label, $owner_uid) {
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 WHERE id = ?
|
|
|
|
AND owner_uid = ? LIMIT 1");
|
|
|
|
$sth->execute([$label, $owner_uid]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
if ($row = $sth->fetch()) {
|
|
|
|
return $row['caption'];
|
2013-01-22 19:32:17 +01:00
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function get_all_labels($owner_uid) {
|
2013-01-22 19:32:17 +01:00
|
|
|
$rv = array();
|
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2
|
|
|
|
WHERE owner_uid = ? ORDER BY caption");
|
|
|
|
$sth->execute([$owner_uid]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
while ($line = $sth->fetch()) {
|
2013-01-22 19:32:17 +01:00
|
|
|
array_push($rv, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rv;
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function update_cache($owner_uid, $id, $labels = false, $force = false) {
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if ($force)
|
2017-05-04 14:57:40 +02:00
|
|
|
Labels::clear_cache($id);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if (!$labels)
|
2017-05-04 14:57:40 +02:00
|
|
|
$labels = Article::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
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET
|
|
|
|
label_cache = ? WHERE ref_id = ? AND owner_uid = ?");
|
|
|
|
$sth->execute([$labels, $id, $owner_uid]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function clear_cache($id) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET
|
|
|
|
label_cache = '' WHERE ref_id = ?");
|
|
|
|
$sth->execute([$id]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function remove_article($id, $label, $owner_uid) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
$label_id = Labels::find_id($label, $owner_uid);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if (!$label_id) return;
|
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("DELETE FROM ttrss_user_labels2
|
2013-01-22 19:32:17 +01:00
|
|
|
WHERE
|
2017-12-01 22:06:17 +01:00
|
|
|
label_id = ? AND
|
|
|
|
article_id = ?");
|
|
|
|
|
|
|
|
$sth->execute([$label_id, $id]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
Labels::clear_cache($id);
|
2013-01-22 19:32:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function add_article($id, $label, $owner_uid) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
$label_id = Labels::find_id($label, $owner_uid);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if (!$label_id) return;
|
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT
|
2013-01-22 19:32:17 +01:00
|
|
|
article_id FROM ttrss_labels2, ttrss_user_labels2
|
|
|
|
WHERE
|
|
|
|
label_id = id AND
|
2017-12-01 22:06:17 +01:00
|
|
|
label_id = ? AND
|
|
|
|
article_id = ? AND owner_uid = ?
|
2013-01-22 19:32:17 +01:00
|
|
|
LIMIT 1");
|
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$sth->execute([$label_id, $id, $owner_uid]);
|
|
|
|
|
|
|
|
if (!$sth->fetch()) {
|
|
|
|
$sth = $pdo->prepare("INSERT INTO ttrss_user_labels2
|
|
|
|
(label_id, article_id) VALUES (?, ?)");
|
|
|
|
|
|
|
|
$sth->execute([$label_id, $id]);
|
2013-01-22 19:32:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
Labels::clear_cache($id);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function remove($id, $owner_uid) {
|
2013-01-22 19:32:17 +01:00
|
|
|
if (!$owner_uid) $owner_uid = $_SESSION["uid"];
|
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
2017-12-01 23:08:30 +01:00
|
|
|
$tr_in_progress = false;
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 23:08:30 +01:00
|
|
|
try {
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$tr_in_progress = true;
|
|
|
|
}
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$sth = $pdo->prepare("SELECT caption FROM ttrss_labels2
|
|
|
|
WHERE id = ?");
|
|
|
|
$sth->execute([$id]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$row = $sth->fetch();
|
|
|
|
$caption = $row['caption'];
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$sth = $pdo->prepare("DELETE FROM ttrss_labels2 WHERE id = ?
|
|
|
|
AND owner_uid = ?");
|
|
|
|
$sth->execute([$id, $owner_uid]);
|
|
|
|
|
|
|
|
if ($sth->rowCount() != 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
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$sth = $pdo->prepare("DELETE FROM ttrss_access_keys WHERE
|
|
|
|
feed_id = ? AND owner_uid = ?");
|
|
|
|
$sth->execute([$ext_id, $owner_uid]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
/* Remove cached data */
|
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
|
|
|
|
WHERE label_cache LIKE ? AND owner_uid = ?");
|
|
|
|
$sth->execute(["%$caption%", $owner_uid]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-01 23:08:30 +01:00
|
|
|
if (!$tr_in_progress) $pdo->commit();
|
2013-01-22 19:32:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:40 +02:00
|
|
|
static function create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
if (!$owner_uid) $owner_uid = $_SESSION['uid'];
|
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
2017-12-01 23:08:30 +01:00
|
|
|
$tr_in_progress = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$tr_in_progress = true;
|
|
|
|
}
|
2017-12-01 22:06:17 +01:00
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT id FROM ttrss_labels2
|
|
|
|
WHERE caption = ? AND owner_uid = ?");
|
|
|
|
$sth->execute([$caption, $owner_uid]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
if (!$sth->fetch()) {
|
|
|
|
$sth = $pdo->prepare("INSERT INTO ttrss_labels2
|
|
|
|
(caption,owner_uid,fg_color,bg_color) VALUES (?, ?, ?, ?)");
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$sth->execute([$caption, $owner_uid, $fg_color, $bg_color]);
|
2013-01-22 19:32:17 +01:00
|
|
|
|
2017-12-01 22:06:17 +01:00
|
|
|
$result = $sth->rowCount();
|
2013-01-22 19:32:17 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 23:08:30 +01:00
|
|
|
if (!$tr_in_progress) $pdo->commit();
|
2013-01-22 19:32:17 +01:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2017-05-04 14:57:40 +02:00
|
|
|
}
|