This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Controllers/HomeController.php

95 lines
2.5 KiB
PHP

<?php
/**
* Holds the homepage controller.
* @package Sakura
*/
namespace Sakura\Controllers;
use Sakura\Config;
use Sakura\DB;
use Sakura\News\Post;
use Sakura\Template;
use Sakura\User;
use stdClass;
/**
* Homepage controller.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class HomeController extends Controller
{
/**
* Serves the site index.
* @return string
*/
public function index(): string
{
// Get the newest user
$newestUserId = DB::table('users')
->where('rank_main', '!=', config('rank.banned'))
->where('user_activated', 1)
->where('user_restricted', 0)
->orderBy('user_id', 'desc')
->limit(1)
->get(['user_id']);
$newestUser = User::construct($newestUserId ? $newestUserId[0]->user_id : 0);
// Get all the currently online users
$timeRange = time() - 120;
// Create a storage variable
$onlineUsers = [];
// Get all online users
$getOnline = DB::table('users')
->where('user_last_online', '>', $timeRange)
->get(['user_id']);
$getOnline = array_column($getOnline, 'user_id');
foreach ($getOnline as $user) {
$user = User::construct($user);
// Do a second check
if (!$user->isOnline()) {
continue;
}
$onlineUsers[$user->id] = $user;
}
$news = DB::table('news_posts')
->whereNull('deleted_at')
->whereIn('category_id', explode(',', config('general.homepage-news')))
->limit(3)
->get();
$news = array_map(function (stdClass $row) {
return Post::fromRow($row);
}, $news);
$userCount = DB::table('users')
->where('rank_main', '!=', config('rank.banned'))
->where('user_activated', 1)
->where('user_restricted', 0)
->count();
$lastRegDate = date_diff(
date_create(date('Y-m-d', $newestUser->registered)),
date_create(date('Y-m-d'))
)->format('%a');
$trashId = config('forum.trash');
$topicCount = DB::table('topics')
->where('forum_id', '!=', $trashId)
->count();
$postCount = DB::table('posts')
->where('forum_id', '!=', $trashId)
->count();
return view('home/index', compact('news', 'userCount', 'newestUser', 'lastRegDate', 'topicCount', 'postCount', 'onlineUsers'));
}
}