Oppaitime'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.

notificationsmanager.class.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?
  2. class NotificationsManager {
  3. // Option types
  4. const OPT_DISABLED = 0;
  5. const OPT_POPUP = 1;
  6. const OPT_TRADITIONAL = 2;
  7. // Importances
  8. const IMPORTANT = 'information';
  9. const CRITICAL = 'error';
  10. const WARNING = 'warning';
  11. const INFO = 'confirmation';
  12. public static $Importances = array(
  13. 'important' => self::IMPORTANT,
  14. 'critical' => self::CRITICAL,
  15. 'warning' => self::WARNING,
  16. 'info' => self::INFO);
  17. // Types. These names must correspond to column names in users_notifications_settings
  18. const NEWS = 'News';
  19. const BLOG = 'Blog';
  20. const STAFFBLOG = 'StaffBlog';
  21. const STAFFPM = 'StaffPM';
  22. const INBOX = 'Inbox';
  23. const QUOTES = 'Quotes';
  24. const SUBSCRIPTIONS = 'Subscriptions';
  25. const TORRENTS = 'Torrents';
  26. const COLLAGES = 'Collages';
  27. const SITEALERTS = 'SiteAlerts';
  28. const FORUMALERTS = 'ForumAlerts';
  29. const REQUESTALERTS = 'RequestAlerts';
  30. const COLLAGEALERTS = 'CollageAlerts';
  31. const TORRENTALERTS = 'TorrentAlerts';
  32. const GLOBALNOTICE = 'Global';
  33. public static $Types = array(
  34. 'News',
  35. 'Blog',
  36. 'StaffPM',
  37. 'Inbox',
  38. 'Quotes',
  39. 'Subscriptions',
  40. 'Torrents',
  41. 'Collages',
  42. 'SiteAlerts',
  43. 'ForumAlerts',
  44. 'RequestAlerts',
  45. 'CollageAlerts',
  46. 'TorrentAlerts');
  47. private $UserID;
  48. private $Notifications;
  49. private $Settings;
  50. private $Skipped;
  51. function __construct($UserID, $Skip = [], $Load = true, $AutoSkip = true) {
  52. $this->UserID = $UserID;
  53. $this->Notifications = [];
  54. $this->Settings = self::get_settings($UserID);
  55. $this->Skipped = $Skip;
  56. if ($AutoSkip) {
  57. foreach ($this->Settings as $Key => $Value) {
  58. // Skip disabled and traditional settings
  59. if ($Value == self::OPT_DISABLED || $this->is_traditional($Key)) {
  60. $this->Skipped[$Key] = true;
  61. }
  62. }
  63. }
  64. if ($Load) {
  65. $this->load_global_notification();
  66. if (!isset($this->Skipped[self::NEWS])) {
  67. $this->load_news();
  68. }
  69. if (!isset($this->Skipped[self::BLOG])) {
  70. $this->load_blog();
  71. }
  72. // if (!isset($this->Skipped[self::STAFFBLOG])) {
  73. // $this->load_staff_blog();
  74. // }
  75. if (!isset($this->Skipped[self::STAFFPM])) {
  76. $this->load_staff_pms();
  77. }
  78. if (!isset($this->Skipped[self::INBOX])) {
  79. $this->load_inbox();
  80. }
  81. if (!isset($this->Skipped[self::TORRENTS])) {
  82. $this->load_torrent_notifications();
  83. }
  84. if (!isset($this->Skipped[self::COLLAGES])) {
  85. $this->load_collage_subscriptions();
  86. }
  87. if (!isset($this->Skipped[self::QUOTES])) {
  88. $this->load_quote_notifications();
  89. }
  90. if (!isset($this->Skipped[self::SUBSCRIPTIONS])) {
  91. $this->load_subscriptions();
  92. }
  93. // $this->load_one_reads(); // The code that sets these notices is commented out.
  94. }
  95. }
  96. public function get_notifications() {
  97. return $this->Notifications;
  98. }
  99. public function clear_notifications_array() {
  100. unset($this->Notifications);
  101. $this->Notifications = [];
  102. }
  103. private function create_notification($Type, $ID, $Message, $URL, $Importance) {
  104. $this->Notifications[$Type] = array(
  105. 'id' => (int)$ID,
  106. 'message' => $Message,
  107. 'url' => $URL,
  108. 'importance' => $Importance);
  109. }
  110. public static function notify_user($UserID, $Type, $Message, $URL, $Importance) {
  111. self::notify_users(array($UserID), $Type, $Message, $URL, $Importance);
  112. }
  113. public static function notify_users($UserIDs, $Type, $Message, $URL, $Importance) {
  114. /**
  115. if (!isset($Importance)) {
  116. $Importance = self::INFO;
  117. }
  118. $Type = db_string($Type);
  119. if (!empty($UserIDs)) {
  120. $UserIDs = implode(',', $UserIDs);
  121. $QueryID = G::$DB->get_query_id();
  122. G::$DB->query("
  123. SELECT UserID
  124. FROM users_notifications_settings
  125. WHERE $Type != 0
  126. AND UserID IN ($UserIDs)");
  127. $UserIDs = [];
  128. while (list($ID) = G::$DB->next_record()) {
  129. $UserIDs[] = $ID;
  130. }
  131. G::$DB->set_query_id($QueryID);
  132. foreach ($UserIDs as $UserID) {
  133. $OneReads = G::$Cache->get_value("notifications_one_reads_$UserID");
  134. if (!$OneReads) {
  135. $OneReads = [];
  136. }
  137. array_unshift($OneReads, $this->create_notification($OneReads, "oneread_" . uniqid(), null, $Message, $URL, $Importance));
  138. $OneReads = array_filter($OneReads);
  139. G::$Cache->cache_value("notifications_one_reads_$UserID", $OneReads, 0);
  140. }
  141. }
  142. **/
  143. }
  144. public static function get_notification_enabled_users($Type, $UserID) {
  145. $Type = db_string($Type);
  146. $UserWhere = '';
  147. if (isset($UserID)) {
  148. $UserID = (int)$UserID;
  149. $UserWhere = " AND UserID = '$UserID'";
  150. }
  151. $QueryID = G::$DB->get_query_id();
  152. G::$DB->query("
  153. SELECT UserID
  154. FROM users_notifications_settings
  155. WHERE $Type != 0
  156. $UserWhere");
  157. $IDs = [];
  158. while (list($ID) = G::$DB->next_record()) {
  159. $IDs[] = $ID;
  160. }
  161. G::$DB->set_query_id($QueryID);
  162. return $IDs;
  163. }
  164. public function load_one_reads() {
  165. $OneReads = G::$Cache->get_value('notifications_one_reads_' . G::$LoggedUser['ID']);
  166. if (is_array($OneReads)) {
  167. $this->Notifications = $this->Notifications + $OneReads;
  168. }
  169. }
  170. public static function clear_one_read($ID) {
  171. $OneReads = G::$Cache->get_value('notifications_one_reads_' . G::$LoggedUser['ID']);
  172. if ($OneReads) {
  173. unset($OneReads[$ID]);
  174. if (count($OneReads) > 0) {
  175. G::$Cache->cache_value('notifications_one_reads_' . G::$LoggedUser['ID'], $OneReads, 0);
  176. } else {
  177. G::$Cache->delete_value('notifications_one_reads_' . G::$LoggedUser['ID']);
  178. }
  179. }
  180. }
  181. public function load_global_notification() {
  182. $GlobalNotification = G::$Cache->get_value('global_notification');
  183. if ($GlobalNotification) {
  184. $Read = G::$Cache->get_value('user_read_global_' . G::$LoggedUser['ID']);
  185. if (!$Read) {
  186. $this->create_notification(self::GLOBALNOTICE, 0, $GlobalNotification['Message'], $GlobalNotification['URL'], $GlobalNotification['Importance']);
  187. }
  188. }
  189. }
  190. public static function get_global_notification() {
  191. return G::$Cache->get_value('global_notification');
  192. }
  193. public static function set_global_notification($Message, $URL, $Importance, $Expiration) {
  194. if (empty($Message) || empty($Expiration)) {
  195. error('Error setting notification');
  196. }
  197. G::$Cache->cache_value('global_notification', array("Message" => $Message, "URL" => $URL, "Importance" => $Importance, "Expiration" => $Expiration), $Expiration);
  198. }
  199. public static function delete_global_notification() {
  200. G::$Cache->delete_value('global_notification');
  201. }
  202. public static function clear_global_notification() {
  203. $GlobalNotification = G::$Cache->get_value('global_notification');
  204. if ($GlobalNotification) {
  205. // This is some trickery
  206. // since we can't know which users have the read cache key set
  207. // we set the expiration time of their cache key to that of the length of the notification
  208. // this gaurantees that their cache key will expire after the notification expires
  209. G::$Cache->cache_value('user_read_global_' . G::$LoggedUser['ID'], true, $GlobalNotification['Expiration']);
  210. }
  211. }
  212. public function load_news() {
  213. $MyNews = G::$LoggedUser['LastReadNews'];
  214. $CurrentNews = G::$Cache->get_value('news_latest_id');
  215. $Title = G::$Cache->get_value('news_latest_title');
  216. if ($CurrentNews === false || $Title === false) {
  217. $QueryID = G::$DB->get_query_id();
  218. G::$DB->query('
  219. SELECT ID, Title
  220. FROM news
  221. ORDER BY Time DESC
  222. LIMIT 1');
  223. if (G::$DB->has_results()) {
  224. list($CurrentNews, $Title) = G::$DB->next_record();
  225. } else {
  226. $CurrentNews = -1;
  227. }
  228. G::$DB->set_query_id($QueryID);
  229. G::$Cache->cache_value('news_latest_id', $CurrentNews, 0);
  230. G::$Cache->cache_value('news_latest_title', $Title, 0);
  231. }
  232. if ($MyNews < $CurrentNews) {
  233. $this->create_notification(self::NEWS, $CurrentNews, "Announcement: $Title", "index.php#news$CurrentNews", self::IMPORTANT);
  234. }
  235. }
  236. public function load_blog() {
  237. $MyBlog = G::$LoggedUser['LastReadBlog'];
  238. $CurrentBlog = G::$Cache->get_value('blog_latest_id');
  239. $Title = G::$Cache->get_value('blog_latest_title');
  240. if ($CurrentBlog === false) {
  241. $QueryID = G::$DB->get_query_id();
  242. G::$DB->query('
  243. SELECT ID, Title
  244. FROM blog
  245. WHERE Important = 1
  246. ORDER BY Time DESC
  247. LIMIT 1');
  248. if (G::$DB->has_results()) {
  249. list($CurrentBlog, $Title) = G::$DB->next_record();
  250. } else {
  251. $CurrentBlog = -1;
  252. }
  253. G::$DB->set_query_id($QueryID);
  254. G::$Cache->cache_value('blog_latest_id', $CurrentBlog, 0);
  255. G::$Cache->cache_value('blog_latest_title', $Title, 0);
  256. }
  257. if ($MyBlog < $CurrentBlog) {
  258. $this->create_notification(self::BLOG, $CurrentBlog, "Blog: $Title", "blog.php#blog$CurrentBlog", self::IMPORTANT);
  259. }
  260. }
  261. public function load_staff_blog() {
  262. if (check_perms('users_mod')) {
  263. global $SBlogReadTime, $LatestSBlogTime;
  264. if (!$SBlogReadTime && ($SBlogReadTime = G::$Cache->get_value('staff_blog_read_' . G::$LoggedUser['ID'])) === false) {
  265. $QueryID = G::$DB->get_query_id();
  266. G::$DB->query("
  267. SELECT Time
  268. FROM staff_blog_visits
  269. WHERE UserID = " . G::$LoggedUser['ID']);
  270. if (list($SBlogReadTime) = G::$DB->next_record()) {
  271. $SBlogReadTime = strtotime($SBlogReadTime);
  272. } else {
  273. $SBlogReadTime = 0;
  274. }
  275. G::$DB->set_query_id($QueryID);
  276. G::$Cache->cache_value('staff_blog_read_' . G::$LoggedUser['ID'], $SBlogReadTime, 1209600);
  277. }
  278. if (!$LatestSBlogTime && ($LatestSBlogTime = G::$Cache->get_value('staff_blog_latest_time')) === false) {
  279. $QueryID = G::$DB->get_query_id();
  280. G::$DB->query('
  281. SELECT MAX(Time)
  282. FROM staff_blog');
  283. if (list($LatestSBlogTime) = G::$DB->next_record()) {
  284. $LatestSBlogTime = strtotime($LatestSBlogTime);
  285. } else {
  286. $LatestSBlogTime = 0;
  287. }
  288. G::$DB->set_query_id($QueryID);
  289. G::$Cache->cache_value('staff_blog_latest_time', $LatestSBlogTime, 1209600);
  290. }
  291. if ($SBlogReadTime < $LatestSBlogTime) {
  292. $this->create_notification(self::STAFFBLOG, 0, 'New Staff Blog Post!', 'staffblog.php', self::IMPORTANT);
  293. }
  294. }
  295. }
  296. public function load_staff_pms() {
  297. $NewStaffPMs = G::$Cache->get_value('staff_pm_new_' . G::$LoggedUser['ID']);
  298. if ($NewStaffPMs === false) {
  299. $QueryID = G::$DB->get_query_id();
  300. G::$DB->query("
  301. SELECT COUNT(ID)
  302. FROM staff_pm_conversations
  303. WHERE UserID = '" . G::$LoggedUser['ID'] . "'
  304. AND Unread = '1'");
  305. list($NewStaffPMs) = G::$DB->next_record();
  306. G::$DB->set_query_id($QueryID);
  307. G::$Cache->cache_value('staff_pm_new_' . G::$LoggedUser['ID'], $NewStaffPMs, 0);
  308. }
  309. if ($NewStaffPMs > 0) {
  310. $Title = 'You have ' . ($NewStaffPMs == 1 ? 'a' : $NewStaffPMs) . ' new Staff PM' . ($NewStaffPMs > 1 ? 's' : '');
  311. $this->create_notification(self::STAFFPM, 0, $Title, 'staffpm.php', self::INFO);
  312. }
  313. }
  314. public function load_inbox() {
  315. $NewMessages = G::$Cache->get_value('inbox_new_' . G::$LoggedUser['ID']);
  316. if ($NewMessages === false) {
  317. $QueryID = G::$DB->get_query_id();
  318. G::$DB->query("
  319. SELECT COUNT(UnRead)
  320. FROM pm_conversations_users
  321. WHERE UserID = '" . G::$LoggedUser['ID'] . "'
  322. AND UnRead = '1'
  323. AND InInbox = '1'");
  324. list($NewMessages) = G::$DB->next_record();
  325. G::$DB->set_query_id($QueryID);
  326. G::$Cache->cache_value('inbox_new_' . G::$LoggedUser['ID'], $NewMessages, 0);
  327. }
  328. if ($NewMessages > 0) {
  329. $Title = 'You have ' . ($NewMessages == 1 ? 'a' : $NewMessages) . ' new message' . ($NewMessages > 1 ? 's' : '');
  330. $this->create_notification(self::INBOX, 0, $Title, Inbox::get_inbox_link(), self::INFO);
  331. }
  332. }
  333. public function load_torrent_notifications() {
  334. if (check_perms('site_torrents_notify')) {
  335. $NewNotifications = G::$Cache->get_value('notifications_new_' . G::$LoggedUser['ID']);
  336. if ($NewNotifications === false) {
  337. $QueryID = G::$DB->get_query_id();
  338. G::$DB->query("
  339. SELECT COUNT(UserID)
  340. FROM users_notify_torrents
  341. WHERE UserID = ' " . G::$LoggedUser['ID'] . "'
  342. AND UnRead = '1'");
  343. list($NewNotifications) = G::$DB->next_record();
  344. G::$DB->set_query_id($QueryID);
  345. G::$Cache->cache_value('notifications_new_' . G::$LoggedUser['ID'], $NewNotifications, 0);
  346. }
  347. }
  348. if (isset($NewNotifications) && $NewNotifications > 0) {
  349. $Title = 'You have ' . ($NewNotifications == 1 ? 'a' : $NewNotifications) . ' new torrent notification' . ($NewNotifications > 1 ? 's' : '');
  350. $this->create_notification(self::TORRENTS, 0, $Title, 'torrents.php?action=notify', self::INFO);
  351. }
  352. }
  353. public function load_collage_subscriptions() {
  354. if (check_perms('site_collages_subscribe')) {
  355. $NewCollages = G::$Cache->get_value('collage_subs_user_new_' . G::$LoggedUser['ID']);
  356. if ($NewCollages === false) {
  357. $QueryID = G::$DB->get_query_id();
  358. G::$DB->query("
  359. SELECT COUNT(DISTINCT s.CollageID)
  360. FROM users_collage_subs AS s
  361. JOIN collages AS c ON s.CollageID = c.ID
  362. JOIN collages_torrents AS ct ON ct.CollageID = c.ID
  363. WHERE s.UserID = " . G::$LoggedUser['ID'] . "
  364. AND ct.AddedOn > s.LastVisit
  365. AND c.Deleted = '0'");
  366. list($NewCollages) = G::$DB->next_record();
  367. G::$DB->set_query_id($QueryID);
  368. G::$Cache->cache_value('collage_subs_user_new_' . G::$LoggedUser['ID'], $NewCollages, 0);
  369. }
  370. if ($NewCollages > 0) {
  371. $Title = 'You have ' . ($NewCollages == 1 ? 'a' : $NewCollages) . ' new collage update' . ($NewCollages > 1 ? 's' : '');
  372. $this->create_notification(self::COLLAGES, 0, $Title, 'userhistory.php?action=subscribed_collages', self::INFO);
  373. }
  374. }
  375. }
  376. public function load_quote_notifications() {
  377. if (isset(G::$LoggedUser['NotifyOnQuote']) && G::$LoggedUser['NotifyOnQuote']) {
  378. $QuoteNotificationsCount = Subscriptions::has_new_quote_notifications();
  379. if ($QuoteNotificationsCount > 0) {
  380. $Title = 'New quote' . ($QuoteNotificationsCount > 1 ? 's' : '');
  381. $this->create_notification(self::QUOTES, 0, $Title, 'userhistory.php?action=quote_notifications', self::INFO);
  382. }
  383. }
  384. }
  385. public function load_subscriptions() {
  386. $SubscriptionsCount = Subscriptions::has_new_subscriptions();
  387. if ($SubscriptionsCount > 0) {
  388. $Title = 'New subscription' . ($SubscriptionsCount > 1 ? 's' : '');
  389. $this->create_notification(self::SUBSCRIPTIONS, 0, $Title, 'userhistory.php?action=subscriptions', self::INFO);
  390. }
  391. }
  392. public static function clear_news($News) {
  393. $QueryID = G::$DB->get_query_id();
  394. if (!$News) {
  395. if (!$News = G::$Cache->get_value('news')) {
  396. G::$DB->query('
  397. SELECT
  398. ID,
  399. Title,
  400. Body,
  401. Time
  402. FROM news
  403. ORDER BY Time DESC
  404. LIMIT 1');
  405. $News = G::$DB->to_array(false, MYSQLI_NUM, false);
  406. G::$Cache->cache_value('news_latest_id', $News[0][0], 0);
  407. }
  408. }
  409. if (G::$LoggedUser['LastReadNews'] != $News[0][0]) {
  410. G::$Cache->begin_transaction('user_info_heavy_' . G::$LoggedUser['ID']);
  411. G::$Cache->update_row(false, array('LastReadNews' => $News[0][0]));
  412. G::$Cache->commit_transaction(0);
  413. G::$DB->query("
  414. UPDATE users_info
  415. SET LastReadNews = '".$News[0][0]."'
  416. WHERE UserID = " . G::$LoggedUser['ID']);
  417. G::$LoggedUser['LastReadNews'] = $News[0][0];
  418. }
  419. G::$DB->set_query_id($QueryID);
  420. }
  421. public static function clear_blog($Blog) {
  422. $QueryID = G::$DB->get_query_id();
  423. if (!isset($Blog) || !$Blog) {
  424. if (!$Blog = G::$Cache->get_value('blog')) {
  425. G::$DB->query("
  426. SELECT
  427. b.ID,
  428. um.Username,
  429. b.UserID,
  430. b.Title,
  431. b.Body,
  432. b.Time,
  433. b.ThreadID
  434. FROM blog AS b
  435. LEFT JOIN users_main AS um ON b.UserID = um.ID
  436. ORDER BY Time DESC
  437. LIMIT 1");
  438. $Blog = G::$DB->to_array();
  439. }
  440. }
  441. if (G::$LoggedUser['LastReadBlog'] < $Blog[0][0]) {
  442. G::$Cache->begin_transaction('user_info_heavy_' . G::$LoggedUser['ID']);
  443. G::$Cache->update_row(false, array('LastReadBlog' => $Blog[0][0]));
  444. G::$Cache->commit_transaction(0);
  445. G::$DB->query("
  446. UPDATE users_info
  447. SET LastReadBlog = '". $Blog[0][0]."'
  448. WHERE UserID = " . G::$LoggedUser['ID']);
  449. G::$LoggedUser['LastReadBlog'] = $Blog[0][0];
  450. }
  451. G::$DB->set_query_id($QueryID);
  452. }
  453. public static function clear_staff_pms() {
  454. $QueryID = G::$DB->get_query_id();
  455. G::$DB->query("
  456. SELECT ID
  457. FROM staff_pm_conversations
  458. WHERE Unread = true
  459. AND UserID = " . G::$LoggedUser['ID']);
  460. $IDs = [];
  461. while (list($ID) = G::$DB->next_record()) {
  462. $IDs[] = $ID;
  463. }
  464. $IDs = implode(',', $IDs);
  465. if (!empty($IDs)) {
  466. G::$DB->query("
  467. UPDATE staff_pm_conversations
  468. SET Unread = false
  469. WHERE ID IN ($IDs)");
  470. }
  471. G::$Cache->delete_value('staff_pm_new_' . G::$LoggedUser['ID']);
  472. G::$DB->set_query_id($QueryID);
  473. }
  474. public static function clear_inbox() {
  475. $QueryID = G::$DB->get_query_id();
  476. G::$DB->query("
  477. SELECT ConvID
  478. FROM pm_conversations_users
  479. WHERE Unread = '1'
  480. AND UserID = " . G::$LoggedUser['ID']);
  481. $IDs = [];
  482. while (list($ID) = G::$DB->next_record()) {
  483. $IDs[] = $ID;
  484. }
  485. $IDs = implode(',', $IDs);
  486. if (!empty($IDs)) {
  487. G::$DB->query("
  488. UPDATE pm_conversations_users
  489. SET Unread = '0'
  490. WHERE ConvID IN ($IDs)
  491. AND UserID = " . G::$LoggedUser['ID']);
  492. }
  493. G::$Cache->delete_value('inbox_new_' . G::$LoggedUser['ID']);
  494. G::$DB->set_query_id($QueryID);
  495. }
  496. public static function clear_torrents() {
  497. $QueryID = G::$DB->get_query_id();
  498. G::$DB->query("
  499. SELECT TorrentID
  500. FROM users_notify_torrents
  501. WHERE UserID = ' " . G::$LoggedUser['ID'] . "'
  502. AND UnRead = '1'");
  503. $IDs = [];
  504. while (list($ID) = G::$DB->next_record()) {
  505. $IDs[] = $ID;
  506. }
  507. $IDs = implode(',', $IDs);
  508. if (!empty($IDs)) {
  509. G::$DB->query("
  510. UPDATE users_notify_torrents
  511. SET Unread = '0'
  512. WHERE TorrentID IN ($IDs)
  513. AND UserID = " . G::$LoggedUser['ID']);
  514. }
  515. G::$Cache->delete_value('notifications_new_' . G::$LoggedUser['ID']);
  516. G::$DB->set_query_id($QueryID);
  517. }
  518. public static function clear_collages() {
  519. $QueryID = G::$DB->get_query_id();
  520. G::$DB->query("
  521. UPDATE users_collage_subs
  522. SET LastVisit = NOW()
  523. WHERE UserID = " . G::$LoggedUser['ID']);
  524. G::$Cache->delete_value('collage_subs_user_new_' . G::$LoggedUser['ID']);
  525. G::$DB->set_query_id($QueryID);
  526. }
  527. public static function clear_quotes() {
  528. $QueryID = G::$DB->get_query_id();
  529. G::$DB->query("
  530. UPDATE users_notify_quoted
  531. SET UnRead = '0'
  532. WHERE UserID = " . G::$LoggedUser['ID']);
  533. G::$Cache->delete_value('notify_quoted_' . G::$LoggedUser['ID']);
  534. G::$DB->set_query_id($QueryID);
  535. }
  536. public static function clear_subscriptions() {
  537. $QueryID = G::$DB->get_query_id();
  538. if (($UserSubscriptions = G::$Cache->get_value('subscriptions_user_' . G::$LoggedUser['ID'])) === false) {
  539. G::$DB->query("
  540. SELECT TopicID
  541. FROM users_subscriptions
  542. WHERE UserID = " . G::$LoggedUser['ID']);
  543. if ($UserSubscriptions = G::$DB->collect(0)) {
  544. G::$Cache->cache_value('subscriptions_user_' . G::$LoggedUser['ID'], $UserSubscriptions, 0);
  545. }
  546. }
  547. if (!empty($UserSubscriptions)) {
  548. G::$DB->query("
  549. INSERT INTO forums_last_read_topics (UserID, TopicID, PostID)
  550. SELECT '" . G::$LoggedUser['ID'] . "', ID, LastPostID
  551. FROM forums_topics
  552. WHERE ID IN (".implode(',', $UserSubscriptions).')
  553. ON DUPLICATE KEY UPDATE
  554. PostID = LastPostID');
  555. }
  556. G::$Cache->delete_value('subscriptions_user_new_' . G::$LoggedUser['ID']);
  557. G::$DB->set_query_id($QueryID);
  558. }
  559. /*
  560. // TODO: Figure out what these functions are supposed to do and fix them
  561. public static function send_notification($UserID, $ID, $Type, $Message, $URL, $Importance = 'alert', $AutoExpire = false) {
  562. $Notifications = G::$Cache->get_value("user_cache_notifications_$UserID");
  563. if (empty($Notifications)) {
  564. $Notifications = [];
  565. }
  566. array_unshift($Notifications, $this->create_notification($Type, $ID, $Message, $URL, $Importance, $AutoExpire));
  567. G::$Cache->cache_value("user_cache_notifications_$UserID", $Notifications, 0);
  568. }
  569. public static function clear_notification($UserID, $Index) {
  570. $Notifications = G::$Cache->get_value("user_cache_notifications_$UserID");
  571. if (count($Notifications)) {
  572. unset($Notifications[$Index]);
  573. $Notifications = array_values($Notifications);
  574. G::$Cache->cache_value("user_cache_notifications_$UserID", $Notifications, 0);
  575. }
  576. }
  577. */
  578. public static function get_settings($UserID) {
  579. $Results = G::$Cache->get_value("users_notifications_settings_$UserID");
  580. if (!$Results) {
  581. $QueryID = G::$DB->get_query_id();
  582. G::$DB->query("
  583. SELECT *
  584. FROM users_notifications_settings
  585. WHERE UserID = ?", $UserID);
  586. $Results = G::$DB->next_record(MYSQLI_ASSOC, false);
  587. G::$DB->set_query_id($QueryID);
  588. G::$Cache->cache_value("users_notifications_settings_$UserID", $Results, 0);
  589. }
  590. return $Results;
  591. }
  592. public static function save_settings($UserID, $Settings = false) {
  593. if (!is_array($Settings)) {
  594. // A little cheat technique, gets all keys in the $_POST array starting with 'notifications_'
  595. $Settings = array_intersect_key($_POST, array_flip(preg_grep('/^notifications_/', array_keys($_POST))));
  596. }
  597. $Update = [];
  598. foreach (self::$Types as $Type) {
  599. $Popup = array_key_exists("notifications_{$Type}_popup", $Settings);
  600. $Traditional = array_key_exists("notifications_{$Type}_traditional", $Settings);
  601. $Result = self::OPT_DISABLED;
  602. if ($Popup) {
  603. $Result = self::OPT_POPUP;
  604. } elseif ($Traditional) {
  605. $Result = self::OPT_TRADITIONAL;
  606. }
  607. $Update[] = "$Type = $Result";
  608. }
  609. $Update = implode(',', $Update);
  610. $QueryID = G::$DB->get_query_id();
  611. G::$DB->query("
  612. UPDATE users_notifications_settings
  613. SET $Update
  614. WHERE UserID = ?", $UserID);
  615. G::$DB->set_query_id($QueryID);
  616. G::$Cache->delete_value("users_notifications_settings_$UserID");
  617. }
  618. public function is_traditional($Type) {
  619. return $this->Settings[$Type] == self::OPT_TRADITIONAL;
  620. }
  621. public function is_skipped($Type) {
  622. return isset($this->Skipped[$Type]);
  623. }
  624. public function use_noty() {
  625. return in_array(self::OPT_POPUP, $this->Settings);
  626. }
  627. }