labels.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. class Labels
  3. {
  4. static function label_to_feed_id($label) {
  5. return LABEL_BASE_INDEX - 1 - abs($label);
  6. }
  7. static function feed_to_label_id($feed) {
  8. return LABEL_BASE_INDEX - 1 + abs($feed);
  9. }
  10. static function find_id($label, $owner_uid) {
  11. $pdo = Db::pdo();
  12. $sth = $pdo->prepare("SELECT id FROM ttrss_labels2 WHERE caption = ?
  13. AND owner_uid = ? LIMIT 1");
  14. $sth->execute([$label, $owner_uid]);
  15. if ($row = $sth->fetch()) {
  16. return $row['id'];
  17. } else {
  18. return 0;
  19. }
  20. }
  21. static function find_caption($label, $owner_uid) {
  22. $pdo = Db::pdo();
  23. $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 WHERE id = ?
  24. AND owner_uid = ? LIMIT 1");
  25. $sth->execute([$label, $owner_uid]);
  26. if ($row = $sth->fetch()) {
  27. return $row['caption'];
  28. } else {
  29. return "";
  30. }
  31. }
  32. static function get_all_labels($owner_uid) {
  33. $rv = array();
  34. $pdo = Db::pdo();
  35. $sth = $pdo->prepare("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2
  36. WHERE owner_uid = ? ORDER BY caption");
  37. $sth->execute([$owner_uid]);
  38. while ($line = $sth->fetch()) {
  39. array_push($rv, $line);
  40. }
  41. return $rv;
  42. }
  43. static function update_cache($owner_uid, $id, $labels = false, $force = false) {
  44. $pdo = Db::pdo();
  45. if ($force)
  46. Labels::clear_cache($id);
  47. if (!$labels)
  48. $labels = Article::get_article_labels($id);
  49. $labels = json_encode($labels);
  50. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  51. label_cache = ? WHERE ref_id = ? AND owner_uid = ?");
  52. $sth->execute([$labels, $id, $owner_uid]);
  53. }
  54. static function clear_cache($id) {
  55. $pdo = Db::pdo();
  56. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  57. label_cache = '' WHERE ref_id = ?");
  58. $sth->execute([$id]);
  59. }
  60. static function remove_article($id, $label, $owner_uid) {
  61. $label_id = Labels::find_id($label, $owner_uid);
  62. if (!$label_id) return;
  63. $pdo = Db::pdo();
  64. $sth = $pdo->prepare("DELETE FROM ttrss_user_labels2
  65. WHERE
  66. label_id = ? AND
  67. article_id = ?");
  68. $sth->execute([$label_id, $id]);
  69. Labels::clear_cache($id);
  70. }
  71. static function add_article($id, $label, $owner_uid) {
  72. $label_id = Labels::find_id($label, $owner_uid);
  73. if (!$label_id) return;
  74. $pdo = Db::pdo();
  75. $sth = $pdo->prepare("SELECT
  76. article_id FROM ttrss_labels2, ttrss_user_labels2
  77. WHERE
  78. label_id = id AND
  79. label_id = ? AND
  80. article_id = ? AND owner_uid = ?
  81. LIMIT 1");
  82. $sth->execute([$label_id, $id, $owner_uid]);
  83. if (!$sth->fetch()) {
  84. $sth = $pdo->prepare("INSERT INTO ttrss_user_labels2
  85. (label_id, article_id) VALUES (?, ?)");
  86. $sth->execute([$label_id, $id]);
  87. }
  88. Labels::clear_cache($id);
  89. }
  90. static function remove($id, $owner_uid) {
  91. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  92. $pdo = Db::pdo();
  93. $tr_in_progress = false;
  94. try {
  95. $pdo->beginTransaction();
  96. } catch (Exception $e) {
  97. $tr_in_progress = true;
  98. }
  99. $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2
  100. WHERE id = ?");
  101. $sth->execute([$id]);
  102. $row = $sth->fetch();
  103. $caption = $row['caption'];
  104. $sth = $pdo->prepare("DELETE FROM ttrss_labels2 WHERE id = ?
  105. AND owner_uid = ?");
  106. $sth->execute([$id, $owner_uid]);
  107. if ($sth->rowCount() != 0 && $caption) {
  108. /* Remove access key for the label */
  109. $ext_id = LABEL_BASE_INDEX - 1 - $id;
  110. $sth = $pdo->prepare("DELETE FROM ttrss_access_keys WHERE
  111. feed_id = ? AND owner_uid = ?");
  112. $sth->execute([$ext_id, $owner_uid]);
  113. /* Remove cached data */
  114. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET label_cache = ''
  115. WHERE owner_uid = ?");
  116. $sth->execute([$owner_uid]);
  117. }
  118. if (!$tr_in_progress) $pdo->commit();
  119. }
  120. static function create($caption, $fg_color = '', $bg_color = '', $owner_uid = false) {
  121. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  122. $pdo = Db::pdo();
  123. $tr_in_progress = false;
  124. try {
  125. $pdo->beginTransaction();
  126. } catch (Exception $e) {
  127. $tr_in_progress = true;
  128. }
  129. $sth = $pdo->prepare("SELECT id FROM ttrss_labels2
  130. WHERE caption = ? AND owner_uid = ?");
  131. $sth->execute([$caption, $owner_uid]);
  132. if (!$sth->fetch()) {
  133. $sth = $pdo->prepare("INSERT INTO ttrss_labels2
  134. (caption,owner_uid,fg_color,bg_color) VALUES (?, ?, ?, ?)");
  135. $sth->execute([$caption, $owner_uid, $fg_color, $bg_color]);
  136. $result = $sth->rowCount();
  137. }
  138. if (!$tr_in_progress) $pdo->commit();
  139. return $result;
  140. }
  141. }