query('SELECT nl2_languages.short_code AS `short_code` FROM nl2_users LEFT JOIN nl2_languages ON nl2_languages.id = nl2_users.language_id WHERE nl2_users.id = ?', [$user_id]); if (!$language->count()) { return; } $language = new Language($text_short['path'], $language->first()->short_code); $text_short = $text_short['content'] ?? str_replace($text_short['replace'] ?? '', $text_short['replace_with'] ?? '', $language->get($text_short['file'], $text_short['term'])); $text = $text['content'] ?? str_replace($text['replace'] ?? '', $text['replace_with'] ?? '', $language->get($text['file'], $text['term'])); $db->insert('alerts', [ 'user_id' => $user_id, 'type' => $type, 'url' => $link, 'content_short' => $text_short, 'content' => $text, 'content_rich' => $content, 'created' => date('U'), ]); } /** * Post a new alert to a user. * * @param int $userId * @param string $title * @param string|null $content * @param string|null $link Optional link to redirect the user to on click * @param bool $skipPurify If true the content will not be purified before displaying to user - use with care * @return void */ public static function send(int $userId, string $title, ?string $content, ?string $link = '', bool $skipPurify = false) { DB::getInstance()->insert('alerts', [ 'user_id' => $userId, 'type' => 'alert', 'url' => $link ?? '', 'content_short' => $title, // Column maintained for legacy reasons 'content' => $title, 'content_rich' => $content ?? '', 'created' => date('U'), 'bypass_purify' => $skipPurify, ]); } /** * Get user alerts. * * @param int $user_id Contains the ID of the user who we are getting alerts for. * @param bool $all Do we want to get all alerts (including read), or not; defaults to false). * * @return array All their alerts. */ public static function getAlerts(int $user_id, bool $all = false): array { $db = DB::getInstance(); if ($all == true) { $alerts = $db->get('alerts', ['user_id', $user_id])->results(); } else { $alerts = $db->query('SELECT * FROM nl2_alerts WHERE user_id = ? AND `read` = 0', [$user_id])->results(); } return array_map( static fn($alert) => [ ...(array) $alert, 'content' => Output::getClean($alert->content), 'content_short' => Output::getClean($alert->content_short), 'content_rich' => $alert->bypass_purify ? $alert->content_rich : Output::getPurified($alert->content_rich, false, false), ], $alerts ); } /** * Get a users unread messages. * * @param int $user_id The ID of the user who we are getting messages for. * @param bool $all Get all alerts (including read), or not. Defaults to false. * * @return array All their messages matching the $all filter. */ public static function getPMs(int $user_id, bool $all = false): array { $db = DB::getInstance(); if ($all == true) { $pms_access = $db->get('private_messages_users', ['user_id', $user_id])->results(); $pms = []; foreach ($pms_access as $pm) { // Get actual PM information $pm_full = $db->get('private_messages', ['id', $pm->pm_id])->results(); if (!count($pm_full)) { continue; } $pm_full = $pm_full[0]; $pms[] = [ 'id' => $pm_full->id, 'title' => Output::getClean($pm_full->title), 'created' => $pm_full->created, 'author_id' => $pm_full->author_id, 'last_reply_user' => $pm_full->last_reply_user, 'last_reply_date' => $pm_full->last_reply_date, ]; } return $pms; } $pms = $db->get('private_messages_users', ['user_id', $user_id])->results(); $unread = []; foreach ($pms as $pm) { if ($pm->read == 0) { $pm_full = $db->get('private_messages', ['id', $pm->pm_id])->results(); if (!count($pm_full)) { continue; } $pm_full = $pm_full[0]; $unread[] = [ 'id' => $pm_full->id, 'title' => Output::getClean($pm_full->title), 'created' => $pm_full->created, 'author_id' => $pm_full->author_id, 'last_reply_user' => $pm_full->last_reply_user, 'last_reply_date' => $pm_full->last_reply_date, ]; } } return $unread; } }