redid news backend
This commit is contained in:
parent
a5750187d7
commit
a35d28d22b
12 changed files with 114 additions and 55 deletions
|
@ -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'))
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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']);
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,6 @@
|
|||
{% endif %}
|
||||
</div>
|
||||
{% if pagination_url is defined %}
|
||||
{{ pagination(pagination_url, pagination_pages, 'rightSide') }}
|
||||
{{ pagination(pagination_url, pagination_pages, get.page|default(1), 'rightSide') }}
|
||||
{% endif %}
|
||||
</div>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
{% from 'macros.twig' import news_post %}
|
||||
|
||||
{% block content %}
|
||||
<div class="content homepage">
|
||||
<div class="content">
|
||||
<div class="content--right" id="indexPanel">
|
||||
{% if user.isActive %}
|
||||
<div class="user" style="background-image: url({{ route('user.header', user.id) }});">
|
||||
|
@ -50,7 +50,7 @@
|
|||
<div class="content--left">
|
||||
<div class="content__header">News</div>
|
||||
{% 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 %}
|
||||
</div>
|
||||
<div class="clear"></div>
|
||||
|
|
|
@ -1,28 +1,29 @@
|
|||
{% macro news_post(id, text, created, user, title, comments) %}
|
||||
{% if title is defined and title %}
|
||||
<a href="{{ route('news.post', id) }}" class="news__head" id="p{{ id }}">{{ title }}</a>
|
||||
{% macro news_post(post, display_title, display_comments, display_category) %}
|
||||
{% if display_title|default(false) %}
|
||||
<a href="{{ route('news.post', post.id) }}" class="news__head" id="p{{ post.id }}">{{ post.title }}</a>
|
||||
{% endif %}
|
||||
<div class="news__body">
|
||||
<a class="news__poster" href="{{ route('user.profile', user.id) }}">
|
||||
<div class="avatar avatar--border news__avatar" style="background-image: url('{{ route('user.avatar', user.id) }}')"></div>
|
||||
<div class="news__username" style="color: {{ user.colour }}; text-shadow: 0 0 7px {% if user.colour != 'inherit' %}{{ user.colour }}{% else %}#222{% endif %}">
|
||||
{{ user.username }}
|
||||
<a class="news__poster" href="{{ route('user.profile', post.user.id) }}">
|
||||
<div class="avatar avatar--border news__avatar" style="background-image: url('{{ route('user.avatar', post.user.id) }}')"></div>
|
||||
<div class="news__username" style="color: {{ post.user.colour }}; text-shadow: 0 0 7px {% if post.user.colour != 'inherit' %}{{ post.user.colour }}{% else %}#222{% endif %}">
|
||||
{{ post.user.username }}
|
||||
</div>
|
||||
</a>
|
||||
<div class="bbcode">
|
||||
{{ text|raw }}
|
||||
{{ post.text|raw }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="clear"></div>
|
||||
<div class="news__date">
|
||||
Posted <time class="time-ago" datetime="{{ created.format('c') }}">{{ created.format(config('general.date_format')) }}</time>
|
||||
{% if comments is defined and comments %}<a href="{{ route('news.post', id) }}#comments">{{ comments }} comment{% if comments != 1 %}s{% endif %}</a>{% endif %}
|
||||
Posted <time class="time-ago" datetime="{{ post.created.format('c') }}">{{ post.created.format(config('general.date_format')) }}</time>
|
||||
{% if display_comments|default(false) %}<a href="{{ route('news.post', post.id) }}#comments">{{ post.commentCount }} comment{% if post.commentCount != 1 %}s{% endif %}</a>{% endif %}
|
||||
{% if display_category|default(false) %}in <a href="{{ route('news.category', post.category.id) }}">{{ post.category.name }}</a>{% endif %}
|
||||
</div>
|
||||
{% 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') ~ "=" %}
|
||||
|
||||
<div class="pagination {{ class }}">
|
||||
|
|
|
@ -1,23 +1,24 @@
|
|||
{% extends 'master.twig' %}
|
||||
{% from 'macros.twig' import news_post %}
|
||||
{% from 'macros.twig' import news_post, pagination %}
|
||||
|
||||
{% set title = 'News' %}
|
||||
{% set posts = category.posts|batch(3) %}
|
||||
|
||||
{% set paginationPages = posts|keys %}
|
||||
{% set paginationUrl %}{{ route('news.category', category.name) }}{% endset %}
|
||||
|
||||
{% block content %}
|
||||
<div class="announce-box">
|
||||
<h1>{{ category.name }}</h1>
|
||||
<h3>{{ category.description }}</h3>
|
||||
</div>
|
||||
<div class="content news">
|
||||
<div class="content__header">News</div>
|
||||
{% for post in posts[get.page|default(1) - 1] %}
|
||||
{{ news_post(post.id, post.text, post.created, post.userData, post.title, post.commentCount) }}
|
||||
{{ news_post(post, true, true) }}
|
||||
{% endfor %}
|
||||
{% if posts|length > 1 %}
|
||||
<div>
|
||||
{% include 'elements/pagination.twig' %}
|
||||
<div class="clear"></div>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div>
|
||||
<a href="{{ route('news.index') }}" class="input__button"><span class="fa fa-backward"></span> Back</a>
|
||||
{% if posts|length > 1 %}
|
||||
{{ pagination(route('news.category', category.id), posts|keys, get.page|default(1)) }}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
|
@ -1,15 +1,24 @@
|
|||
{% extends 'master.twig' %}
|
||||
{% from 'macros.twig' import news_post %}
|
||||
|
||||
{% block content %}
|
||||
<div class="content">
|
||||
<div class="content--right">
|
||||
<div class="content__header content__header--alt">Categories</div>
|
||||
{% for category in categories %}
|
||||
<a href="{{ route('news.category', category.category_id) }}">{{ category.category_name }}</a>
|
||||
<a class="news__category-link" href="{{ route('news.category', category.category_id) }}">{{ category.category_name }}</a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="content--left">
|
||||
<div class="content__header">News</div>
|
||||
{% if posts is defined and posts|length > 0 %}
|
||||
{% for post in posts %}
|
||||
{{ news_post(post, true, true, true) }}
|
||||
{% endfor %}
|
||||
<h1 class="news__more">Pick a category from the sidebar to view more!</h1>
|
||||
{% else %}
|
||||
<h1 class="news__more">There are no news posts!</h1>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="clear"></div>
|
||||
</div>
|
||||
|
|
|
@ -3,13 +3,14 @@
|
|||
|
||||
{% set title = post.title %}
|
||||
|
||||
{% set commentsCategory = 'news-' ~ post.category ~ '-' ~ post.id %}
|
||||
{% set commentsCategory = 'news-' ~ post.category.id ~ '-' ~ post.id %}
|
||||
{% set comments = post.comments %}
|
||||
|
||||
{% block content %}
|
||||
<div class="content news">
|
||||
<div class="content__header">{{ post.title }}</div>
|
||||
{{ news_post(post.id, post.text, post.created, post.userData) }}
|
||||
{{ news_post(post) }}
|
||||
<a href="{{ route('news.category', post.category.id) }}" class="input__button"><span class="fa fa-backward"></span> Back</a>
|
||||
{% include 'elements/comments.twig' %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
Reference in a new issue