2013-01-22 18:32:17 +00:00
|
|
|
<?php
|
2017-05-04 12:57:40 +00:00
|
|
|
class Labels
|
|
|
|
{
|
2021-11-11 22:28:13 +00:00
|
|
|
static function label_to_feed_id(int $label): int {
|
2017-05-04 12:36:36 +00:00
|
|
|
return LABEL_BASE_INDEX - 1 - abs($label);
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
static function feed_to_label_id(int $feed): int {
|
2017-05-04 12:36:36 +00:00
|
|
|
return LABEL_BASE_INDEX - 1 + abs($feed);
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
static function find_id(string $label, int $owner_uid): int {
|
2017-12-01 21:06:17 +00:00
|
|
|
$pdo = Db::pdo();
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2020-04-04 11:34:08 +00:00
|
|
|
$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'];
|
2013-01-22 18:32:17 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
static function find_caption(int $label, int $owner_uid): string {
|
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]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2017-12-01 21:06:17 +00:00
|
|
|
if ($row = $sth->fetch()) {
|
|
|
|
return $row['caption'];
|
2013-01-22 18:32:17 +00:00
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
/**
|
|
|
|
* @return array<int, array<string, string>>
|
|
|
|
*/
|
|
|
|
static function get_as_hash(int $owner_uid): array {
|
2021-02-21 06:35:07 +00:00
|
|
|
$rv = [];
|
|
|
|
$labels = Labels::get_all($owner_uid);
|
|
|
|
|
|
|
|
foreach ($labels as $i => $label) {
|
2021-11-11 22:28:13 +00:00
|
|
|
$rv[(int)$label["id"]] = $labels[$i];
|
2021-02-21 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rv;
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
/**
|
|
|
|
* @return array<int, array<string, string>> An array of label detail arrays
|
|
|
|
*/
|
|
|
|
static function get_all(int $owner_uid) {
|
2013-01-22 18:32:17 +00:00
|
|
|
$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]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2021-02-21 06:35:07 +00:00
|
|
|
while ($line = $sth->fetch(PDO::FETCH_ASSOC)) {
|
2013-01-22 18:32:17 +00:00
|
|
|
array_push($rv, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rv;
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
/**
|
2021-11-12 05:42:55 +00:00
|
|
|
* @param array<int, array<int, array<int, int|string>>> $labels
|
2021-11-11 22:28:13 +00:00
|
|
|
*
|
|
|
|
* @see Article::_get_labels()
|
|
|
|
*/
|
2021-11-12 01:50:40 +00:00
|
|
|
static function update_cache(int $owner_uid, int $id, array $labels, bool $force = false): void {
|
2017-12-01 21:06:17 +00:00
|
|
|
$pdo = Db::pdo();
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
if ($force)
|
2020-09-22 11:54:15 +00:00
|
|
|
self::clear_cache($id);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
if (!$labels)
|
2021-02-15 12:52:28 +00:00
|
|
|
$labels = Article::_get_labels($id);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2017-12-02 12:47:28 +00:00
|
|
|
$labels = json_encode($labels);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
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]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
static function clear_cache(int $id): void {
|
2013-01-22 18:32:17 +00:00
|
|
|
|
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]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
static function remove_article(int $id, string $label, int $owner_uid): void {
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
$label_id = self::find_id($label, $owner_uid);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
if (!$label_id) return;
|
|
|
|
|
2017-12-01 21:06:17 +00:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("DELETE FROM ttrss_user_labels2
|
2013-01-22 18:32:17 +00:00
|
|
|
WHERE
|
2017-12-01 21:06:17 +00:00
|
|
|
label_id = ? AND
|
|
|
|
article_id = ?");
|
|
|
|
|
|
|
|
$sth->execute([$label_id, $id]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
self::clear_cache($id);
|
2013-01-22 18:32:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
static function add_article(int $id, string $label, int $owner_uid): void {
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
$label_id = self::find_id($label, $owner_uid);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
if (!$label_id) return;
|
|
|
|
|
2017-12-01 21:06:17 +00:00
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT
|
2013-01-22 18:32:17 +00:00
|
|
|
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 = ?
|
2013-01-22 18:32:17 +00:00
|
|
|
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]);
|
2013-01-22 18:32:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
self::clear_cache($id);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
static function remove(int $id, int $owner_uid): void {
|
2013-01-22 18:32:17 +00:00
|
|
|
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;
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2017-12-01 22:08:30 +00:00
|
|
|
try {
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$tr_in_progress = true;
|
|
|
|
}
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2017-12-01 21:06:17 +00:00
|
|
|
$sth = $pdo->prepare("SELECT caption FROM ttrss_labels2
|
|
|
|
WHERE id = ?");
|
|
|
|
$sth->execute([$id]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2017-12-01 21:06:17 +00:00
|
|
|
$row = $sth->fetch();
|
|
|
|
$caption = $row['caption'];
|
2013-01-22 18:32:17 +00:00
|
|
|
|
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) {
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
/* Remove access key for the label */
|
|
|
|
|
2013-03-27 05:40:07 +00:00
|
|
|
$ext_id = LABEL_BASE_INDEX - 1 - $id;
|
2013-01-22 18:32:17 +00:00
|
|
|
|
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]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
/* Remove cached data */
|
|
|
|
|
2017-12-01 21:06:17 +00:00
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
|
2018-12-06 05:29:32 +00:00
|
|
|
WHERE owner_uid = ?");
|
|
|
|
$sth->execute([$owner_uid]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:08:30 +00:00
|
|
|
if (!$tr_in_progress) $pdo->commit();
|
2013-01-22 18:32:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:28:13 +00:00
|
|
|
/**
|
|
|
|
* @return false|int false if the check for an existing label failed, otherwise the number of rows inserted (1 on success)
|
|
|
|
*/
|
|
|
|
static function create(string $caption, ?string $fg_color = '', ?string $bg_color = '', ?int $owner_uid = null) {
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
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
|
2020-04-04 11:34:08 +00:00
|
|
|
WHERE LOWER(caption) = LOWER(?) AND owner_uid = ?");
|
2017-12-01 21:06:17 +00:00
|
|
|
$sth->execute([$caption, $owner_uid]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
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 (?, ?, ?, ?)");
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2017-12-01 21:06:17 +00:00
|
|
|
$sth->execute([$caption, $owner_uid, $fg_color, $bg_color]);
|
2013-01-22 18:32:17 +00:00
|
|
|
|
2017-12-01 21:06:17 +00:00
|
|
|
$result = $sth->rowCount();
|
2021-02-06 14:38:24 +00:00
|
|
|
} else {
|
|
|
|
$result = false;
|
2013-01-22 18:32:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 22:08:30 +00:00
|
|
|
if (!$tr_in_progress) $pdo->commit();
|
2013-01-22 18:32:17 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2020-04-04 11:34:08 +00:00
|
|
|
}
|