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

96 lines
2.6 KiB
PHP
Raw Normal View History

2016-01-30 00:18:23 +00:00
<?php
2016-02-03 22:22:56 +00:00
/**
2016-12-21 16:28:51 +00:00
* Holds the homepage controller.
2016-02-03 22:22:56 +00:00
* @package Sakura
*/
2016-01-30 00:18:23 +00:00
namespace Sakura\Controllers;
2016-01-30 13:25:18 +00:00
use Sakura\Config;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
2016-12-21 19:55:17 +00:00
use Sakura\News\Post;
2016-01-30 00:18:23 +00:00
use Sakura\Template;
2016-01-30 13:25:18 +00:00
use Sakura\User;
2016-12-21 19:55:17 +00:00
use stdClass;
2016-01-30 00:18:23 +00:00
/**
2016-12-21 16:28:51 +00:00
* Homepage controller.
2016-01-30 00:18:23 +00:00
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
2016-01-30 00:18:23 +00:00
*/
2016-12-21 16:28:51 +00:00
class HomeController extends Controller
2016-01-30 00:18:23 +00:00
{
2016-02-02 21:04:15 +00:00
/**
* Serves the site index.
2016-08-05 02:35:37 +00:00
* @return string
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function index(): string
2016-01-30 13:25:18 +00:00
{
2016-03-27 21:18:57 +00:00
// Get the newest user
$newestUserId = DB::table('users')
2016-12-07 15:33:26 +00:00
->where('rank_main', '!=', config('rank.banned'))
->where('user_activated', 1)
->where('user_restricted', 0)
2016-03-27 21:18:57 +00:00
->orderBy('user_id', 'desc')
->limit(1)
->get(['user_id']);
2016-12-21 16:46:12 +00:00
2016-03-27 21:18:57 +00:00
$newestUser = User::construct($newestUserId ? $newestUserId[0]->user_id : 0);
// Get all the currently online users
2016-07-26 17:29:53 +00:00
$timeRange = time() - 120;
2016-03-27 21:18:57 +00:00
// 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;
}
2016-12-21 19:55:17 +00:00
$news = DB::table('news_posts')
2016-12-21 20:11:03 +00:00
->whereNull('deleted_at')
2016-12-21 19:55:17 +00:00
->whereIn('category_id', explode(',', config('general.homepage-news')))
->limit(3)
2017-03-22 19:51:50 +00:00
->orderBy('created_at', 'desc')
2016-12-21 19:55:17 +00:00
->get();
$news = array_map(function (stdClass $row) {
return Post::fromRow($row);
}, $news);
2016-12-21 16:46:12 +00:00
$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');
2016-03-28 14:47:43 +00:00
2016-12-21 16:46:12 +00:00
$trashId = config('forum.trash');
$topicCount = DB::table('topics')
->where('forum_id', '!=', $trashId)
->count();
$postCount = DB::table('posts')
->where('forum_id', '!=', $trashId)
->count();
2016-01-30 13:25:18 +00:00
2016-12-21 16:46:12 +00:00
return view('home/index', compact('news', 'userCount', 'newestUser', 'lastRegDate', 'topicCount', 'postCount', 'onlineUsers'));
2016-01-30 13:25:18 +00:00
}
2016-01-30 00:18:23 +00:00
}