mc-cms-namelessmc/core/classes/Core/Alert.php

170 lines
6 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Provides access to create & get alerts for a user, as well as their PMs.
*
* @package NamelessMC\Core
* @author Samerton
* @version 2.0.0-pr8
* @license MIT
2020-12-13 19:38:04 +01:00
*/
class Alert
{
/**
* Creates an alert for the specified user.
2021-12-07 22:14:12 -08:00
*
* @deprecated Use Alert::send instead
*
* @param int $user_id Contains the ID of the user who we are creating the alert for.
* @param string $type Contains the alert type, eg 'tag' for user tagging.
* @param array $text_short Contains the alert text in short form for the dropdown.
* @param array $text Contains full information about the alert.
* @param ?string $link Contains link to view the alert, defaults to #.
* @param ?string $content Optional alert content.
*/
public static function create(int $user_id, string $type, array $text_short, array $text, ?string $link = '#', ?string $content = null): void
{
2020-12-13 19:38:04 +01:00
$db = DB::getInstance();
2022-05-01 23:16:22 -07:00
$language = $db->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]);
2020-12-13 19:38:04 +01:00
if (!$language->count()) {
return;
2020-12-13 19:38:04 +01:00
}
2022-04-23 16:55:44 +02:00
$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,
]);
2020-12-13 19:38:04 +01:00
}
/**
* Get user alerts.
2021-12-07 22:14:12 -08:00
*
* @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).
2021-12-07 22:14:12 -08:00
*
* @return array All their alerts.
*/
public static function getAlerts(int $user_id, bool $all = false): array
{
2020-12-13 19:38:04 +01:00
$db = DB::getInstance();
2020-12-13 20:42:04 -08:00
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
);
2020-12-13 19:38:04 +01:00
}
/**
* Get a users unread messages.
2021-12-07 22:14:12 -08:00
*
* @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.
2021-12-07 22:14:12 -08:00
*
* @return array All their messages matching the $all filter.
*/
public static function getPMs(int $user_id, bool $all = false): array
{
2020-12-13 19:38:04 +01:00
$db = DB::getInstance();
2020-12-13 20:42:04 -08:00
if ($all == true) {
2022-05-01 23:22:19 -07:00
$pms_access = $db->get('private_messages_users', ['user_id', $user_id])->results();
2021-10-29 22:00:05 -07:00
$pms = [];
2020-12-13 19:38:04 +01:00
2020-12-13 20:42:04 -08:00
foreach ($pms_access as $pm) {
2020-12-13 19:38:04 +01:00
// Get actual PM information
2022-05-01 23:22:19 -07:00
$pm_full = $db->get('private_messages', ['id', $pm->pm_id])->results();
2020-12-13 19:38:04 +01:00
2021-12-07 22:14:12 -08:00
if (!count($pm_full)) {
continue;
}
2020-12-13 19:38:04 +01:00
2022-01-15 15:18:05 -08:00
$pm_full = $pm_full[0];
2021-10-29 22:00:05 -07:00
$pms[] = [
2020-12-13 19:38:04 +01:00
'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,
2021-10-29 22:00:05 -07:00
];
2020-12-13 19:38:04 +01:00
}
return $pms;
}
2022-05-01 23:22:19 -07:00
$pms = $db->get('private_messages_users', ['user_id', $user_id])->results();
2021-10-29 22:00:05 -07:00
$unread = [];
foreach ($pms as $pm) {
if ($pm->read == 0) {
2022-05-01 23:22:19 -07:00
$pm_full = $db->get('private_messages', ['id', $pm->pm_id])->results();
2021-12-07 22:14:12 -08:00
if (!count($pm_full)) {
continue;
}
2022-01-15 15:18:05 -08:00
$pm_full = $pm_full[0];
2021-10-29 22:00:05 -07:00
$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,
2021-10-29 22:00:05 -07:00
];
2020-12-13 19:38:04 +01:00
}
}
2021-12-07 22:14:12 -08:00
return $unread;
2020-12-13 19:38:04 +01:00
}
}