2022-09-13 13:14:49 +00:00
< ? php
namespace Misuzu\Http\Handlers ;
use Misuzu\Config ;
2023-01-01 20:23:53 +00:00
use Misuzu\Config\CfgType ;
2022-09-13 13:14:49 +00:00
use Misuzu\DB ;
use Misuzu\Pagination ;
use Misuzu\Template ;
use Misuzu\Changelog\ChangelogChange ;
use Misuzu\News\NewsPost ;
use Misuzu\Users\User ;
use Misuzu\Users\UserSession ;
final class HomeHandler extends Handler {
public function index ( $response , $request ) : void {
if ( UserSession :: hasCurrent ())
$this -> home ( $response , $request );
else
$this -> landing ( $response , $request );
}
public function landing ( $response , $request ) : void {
2023-01-01 20:23:53 +00:00
$linkedData = Config :: get ( 'social.embed_linked' , CfgType :: T_BOOL )
2022-09-13 13:14:49 +00:00
? [
2023-01-01 20:23:53 +00:00
'name' => Config :: get ( 'site.name' , CfgType :: T_STR , 'Misuzu' ),
'url' => Config :: get ( 'site.url' , CfgType :: T_STR ),
'logo' => Config :: get ( 'site.ext_logo' , CfgType :: T_STR ),
'same_as' => Config :: get ( 'social.linked' , CfgType :: T_ARR ),
2022-09-13 13:14:49 +00:00
] : null ;
$featuredNews = NewsPost :: all ( new Pagination ( 3 ), true );
$stats = DB :: query (
'SELECT'
. ' (SELECT COUNT(`user_id`) FROM `msz_users` WHERE `user_deleted` IS NULL) AS `count_users_all`,'
. ' (SELECT COUNT(`user_id`) FROM `msz_users` WHERE `user_active` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)) AS `count_users_online`,'
. ' (SELECT COUNT(`user_id`) FROM `msz_users` WHERE `user_active` >= DATE_SUB(NOW(), INTERVAL 24 HOUR)) AS `count_users_active`,'
. ' (SELECT COUNT(`comment_id`) FROM `msz_comments_posts` WHERE `comment_deleted` IS NULL) AS `count_comments`,'
. ' (SELECT COUNT(`topic_id`) FROM `msz_forum_topics` WHERE `topic_deleted` IS NULL) AS `count_forum_topics`,'
. ' (SELECT COUNT(`post_id`) FROM `msz_forum_posts` WHERE `post_deleted` IS NULL) AS `count_forum_posts`'
) -> fetch ();
$onlineUsers = DB :: query (
'SELECT u.`user_id`, u.`username`, COALESCE(u.`user_colour`, r.`role_colour`) AS `user_colour`'
. ' FROM `msz_users` AS u'
. ' LEFT JOIN `msz_roles` AS r'
. ' ON r.`role_id` = u.`display_role`'
. ' WHERE u.`user_active` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)'
. ' ORDER BY u.`user_active` DESC, RAND()'
. ' LIMIT 100'
) -> fetchAll ();
// TODO: don't hardcode forum ids
2023-01-01 20:23:53 +00:00
$featuredForums = Config :: get ( 'landing.forum_categories' , CfgType :: T_ARR );
2022-09-13 13:14:49 +00:00
$popularTopics = [];
$activeTopics = [];
if ( ! empty ( $featuredForums )) {
$getPopularTopics = DB :: prepare (
'SELECT t.`topic_id`, c.`forum_id`, t.`topic_title`, c.`forum_icon`, t.`topic_count_views`'
. ', (SELECT COUNT(*) FROM `msz_forum_posts` AS p WHERE p.`topic_id` = t.`topic_id` AND `post_deleted` IS NULL) AS `topic_count_posts`'
. ' FROM `msz_forum_topics` AS t'
. ' LEFT JOIN `msz_forum_categories` AS c ON c.`forum_id` = t.`forum_id`'
. ' WHERE c.`forum_id` IN (' . implode ( ',' , $featuredForums ) . ') AND `topic_deleted` IS NULL AND `topic_locked` IS NULL'
. ' ORDER BY (SELECT COUNT(*) FROM `msz_forum_posts` AS p WHERE p.`topic_id` = t.`topic_id` AND `post_deleted` IS NULL AND `post_created` > NOW() - INTERVAL 3 MONTH) DESC'
) -> stmt ;
$getPopularTopics -> execute ();
for ( $i = 0 ; $i < 10 ; ++ $i ) {
$topicInfo = $getPopularTopics -> fetchObject ();
if ( empty ( $topicInfo ))
break ;
$popularTopics [] = $topicInfo ;
}
$getActiveTopics = DB :: prepare (
'SELECT t.`topic_id`, c.`forum_id`, t.`topic_title`, c.`forum_icon`, t.`topic_count_views`'
. ', (SELECT COUNT(*) FROM `msz_forum_posts` AS p WHERE p.`topic_id` = t.`topic_id` AND `post_deleted` IS NULL) AS `topic_count_posts`'
. ', (SELECT MAX(`post_id`) FROM `msz_forum_posts` AS p WHERE p.`topic_id` = t.`topic_id` AND `post_deleted` IS NULL) AS `latest_post_id`'
. ' FROM `msz_forum_topics` AS t'
. ' LEFT JOIN `msz_forum_categories` AS c ON c.`forum_id` = t.`forum_id`'
. ' WHERE c.`forum_id` IN (' . implode ( ',' , $featuredForums ) . ') AND `topic_deleted` IS NULL AND `topic_locked` IS NULL'
. ' ORDER BY `topic_bumped` DESC'
) -> stmt ;
$getActiveTopics -> execute ();
for ( $i = 0 ; $i < 10 ; ++ $i ) {
$topicInfo = $getActiveTopics -> fetchObject ();
if ( empty ( $topicInfo ))
break ;
$activeTopics [] = $topicInfo ;
}
}
$response -> setContent ( Template :: renderRaw ( 'home.landing' , [
'statistics' => $stats ,
'online_users' => $onlineUsers ,
'featured_news' => $featuredNews ,
'linked_data' => $linkedData ,
'forum_popular' => $popularTopics ,
'forum_active' => $activeTopics ,
]));
}
public function home ( $response , $request ) : void {
$featuredNews = NewsPost :: all ( new Pagination ( 5 ), true );
$stats = DB :: query (
'SELECT'
. ' (SELECT COUNT(`user_id`) FROM `msz_users` WHERE `user_deleted` IS NULL) AS `count_users_all`,'
. ' (SELECT COUNT(`user_id`) FROM `msz_users` WHERE `user_active` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)) AS `count_users_online`,'
. ' (SELECT COUNT(`user_id`) FROM `msz_users` WHERE `user_active` >= DATE_SUB(NOW(), INTERVAL 24 HOUR)) AS `count_users_active`,'
. ' (SELECT COUNT(`comment_id`) FROM `msz_comments_posts` WHERE `comment_deleted` IS NULL) AS `count_comments`,'
. ' (SELECT COUNT(`topic_id`) FROM `msz_forum_topics` WHERE `topic_deleted` IS NULL) AS `count_forum_topics`,'
. ' (SELECT COUNT(`post_id`) FROM `msz_forum_posts` WHERE `post_deleted` IS NULL) AS `count_forum_posts`'
) -> fetch ();
$changelog = ChangelogChange :: all ( new Pagination ( 10 ));
$birthdays = User :: byBirthdate ();
$latestUser = ! empty ( $birthdays ) ? null : User :: byLatest ();
$onlineUsers = DB :: query (
'SELECT u.`user_id`, u.`username`, COALESCE(u.`user_colour`, r.`role_colour`) AS `user_colour`'
. ' FROM `msz_users` AS u'
. ' LEFT JOIN `msz_roles` AS r'
. ' ON r.`role_id` = u.`display_role`'
. ' WHERE u.`user_active` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)'
. ' ORDER BY u.`user_active` DESC, RAND()'
. ' LIMIT 104'
) -> fetchAll ();
$response -> setContent ( Template :: renderRaw ( 'home.home' , [
'statistics' => $stats ,
'latest_user' => $latestUser ,
'online_users' => $onlineUsers ,
'birthdays' => $birthdays ,
'featured_changelog' => $changelog ,
'featured_news' => $featuredNews ,
]));
}
}