From a35d28d22be751e799e318e29e3ded46031dca09 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 21 Dec 2016 20:55:17 +0100 Subject: [PATCH] redid news backend --- app/Controllers/HomeController.php | 12 ++++- app/Controllers/NewsController.php | 23 ++++++++-- app/News/Category.php | 2 +- app/News/Post.php | 45 ++++++++++++------- config/config.example.ini | 4 +- resources/assets/less/yuuno/bem/news.less | 13 ++++++ .../views/yuuno/forum/elements/forumBtns.twig | 2 +- resources/views/yuuno/home/index.twig | 4 +- resources/views/yuuno/macros.twig | 25 ++++++----- resources/views/yuuno/news/category.twig | 23 +++++----- resources/views/yuuno/news/index.twig | 11 ++++- resources/views/yuuno/news/post.twig | 5 ++- 12 files changed, 114 insertions(+), 55 deletions(-) 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 %} -
+
{% if user.isActive %}
@@ -50,7 +50,7 @@
News
{% for post in news %} - {{ news_post(post.id, post.text, post.created, post.userData, post.title, post.commentCount) }} + {{ news_post(post, true, true, true) }} {% endfor %}
diff --git a/resources/views/yuuno/macros.twig b/resources/views/yuuno/macros.twig index 87c9952..119a0e7 100644 --- a/resources/views/yuuno/macros.twig +++ b/resources/views/yuuno/macros.twig @@ -1,28 +1,29 @@ -{% macro news_post(id, text, created, user, title, comments) %} - {% if title is defined and title %} - {{ title }} +{% macro news_post(post, display_title, display_comments, display_category) %} + {% if display_title|default(false) %} + {{ post.title }} {% endif %}
- -
-
- {{ user.username }} + +
+
+ {{ post.user.username }}
- {{ text|raw }} + {{ post.text|raw }}
- Posted - {% if comments is defined and comments %}{{ comments }} comment{% if comments != 1 %}s{% endif %}{% endif %} + Posted + {% if display_comments|default(false) %}{{ post.commentCount }} comment{% if post.commentCount != 1 %}s{% endif %}{% endif %} + {% if display_category|default(false) %}in {{ post.category.name }}{% endif %}
{% endmacro %} -{% macro pagination(url, pages, class, name) %} +{% macro pagination(url, pages, current, class, name) %} {% set separator %}{% if '%3F' in url|default('')|url_encode %}&{% else %}?{% endif %}{% endset %} - {% set current_page = get[name|default('page')]|default(1) %} + {% set current_page = current|default(1) %} {% set url = url ~ separator ~ name|default('page') ~ "=" %}