BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bookmarks.class.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. #declare(strict_types=1);
  3. class Bookmarks
  4. {
  5. /**
  6. * Check if can bookmark
  7. *
  8. * @param string $Type
  9. * @return boolean
  10. */
  11. public static function can_bookmark($Type)
  12. {
  13. return in_array($Type, array(
  14. 'torrent',
  15. 'artist',
  16. 'collage',
  17. 'request'
  18. ));
  19. }
  20. /**
  21. * Get the bookmark schema.
  22. * Recommended usage:
  23. * list($Table, $Col) = bookmark_schema('torrent');
  24. *
  25. * @param string $Type the type to get the schema for
  26. */
  27. public static function bookmark_schema($Type)
  28. {
  29. switch ($Type) {
  30. case 'torrent':
  31. return array(
  32. 'bookmarks_torrents',
  33. 'GroupID'
  34. );
  35. break;
  36. case 'artist':
  37. return array(
  38. 'bookmarks_artists',
  39. 'ArtistID'
  40. );
  41. break;
  42. case 'collage':
  43. return array(
  44. 'bookmarks_collages',
  45. 'CollageID'
  46. );
  47. break;
  48. case 'request':
  49. return array(
  50. 'bookmarks_requests',
  51. 'RequestID'
  52. );
  53. break;
  54. default:
  55. error('h4x');
  56. }
  57. }
  58. /**
  59. * Check if something is bookmarked
  60. *
  61. * @param string $Type
  62. * type of bookmarks to check
  63. * @param int $ID
  64. * bookmark's id
  65. * @return boolean
  66. */
  67. public static function has_bookmarked($Type, $ID)
  68. {
  69. return in_array($ID, self::all_bookmarks($Type));
  70. }
  71. /**
  72. * Fetch all bookmarks of a certain type for a user.
  73. * If UserID is false than defaults to G::$LoggedUser['ID']
  74. *
  75. * @param string $Type
  76. * type of bookmarks to fetch
  77. * @param int $UserID
  78. * userid whose bookmarks to get
  79. * @return array the bookmarks
  80. */
  81. public static function all_bookmarks($Type, $UserID = false)
  82. {
  83. if ($UserID === false) {
  84. $UserID = G::$LoggedUser['ID'];
  85. }
  86. $CacheKey = "bookmarks_$Type".'_'.$UserID;
  87. if (($Bookmarks = G::$Cache->get_value($CacheKey)) === false) {
  88. list($Table, $Col) = self::bookmark_schema($Type);
  89. $QueryID = G::$DB->get_query_id();
  90. G::$DB->query("
  91. SELECT `$Col`
  92. FROM `$Table`
  93. WHERE UserID = '$UserID'");
  94. $Bookmarks = G::$DB->collect($Col);
  95. G::$DB->set_query_id($QueryID);
  96. G::$Cache->cache_value($CacheKey, $Bookmarks, 0);
  97. }
  98. return $Bookmarks;
  99. }
  100. }