diff --git a/app/Controllers/HomeController.php b/app/Controllers/HomeController.php index 1eade3c..b62eb27 100644 --- a/app/Controllers/HomeController.php +++ b/app/Controllers/HomeController.php @@ -8,9 +8,10 @@ namespace Sakura\Controllers; use Sakura\Config; use Sakura\DB; -use Sakura\News\Category; +use Sakura\News\Post; use Sakura\Template; use Sakura\User; +use stdClass; /** * Homepage controller. @@ -59,7 +60,14 @@ class HomeController extends Controller $onlineUsers[$user->id] = $user; } - $news = []; //new Category(config('general.news')); + $news = DB::table('news_posts') + ->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')) diff --git a/app/Controllers/NewsController.php b/app/Controllers/NewsController.php index 8ffc23f..ddd1335 100644 --- a/app/Controllers/NewsController.php +++ b/app/Controllers/NewsController.php @@ -6,11 +6,13 @@ namespace Sakura\Controllers; +use Illuminate\Database\Query\JoinClause; use Phroute\Phroute\Exception\HttpRouteNotFoundException; use Sakura\DB; use Sakura\Config; use Sakura\News\Category; use Sakura\News\Post; +use stdClass; /** * News controller. @@ -35,10 +37,23 @@ class NewsController extends Controller */ public function index(): string { - $categories = DB::table('news_categories')->get(); - $news = new Category; + $categories = DB::table('news_categories') + ->where('category_hidden', 0) + ->get(); - return view('news/index', compact('categories', 'news')); + $posts = DB::table('news_posts') + ->join('news_categories', function (JoinClause $join) { + $join->on('news_posts.category_id', '=', 'news_categories.category_id') + ->where('news_categories.category_hidden', '=', 0); + }) + ->limit(5) + ->get(); + + $posts = array_map(function (stdClass $row) { + return Post::fromRow($row); + }, $posts); + + return view('news/index', compact('categories', 'posts')); } /** @@ -47,7 +62,7 @@ class NewsController extends Controller * @throws HttpRouteNotFoundException * @return string */ - public function category(int $catId = 1): string + public function category(int $catId): string { $category = new Category($catId); diff --git a/app/News/Category.php b/app/News/Category.php index 5dd9f90..05cc0ea 100644 --- a/app/News/Category.php +++ b/app/News/Category.php @@ -60,7 +60,7 @@ class Category if ($data) { $this->id = intval($data->category_id); $this->name = $data->category_name; - $this->category = $data->category_description; + $this->description = $data->category_description; $this->hidden = boolval($data->category_hidden); } } diff --git a/app/News/Post.php b/app/News/Post.php index af016f3..535b6e0 100644 --- a/app/News/Post.php +++ b/app/News/Post.php @@ -31,15 +31,15 @@ class Post /** * The category this post is part of. - * @var int + * @var Category */ - public $category = 0; + public $category = null; /** * The user who made this post. - * @var int + * @var User */ - public $user = 0; + public $user = null; /** * The title of this news post. @@ -89,14 +89,18 @@ class Post */ public function __construct(int $id = 0) { + if ($id === 0) { + return; + } + $data = DB::table('news_posts') ->where('post_id', $id) ->first(); if ($data) { $this->id = intval($data->post_id); - $this->category = intval($data->category_id); - $this->user = intval($data->user_id); + $this->category = new Category(intval($data->category_id)); + $this->user = User::construct(intval($data->user_id)); $this->title = $data->post_title; $this->text = $data->post_text; $this->created = new Carbon($data->created_at); @@ -105,6 +109,22 @@ class Post } } + public static function fromRow($data) + { + $cat = new Post; + + $cat->id = intval($data->post_id); + $cat->category = new Category(intval($data->category_id)); + $cat->user = User::construct(intval($data->user_id)); + $cat->title = $data->post_title; + $cat->text = $data->post_text; + $cat->created = new Carbon($data->created_at); + $cat->updated = new Carbon($data->updated_at); + $cat->deleted = new Carbon($data->deleted_at); + + return $cat; + } + /** * Saving changes to this news post. */ @@ -142,15 +162,6 @@ class Post // $this->id = 0; } - /** - * Get the user object of the poster. - * @return User - */ - public function userData(): User - { - return User::construct($this->user); - } - /** * Count the amount of comments this post has. * @return int @@ -159,7 +170,7 @@ class Post { if (!$this->commentCountCache) { $this->commentCountCache = DB::table('comments') - ->where('comment_category', sprintf(self::COMMENT_CATEGORY_FORMAT, $this->category, $this->id)) + ->where('comment_category', sprintf(self::COMMENT_CATEGORY_FORMAT, $this->category->id, $this->id)) ->count(); } @@ -174,7 +185,7 @@ class Post { if (!$this->commentsCache) { $commentIds = DB::table('comments') - ->where('comment_category', sprintf(self::COMMENT_CATEGORY_FORMAT, $this->category, $this->id)) + ->where('comment_category', sprintf(self::COMMENT_CATEGORY_FORMAT, $this->category->id, $this->id)) ->orderBy('comment_id', 'desc') ->where('comment_reply_to', 0) ->get(['comment_id']); diff --git a/config/config.example.ini b/config/config.example.ini index 3d3d382..2cc04c3 100644 --- a/config/config.example.ini +++ b/config/config.example.ini @@ -43,8 +43,8 @@ description = Test site ; Design used by the site, must be a folder in resources/views/ design = yuuno -; Category to be used for site news -news = site-news +; Categories displayed on the front page, comma separated +homepage-news = 1 ; Cover to be used when no other one is specified cover = diff --git a/resources/assets/less/yuuno/bem/news.less b/resources/assets/less/yuuno/bem/news.less index 8275e79..e1499f5 100644 --- a/resources/assets/less/yuuno/bem/news.less +++ b/resources/assets/less/yuuno/bem/news.less @@ -57,4 +57,17 @@ margin-top: 22px; } } + + &__category-link { + display: block; + font-size: 14px; + line-height: 25px; + padding: 0 10px; + text-align: right; + } + + &__more { + text-align: center; + margin: 2em auto; + } } diff --git a/resources/views/yuuno/forum/elements/forumBtns.twig b/resources/views/yuuno/forum/elements/forumBtns.twig index a052795..e4e42f0 100644 --- a/resources/views/yuuno/forum/elements/forumBtns.twig +++ b/resources/views/yuuno/forum/elements/forumBtns.twig @@ -19,6 +19,6 @@ {% endif %} {% if pagination_url is defined %} - {{ pagination(pagination_url, pagination_pages, 'rightSide') }} + {{ pagination(pagination_url, pagination_pages, get.page|default(1), 'rightSide') }} {% endif %} diff --git a/resources/views/yuuno/home/index.twig b/resources/views/yuuno/home/index.twig index beabda5..4cb5f20 100644 --- a/resources/views/yuuno/home/index.twig +++ b/resources/views/yuuno/home/index.twig @@ -2,7 +2,7 @@ {% from 'macros.twig' import news_post %} {% block content %} -