ttrss/classes/labels.php

219 lines
4.5 KiB
PHP
Raw Permalink Normal View History

<?php
class Labels
{
static function label_to_feed_id($label) {
return LABEL_BASE_INDEX - 1 - abs($label);
}
static function feed_to_label_id($feed) {
return LABEL_BASE_INDEX - 1 + abs($feed);
}
static function find_id($label, $owner_uid) {
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
$sth = $pdo->prepare("SELECT id FROM ttrss_labels2 WHERE LOWER(caption) = LOWER(?)
2017-12-01 21:06:17 +00:00
AND owner_uid = ? LIMIT 1");
$sth->execute([$label, $owner_uid]);
if ($row = $sth->fetch()) {
return $row['id'];
} else {
return 0;
}
}
static function find_caption($label, $owner_uid) {
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
$sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 WHERE id = ?
AND owner_uid = ? LIMIT 1");
$sth->execute([$label, $owner_uid]);
2017-12-01 21:06:17 +00:00
if ($row = $sth->fetch()) {
return $row['caption'];
} else {
return "";
}
}
static function get_as_hash($owner_uid) {
$rv = [];
$labels = Labels::get_all($owner_uid);
foreach ($labels as $i => $label) {
$rv[$label["id"]] = $labels[$i];
}
return $rv;
}
static function get_all($owner_uid) {
$rv = array();
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
2018-11-03 12:08:43 +00:00
$sth = $pdo->prepare("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2
2017-12-01 21:06:17 +00:00
WHERE owner_uid = ? ORDER BY caption");
$sth->execute([$owner_uid]);
while ($line = $sth->fetch(PDO::FETCH_ASSOC)) {
array_push($rv, $line);
}
return $rv;
}
static function update_cache($owner_uid, $id, $labels = false, $force = false) {
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
if ($force)
self::clear_cache($id);
if (!$labels)
2021-02-15 12:52:28 +00:00
$labels = Article::_get_labels($id);
$labels = json_encode($labels);
2017-12-01 21:06:17 +00:00
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET
label_cache = ? WHERE ref_id = ? AND owner_uid = ?");
$sth->execute([$labels, $id, $owner_uid]);
}
static function clear_cache($id) {
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET
label_cache = '' WHERE ref_id = ?");
$sth->execute([$id]);
}
static function remove_article($id, $label, $owner_uid) {
$label_id = self::find_id($label, $owner_uid);
if (!$label_id) return;
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
$sth = $pdo->prepare("DELETE FROM ttrss_user_labels2
WHERE
2017-12-01 21:06:17 +00:00
label_id = ? AND
article_id = ?");
$sth->execute([$label_id, $id]);
self::clear_cache($id);
}
static function add_article($id, $label, $owner_uid) {
$label_id = self::find_id($label, $owner_uid);
if (!$label_id) return;
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
$sth = $pdo->prepare("SELECT
article_id FROM ttrss_labels2, ttrss_user_labels2
WHERE
label_id = id AND
2017-12-01 21:06:17 +00:00
label_id = ? AND
article_id = ? AND owner_uid = ?
LIMIT 1");
2017-12-01 21:06:17 +00: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]);
}
self::clear_cache($id);
}
static function remove($id, $owner_uid) {
if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
2017-12-01 22:08:30 +00:00
$tr_in_progress = false;
2017-12-01 22:08:30 +00:00
try {
$pdo->beginTransaction();
} catch (Exception $e) {
$tr_in_progress = true;
}
2017-12-01 21:06:17 +00:00
$sth = $pdo->prepare("SELECT caption FROM ttrss_labels2
WHERE id = ?");
$sth->execute([$id]);
2017-12-01 21:06:17 +00:00
$row = $sth->fetch();
$caption = $row['caption'];
2017-12-01 21:06:17 +00:00
$sth = $pdo->prepare("DELETE FROM ttrss_labels2 WHERE id = ?
AND owner_uid = ?");
$sth->execute([$id, $owner_uid]);
if ($sth->rowCount() != 0 && $caption) {
/* Remove access key for the label */
$ext_id = LABEL_BASE_INDEX - 1 - $id;
2017-12-01 21:06:17 +00:00
$sth = $pdo->prepare("DELETE FROM ttrss_access_keys WHERE
feed_id = ? AND owner_uid = ?");
$sth->execute([$ext_id, $owner_uid]);
/* Remove cached data */
2017-12-01 21:06:17 +00:00
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
WHERE owner_uid = ?");
$sth->execute([$owner_uid]);
}
2017-12-01 22:08:30 +00:00
if (!$tr_in_progress) $pdo->commit();
}
static function create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
if (!$owner_uid) $owner_uid = $_SESSION['uid'];
2017-12-01 21:06:17 +00:00
$pdo = Db::pdo();
2017-12-01 22:08:30 +00:00
$tr_in_progress = false;
try {
$pdo->beginTransaction();
} catch (Exception $e) {
$tr_in_progress = true;
}
2017-12-01 21:06:17 +00:00
$sth = $pdo->prepare("SELECT id FROM ttrss_labels2
WHERE LOWER(caption) = LOWER(?) AND owner_uid = ?");
2017-12-01 21:06:17 +00:00
$sth->execute([$caption, $owner_uid]);
2017-12-01 21:06:17 +00:00
if (!$sth->fetch()) {
2018-11-03 12:08:43 +00:00
$sth = $pdo->prepare("INSERT INTO ttrss_labels2
2017-12-01 21:06:17 +00:00
(caption,owner_uid,fg_color,bg_color) VALUES (?, ?, ?, ?)");
2017-12-01 21:06:17 +00:00
$sth->execute([$caption, $owner_uid, $fg_color, $bg_color]);
2017-12-01 21:06:17 +00:00
$result = $sth->rowCount();
} else {
$result = false;
}
2017-12-01 22:08:30 +00:00
if (!$tr_in_progress) $pdo->commit();
return $result;
}
}