2016-11-29 22:27:19 +00:00
< ? php
2025-03-29 10:50:03 +00:00
2022-02-14 20:48:15 -08:00
/**
* Provides access to create & get alerts for a user , as well as their PMs .
2016-11-29 22:27:19 +00:00
*
2022-02-14 20:48:15 -08:00
* @ package NamelessMC\Core
* @ author Samerton
* @ version 2.0 . 0 - pr8
* @ license MIT
2020-12-13 19:38:04 +01:00
*/
2024-03-09 03:19:55 -08:00
class Alert
{
2021-04-12 18:36:34 -07:00
/**
* Creates an alert for the specified user .
2021-12-07 22:14:12 -08:00
*
2024-03-31 20:19:43 +01: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 .
2021-04-12 18:36:34 -07:00
*/
2025-03-29 10:50:03 +00:00
public static function create ( int $user_id , string $type , array $text_short , array $text , ? string $link = '#' , ? string $content = null ) : void
2024-03-09 03:19:55 -08:00
{
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
2022-02-14 20:48:15 -08:00
if ( ! $language -> count ()) {
return ;
2020-12-13 19:38:04 +01:00
}
2022-02-14 20:48:15 -08:00
2022-04-23 16:55:44 +02:00
$language = new Language ( $text_short [ 'path' ], $language -> first () -> short_code );
2022-02-14 20:48:15 -08:00
2024-03-31 20:19:43 +01:00
$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' ]));
2022-02-14 20:48:15 -08:00
$db -> insert ( 'alerts' , [
'user_id' => $user_id ,
'type' => $type ,
'url' => $link ,
2024-03-31 20:19:43 +01:00
'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
2025-04-29 07:43:09 -07:00
* @ param string | null $content
2024-03-31 20:19:43 +01:00
* @ 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
*/
2025-04-29 07:43:09 -07:00
public static function send ( int $userId , string $title , ? string $content , ? string $link = '' , bool $skipPurify = false )
2024-03-31 20:19:43 +01:00
{
DB :: getInstance () -> insert ( 'alerts' , [
'user_id' => $userId ,
'type' => 'alert' ,
'url' => $link ? ? '' ,
'content_short' => $title , // Column maintained for legacy reasons
'content' => $title ,
2025-04-29 07:43:09 -07:00
'content_rich' => $content ? ? '' ,
2024-03-09 03:19:55 -08:00
'created' => date ( 'U' ),
2024-03-31 20:19:43 +01:00
'bypass_purify' => $skipPurify ,
2022-02-14 20:48:15 -08:00
]);
2020-12-13 19:38:04 +01:00
}
2021-04-12 18:36:34 -07:00
/**
* Get user alerts .
2021-12-07 22:14:12 -08:00
*
2024-03-09 03:19:55 -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
*
2021-04-12 18:36:34 -07:00
* @ return array All their alerts .
*/
2024-03-09 03:19:55 -08:00
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 ) {
2026-08-16 09:22:17 +01:00
$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 ();
2021-04-12 18:36:34 -07:00
}
2026-08-16 09:22:17 +01:00
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
}
2021-04-12 18:36:34 -07:00
/**
2022-02-14 20:48:15 -08:00
* Get a users unread messages .
2021-12-07 22:14:12 -08:00
*
2024-03-09 03:19:55 -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
*
2021-04-12 18:36:34 -07:00
* @ return array All their messages matching the $all filter .
*/
2024-03-09 03:19:55 -08:00
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 ,
2024-03-09 03:19:55 -08:00
'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 ;
2021-04-12 18:36:34 -07:00
}
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 = [];
2021-04-12 18:36:34 -07:00
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-04-12 18:36:34 -07:00
2021-12-07 22:14:12 -08:00
if ( ! count ( $pm_full )) {
continue ;
}
2021-04-12 18:36:34 -07:00
2022-01-15 15:18:05 -08:00
$pm_full = $pm_full [ 0 ];
2021-10-29 22:00:05 -07:00
$unread [] = [
2021-04-12 18:36:34 -07: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 ,
2024-03-09 03:19:55 -08:00
'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
2021-04-12 18:36:34 -07:00
return $unread ;
2020-12-13 19:38:04 +01:00
}
}