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 Permalink Normal View History

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