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 25KB

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